| @@ -4,12 +4,17 @@ | ||
| 4 | 4 | use Exception; |
| 5 | 5 | use Activitypub\Signature; |
| 6 | 6 | use Activitypub\Collection\Users; |
| 7 | 7 | use Activitypub\Collection\Followers; |
| 8 | +use Activitypub\Collection\Extra_Fields; | |
| 8 | 9 | |
| 10 | +use function Activitypub\is_comment; | |
| 9 | 11 | use function Activitypub\sanitize_url; |
| 10 | -use function Activitypub\is_comment; | |
| 12 | +use function Activitypub\is_local_comment; | |
| 13 | +use function Activitypub\site_supports_blocks; | |
| 14 | +use function Activitypub\is_user_type_disabled; | |
| 11 | 15 | use function Activitypub\is_activitypub_request; |
| 16 | +use function Activitypub\should_comment_be_federated; | |
| 12 | 17 | |
| 13 | 18 | /** |
| 14 | 19 | * ActivityPub Class |
| 15 | 20 | * |
| @@ -23,12 +28,11 @@ | ||
| 23 | 28 | \add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 ); |
| 24 | 29 | \add_action( 'template_redirect', array( self::class, 'template_redirect' ) ); |
| 25 | 30 | \add_filter( 'query_vars', array( self::class, 'add_query_vars' ) ); |
| 26 | 31 | \add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 ); |
| 27 | - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 ); | |
| 28 | 32 | |
| 29 | 33 | // Add support for ActivityPub to custom post types |
| 30 | - $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array(); | |
| 34 | + $post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post' ) ) : array(); | |
| 31 | 35 | |
| 32 | 36 | foreach ( $post_types as $post_type ) { |
| 33 | 37 | \add_post_type_support( $post_type, 'activitypub' ); |
| 34 | 38 | } |
| @@ -36,15 +40,20 @@ | ||
| 36 | 40 | \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 ); |
| 37 | 41 | \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 ); |
| 38 | 42 | |
| 39 | 43 | \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 ); |
| 44 | + \add_action( 'init', array( self::class, 'theme_compat' ), 11 ); | |
| 40 | 45 | |
| 41 | - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 ); | |
| 46 | + \add_action( 'user_register', array( self::class, 'user_register' ) ); | |
| 42 | 47 | |
| 43 | 48 | \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); |
| 44 | 49 | |
| 45 | - \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 ); | |
| 50 | + if ( site_supports_blocks() ) { | |
| 51 | + \add_action( 'tool_box', array( self::class, 'tool_box' ) ); | |
| 52 | + } | |
| 46 | 53 | |
| 54 | + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 ); | |
| 55 | + | |
| 47 | 56 | // register several post_types |
| 48 | 57 | self::register_post_types(); |
| 49 | 58 | } |
| 50 | 59 | |
| @@ -94,40 +103,101 @@ | ||
| 94 | 103 | } |
| 95 | 104 | |
| 96 | 105 | $json_template = false; |
| 97 | 106 | |
| 98 | - // check if user can publish posts | |
| 99 | - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) { | |
| 100 | - return $template; | |
| 101 | - } | |
| 102 | - | |
| 103 | - if ( \is_author() ) { | |
| 104 | - $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php'; | |
| 107 | + if ( \is_author() && ! is_user_disabled( \get_the_author_meta( 'ID' ) ) ) { | |
| 108 | + $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/user-json.php'; | |
| 105 | 109 | } elseif ( is_comment() ) { |
| 106 | 110 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php'; |
| 107 | 111 | } elseif ( \is_singular() ) { |
| 108 | 112 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php'; |
| 109 | - } elseif ( \is_home() ) { | |
| 113 | + } elseif ( \is_home() && ! is_user_type_disabled( 'blog' ) ) { | |
| 110 | 114 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php'; |
| 111 | 115 | } |
| 112 | 116 | |
| 113 | - if ( ACTIVITYPUB_AUTHORIZED_FETCH ) { | |
| 117 | + /* | |
| 118 | + * Check if the request is authorized. | |
| 119 | + * | |
| 120 | + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch | |
| 121 | + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch | |
| 122 | + */ | |
| 123 | + if ( $json_template && ACTIVITYPUB_AUTHORIZED_FETCH ) { | |
| 114 | 124 | $verification = Signature::verify_http_signature( $_SERVER ); |
| 115 | 125 | if ( \is_wp_error( $verification ) ) { |
| 126 | + header( 'HTTP/1.1 401 Unauthorized' ); | |
| 127 | + | |
| 116 | 128 | // fallback as template_loader can't return http headers |
| 117 | 129 | return $template; |
| 118 | 130 | } |
| 119 | 131 | } |
| 120 | 132 | |
| 121 | - return $json_template; | |
| 133 | + if ( $json_template ) { | |
| 134 | + return $json_template; | |
| 135 | + } | |
| 136 | + | |
| 137 | + return $template; | |
| 122 | 138 | } |
| 123 | 139 | |
| 124 | 140 | /** |
| 141 | + * Add the 'self' link to the header. | |
| 142 | + * | |
| 143 | + * @see | |
| 144 | + * | |
| 145 | + * @return void | |
| 146 | + */ | |
| 147 | + public static function add_headers() { | |
| 148 | + // phpcs:ignore | |
| 149 | + $request_uri = $_SERVER['REQUEST_URI']; | |
| 150 | + | |
| 151 | + if ( ! $request_uri ) { | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + | |
| 155 | + // only add self link to author pages... | |
| 156 | + if ( is_author() ) { | |
| 157 | + if ( is_user_disabled( get_queried_object_id() ) ) { | |
| 158 | + return; | |
| 159 | + } | |
| 160 | + } elseif ( is_singular() ) { // or posts/pages/custom-post-types... | |
| 161 | + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) { | |
| 162 | + return; | |
| 163 | + } | |
| 164 | + } else { // otherwise return | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 168 | + // add self link to html and http header | |
| 169 | + $host = wp_parse_url( home_url() ); | |
| 170 | + $self_link = esc_url( | |
| 171 | + apply_filters( | |
| 172 | + 'self_link', | |
| 173 | + set_url_scheme( | |
| 174 | + // phpcs:ignore | |
| 175 | + 'http://' . $host['host'] . wp_unslash( $request_uri ) | |
| 176 | + ) | |
| 177 | + ) | |
| 178 | + ); | |
| 179 | + | |
| 180 | + if ( ! headers_sent() ) { | |
| 181 | + header( 'Link: <' . $self_link . '>; rel="alternate"; type="application/activity+json"' ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + add_action( | |
| 185 | + 'wp_head', | |
| 186 | + function () use ( $self_link ) { | |
| 187 | + echo PHP_EOL . '<link rel="alternate" type="application/activity+json" href="' . esc_url( $self_link ) . '" />' . PHP_EOL; | |
| 188 | + } | |
| 189 | + ); | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 125 | 193 | * Custom redirects for ActivityPub requests. |
| 126 | 194 | * |
| 127 | 195 | * @return void |
| 128 | 196 | */ |
| 129 | 197 | public static function template_redirect() { |
| 198 | + self::add_headers(); | |
| 199 | + | |
| 130 | 200 | $comment_id = get_query_var( 'c', null ); |
| 131 | 201 | |
| 132 | 202 | // check if it seems to be a comment |
| 133 | 203 | if ( ! $comment_id ) { |
| @@ -143,9 +213,9 @@ | ||
| 143 | 213 | return; |
| 144 | 214 | } |
| 145 | 215 | |
| 146 | 216 | // stop if it's not an ActivityPub comment |
| 147 | - if ( is_activitypub_request() && $comment->user_id ) { | |
| 217 | + if ( is_activitypub_request() && ! is_local_comment( $comment ) ) { | |
| 148 | 218 | return; |
| 149 | 219 | } |
| 150 | 220 | |
| 151 | 221 | wp_safe_redirect( get_comment_link( $comment ) ); |
| @@ -197,16 +267,18 @@ | ||
| 197 | 267 | // Check if comment has an avatar. |
| 198 | 268 | $avatar = self::get_avatar_url( $id_or_email->comment_ID ); |
| 199 | 269 | |
| 200 | 270 | if ( $avatar ) { |
| 201 | - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) { | |
| 202 | - $args['class'] = array( 'u-photo' ); | |
| 203 | - } else { | |
| 204 | - $args['class'][] = 'u-photo'; | |
| 205 | - $args['class'] = \array_unique( $args['class'] ); | |
| 271 | + if ( empty( $args['class'] ) ) { | |
| 272 | + $args['class'] = array(); | |
| 273 | + } elseif ( \is_string( $args['class'] ) ) { | |
| 274 | + $args['class'] = \explode( ' ', $args['class'] ); | |
| 206 | 275 | } |
| 276 | + | |
| 207 | 277 | $args['url'] = $avatar; |
| 208 | 278 | $args['class'][] = 'avatar-activitypub'; |
| 279 | + $args['class'][] = 'u-photo'; | |
| 280 | + $args['class'] = \array_unique( $args['class'] ); | |
| 209 | 281 | } |
| 210 | 282 | |
| 211 | 283 | return $args; |
| 212 | 284 | } |
| @@ -225,32 +297,8 @@ | ||
| 225 | 297 | return \get_comment_meta( $comment->comment_ID, 'avatar_url', true ); |
| 226 | 298 | } |
| 227 | 299 | |
| 228 | 300 | /** |
| 229 | - * Link remote comments to source url. | |
| 230 | - * | |
| 231 | - * @param string $comment_link | |
| 232 | - * @param object|WP_Comment $comment | |
| 233 | - * | |
| 234 | - * @return string $url | |
| 235 | - */ | |
| 236 | - public static function remote_comment_link( $comment_link, $comment ) { | |
| 237 | - if ( ! $comment || is_admin() ) { | |
| 238 | - return $comment_link; | |
| 239 | - } | |
| 240 | - | |
| 241 | - $comment_meta = \get_comment_meta( $comment->comment_ID ); | |
| 242 | - | |
| 243 | - if ( ! empty( $comment_meta['source_url'][0] ) ) { | |
| 244 | - return $comment_meta['source_url'][0]; | |
| 245 | - } elseif ( ! empty( $comment_meta['source_id'][0] ) ) { | |
| 246 | - return $comment_meta['source_id'][0]; | |
| 247 | - } | |
| 248 | - | |
| 249 | - return $comment_link; | |
| 250 | - } | |
| 251 | - | |
| 252 | - /** | |
| 253 | 301 | * Store permalink in meta, to send delete Activity. |
| 254 | 302 | * |
| 255 | 303 | * @param string $post_id The Post ID. |
| 256 | 304 | * |
| @@ -308,9 +356,9 @@ | ||
| 308 | 356 | } |
| 309 | 357 | |
| 310 | 358 | \add_rewrite_rule( |
| 311 | 359 | '^@([\w\-\.]+)', |
| 312 | - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]', | |
| 360 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]', | |
| 313 | 361 | 'top' |
| 314 | 362 | ); |
| 315 | 363 | |
| 316 | 364 | \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); |
| @@ -324,37 +372,40 @@ | ||
| 324 | 372 | \flush_rewrite_rules(); |
| 325 | 373 | } |
| 326 | 374 | |
| 327 | 375 | /** |
| 376 | + * Adds metabox on wp-admin/tools.php | |
| 377 | + * | |
| 378 | + * @return void | |
| 379 | + */ | |
| 380 | + public static function tool_box() { | |
| 381 | + if ( \current_user_can( 'edit_posts' ) ) { | |
| 382 | + \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/toolbox.php' ); | |
| 383 | + } | |
| 384 | + } | |
| 385 | + | |
| 386 | + /** | |
| 328 | 387 | * Theme compatibility stuff |
| 329 | 388 | * |
| 330 | 389 | * @return void |
| 331 | 390 | */ |
| 332 | 391 | public static function theme_compat() { |
| 333 | - $site_icon = get_theme_support( 'custom-logo' ); | |
| 334 | - | |
| 335 | - if ( ! $site_icon ) { | |
| 336 | - // custom logo support | |
| 337 | - add_theme_support( | |
| 338 | - 'custom-logo', | |
| 339 | - array( | |
| 340 | - 'height' => 80, | |
| 341 | - 'width' => 80, | |
| 342 | - ) | |
| 343 | - ); | |
| 392 | + // We assume that you want to use Post-Formats when enabling the setting | |
| 393 | + if ( 'wordpress-post-format' === \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ) ) { | |
| 394 | + if ( ! get_theme_support( 'post-formats' ) ) { | |
| 395 | + // Add support for the Aside, Gallery Post Formats... | |
| 396 | + add_theme_support( | |
| 397 | + 'post-formats', | |
| 398 | + array( | |
| 399 | + 'gallery', | |
| 400 | + 'status', | |
| 401 | + 'image', | |
| 402 | + 'video', | |
| 403 | + 'audio', | |
| 404 | + ) | |
| 405 | + ); | |
| 406 | + } | |
| 344 | 407 | } |
| 345 | - | |
| 346 | - $custom_header = get_theme_support( 'custom-header' ); | |
| 347 | - | |
| 348 | - if ( ! $custom_header ) { | |
| 349 | - // This theme supports a custom header | |
| 350 | - $custom_header_args = array( | |
| 351 | - 'width' => 1250, | |
| 352 | - 'height' => 600, | |
| 353 | - 'header-text' => true, | |
| 354 | - ); | |
| 355 | - add_theme_support( 'custom-header', $custom_header_args ); | |
| 356 | - } | |
| 357 | 408 | } |
| 358 | 409 | |
| 359 | 410 | /** |
| 360 | 411 | * Display plugin upgrade notice to users |
| @@ -454,25 +505,51 @@ | ||
| 454 | 505 | }, |
| 455 | 506 | ) |
| 456 | 507 | ); |
| 457 | 508 | |
| 509 | + // Both User and Blog Extra Fields types have the same args. | |
| 510 | + $args = array( | |
| 511 | + 'labels' => array( | |
| 512 | + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ), | |
| 513 | + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ), | |
| 514 | + 'add_new' => __( 'Add new', 'activitypub' ), | |
| 515 | + 'add_new_item' => __( 'Add new extra field', 'activitypub' ), | |
| 516 | + 'new_item' => __( 'New extra field', 'activitypub' ), | |
| 517 | + 'edit_item' => __( 'Edit extra field', 'activitypub' ), | |
| 518 | + 'view_item' => __( 'View extra field', 'activitypub' ), | |
| 519 | + 'all_items' => __( 'All extra fields', 'activitypub' ), | |
| 520 | + ), | |
| 521 | + 'public' => false, | |
| 522 | + 'hierarchical' => false, | |
| 523 | + 'query_var' => false, | |
| 524 | + 'has_archive' => false, | |
| 525 | + 'publicly_queryable' => false, | |
| 526 | + 'show_in_menu' => false, | |
| 527 | + 'delete_with_user' => true, | |
| 528 | + 'can_export' => true, | |
| 529 | + 'exclude_from_search' => true, | |
| 530 | + 'show_in_rest' => true, | |
| 531 | + 'map_meta_cap' => true, | |
| 532 | + 'show_ui' => true, | |
| 533 | + 'supports' => array( 'title', 'editor', 'page-attributes' ), | |
| 534 | + ); | |
| 535 | + | |
| 536 | + \register_post_type( Extra_Fields::USER_POST_TYPE, $args ); | |
| 537 | + \register_post_type( Extra_Fields::BLOG_POST_TYPE, $args ); | |
| 538 | + | |
| 458 | 539 | \do_action( 'activitypub_after_register_post_type' ); |
| 459 | 540 | } |
| 460 | 541 | |
| 461 | 542 | /** |
| 462 | - * Filters the CSS classes to add an ActivityPub class. | |
| 543 | + * Add the 'activitypub' capability to users who can publish posts. | |
| 463 | 544 | * |
| 464 | - * @param string[] $classes An array of comment classes. | |
| 465 | - * @param string[] $css_class An array of additional classes added to the list. | |
| 466 | - * @param string $comment_id The comment ID as a numeric string. | |
| 545 | + * @param int $user_id User ID. | |
| 467 | 546 | * |
| 468 | - * @return string[] An array of classes. | |
| 547 | + * @param array $userdata The raw array of data passed to wp_insert_user(). | |
| 469 | 548 | */ |
| 470 | - public static function comment_class( $classes, $css_class, $comment_id ) { | |
| 471 | - // check if ActivityPub comment | |
| 472 | - if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) { | |
| 473 | - $classes[] = 'activitypub-comment'; | |
| 549 | + public static function user_register( $user_id ) { | |
| 550 | + if ( \user_can( $user_id, 'publish_posts' ) ) { | |
| 551 | + $user = \get_user_by( 'id', $user_id ); | |
| 552 | + $user->add_cap( 'activitypub' ); | |
| 474 | 553 | } |
| 475 | - | |
| 476 | - return $classes; | |
| 477 | 554 | } |
| 478 | 555 | } |