| @@ -4,12 +4,15 @@ | ||
| 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 | |
| 9 | 10 | use function Activitypub\is_comment; |
| 10 | 11 | use function Activitypub\sanitize_url; |
| 11 | 12 | use function Activitypub\is_local_comment; |
| 13 | +use function Activitypub\site_supports_blocks; | |
| 14 | +use function Activitypub\is_user_type_disabled; | |
| 12 | 15 | use function Activitypub\is_activitypub_request; |
| 13 | 16 | use function Activitypub\should_comment_be_federated; |
| 14 | 17 | |
| 15 | 18 | /** |
| @@ -37,13 +40,20 @@ | ||
| 37 | 40 | \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 ); |
| 38 | 41 | \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 ); |
| 39 | 42 | |
| 40 | 43 | \add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 ); |
| 44 | + \add_action( 'init', array( self::class, 'theme_compat' ), 11 ); | |
| 41 | 45 | |
| 42 | - \add_action( 'after_setup_theme', array( self::class, 'theme_compat' ), 99 ); | |
| 46 | + \add_action( 'user_register', array( self::class, 'user_register' ) ); | |
| 43 | 47 | |
| 44 | 48 | \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); |
| 45 | 49 | |
| 50 | + if ( site_supports_blocks() ) { | |
| 51 | + \add_action( 'tool_box', array( self::class, 'tool_box' ) ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + \add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 ); | |
| 55 | + | |
| 46 | 56 | // register several post_types |
| 47 | 57 | self::register_post_types(); |
| 48 | 58 | } |
| 49 | 59 | |
| @@ -93,45 +103,101 @@ | ||
| 93 | 103 | } |
| 94 | 104 | |
| 95 | 105 | $json_template = false; |
| 96 | 106 | |
| 97 | - // check if user can publish posts | |
| 98 | - if ( \is_author() && is_wp_error( Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) ) { | |
| 99 | - return $template; | |
| 100 | - } | |
| 101 | - | |
| 102 | - // check if blog-user is enabled | |
| 103 | - if ( \is_home() && is_wp_error( Users::get_by_id( Users::BLOG_USER_ID ) ) ) { | |
| 104 | - return $template; | |
| 105 | - } | |
| 106 | - | |
| 107 | - if ( \is_author() ) { | |
| 108 | - $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'; | |
| 109 | 109 | } elseif ( is_comment() ) { |
| 110 | 110 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php'; |
| 111 | 111 | } elseif ( \is_singular() ) { |
| 112 | 112 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php'; |
| 113 | - } elseif ( \is_home() ) { | |
| 113 | + } elseif ( \is_home() && ! is_user_type_disabled( 'blog' ) ) { | |
| 114 | 114 | $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/blog-json.php'; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - 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 ) { | |
| 118 | 124 | $verification = Signature::verify_http_signature( $_SERVER ); |
| 119 | 125 | if ( \is_wp_error( $verification ) ) { |
| 126 | + header( 'HTTP/1.1 401 Unauthorized' ); | |
| 127 | + | |
| 120 | 128 | // fallback as template_loader can't return http headers |
| 121 | 129 | return $template; |
| 122 | 130 | } |
| 123 | 131 | } |
| 124 | 132 | |
| 125 | - return $json_template; | |
| 133 | + if ( $json_template ) { | |
| 134 | + return $json_template; | |
| 135 | + } | |
| 136 | + | |
| 137 | + return $template; | |
| 126 | 138 | } |
| 127 | 139 | |
| 128 | 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 | + /** | |
| 129 | 193 | * Custom redirects for ActivityPub requests. |
| 130 | 194 | * |
| 131 | 195 | * @return void |
| 132 | 196 | */ |
| 133 | 197 | public static function template_redirect() { |
| 198 | + self::add_headers(); | |
| 199 | + | |
| 134 | 200 | $comment_id = get_query_var( 'c', null ); |
| 135 | 201 | |
| 136 | 202 | // check if it seems to be a comment |
| 137 | 203 | if ( ! $comment_id ) { |
| @@ -201,16 +267,18 @@ | ||
| 201 | 267 | // Check if comment has an avatar. |
| 202 | 268 | $avatar = self::get_avatar_url( $id_or_email->comment_ID ); |
| 203 | 269 | |
| 204 | 270 | if ( $avatar ) { |
| 205 | - if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) { | |
| 206 | - $args['class'] = array( 'u-photo' ); | |
| 207 | - } else { | |
| 208 | - $args['class'][] = 'u-photo'; | |
| 209 | - $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'] ); | |
| 210 | 275 | } |
| 276 | + | |
| 211 | 277 | $args['url'] = $avatar; |
| 212 | 278 | $args['class'][] = 'avatar-activitypub'; |
| 279 | + $args['class'][] = 'u-photo'; | |
| 280 | + $args['class'] = \array_unique( $args['class'] ); | |
| 213 | 281 | } |
| 214 | 282 | |
| 215 | 283 | return $args; |
| 216 | 284 | } |
| @@ -288,9 +356,9 @@ | ||
| 288 | 356 | } |
| 289 | 357 | |
| 290 | 358 | \add_rewrite_rule( |
| 291 | 359 | '^@([\w\-\.]+)', |
| 292 | - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]', | |
| 360 | + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1]', | |
| 293 | 361 | 'top' |
| 294 | 362 | ); |
| 295 | 363 | |
| 296 | 364 | \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); |
| @@ -304,37 +372,40 @@ | ||
| 304 | 372 | \flush_rewrite_rules(); |
| 305 | 373 | } |
| 306 | 374 | |
| 307 | 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 | + /** | |
| 308 | 387 | * Theme compatibility stuff |
| 309 | 388 | * |
| 310 | 389 | * @return void |
| 311 | 390 | */ |
| 312 | 391 | public static function theme_compat() { |
| 313 | - $site_icon = get_theme_support( 'custom-logo' ); | |
| 314 | - | |
| 315 | - if ( ! $site_icon ) { | |
| 316 | - // custom logo support | |
| 317 | - add_theme_support( | |
| 318 | - 'custom-logo', | |
| 319 | - array( | |
| 320 | - 'height' => 80, | |
| 321 | - 'width' => 80, | |
| 322 | - ) | |
| 323 | - ); | |
| 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 | + } | |
| 324 | 407 | } |
| 325 | - | |
| 326 | - $custom_header = get_theme_support( 'custom-header' ); | |
| 327 | - | |
| 328 | - if ( ! $custom_header ) { | |
| 329 | - // This theme supports a custom header | |
| 330 | - $custom_header_args = array( | |
| 331 | - 'width' => 1250, | |
| 332 | - 'height' => 600, | |
| 333 | - 'header-text' => true, | |
| 334 | - ); | |
| 335 | - add_theme_support( 'custom-header', $custom_header_args ); | |
| 336 | - } | |
| 337 | 408 | } |
| 338 | 409 | |
| 339 | 410 | /** |
| 340 | 411 | * Display plugin upgrade notice to users |
| @@ -434,7 +505,51 @@ | ||
| 434 | 505 | }, |
| 435 | 506 | ) |
| 436 | 507 | ); |
| 437 | 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 | + | |
| 438 | 539 | \do_action( 'activitypub_after_register_post_type' ); |
| 540 | + } | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Add the 'activitypub' capability to users who can publish posts. | |
| 544 | + * | |
| 545 | + * @param int $user_id User ID. | |
| 546 | + * | |
| 547 | + * @param array $userdata The raw array of data passed to wp_insert_user(). | |
| 548 | + */ | |
| 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' ); | |
| 553 | + } | |
| 439 | 554 | } |
| 440 | 555 | } |