| @@ -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 | * |
| @@ -32,46 +32,12 @@ | ||
| 32 | 32 | \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 11, 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 ); // 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 | |
| @@ -183,14 +113,13 @@ | ||
| 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 | } |
| @@ -591,9 +509,9 @@ | ||
| 591 | 509 | */ |
| 592 | 510 | public static function get_comment_types() { |
| 593 | 511 | global $activitypub_comment_types; |
| 594 | 512 | |
| 595 | - return (array) $activitypub_comment_types; | |
| 513 | + return $activitypub_comment_types; | |
| 596 | 514 | } |
| 597 | 515 | |
| 598 | 516 | /** |
| 599 | 517 | * Is this a registered comment type. |
| @@ -778,16 +696,16 @@ | ||
| 778 | 696 | if ( defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) { |
| 779 | 697 | return; |
| 780 | 698 | } |
| 781 | 699 | |
| 782 | - // Do not exclude likes and reposts on REST requests (handled by rest_comment_query). | |
| 700 | + // Do not exclude likes and reposts on REST requests. | |
| 783 | 701 | if ( \wp_is_serving_rest_request() ) { |
| 784 | 702 | return; |
| 785 | 703 | } |
| 786 | 704 | |
| 787 | - // Filter post types for admin requests. | |
| 705 | + // Do only exclude interactions of `ap_post` post type. | |
| 788 | 706 | if ( \is_admin() ) { |
| 789 | - $query->query_vars['post_type'] = self::get_allowed_comment_post_types(); | |
| 707 | + $query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::hide_for() ); | |
| 790 | 708 | return; |
| 791 | 709 | } |
| 792 | 710 | |
| 793 | 711 | // Do not exclude likes and reposts on non-singular pages. |
| @@ -794,60 +712,18 @@ | ||
| 794 | 712 | if ( ! \is_singular() ) { |
| 795 | 713 | return; |
| 796 | 714 | } |
| 797 | 715 | |
| 798 | - // Do not exclude likes and reposts if the query is for specific types. | |
| 716 | + // Do not exclude likes and reposts if the query is for comments. | |
| 799 | 717 | if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) { |
| 800 | 718 | return; |
| 801 | 719 | } |
| 802 | 720 | |
| 803 | - // Do not exclude likes and reposts if the query is already excluding other comment types. | |
| 804 | - if ( ! empty( $query->query_vars['type__not_in'] ) ) { | |
| 805 | - return; | |
| 806 | - } | |
| 807 | - | |
| 808 | 721 | // Exclude likes and reposts by the ActivityPub plugin. |
| 809 | 722 | $query->query_vars['type__not_in'] = self::get_comment_type_slugs(); |
| 810 | 723 | } |
| 811 | 724 | |
| 812 | 725 | /** |
| 813 | - * Filters comments in REST API requests. | |
| 814 | - * | |
| 815 | - * Excludes comments on ActivityPub post types and ActivityPub comment | |
| 816 | - * types (likes, reposts) from the REST API. | |
| 817 | - * | |
| 818 | - * @param array $prepared_args Array of arguments for WP_Comment_Query. | |
| 819 | - * | |
| 820 | - * @return array Modified array of arguments. | |
| 821 | - */ | |
| 822 | - public static function rest_comment_query( $prepared_args ) { | |
| 823 | - // Exclude comments on ActivityPub post types. | |
| 824 | - $prepared_args['post_type'] = self::get_allowed_comment_post_types(); | |
| 825 | - | |
| 826 | - // Exclude ActivityPub comment types (likes, reposts) unless explicitly requested. | |
| 827 | - if ( empty( $prepared_args['type'] ) && empty( $prepared_args['type__in'] ) ) { | |
| 828 | - $prepared_args['type__not_in'] = self::get_comment_type_slugs(); | |
| 829 | - } | |
| 830 | - | |
| 831 | - return $prepared_args; | |
| 832 | - } | |
| 833 | - | |
| 834 | - /** | |
| 835 | - * Returns post types that should show comments (excluding hidden post types). | |
| 836 | - * | |
| 837 | - * @return array Array of post type names. | |
| 838 | - */ | |
| 839 | - private static function get_allowed_comment_post_types() { | |
| 840 | - $hide_for = self::hide_for(); | |
| 841 | - | |
| 842 | - if ( empty( $hide_for ) ) { | |
| 843 | - return \get_post_types_by_support( 'comments' ); | |
| 844 | - } | |
| 845 | - | |
| 846 | - return \array_diff( \get_post_types_by_support( 'comments' ), $hide_for ); | |
| 847 | - } | |
| 848 | - | |
| 849 | - /** | |
| 850 | 726 | * Filter the comment status before it is set. |
| 851 | 727 | * |
| 852 | 728 | * @param int|string|\WP_Error $approved The approved comment status. |
| 853 | 729 | * @param array $comment_data The comment data. |
| @@ -871,19 +747,8 @@ | ||
| 871 | 747 | ) { |
| 872 | 748 | return 1; |
| 873 | 749 | } |
| 874 | 750 | |
| 875 | - /* | |
| 876 | - * Always auto-approve comments on remote posts (ap_post) since | |
| 877 | - * they are not visible in the WP admin comment moderation screen. | |
| 878 | - */ | |
| 879 | - $post_id = $comment_data['comment_post_ID']; | |
| 880 | - $post = \get_post( $post_id ); | |
| 881 | - | |
| 882 | - if ( $post && \in_array( $post->post_type, self::hide_for(), true ) ) { | |
| 883 | - return 1; | |
| 884 | - } | |
| 885 | - | |
| 886 | 751 | if ( '1' !== \get_option( 'comment_previously_approved' ) ) { |
| 887 | 752 | return $approved; |
| 888 | 753 | } |
| 889 | 754 | |
| @@ -904,8 +769,15 @@ | ||
| 904 | 769 | if ( 1 === (int) $ok_to_comment ) { |
| 905 | 770 | return 1; |
| 906 | 771 | } |
| 907 | 772 | |
| 773 | + $post_id = $comment_data['comment_post_ID']; | |
| 774 | + $post = \get_post( $post_id ); | |
| 775 | + | |
| 776 | + if ( $post && in_array( $post->post_type, self::hide_for(), true ) ) { | |
| 777 | + return 1; | |
| 778 | + } | |
| 779 | + | |
| 908 | 780 | return $approved; |
| 909 | 781 | } |
| 910 | 782 | |
| 911 | 783 | /** |
| @@ -935,31 +807,8 @@ | ||
| 935 | 807 | if ( null === $new_count ) { |
| 936 | 808 | $excluded_types = array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) ); |
| 937 | 809 | |
| 938 | 810 | if ( ! empty( $excluded_types ) ) { |
| 939 | - /* | |
| 940 | - * Include 'note' type when Gutenberg's filter is registered, so a | |
| 941 | - * single query excludes both ActivityPub and Gutenberg types. | |
| 942 | - */ | |
| 943 | - if ( \has_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count' ) ) { | |
| 944 | - $excluded_types[] = 'note'; | |
| 945 | - } | |
| 946 | - | |
| 947 | - /** | |
| 948 | - * Filters the comment types excluded from the comment count. | |
| 949 | - * | |
| 950 | - * Runs at priority 5 on `pre_wp_update_comment_count_now` so that | |
| 951 | - * a single query can exclude types from multiple plugins. Other | |
| 952 | - * plugins can hook here to add their own comment types. | |
| 953 | - * | |
| 954 | - * @since 8.0.0 | |
| 955 | - * | |
| 956 | - * @param string[] $excluded_types The comment type slugs to exclude. | |
| 957 | - * @param int $post_id The post ID. | |
| 958 | - */ | |
| 959 | - $excluded_types = \apply_filters( 'activitypub_excluded_comment_types', $excluded_types, $post_id ); | |
| 960 | - $excluded_types = array_unique( array_filter( $excluded_types ) ); | |
| 961 | - | |
| 962 | 811 | global $wpdb; |
| 963 | 812 | |
| 964 | 813 | // phpcs:ignore WordPress.DB |
| 965 | 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 ) ); |
| @@ -987,9 +836,9 @@ | ||
| 987 | 836 | * |
| 988 | 837 | * @return string[] Array of post type names to hide comments for. |
| 989 | 838 | */ |
| 990 | 839 | public static function hide_for() { |
| 991 | - $post_types = array( Remote_Posts::POST_TYPE ); | |
| 840 | + $post_types = array( Posts::POST_TYPE ); | |
| 992 | 841 | |
| 993 | 842 | /** |
| 994 | 843 | * Filters the list of post types to hide comments for. |
| 995 | 844 | * |
| @@ -995,55 +844,6 @@ | ||
| 995 | 844 | * |
| 996 | 845 | * @param string[] $post_types Array of post type names to hide comments for. |
| 997 | 846 | */ |
| 998 | 847 | return \apply_filters( 'activitypub_hide_comments_for', $post_types ); |
| 999 | - } | |
| 1000 | - | |
| 1001 | - /** | |
| 1002 | - * Render emoji in comment author name. | |
| 1003 | - * | |
| 1004 | - * Replaces emoji shortcodes with img tags on the get_comment_author filter. | |
| 1005 | - * Emoji data is retrieved from the linked remote actor. | |
| 1006 | - * | |
| 1007 | - * @param string $author The comment author name. | |
| 1008 | - * @param string $comment_id The comment ID as a numeric string. | |
| 1009 | - * | |
| 1010 | - * @return string The comment author name with rendered emoji. | |
| 1011 | - */ | |
| 1012 | - public static function render_emoji( $author, $comment_id ) { | |
| 1013 | - $remote_actor_id = \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ); | |
| 1014 | - | |
| 1015 | - if ( empty( $remote_actor_id ) ) { | |
| 1016 | - return $author; | |
| 1017 | - } | |
| 1018 | - | |
| 1019 | - $emoji_data = \get_post_meta( $remote_actor_id, '_activitypub_emoji', true ); | |
| 1020 | - | |
| 1021 | - if ( empty( $emoji_data ) ) { | |
| 1022 | - return $author; | |
| 1023 | - } | |
| 1024 | - | |
| 1025 | - return Emoji::replace_from_json( $author, $emoji_data ); | |
| 1026 | - } | |
| 1027 | - | |
| 1028 | - /** | |
| 1029 | - * Selectively unescape emoji images in comment author. | |
| 1030 | - * | |
| 1031 | - * This runs at priority 20 after WordPress's esc_html() filter on comment_author. | |
| 1032 | - * | |
| 1033 | - * @param string $author The comment author name (already escaped by WordPress). | |
| 1034 | - * | |
| 1035 | - * @return string The comment author name with emoji images unescaped. | |
| 1036 | - */ | |
| 1037 | - public static function unescape_emoji( $author ) { | |
| 1038 | - // Only attempt to unescape if there are emoji images present in the escaped string. | |
| 1039 | - if ( false === \strpos( $author, 'class="emoji"' ) ) { | |
| 1040 | - return $author; | |
| 1041 | - } | |
| 1042 | - | |
| 1043 | - // Decode entities so we can selectively restore emoji <img> tags. | |
| 1044 | - $decoded = \html_entity_decode( $author, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); | |
| 1045 | - | |
| 1046 | - // Use strict KSES validation to only allow valid emoji img tags. | |
| 1047 | - return \wp_kses( $decoded, Emoji::get_kses_allowed_html() ); | |
| 1048 | 848 | } |
| 1049 | 849 | } |