| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Collection\Actors; |
| 11 | -use Activitypub\Collection\Remote_Posts; | |
| 11 | +use WP_Comment_Query; | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * ActivityPub Comment Class. |
| 15 | 15 | * |
| @@ -22,82 +22,25 @@ | ||
| 22 | 22 | */ |
| 23 | 23 | public static function init() { |
| 24 | 24 | self::register_comment_types(); |
| 25 | 25 | |
| 26 | - \add_filter( 'map_meta_cap', array( self::class, 'map_meta_cap' ), 10, 4 ); | |
| 27 | 26 | \add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 3 ); |
| 28 | 27 | \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 ); |
| 29 | - \add_filter( 'comment_feed_where', array( static::class, 'comment_feed_where' ) ); | |
| 30 | 28 | \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 ); |
| 29 | + \add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); | |
| 31 | 30 | \add_action( 'pre_get_comments', array( static::class, 'comment_query' ) ); |
| 32 | - \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 11, 2 ); | |
| 31 | + \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 ); | |
| 33 | 32 | \add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 ); |
| 34 | 33 | \add_action( 'update_option_activitypub_allow_likes', array( self::class, 'maybe_update_comment_counts' ), 10, 2 ); |
| 35 | 34 | \add_action( 'update_option_activitypub_allow_reposts', array( self::class, 'maybe_update_comment_counts' ), 10, 2 ); |
| 36 | - \add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 5, 3 ); | |
| 37 | - \add_filter( 'get_comment_author', array( static::class, 'render_emoji' ), 10, 2 ); | |
| 38 | - \add_filter( 'comment_author', array( static::class, 'unescape_emoji' ), 20 ); // After esc_html(). | |
| 39 | - \add_filter( 'rest_comment_query', array( static::class, 'rest_comment_query' ) ); | |
| 40 | - \add_filter( 'comment_text', array( static::class, 'render_blocks' ), 5 ); // Before other filters. | |
| 35 | + \add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 ); | |
| 41 | 36 | } |
| 42 | 37 | |
| 43 | 38 | /** |
| 44 | - * Render blocks in comment content. | |
| 45 | - * | |
| 46 | - * Comments don't automatically parse blocks like posts do. | |
| 47 | - * This filter applies do_blocks() to render activitypub/emoji | |
| 48 | - * and activitypub/image blocks in comment content. | |
| 49 | - * | |
| 50 | - * @param string $content The comment content. | |
| 51 | - * | |
| 52 | - * @return string The content with blocks rendered. | |
| 53 | - */ | |
| 54 | - public static function render_blocks( $content ) { | |
| 55 | - if ( empty( $content ) || ! \str_contains( $content, '<!-- wp:activitypub/' ) ) { | |
| 56 | - return $content; | |
| 57 | - } | |
| 58 | - | |
| 59 | - $blocks = \parse_blocks( $content ); | |
| 60 | - $output = ''; | |
| 61 | - | |
| 62 | - foreach ( $blocks as $block ) { | |
| 63 | - if ( ! empty( $block['blockName'] ) && \str_starts_with( $block['blockName'], 'activitypub/' ) ) { | |
| 64 | - $output .= \render_block( $block ); | |
| 65 | - } else { | |
| 66 | - $output .= \serialize_block( $block ); | |
| 67 | - } | |
| 68 | - } | |
| 69 | - | |
| 70 | - return $output; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Remove edit capabilities for comments received via ActivityPub. | |
| 75 | - * | |
| 76 | - * @param array $caps Array of capabilities. | |
| 77 | - * @param string $cap Capability name. | |
| 78 | - * @param int $user_id User ID. | |
| 79 | - * @param array $args Array of arguments. | |
| 80 | - * | |
| 81 | - * @return array Modified array of capabilities. | |
| 82 | - */ | |
| 83 | - public static function map_meta_cap( $caps, $cap, $user_id, $args ) { | |
| 84 | - if ( 'edit_comment' === $cap && self::was_received( $args[0] ) ) { | |
| 85 | - if ( ! \is_admin() || ( isset( $GLOBALS['current_screen'] ) && 'comment' === $GLOBALS['current_screen']->id ) ) { | |
| 86 | - $caps[] = 'do_not_allow'; | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - return $caps; | |
| 91 | - } | |
| 92 | - | |
| 93 | - /** | |
| 94 | 39 | * Filter the comment reply link. |
| 95 | 40 | * |
| 96 | - * Handles three cases for replies to fediverse comments: | |
| 97 | - * 1. User can federate → show normal reply link | |
| 98 | - * 2. User is logged in but can't federate → show warning (no reply link) | |
| 99 | - * 3. User is not logged in → show remote reply block | |
| 41 | + * We don't want to show the comment reply link for federated comments | |
| 42 | + * if the user is disabled for federation. | |
| 100 | 43 | * |
| 101 | 44 | * @param string $link The HTML markup for the comment reply link. |
| 102 | 45 | * @param array $args An array of arguments overriding the defaults. |
| 103 | 46 | * @param \WP_Comment $comment The object of the comment being replied. |
| @@ -105,65 +48,58 @@ | ||
| 105 | 48 | * @return string The filtered HTML markup for the comment reply link. |
| 106 | 49 | */ |
| 107 | 50 | public static function comment_reply_link( $link, $args, $comment ) { |
| 108 | 51 | if ( self::are_comments_allowed( $comment ) ) { |
| 109 | - return $link; | |
| 110 | - } | |
| 111 | - | |
| 112 | - // Logged-in user without ActivityPub capability - show warning instead of reply link. | |
| 113 | - if ( \is_user_logged_in() ) { | |
| 114 | - $author = \esc_html( $comment->comment_author ); | |
| 115 | - | |
| 116 | - $message = sprintf( | |
| 117 | - /* translators: %s: comment author name */ | |
| 118 | - \__( '%s is on the Fediverse. To reply to them, ask your administrator to enable ActivityPub for your account.', 'activitypub' ), | |
| 119 | - $author | |
| 120 | - ); | |
| 121 | - | |
| 122 | - // Add link to users page if current user can edit users. | |
| 123 | - if ( \current_user_can( 'edit_users' ) ) { | |
| 124 | - $message = sprintf( | |
| 125 | - /* translators: 1: comment author name, 2: URL to the users management page */ | |
| 126 | - \__( '%1$s is on the Fediverse. To reply to them, <a href="%2$s">enable ActivityPub for your account</a>.', 'activitypub' ), | |
| 127 | - $author, | |
| 128 | - \esc_url( \admin_url( 'users.php' ) ) | |
| 129 | - ); | |
| 52 | + if ( \current_user_can( 'activitypub' ) && self::was_received( $comment ) ) { | |
| 53 | + return self::create_fediverse_reply_link( $link, $args ); | |
| 130 | 54 | } |
| 131 | 55 | |
| 132 | - $warning = sprintf( | |
| 133 | - '<p class="activitypub-reply-warning"><em>%s</em></p>', | |
| 134 | - \wp_kses( $message, array( 'a' => array( 'href' => array() ) ) ) | |
| 135 | - ); | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * Filters the warning message shown to logged-in users without ActivityPub capability. | |
| 139 | - * | |
| 140 | - * @param string $warning The warning HTML markup. | |
| 141 | - * @param \WP_Comment $comment The comment being replied to. | |
| 142 | - */ | |
| 143 | - return \apply_filters( 'activitypub_federation_warning', $warning, $comment ); | |
| 56 | + return $link; | |
| 144 | 57 | } |
| 145 | 58 | |
| 146 | - if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( 'activitypub/remote-reply' ) ) { | |
| 147 | - \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply' ); | |
| 148 | - } | |
| 149 | - | |
| 150 | - $attributes = array( | |
| 59 | + $attrs = array( | |
| 151 | 60 | 'selectedComment' => self::generate_id( $comment ), |
| 152 | 61 | 'commentId' => $comment->comment_ID, |
| 153 | 62 | ); |
| 154 | 63 | |
| 155 | - $block = \do_blocks( \sprintf( '<!-- wp:activitypub/remote-reply %s /-->', \wp_json_encode( $attributes ) ) ); | |
| 64 | + $div = sprintf( | |
| 65 | + '<div class="reply activitypub-remote-reply" data-attrs="%s"></div>', | |
| 66 | + esc_attr( wp_json_encode( $attrs ) ) | |
| 67 | + ); | |
| 156 | 68 | |
| 157 | 69 | /** |
| 158 | 70 | * Filters the HTML markup for the ActivityPub remote comment reply container. |
| 159 | 71 | * |
| 160 | - * @param string $block The HTML markup for the remote reply container. | |
| 72 | + * @param string $div The HTML markup for the remote reply container. Default is a div | |
| 73 | + * with class 'activitypub-remote-reply' and data attributes for | |
| 74 | + * the selected comment ID and internal comment ID. | |
| 161 | 75 | */ |
| 162 | - return \apply_filters( 'activitypub_comment_reply_link', $block ); | |
| 76 | + return apply_filters( 'activitypub_comment_reply_link', $div ); | |
| 163 | 77 | } |
| 164 | 78 | |
| 165 | 79 | /** |
| 80 | + * Create a link to reply to a federated comment. | |
| 81 | + * | |
| 82 | + * This function adds a title attribute to the reply link to inform the user | |
| 83 | + * that the comment was received from the fediverse and the reply will be sent | |
| 84 | + * to the original author. | |
| 85 | + * | |
| 86 | + * @param string $link The HTML markup for the comment reply link. | |
| 87 | + * @param array $args The args provided by the `comment_reply_link` filter. | |
| 88 | + * | |
| 89 | + * @return string The modified HTML markup for the comment reply link. | |
| 90 | + */ | |
| 91 | + private static function create_fediverse_reply_link( $link, $args ) { | |
| 92 | + $str_to_replace = sprintf( '>%s<', $args['reply_text'] ); | |
| 93 | + $replace_with = sprintf( | |
| 94 | + ' title="%s">%s<', | |
| 95 | + esc_attr__( 'This comment was received from the fediverse and your reply will be sent to the original author', 'activitypub' ), | |
| 96 | + esc_html__( 'Reply with federation', 'activitypub' ) | |
| 97 | + ); | |
| 98 | + return str_replace( $str_to_replace, $replace_with, $link ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 166 | 102 | * Check if it is allowed to comment to a comment. |
| 167 | 103 | * |
| 168 | 104 | * Checks if the comment is local only or if the user can comment federated comments. |
| 169 | 105 | * |
| @@ -183,14 +119,13 @@ | ||
| 183 | 119 | if ( ! $current_user ) { |
| 184 | 120 | return false; |
| 185 | 121 | } |
| 186 | 122 | |
| 187 | - if ( is_single_user() && \user_can( $current_user, 'activitypub' ) ) { | |
| 188 | - // On a single user site, comments by users with the `activitypub` capability will be federated as the blog user. | |
| 123 | + if ( is_single_user() && \user_can( $current_user, 'publish_posts' ) ) { | |
| 124 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user. | |
| 189 | 125 | $current_user = Actors::BLOG_USER_ID; |
| 190 | 126 | } |
| 191 | 127 | |
| 192 | - // User is not allowed to federate comments. | |
| 193 | 128 | return user_can_activitypub( $current_user ); |
| 194 | 129 | } |
| 195 | 130 | |
| 196 | 131 | /** |
| @@ -289,9 +224,9 @@ | ||
| 289 | 224 | return false; |
| 290 | 225 | } |
| 291 | 226 | |
| 292 | 227 | if ( is_single_user() && \user_can( $user_id, 'activitypub' ) ) { |
| 293 | - // On a single user site, comments by users with the `activitypub` capability will be federated as the blog user. | |
| 228 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user. | |
| 294 | 229 | $user_id = Actors::BLOG_USER_ID; |
| 295 | 230 | } |
| 296 | 231 | |
| 297 | 232 | // User is not allowed to federate comments. |
| @@ -317,9 +252,9 @@ | ||
| 317 | 252 | * |
| 318 | 253 | * @return \WP_Comment|false Comment object, or false on failure. |
| 319 | 254 | */ |
| 320 | 255 | public static function object_id_to_comment( $id ) { |
| 321 | - $comment_query = new \WP_Comment_Query( | |
| 256 | + $comment_query = new WP_Comment_Query( | |
| 322 | 257 | array( |
| 323 | 258 | 'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 324 | 259 | 'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 325 | 260 | 'orderby' => 'comment_date', |
| @@ -342,18 +277,18 @@ | ||
| 342 | 277 | * |
| 343 | 278 | * @return string|null Comment ID or null if not found. |
| 344 | 279 | */ |
| 345 | 280 | public static function url_to_commentid( $url ) { |
| 346 | - if ( ! $url || ! \filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 281 | + if ( ! $url || ! filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 347 | 282 | return null; |
| 348 | 283 | } |
| 349 | 284 | |
| 350 | 285 | // Check for local comment. |
| 351 | - if ( is_same_domain( $url ) ) { | |
| 286 | + if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) === \wp_parse_url( $url, \PHP_URL_HOST ) ) { | |
| 352 | 287 | $query = \wp_parse_url( $url, \PHP_URL_QUERY ); |
| 353 | 288 | |
| 354 | 289 | if ( $query ) { |
| 355 | - \parse_str( $query, $params ); | |
| 290 | + parse_str( $query, $params ); | |
| 356 | 291 | |
| 357 | 292 | if ( ! empty( $params['c'] ) ) { |
| 358 | 293 | $comment = \get_comment( $params['c'] ); |
| 359 | 294 | |
| @@ -378,9 +313,9 @@ | ||
| 378 | 313 | ), |
| 379 | 314 | ), |
| 380 | 315 | ); |
| 381 | 316 | |
| 382 | - $query = new \WP_Comment_Query(); | |
| 317 | + $query = new WP_Comment_Query(); | |
| 383 | 318 | $comments = $query->query( $args ); |
| 384 | 319 | |
| 385 | 320 | if ( $comments && is_array( $comments ) ) { |
| 386 | 321 | return $comments[0]->comment_ID; |
| @@ -407,40 +342,8 @@ | ||
| 407 | 342 | return $classes; |
| 408 | 343 | } |
| 409 | 344 | |
| 410 | 345 | /** |
| 411 | - * Makes the comment feed filterable by comment type. | |
| 412 | - * | |
| 413 | - * Also excludes ActivityPub comment types from the feed when no type is specified. | |
| 414 | - * | |
| 415 | - * @param string $where The `WHERE` clause for the comment feed query. | |
| 416 | - * | |
| 417 | - * @return string The modified `WHERE` clause. | |
| 418 | - */ | |
| 419 | - public static function comment_feed_where( $where ) { | |
| 420 | - global $wpdb; | |
| 421 | - | |
| 422 | - $comment_type = \get_query_var( 'type' ); | |
| 423 | - | |
| 424 | - if ( 'all' === $comment_type ) { | |
| 425 | - return $where; | |
| 426 | - } | |
| 427 | - | |
| 428 | - $comment_types = self::get_comment_type_slugs(); | |
| 429 | - | |
| 430 | - if ( \in_array( $comment_type, $comment_types, true ) ) { | |
| 431 | - $where .= $wpdb->prepare( ' AND comment_type = %s', $comment_type ); | |
| 432 | - } else { | |
| 433 | - $comment_types = \array_map( 'esc_sql', $comment_types ); | |
| 434 | - $placeholders = implode( ', ', array_fill( 0, count( $comment_types ), '%s' ) ); | |
| 435 | - // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQL.NotPrepared | |
| 436 | - $where .= $wpdb->prepare( sprintf( ' AND comment_type NOT IN (%s)', $placeholders ), ...$comment_types ); | |
| 437 | - } | |
| 438 | - | |
| 439 | - return $where; | |
| 440 | - } | |
| 441 | - | |
| 442 | - /** | |
| 443 | 346 | * Gets the public comment id via the WordPress comments meta. |
| 444 | 347 | * |
| 445 | 348 | * @param int $wp_comment_id The internal WordPress comment ID. |
| 446 | 349 | * @param bool $fallback Whether the code should fall back to `source_url` if `source_id` is not set. |
| @@ -487,18 +390,15 @@ | ||
| 487 | 390 | * |
| 488 | 391 | * @return string $url |
| 489 | 392 | */ |
| 490 | 393 | public static function remote_comment_link( $comment_link, $comment ) { |
| 491 | - if ( ! $comment || \is_admin() || \is_search() ) { | |
| 394 | + if ( ! $comment || is_admin() ) { | |
| 492 | 395 | return $comment_link; |
| 493 | 396 | } |
| 494 | 397 | |
| 495 | - $remote_comment_link = null; | |
| 496 | - if ( 'comment' === $comment->comment_type ) { | |
| 497 | - $remote_comment_link = self::get_source_url( $comment->comment_ID ); | |
| 498 | - } | |
| 398 | + $public_comment_link = self::get_source_url( $comment->comment_ID ); | |
| 499 | 399 | |
| 500 | - return $remote_comment_link ?? $comment_link; | |
| 400 | + return $public_comment_link ?? $comment_link; | |
| 501 | 401 | } |
| 502 | 402 | |
| 503 | 403 | |
| 504 | 404 | /** |
| @@ -518,9 +418,9 @@ | ||
| 518 | 418 | return $public_comment_link; |
| 519 | 419 | } |
| 520 | 420 | |
| 521 | 421 | // Generate URI based on comment ID. |
| 522 | - return \add_query_arg( 'c', $comment->comment_ID, \home_url( '/' ) ); | |
| 422 | + return \add_query_arg( 'c', $comment->comment_ID, \trailingslashit( \home_url() ) ); | |
| 523 | 423 | } |
| 524 | 424 | |
| 525 | 425 | /** |
| 526 | 426 | * Check if a post has remote comments |
| @@ -552,8 +452,61 @@ | ||
| 552 | 452 | return ! empty( $comments ); |
| 553 | 453 | } |
| 554 | 454 | |
| 555 | 455 | /** |
| 456 | + * Enqueue scripts for remote comments | |
| 457 | + */ | |
| 458 | + public static function enqueue_scripts() { | |
| 459 | + if ( ! \is_singular() || \is_user_logged_in() ) { | |
| 460 | + // Only on single pages, only for logged-out users. | |
| 461 | + return; | |
| 462 | + } | |
| 463 | + | |
| 464 | + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) { | |
| 465 | + // Post type does not support ActivityPub. | |
| 466 | + return; | |
| 467 | + } | |
| 468 | + | |
| 469 | + if ( ! \comments_open() || ! \get_comments_number() ) { | |
| 470 | + // No comments, no need to load the script. | |
| 471 | + return; | |
| 472 | + } | |
| 473 | + | |
| 474 | + if ( ! self::post_has_remote_comments( \get_the_ID() ) ) { | |
| 475 | + // No remote comments, no need to load the script. | |
| 476 | + return; | |
| 477 | + } | |
| 478 | + | |
| 479 | + $handle = 'activitypub-remote-reply'; | |
| 480 | + $data = array( | |
| 481 | + 'namespace' => ACTIVITYPUB_REST_NAMESPACE, | |
| 482 | + 'defaultAvatarUrl' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg', | |
| 483 | + ); | |
| 484 | + $js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) ); | |
| 485 | + $asset_file = ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply/index.asset.php'; | |
| 486 | + | |
| 487 | + if ( \file_exists( $asset_file ) ) { | |
| 488 | + $assets = require_once $asset_file; | |
| 489 | + | |
| 490 | + \wp_enqueue_script( | |
| 491 | + $handle, | |
| 492 | + \plugins_url( 'build/remote-reply/index.js', __DIR__ ), | |
| 493 | + $assets['dependencies'], | |
| 494 | + $assets['version'], | |
| 495 | + true | |
| 496 | + ); | |
| 497 | + \wp_add_inline_script( $handle, $js, 'before' ); | |
| 498 | + | |
| 499 | + \wp_enqueue_style( | |
| 500 | + $handle, | |
| 501 | + \plugins_url( 'build/remote-reply/style-index.css', __DIR__ ), | |
| 502 | + array( 'wp-components' ), | |
| 503 | + $assets['version'] | |
| 504 | + ); | |
| 505 | + } | |
| 506 | + } | |
| 507 | + | |
| 508 | + /** | |
| 556 | 509 | * Get the comment type by activity type. |
| 557 | 510 | * |
| 558 | 511 | * @param string $activity_type The activity type. |
| 559 | 512 | * |
| @@ -580,9 +533,9 @@ | ||
| 580 | 533 | */ |
| 581 | 534 | public static function get_comment_types() { |
| 582 | 535 | global $activitypub_comment_types; |
| 583 | 536 | |
| 584 | - return (array) $activitypub_comment_types; | |
| 537 | + return $activitypub_comment_types; | |
| 585 | 538 | } |
| 586 | 539 | |
| 587 | 540 | /** |
| 588 | 541 | * Is this a registered comment type. |
| @@ -605,15 +558,22 @@ | ||
| 605 | 558 | * |
| 606 | 559 | * @return array The registered custom comment type slugs. |
| 607 | 560 | */ |
| 608 | 561 | public static function get_comment_type_slugs() { |
| 609 | - if ( ! did_action( 'init' ) ) { | |
| 610 | - _doing_it_wrong( __METHOD__, 'This function should not be called before the init action has run. Comment types are only available after init.', '7.5.0' ); | |
| 562 | + return array_keys( self::get_comment_types() ); | |
| 563 | + } | |
| 611 | 564 | |
| 612 | - return array(); | |
| 613 | - } | |
| 565 | + /** | |
| 566 | + * Return the registered custom comment type slugs. | |
| 567 | + * | |
| 568 | + * @deprecated 4.5.0 Use get_comment_type_slugs instead. | |
| 569 | + * | |
| 570 | + * @return array The registered custom comment type slugs. | |
| 571 | + */ | |
| 572 | + public static function get_comment_type_names() { | |
| 573 | + _deprecated_function( __METHOD__, '4.5.0', 'get_comment_type_slugs' ); | |
| 614 | 574 | |
| 615 | - return array_keys( self::get_comment_types() ); | |
| 575 | + return self::get_comment_type_slugs(); | |
| 616 | 576 | } |
| 617 | 577 | |
| 618 | 578 | /** |
| 619 | 579 | * Get the custom comment type. |
| @@ -681,9 +641,9 @@ | ||
| 681 | 641 | 'repost', |
| 682 | 642 | array( |
| 683 | 643 | 'label' => __( 'Reposts', 'activitypub' ), |
| 684 | 644 | 'singular' => __( 'Repost', 'activitypub' ), |
| 685 | - 'description' => 'A repost (or Announce) is when a post appears in the timeline because someone else shared it, while still showing the original author as the source.', | |
| 645 | + 'description' => __( 'A repost on the indieweb is a post that is purely a 100% re-publication of another (typically someone else\'s) post.', 'activitypub' ), | |
| 686 | 646 | 'icon' => '♻️', |
| 687 | 647 | 'class' => 'p-repost', |
| 688 | 648 | 'type' => 'repost', |
| 689 | 649 | 'collection' => 'reposts', |
| @@ -700,9 +660,9 @@ | ||
| 700 | 660 | 'like', |
| 701 | 661 | array( |
| 702 | 662 | 'label' => __( 'Likes', 'activitypub' ), |
| 703 | 663 | 'singular' => __( 'Like', 'activitypub' ), |
| 704 | - 'description' => 'A like is a small positive reaction that shows appreciation for a post without sharing it further.', | |
| 664 | + 'description' => __( 'A like is a popular webaction button and in some cases post type on various silos such as Facebook and Instagram.', 'activitypub' ), | |
| 705 | 665 | 'icon' => '👍', |
| 706 | 666 | 'class' => 'p-like', |
| 707 | 667 | 'type' => 'like', |
| 708 | 668 | 'collection' => 'likes', |
| @@ -713,27 +673,8 @@ | ||
| 713 | 673 | /* translators: %d: Number of likes */ |
| 714 | 674 | 'count_plural' => _x( '%d likes', 'number of likes', 'activitypub' ), |
| 715 | 675 | ) |
| 716 | 676 | ); |
| 717 | - | |
| 718 | - register_comment_type( | |
| 719 | - 'quote', | |
| 720 | - array( | |
| 721 | - 'label' => __( 'Quotes', 'activitypub' ), | |
| 722 | - 'singular' => __( 'Quote', 'activitypub' ), | |
| 723 | - 'description' => 'A quote is when a post is shared along with an added comment, so the original post appears together with the sharer’s own words.', | |
| 724 | - 'icon' => '❞', | |
| 725 | - 'class' => 'p-quote', | |
| 726 | - 'type' => 'quote', | |
| 727 | - 'collection' => 'quotes', | |
| 728 | - 'activity_types' => array( 'quote' ), | |
| 729 | - 'excerpt' => html_entity_decode( \__( '… quoted this!', 'activitypub' ) ), | |
| 730 | - /* translators: %d: Number of quotes */ | |
| 731 | - 'count_single' => _x( '%d quote', 'number of quotes', 'activitypub' ), | |
| 732 | - /* translators: %d: Number of quotes */ | |
| 733 | - 'count_plural' => _x( '%d quotes', 'number of quotes', 'activitypub' ), | |
| 734 | - ) | |
| 735 | - ); | |
| 736 | 677 | } |
| 737 | 678 | |
| 738 | 679 | /** |
| 739 | 680 | * Show avatars on Activities if set. |
| @@ -755,12 +696,12 @@ | ||
| 755 | 696 | * @author Jan Boddez |
| 756 | 697 | * |
| 757 | 698 | * @see https://github.com/janboddez/indieblocks/blob/a2d59de358031056a649ee47a1332ce9e39d4ce2/includes/functions.php#L423-L432 |
| 758 | 699 | * |
| 759 | - * @param \WP_Comment_Query $query Comment count. | |
| 700 | + * @param WP_Comment_Query $query Comment count. | |
| 760 | 701 | */ |
| 761 | 702 | public static function comment_query( $query ) { |
| 762 | - if ( ! $query instanceof \WP_Comment_Query ) { | |
| 703 | + if ( ! $query instanceof WP_Comment_Query ) { | |
| 763 | 704 | return; |
| 764 | 705 | } |
| 765 | 706 | |
| 766 | 707 | // Do not exclude likes and reposts on ActivityPub requests. |
| @@ -767,119 +708,47 @@ | ||
| 767 | 708 | if ( defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) { |
| 768 | 709 | return; |
| 769 | 710 | } |
| 770 | 711 | |
| 771 | - // Do not exclude likes and reposts on REST requests (handled by rest_comment_query). | |
| 712 | + // Do not exclude likes and reposts on REST requests. | |
| 772 | 713 | if ( \wp_is_serving_rest_request() ) { |
| 773 | 714 | return; |
| 774 | 715 | } |
| 775 | 716 | |
| 776 | - // Filter post types for admin requests. | |
| 777 | - if ( \is_admin() ) { | |
| 778 | - $query->query_vars['post_type'] = self::get_allowed_comment_post_types(); | |
| 717 | + // Do not exclude likes and reposts on admin pages or on non-singular pages. | |
| 718 | + if ( is_admin() || ! is_singular() ) { | |
| 779 | 719 | return; |
| 780 | 720 | } |
| 781 | 721 | |
| 782 | - // Do not exclude likes and reposts on non-singular pages. | |
| 783 | - if ( ! \is_singular() ) { | |
| 784 | - return; | |
| 785 | - } | |
| 786 | - | |
| 787 | - // Do not exclude likes and reposts if the query is for specific types. | |
| 722 | + // Do not exclude likes and reposts if the query is for comments. | |
| 788 | 723 | if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) { |
| 789 | 724 | return; |
| 790 | 725 | } |
| 791 | 726 | |
| 792 | - // Do not exclude likes and reposts if the query is already excluding other comment types. | |
| 793 | - if ( ! empty( $query->query_vars['type__not_in'] ) ) { | |
| 794 | - return; | |
| 795 | - } | |
| 796 | - | |
| 797 | 727 | // Exclude likes and reposts by the ActivityPub plugin. |
| 798 | 728 | $query->query_vars['type__not_in'] = self::get_comment_type_slugs(); |
| 799 | 729 | } |
| 800 | 730 | |
| 801 | 731 | /** |
| 802 | - * Filters comments in REST API requests. | |
| 803 | - * | |
| 804 | - * Excludes comments on ActivityPub post types and ActivityPub comment | |
| 805 | - * types (likes, reposts) from the REST API. | |
| 806 | - * | |
| 807 | - * @param array $prepared_args Array of arguments for WP_Comment_Query. | |
| 808 | - * | |
| 809 | - * @return array Modified array of arguments. | |
| 810 | - */ | |
| 811 | - public static function rest_comment_query( $prepared_args ) { | |
| 812 | - // Exclude comments on ActivityPub post types. | |
| 813 | - $prepared_args['post_type'] = self::get_allowed_comment_post_types(); | |
| 814 | - | |
| 815 | - // Exclude ActivityPub comment types (likes, reposts) unless explicitly requested. | |
| 816 | - if ( empty( $prepared_args['type'] ) && empty( $prepared_args['type__in'] ) ) { | |
| 817 | - $prepared_args['type__not_in'] = self::get_comment_type_slugs(); | |
| 818 | - } | |
| 819 | - | |
| 820 | - return $prepared_args; | |
| 821 | - } | |
| 822 | - | |
| 823 | - /** | |
| 824 | - * Returns post types that should show comments (excluding hidden post types). | |
| 825 | - * | |
| 826 | - * @return array Array of post type names. | |
| 827 | - */ | |
| 828 | - private static function get_allowed_comment_post_types() { | |
| 829 | - $hide_for = self::hide_for(); | |
| 830 | - | |
| 831 | - if ( empty( $hide_for ) ) { | |
| 832 | - return \get_post_types_by_support( 'comments' ); | |
| 833 | - } | |
| 834 | - | |
| 835 | - return \array_diff( \get_post_types_by_support( 'comments' ), $hide_for ); | |
| 836 | - } | |
| 837 | - | |
| 838 | - /** | |
| 839 | 732 | * Filter the comment status before it is set. |
| 840 | 733 | * |
| 841 | - * @param int|string|\WP_Error $approved The approved comment status. | |
| 842 | - * @param array $comment_data The comment data. | |
| 734 | + * @param string $approved The approved comment status. | |
| 735 | + * @param array $commentdata The comment data. | |
| 843 | 736 | * |
| 844 | - * @return int|string|\WP_Error The approval status. 1, 0, 'spam', 'trash', or WP_Error. | |
| 737 | + * @return boolean `true` if the comment is approved, `false` otherwise. | |
| 845 | 738 | */ |
| 846 | - public static function pre_comment_approved( $approved, $comment_data ) { | |
| 847 | - /* | |
| 848 | - * Only return early for already-approved comments, trash, or errors. | |
| 849 | - * Don't short-circuit on 'spam' - we may want to override Akismet. | |
| 850 | - * Respect 'trash' since it comes from the WordPress disallowed list. | |
| 851 | - */ | |
| 852 | - if ( 1 === $approved || '1' === $approved || 'trash' === $approved || \is_wp_error( $approved ) ) { | |
| 739 | + public static function pre_comment_approved( $approved, $commentdata ) { | |
| 740 | + if ( $approved || \is_wp_error( $approved ) ) { | |
| 853 | 741 | return $approved; |
| 854 | 742 | } |
| 855 | 743 | |
| 856 | - // Maybe auto-approve likes and reposts. | |
| 857 | - if ( | |
| 858 | - \in_array( $comment_data['comment_type'], self::get_comment_type_slugs(), true ) && | |
| 859 | - '1' === \get_option( 'activitypub_auto_approve_reactions' ) | |
| 860 | - ) { | |
| 861 | - return 1; | |
| 862 | - } | |
| 863 | - | |
| 864 | - /* | |
| 865 | - * Always auto-approve comments on remote posts (ap_post) since | |
| 866 | - * they are not visible in the WP admin comment moderation screen. | |
| 867 | - */ | |
| 868 | - $post_id = $comment_data['comment_post_ID']; | |
| 869 | - $post = \get_post( $post_id ); | |
| 870 | - | |
| 871 | - if ( $post && \in_array( $post->post_type, self::hide_for(), true ) ) { | |
| 872 | - return 1; | |
| 873 | - } | |
| 874 | - | |
| 875 | 744 | if ( '1' !== \get_option( 'comment_previously_approved' ) ) { |
| 876 | 745 | return $approved; |
| 877 | 746 | } |
| 878 | 747 | |
| 879 | 748 | if ( |
| 880 | - empty( $comment_data['comment_meta']['protocol'] ) || | |
| 881 | - 'activitypub' !== $comment_data['comment_meta']['protocol'] | |
| 749 | + empty( $commentdata['comment_meta']['protocol'] ) || | |
| 750 | + 'activitypub' !== $commentdata['comment_meta']['protocol'] | |
| 882 | 751 | ) { |
| 883 | 752 | return $approved; |
| 884 | 753 | } |
| 885 | 754 | |
| @@ -884,10 +753,10 @@ | ||
| 884 | 753 | } |
| 885 | 754 | |
| 886 | 755 | global $wpdb; |
| 887 | 756 | |
| 888 | - $author = $comment_data['comment_author']; | |
| 889 | - $author_url = $comment_data['comment_author_url']; | |
| 757 | + $author = $commentdata['comment_author']; | |
| 758 | + $author_url = $commentdata['comment_author_url']; | |
| 890 | 759 | // phpcs:ignore |
| 891 | 760 | $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_url = %s and comment_approved = '1' LIMIT 1", $author, $author_url ) ); |
| 892 | 761 | |
| 893 | 762 | if ( 1 === (int) $ok_to_comment ) { |
| @@ -924,31 +793,8 @@ | ||
| 924 | 793 | if ( null === $new_count ) { |
| 925 | 794 | $excluded_types = array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) ); |
| 926 | 795 | |
| 927 | 796 | if ( ! empty( $excluded_types ) ) { |
| 928 | - /* | |
| 929 | - * Include 'note' type when Gutenberg's filter is registered, so a | |
| 930 | - * single query excludes both ActivityPub and Gutenberg types. | |
| 931 | - */ | |
| 932 | - if ( \has_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count' ) ) { | |
| 933 | - $excluded_types[] = 'note'; | |
| 934 | - } | |
| 935 | - | |
| 936 | - /** | |
| 937 | - * Filters the comment types excluded from the comment count. | |
| 938 | - * | |
| 939 | - * Runs at priority 5 on `pre_wp_update_comment_count_now` so that | |
| 940 | - * a single query can exclude types from multiple plugins. Other | |
| 941 | - * plugins can hook here to add their own comment types. | |
| 942 | - * | |
| 943 | - * @since 8.0.0 | |
| 944 | - * | |
| 945 | - * @param string[] $excluded_types The comment type slugs to exclude. | |
| 946 | - * @param int $post_id The post ID. | |
| 947 | - */ | |
| 948 | - $excluded_types = \apply_filters( 'activitypub_excluded_comment_types', $excluded_types, $post_id ); | |
| 949 | - $excluded_types = array_unique( array_filter( $excluded_types ) ); | |
| 950 | - | |
| 951 | 797 | global $wpdb; |
| 952 | 798 | |
| 953 | 799 | // phpcs:ignore WordPress.DB |
| 954 | 800 | $new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ('" . implode( "','", $excluded_types ) . "')", $post_id ) ); |
| @@ -965,74 +811,6 @@ | ||
| 965 | 811 | * @return bool True if the comment type is enabled. |
| 966 | 812 | */ |
| 967 | 813 | public static function is_comment_type_enabled( $comment_type ) { |
| 968 | 814 | return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' ); |
| 969 | - } | |
| 970 | - | |
| 971 | - /** | |
| 972 | - * Get post types to hide comments for in admin. | |
| 973 | - * | |
| 974 | - * These are non-public post types whose comments should not appear | |
| 975 | - * in the main comments list in the WordPress admin. | |
| 976 | - * | |
| 977 | - * @return string[] Array of post type names to hide comments for. | |
| 978 | - */ | |
| 979 | - public static function hide_for() { | |
| 980 | - $post_types = array( Remote_Posts::POST_TYPE ); | |
| 981 | - | |
| 982 | - /** | |
| 983 | - * Filters the list of post types to hide comments for. | |
| 984 | - * | |
| 985 | - * @param string[] $post_types Array of post type names to hide comments for. | |
| 986 | - */ | |
| 987 | - return \apply_filters( 'activitypub_hide_comments_for', $post_types ); | |
| 988 | - } | |
| 989 | - | |
| 990 | - /** | |
| 991 | - * Render emoji in comment author name. | |
| 992 | - * | |
| 993 | - * Replaces emoji shortcodes with img tags on the get_comment_author filter. | |
| 994 | - * Emoji data is retrieved from the linked remote actor. | |
| 995 | - * | |
| 996 | - * @param string $author The comment author name. | |
| 997 | - * @param string $comment_id The comment ID as a numeric string. | |
| 998 | - * | |
| 999 | - * @return string The comment author name with rendered emoji. | |
| 1000 | - */ | |
| 1001 | - public static function render_emoji( $author, $comment_id ) { | |
| 1002 | - $remote_actor_id = \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ); | |
| 1003 | - | |
| 1004 | - if ( empty( $remote_actor_id ) ) { | |
| 1005 | - return $author; | |
| 1006 | - } | |
| 1007 | - | |
| 1008 | - $emoji_data = \get_post_meta( $remote_actor_id, '_activitypub_emoji', true ); | |
| 1009 | - | |
| 1010 | - if ( empty( $emoji_data ) ) { | |
| 1011 | - return $author; | |
| 1012 | - } | |
| 1013 | - | |
| 1014 | - return Emoji::replace_from_json( $author, $emoji_data ); | |
| 1015 | - } | |
| 1016 | - | |
| 1017 | - /** | |
| 1018 | - * Selectively unescape emoji images in comment author. | |
| 1019 | - * | |
| 1020 | - * This runs at priority 20 after WordPress's esc_html() filter on comment_author. | |
| 1021 | - * | |
| 1022 | - * @param string $author The comment author name (already escaped by WordPress). | |
| 1023 | - * | |
| 1024 | - * @return string The comment author name with emoji images unescaped. | |
| 1025 | - */ | |
| 1026 | - public static function unescape_emoji( $author ) { | |
| 1027 | - // Only attempt to unescape if there are emoji images present in the escaped string. | |
| 1028 | - if ( false === \strpos( $author, 'class="emoji"' ) ) { | |
| 1029 | - return $author; | |
| 1030 | - } | |
| 1031 | - | |
| 1032 | - // Decode entities so we can selectively restore emoji <img> tags. | |
| 1033 | - $decoded = \html_entity_decode( $author, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); | |
| 1034 | - | |
| 1035 | - // Use strict KSES validation to only allow valid emoji img tags. | |
| 1036 | - return \wp_kses( $decoded, Emoji::get_kses_allowed_html() ); | |
| 1037 | 815 | } |
| 1038 | 816 | } |