PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/class-comment.php +69 -319 9.3.07.7.0 View file →
@@ -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 Activitypub\Collection\Posts;
12 12
13 13 /**
14 14 * ActivityPub Comment Class.
15 15 *
@@ -28,50 +28,16 @@
28 28 \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 );
29 29 \add_filter( 'comment_feed_where', array( static::class, 'comment_feed_where' ) );
30 30 \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 );
31 31 \add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
32 - \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 11, 2 );
32 + \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 );
33 33 \add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 );
34 34 \add_action( 'update_option_activitypub_allow_likes', array( self::class, 'maybe_update_comment_counts' ), 10, 2 );
35 35 \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, 2 ); // 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.
36 + \add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
41 37 }
42 38
43 39 /**
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 40 * Remove edit capabilities for comments received via ActivityPub.
75 41 *
76 42 * @param array $caps Array of capabilities.
77 43 * @param string $cap Capability name.
@@ -92,12 +58,10 @@
92 58
93 59 /**
94 60 * Filter the comment reply link.
95 61 *
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
62 + * We don't want to show the comment reply link for federated comments
63 + * if the user is disabled for federation.
100 64 *
101 65 * @param string $link The HTML markup for the comment reply link.
102 66 * @param array $args An array of arguments overriding the defaults.
103 67 * @param \WP_Comment $comment The object of the comment being replied.
@@ -108,42 +72,8 @@
108 72 if ( self::are_comments_allowed( $comment ) ) {
109 73 return $link;
110 74 }
111 75
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 - );
130 - }
131 -
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 );
144 - }
145 -
146 76 if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( 'activitypub/remote-reply' ) ) {
147 77 \register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply' );
148 78 }
149 79
@@ -177,20 +107,19 @@
177 107 if ( ! self::was_received( $comment ) ) {
178 108 return true;
179 109 }
180 110
181 - $current_user = \get_current_user_id();
111 + $current_user = get_current_user_id();
182 112
183 113 if ( ! $current_user ) {
184 114 return false;
185 115 }
186 116
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.
117 + if ( is_single_user() && \user_can( $current_user, 'publish_posts' ) ) {
118 + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user.
189 119 $current_user = Actors::BLOG_USER_ID;
190 120 }
191 121
192 - // User is not allowed to federate comments.
193 122 return user_can_activitypub( $current_user );
194 123 }
195 124
196 125 /**
@@ -289,9 +218,9 @@
289 218 return false;
290 219 }
291 220
292 221 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.
222 + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user.
294 223 $user_id = Actors::BLOG_USER_ID;
295 224 }
296 225
297 226 // User is not allowed to federate comments.
@@ -298,19 +227,8 @@
298 227 if ( ! user_can_activitypub( $user_id ) ) {
299 228 return false;
300 229 }
301 230
302 - /*
303 - * Do not federate brand-new comments on a post that is not federated itself
304 - * (e.g. a private post, a post switched to local visibility, or a non-ActivityPub
305 - * post type). This prevents leaking replies on content the post type's read rules
306 - * would otherwise protect. Comments that were already sent are allowed through so
307 - * their Update and Delete activities can still federate (and tear down remote copies).
308 - */
309 - if ( ! self::was_sent( $comment ) && ! is_post_federated( $comment->comment_post_ID ) ) {
310 - return false;
311 - }
312 -
313 231 // It is a comment to the post and can be federated.
314 232 if ( empty( $comment->comment_parent ) ) {
315 233 return true;
316 234 }
@@ -323,33 +241,22 @@
323 241
324 242 /**
325 243 * Examine a comment ID and look up an existing comment it represents.
326 244 *
327 - * @since 9.1.0 Added the `$args` parameter.
245 + * @param string $id ActivityPub object ID (usually a URL) to check.
328 246 *
329 - * @param string $id ActivityPub object ID (usually a URL) to check.
330 - * @param array $args Optional. Additional WP_Comment_Query arguments. Pass `array( 'status' => 'any' )`
331 - * to also match comments in spam or trash, which the default status excludes.
332 - *
333 247 * @return \WP_Comment|false Comment object, or false on failure.
334 248 */
335 - public static function object_id_to_comment( $id, $args = array() ) {
336 - $args = \wp_parse_args(
337 - $args,
249 + public static function object_id_to_comment( $id ) {
250 + $comment_query = new \WP_Comment_Query(
338 251 array(
339 - 'number' => 1,
340 - 'orderby' => 'comment_date',
341 - 'order' => 'DESC',
252 + 'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
253 + 'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
254 + 'orderby' => 'comment_date',
255 + 'order' => 'DESC',
342 256 )
343 257 );
344 258
345 - // Force the lookup key and full comment objects, so callers cannot break the return contract.
346 - $args['fields'] = 'all';
347 - $args['meta_key'] = 'source_id'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
348 - $args['meta_value'] = $id; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
349 -
350 - $comment_query = new \WP_Comment_Query( $args );
351 -
352 259 if ( ! $comment_query->comments ) {
353 260 return false;
354 261 }
355 262
@@ -403,9 +310,9 @@
403 310
404 311 $query = new \WP_Comment_Query();
405 312 $comments = $query->query( $args );
406 313
407 - if ( $comments && \is_array( $comments ) ) {
314 + if ( $comments && is_array( $comments ) ) {
408 315 return $comments[0]->comment_ID;
409 316 }
410 317
411 318 return null;
@@ -421,9 +328,9 @@
421 328 * @return string[] An array of classes.
422 329 */
423 330 public static function comment_class( $classes, $css_class, $comment_id ) {
424 331 // Check if ActivityPub comment.
425 - if ( 'activitypub' === \get_comment_meta( $comment_id, 'protocol', true ) ) {
332 + if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) {
426 333 $classes[] = 'activitypub-comment';
427 334 }
428 335
429 336 return $classes;
@@ -451,11 +358,12 @@
451 358
452 359 if ( \in_array( $comment_type, $comment_types, true ) ) {
453 360 $where .= $wpdb->prepare( ' AND comment_type = %s', $comment_type );
454 361 } else {
455 - $placeholders = \implode( ', ', \array_fill( 0, \count( $comment_types ), '%s' ) );
362 + $comment_types = \array_map( 'esc_sql', $comment_types );
363 + $placeholders = implode( ', ', array_fill( 0, count( $comment_types ), '%s' ) );
456 364 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQL.NotPrepared
457 - $where .= $wpdb->prepare( \sprintf( ' AND comment_type NOT IN (%s)', $placeholders ), ...$comment_types );
365 + $where .= $wpdb->prepare( sprintf( ' AND comment_type NOT IN (%s)', $placeholders ), ...$comment_types );
458 366 }
459 367
460 368 return $where;
461 369 }
@@ -585,9 +493,9 @@
585 493 $activity_type = \sanitize_key( $activity_type );
586 494 $comment_types = self::get_comment_types();
587 495
588 496 foreach ( $comment_types as $comment_type ) {
589 - if ( \in_array( $activity_type, $comment_type['activity_types'], true ) ) {
497 + if ( in_array( $activity_type, $comment_type['activity_types'], true ) ) {
590 498 return $comment_type;
591 499 }
592 500 }
593 501
@@ -601,9 +509,9 @@
601 509 */
602 510 public static function get_comment_types() {
603 511 global $activitypub_comment_types;
604 512
605 - return (array) $activitypub_comment_types;
513 + return $activitypub_comment_types;
606 514 }
607 515
608 516 /**
609 517 * Is this a registered comment type.
@@ -626,15 +534,15 @@
626 534 *
627 535 * @return array The registered custom comment type slugs.
628 536 */
629 537 public static function get_comment_type_slugs() {
630 - if ( ! \did_action( 'init' ) ) {
631 - \_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' );
538 + if ( ! did_action( 'init' ) ) {
539 + _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' );
632 540
633 541 return array();
634 542 }
635 543
636 - return \array_keys( self::get_comment_types() );
544 + return array_keys( self::get_comment_types() );
637 545 }
638 546
639 547 /**
640 548 * Get the custom comment type.
@@ -648,16 +556,16 @@
648 556 *
649 557 * @return array The comment type.
650 558 */
651 559 public static function get_comment_type( $type ) {
652 - $type = \strtolower( $type );
653 - $type = \sanitize_key( $type );
560 + $type = strtolower( $type );
561 + $type = sanitize_key( $type );
654 562
655 563 $comment_types = self::get_comment_types();
656 564 $type_array = array();
657 565
658 566 // Check array keys.
659 - if ( \in_array( $type, \array_keys( $comment_types ), true ) ) {
567 + if ( in_array( $type, array_keys( $comment_types ), true ) ) {
660 568 $type_array = $comment_types[ $type ];
661 569 }
662 570
663 571 /**
@@ -664,9 +572,9 @@
664 572 * Filter the comment type.
665 573 *
666 574 * @param array $type_array The comment type.
667 575 */
668 - return \apply_filters( "activitypub_comment_type_{$type}", $type_array );
576 + return apply_filters( "activitypub_comment_type_{$type}", $type_array );
669 577 }
670 578
671 579 /**
672 580 * Get a comment type attribute.
@@ -690,9 +598,9 @@
690 598 *
691 599 * @param mixed $value The value of the attribute.
692 600 * @param string $type The comment type.
693 601 */
694 - return \apply_filters( "activitypub_comment_type_{$attr}", $value, $type );
602 + return apply_filters( "activitypub_comment_type_{$attr}", $value, $type );
695 603 }
696 604
697 605 /**
698 606 * Register the comment types used by the ActivityPub plugin.
@@ -700,10 +608,10 @@
700 608 public static function register_comment_types() {
701 609 register_comment_type(
702 610 'repost',
703 611 array(
704 - 'label' => \__( 'Reposts', 'activitypub' ),
705 - 'singular' => \__( 'Repost', 'activitypub' ),
612 + 'label' => __( 'Reposts', 'activitypub' ),
613 + 'singular' => __( 'Repost', 'activitypub' ),
706 614 '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.',
707 615 'icon' => '♻️',
708 616 'class' => 'p-repost',
709 617 'type' => 'repost',
@@ -708,13 +616,13 @@
708 616 'class' => 'p-repost',
709 617 'type' => 'repost',
710 618 'collection' => 'reposts',
711 619 'activity_types' => array( 'announce' ),
712 - 'excerpt' => \html_entity_decode( \__( '&hellip; reposted this!', 'activitypub' ) ),
620 + 'excerpt' => html_entity_decode( \__( '&hellip; reposted this!', 'activitypub' ) ),
713 621 /* translators: %d: Number of reposts */
714 - 'count_single' => \_x( '%d repost', 'number of reposts', 'activitypub' ),
622 + 'count_single' => _x( '%d repost', 'number of reposts', 'activitypub' ),
715 623 /* translators: %d: Number of reposts */
716 - 'count_plural' => \_x( '%d reposts', 'number of reposts', 'activitypub' ),
624 + 'count_plural' => _x( '%d reposts', 'number of reposts', 'activitypub' ),
717 625 )
718 626 );
719 627
720 628 register_comment_type(
@@ -719,10 +627,10 @@
719 627
720 628 register_comment_type(
721 629 'like',
722 630 array(
723 - 'label' => \__( 'Likes', 'activitypub' ),
724 - 'singular' => \__( 'Like', 'activitypub' ),
631 + 'label' => __( 'Likes', 'activitypub' ),
632 + 'singular' => __( 'Like', 'activitypub' ),
725 633 'description' => 'A like is a small positive reaction that shows appreciation for a post without sharing it further.',
726 634 'icon' => '👍',
727 635 'class' => 'p-like',
728 636 'type' => 'like',
@@ -727,13 +635,13 @@
727 635 'class' => 'p-like',
728 636 'type' => 'like',
729 637 'collection' => 'likes',
730 638 'activity_types' => array( 'like' ),
731 - 'excerpt' => \html_entity_decode( \__( '&hellip; liked this!', 'activitypub' ) ),
639 + 'excerpt' => html_entity_decode( \__( '&hellip; liked this!', 'activitypub' ) ),
732 640 /* translators: %d: Number of likes */
733 - 'count_single' => \_x( '%d like', 'number of likes', 'activitypub' ),
641 + 'count_single' => _x( '%d like', 'number of likes', 'activitypub' ),
734 642 /* translators: %d: Number of likes */
735 - 'count_plural' => \_x( '%d likes', 'number of likes', 'activitypub' ),
643 + 'count_plural' => _x( '%d likes', 'number of likes', 'activitypub' ),
736 644 )
737 645 );
738 646
739 647 register_comment_type(
@@ -738,10 +646,10 @@
738 646
739 647 register_comment_type(
740 648 'quote',
741 649 array(
742 - 'label' => \__( 'Quotes', 'activitypub' ),
743 - 'singular' => \__( 'Quote', 'activitypub' ),
650 + 'label' => __( 'Quotes', 'activitypub' ),
651 + 'singular' => __( 'Quote', 'activitypub' ),
744 652 'description' => 'A quote is when a post is shared along with an added comment, so the original post appears together with the sharer&#8217;s own words.',
745 653 'icon' => '❞',
746 654 'class' => 'p-quote',
747 655 'type' => 'quote',
@@ -746,13 +654,13 @@
746 654 'class' => 'p-quote',
747 655 'type' => 'quote',
748 656 'collection' => 'quotes',
749 657 'activity_types' => array( 'quote' ),
750 - 'excerpt' => \html_entity_decode( \__( '&hellip; quoted this!', 'activitypub' ) ),
658 + 'excerpt' => html_entity_decode( \__( '&hellip; quoted this!', 'activitypub' ) ),
751 659 /* translators: %d: Number of quotes */
752 - 'count_single' => \_x( '%d quote', 'number of quotes', 'activitypub' ),
660 + 'count_single' => _x( '%d quote', 'number of quotes', 'activitypub' ),
753 661 /* translators: %d: Number of quotes */
754 - 'count_plural' => \_x( '%d quotes', 'number of quotes', 'activitypub' ),
662 + 'count_plural' => _x( '%d quotes', 'number of quotes', 'activitypub' ),
755 663 )
756 664 );
757 665 }
758 666
@@ -764,11 +672,11 @@
764 672 * @return array show avatars on Activities
765 673 */
766 674 public static function get_avatar_comment_types( $types ) {
767 675 $comment_types = self::get_comment_type_slugs();
768 - $types = \array_merge( $types, $comment_types );
676 + $types = array_merge( $types, $comment_types );
769 677
770 - return \array_unique( $types );
678 + return array_unique( $types );
771 679 }
772 680
773 681 /**
774 682 * Excludes likes and reposts from comment queries.
@@ -784,20 +692,22 @@
784 692 return;
785 693 }
786 694
787 695 // Do not exclude likes and reposts on ActivityPub requests.
788 - if ( \defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) {
696 + if ( defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) {
789 697 return;
790 698 }
791 699
792 - // Do not exclude likes and reposts on REST requests (handled by rest_comment_query).
700 + // Do not exclude likes and reposts on REST requests.
793 701 if ( \wp_is_serving_rest_request() ) {
794 702 return;
795 703 }
796 704
797 - // Filter post types for admin requests.
705 + // Do only exclude interactions of `ap_post` post type.
798 706 if ( \is_admin() ) {
799 - $query->query_vars['post_type'] = self::get_allowed_comment_post_types();
707 + if ( \get_option( 'activitypub_create_posts', false ) ) {
708 + $query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), array( Posts::POST_TYPE ) );
709 + }
800 710 return;
801 711 }
802 712
803 713 // Do not exclude likes and reposts on non-singular pages.
@@ -804,60 +714,18 @@
804 714 if ( ! \is_singular() ) {
805 715 return;
806 716 }
807 717
808 - // Do not exclude likes and reposts if the query is for specific types.
718 + // Do not exclude likes and reposts if the query is for comments.
809 719 if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) {
810 720 return;
811 721 }
812 722
813 - // Do not exclude likes and reposts if the query is already excluding other comment types.
814 - if ( ! empty( $query->query_vars['type__not_in'] ) ) {
815 - return;
816 - }
817 -
818 723 // Exclude likes and reposts by the ActivityPub plugin.
819 724 $query->query_vars['type__not_in'] = self::get_comment_type_slugs();
820 725 }
821 726
822 727 /**
823 - * Filters comments in REST API requests.
824 - *
825 - * Excludes comments on ActivityPub post types and ActivityPub comment
826 - * types (likes, reposts) from the REST API.
827 - *
828 - * @param array $prepared_args Array of arguments for WP_Comment_Query.
829 - *
830 - * @return array Modified array of arguments.
831 - */
832 - public static function rest_comment_query( $prepared_args ) {
833 - // Exclude comments on ActivityPub post types.
834 - $prepared_args['post_type'] = self::get_allowed_comment_post_types();
835 -
836 - // Exclude ActivityPub comment types (likes, reposts) unless explicitly requested.
837 - if ( empty( $prepared_args['type'] ) && empty( $prepared_args['type__in'] ) ) {
838 - $prepared_args['type__not_in'] = self::get_comment_type_slugs();
839 - }
840 -
841 - return $prepared_args;
842 - }
843 -
844 - /**
845 - * Returns post types that should show comments (excluding hidden post types).
846 - *
847 - * @return array Array of post type names.
848 - */
849 - private static function get_allowed_comment_post_types() {
850 - $hide_for = self::hide_for();
851 -
852 - if ( empty( $hide_for ) ) {
853 - return \get_post_types_by_support( 'comments' );
854 - }
855 -
856 - return \array_diff( \get_post_types_by_support( 'comments' ), $hide_for );
857 - }
858 -
859 - /**
860 728 * Filter the comment status before it is set.
861 729 *
862 730 * @param int|string|\WP_Error $approved The approved comment status.
863 731 * @param array $comment_data The comment data.
@@ -864,14 +732,9 @@
864 732 *
865 733 * @return int|string|\WP_Error The approval status. 1, 0, 'spam', 'trash', or WP_Error.
866 734 */
867 735 public static function pre_comment_approved( $approved, $comment_data ) {
868 - /*
869 - * Only return early for already-approved comments, trash, or errors.
870 - * Don't short-circuit on 'spam' - we may want to override Akismet.
871 - * Respect 'trash' since it comes from the WordPress disallowed list.
872 - */
873 - if ( 1 === $approved || '1' === $approved || 'trash' === $approved || \is_wp_error( $approved ) ) {
736 + if ( $approved || \is_wp_error( $approved ) ) {
874 737 return $approved;
875 738 }
876 739
877 740 // Maybe auto-approve likes and reposts.
@@ -881,19 +744,8 @@
881 744 ) {
882 745 return 1;
883 746 }
884 747
885 - /*
886 - * Always auto-approve comments on remote posts (ap_post) since
887 - * they are not visible in the WP admin comment moderation screen.
888 - */
889 - $post_id = $comment_data['comment_post_ID'];
890 - $post = \get_post( $post_id );
891 -
892 - if ( $post && \in_array( $post->post_type, self::hide_for(), true ) ) {
893 - return 1;
894 - }
895 -
896 748 if ( '1' !== \get_option( 'comment_previously_approved' ) ) {
897 749 return $approved;
898 750 }
899 751
@@ -914,8 +766,18 @@
914 766 if ( 1 === (int) $ok_to_comment ) {
915 767 return 1;
916 768 }
917 769
770 + // Auto approve reactions to an `ap_post`.
771 + if ( \get_option( 'activitypub_create_posts', false ) ) {
772 + $post_id = $comment_data['comment_post_ID'];
773 + $post = \get_post( $post_id );
774 +
775 + if ( $post && Posts::POST_TYPE === $post->post_type ) {
776 + return 1;
777 + }
778 + }
779 +
918 780 return $approved;
919 781 }
920 782
921 783 /**
@@ -942,38 +804,15 @@
942 804 * @return int|null The updated comment count, or null to use the default query.
943 805 */
944 806 public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) {
945 807 if ( null === $new_count ) {
946 - $excluded_types = \array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) );
808 + $excluded_types = array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) );
947 809
948 810 if ( ! empty( $excluded_types ) ) {
949 - /*
950 - * Include 'note' type when Gutenberg's filter is registered, so a
951 - * single query excludes both ActivityPub and Gutenberg types.
952 - */
953 - if ( \has_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count' ) ) {
954 - $excluded_types[] = 'note';
955 - }
956 -
957 - /**
958 - * Filters the comment types excluded from the comment count.
959 - *
960 - * Runs at priority 5 on `pre_wp_update_comment_count_now` so that
961 - * a single query can exclude types from multiple plugins. Other
962 - * plugins can hook here to add their own comment types.
963 - *
964 - * @since 8.0.0
965 - *
966 - * @param string[] $excluded_types The comment type slugs to exclude.
967 - * @param int $post_id The post ID.
968 - */
969 - $excluded_types = \apply_filters( 'activitypub_excluded_comment_types', $excluded_types, $post_id );
970 - $excluded_types = \array_unique( \array_filter( $excluded_types ) );
971 -
972 811 global $wpdb;
973 812
974 813 // phpcs:ignore WordPress.DB
975 - $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 ) );
814 + $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 ) );
976 815 }
977 816 }
978 817
979 818 return $new_count;
@@ -985,96 +824,7 @@
985 824 * @param string $comment_type The comment type.
986 825 * @return bool True if the comment type is enabled.
987 826 */
988 827 public static function is_comment_type_enabled( $comment_type ) {
989 - return '1' === \get_option( "activitypub_allow_{$comment_type}s", '1' );
990 - }
991 -
992 - /**
993 - * Get post types to hide comments for in admin.
994 - *
995 - * These are non-public post types whose comments should not appear
996 - * in the main comments list in the WordPress admin.
997 - *
998 - * @return string[] Array of post type names to hide comments for.
999 - */
1000 - public static function hide_for() {
1001 - $post_types = array( Remote_Posts::POST_TYPE );
1002 -
1003 - /**
1004 - * Filters the list of post types to hide comments for.
1005 - *
1006 - * @param string[] $post_types Array of post type names to hide comments for.
1007 - */
1008 - return \apply_filters( 'activitypub_hide_comments_for', $post_types );
1009 - }
1010 -
1011 - /**
1012 - * Render emoji in comment author name.
1013 - *
1014 - * Replaces emoji shortcodes with img tags on the get_comment_author filter.
1015 - * Emoji data is retrieved from the linked remote actor.
1016 - *
1017 - * @param string $author The comment author name.
1018 - * @param string $comment_id The comment ID as a numeric string.
1019 - *
1020 - * @return string The comment author name with rendered emoji.
1021 - */
1022 - public static function render_emoji( $author, $comment_id ) {
1023 - $remote_actor_id = \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true );
1024 -
1025 - if ( empty( $remote_actor_id ) ) {
1026 - return $author;
1027 - }
1028 -
1029 - $emoji_data = \get_post_meta( $remote_actor_id, '_activitypub_emoji', true );
1030 -
1031 - if ( empty( $emoji_data ) ) {
1032 - return $author;
1033 - }
1034 -
1035 - return Emoji::replace_from_json( $author, $emoji_data );
1036 - }
1037 -
1038 - /**
1039 - * Selectively unescape emoji images in comment author.
1040 - *
1041 - * This runs at priority 20 after WordPress's esc_html() filter on comment_author.
1042 - *
1043 - * @since 9.3.0 Added the `$comment_id` parameter.
1044 - *
1045 - * @param string $author The comment author name (already escaped by WordPress).
1046 - * @param int|string $comment_id Optional. The comment ID, as a numeric string from core. Default 0.
1047 - *
1048 - * @return string The comment author name with emoji images unescaped.
1049 - */
1050 - public static function unescape_emoji( $author, $comment_id = 0 ) {
1051 - /*
1052 - * Core always passes the comment ID, but plugins and themes re-apply this filter
1053 - * with the name alone. Fall back to the comment in scope so a one-argument caller
1054 - * does not leave the emoji img sitting there as escaped text.
1055 - */
1056 - if ( ! $comment_id ) {
1057 - $comment_id = \get_comment_ID();
1058 - }
1059 -
1060 - /*
1061 - * Only ActivityPub comments can carry emoji, since render_emoji() is what puts the
1062 - * img tags there in the first place. Scope this the same way, so an author name
1063 - * written by anything else is never decoded -- the substring check below is not a
1064 - * reliable signal on its own, and this filter runs on every comment on the site.
1065 - */
1066 - if ( false === \strpos( $author, 'class=&quot;emoji&quot;' ) ) {
1067 - return $author;
1068 - }
1069 -
1070 - if ( ! \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ) ) {
1071 - return $author;
1072 - }
1073 -
1074 - // Decode entities so we can selectively restore emoji <img> tags.
1075 - $decoded = \html_entity_decode( $author, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
1076 -
1077 - // Use strict KSES validation to only allow valid emoji img tags.
1078 - return \wp_kses( $decoded, Emoji::get_kses_allowed_html() );
828 + return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
1079 829 }
1080 830 }