| @@ -1,18 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * ActivityPub Comment Class | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | namespace Activitypub; |
| 9 | 4 | |
| 10 | -use Activitypub\Collection\Actors; | |
| 11 | -use Activitypub\Collection\Remote_Posts; | |
| 5 | +use Activitypub\Collection\Users; | |
| 6 | +use WP_Comment_Query; | |
| 12 | 7 | |
| 8 | +use function Activitypub\is_user_disabled; | |
| 9 | +use function Activitypub\is_single_user; | |
| 10 | + | |
| 13 | 11 | /** |
| 14 | - * ActivityPub Comment Class. | |
| 12 | + * ActivityPub Comment Class | |
| 15 | 13 | * |
| 16 | 14 | * This class is a helper/utils class that provides a collection of static |
| 17 | 15 | * methods that are used to handle comments. |
| 18 | 16 | */ |
| @@ -17,150 +15,76 @@ | ||
| 17 | 15 | * methods that are used to handle comments. |
| 18 | 16 | */ |
| 19 | 17 | class Comment { |
| 20 | 18 | /** |
| 21 | - * Initialize the class, registering WordPress hooks. | |
| 19 | + * Initialize the class, registering WordPress hooks | |
| 22 | 20 | */ |
| 23 | 21 | public static function init() { |
| 24 | 22 | self::register_comment_types(); |
| 25 | 23 | |
| 26 | - \add_filter( 'map_meta_cap', array( self::class, 'map_meta_cap' ), 10, 4 ); | |
| 27 | 24 | \add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 3 ); |
| 28 | 25 | \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 ); |
| 29 | - \add_filter( 'comment_feed_where', array( static::class, 'comment_feed_where' ) ); | |
| 30 | - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 ); | |
| 26 | + \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 ); | |
| 27 | + \add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); | |
| 31 | 28 | \add_action( 'pre_get_comments', array( static::class, 'comment_query' ) ); |
| 32 | - \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 11, 2 ); | |
| 29 | + | |
| 33 | 30 | \add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 ); |
| 34 | - \add_action( 'update_option_activitypub_allow_likes', array( self::class, 'maybe_update_comment_counts' ), 10, 2 ); | |
| 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. | |
| 41 | 31 | } |
| 42 | 32 | |
| 43 | 33 | /** |
| 44 | - * Render blocks in comment content. | |
| 34 | + * Filter the comment reply link. | |
| 45 | 35 | * |
| 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. | |
| 36 | + * We don't want to show the comment reply link for federated comments | |
| 37 | + * if the user is disabled for federation. | |
| 49 | 38 | * |
| 50 | - * @param string $content The comment content. | |
| 39 | + * @param string $link The HTML markup for the comment reply link. | |
| 40 | + * @param array $args An array of arguments overriding the defaults. | |
| 41 | + * @param WP_Comment $comment The object of the comment being replied. | |
| 51 | 42 | * |
| 52 | - * @return string The content with blocks rendered. | |
| 43 | + * @return string The filtered HTML markup for the comment reply link. | |
| 53 | 44 | */ |
| 54 | - public static function render_blocks( $content ) { | |
| 55 | - if ( empty( $content ) || ! \str_contains( $content, '<!-- wp:activitypub/' ) ) { | |
| 56 | - return $content; | |
| 57 | - } | |
| 45 | + public static function comment_reply_link( $link, $args, $comment ) { | |
| 46 | + if ( self::are_comments_allowed( $comment ) ) { | |
| 47 | + $user_id = get_current_user_id(); | |
| 48 | + if ( $user_id && self::was_received( $comment ) && \user_can( $user_id, 'activitypub' ) ) { | |
| 49 | + return self::create_fediverse_reply_link( $link, $args ); | |
| 50 | + } | |
| 58 | 51 | |
| 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 | - } | |
| 52 | + return $link; | |
| 68 | 53 | } |
| 69 | 54 | |
| 70 | - return $output; | |
| 71 | - } | |
| 55 | + $attrs = array( | |
| 56 | + 'selectedComment' => self::generate_id( $comment ), | |
| 57 | + 'commentId' => $comment->comment_ID, | |
| 58 | + ); | |
| 72 | 59 | |
| 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 | - } | |
| 60 | + $div = sprintf( | |
| 61 | + '<div class="activitypub-remote-reply" data-attrs="%s"></div>', | |
| 62 | + esc_attr( wp_json_encode( $attrs ) ) | |
| 63 | + ); | |
| 89 | 64 | |
| 90 | - return $caps; | |
| 65 | + return apply_filters( 'activitypub_comment_reply_link', $div ); | |
| 91 | 66 | } |
| 92 | 67 | |
| 93 | 68 | /** |
| 94 | - * Filter the comment reply link. | |
| 69 | + * Create a link to reply to a federated comment. | |
| 70 | + * This function adds a title attribute to the reply link to inform the user | |
| 71 | + * that the comment was received from the fediverse and the reply will be sent | |
| 72 | + * to the original author. | |
| 95 | 73 | * |
| 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 | |
| 74 | + * @param string $link The HTML markup for the comment reply link. | |
| 75 | + * @param array $args The args provided by the `comment_reply_link` filter. | |
| 100 | 76 | * |
| 101 | - * @param string $link The HTML markup for the comment reply link. | |
| 102 | - * @param array $args An array of arguments overriding the defaults. | |
| 103 | - * @param \WP_Comment $comment The object of the comment being replied. | |
| 104 | - * | |
| 105 | - * @return string The filtered HTML markup for the comment reply link. | |
| 77 | + * @return string The modified HTML markup for the comment reply link. | |
| 106 | 78 | */ |
| 107 | - public static function comment_reply_link( $link, $args, $comment ) { | |
| 108 | - 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 | - ); | |
| 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 | - 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( | |
| 151 | - 'selectedComment' => self::generate_id( $comment ), | |
| 152 | - 'commentId' => $comment->comment_ID, | |
| 79 | + private static function create_fediverse_reply_link( $link, $args ) { | |
| 80 | + $str_to_replace = sprintf( '>%s<', $args['reply_text'] ); | |
| 81 | + $replace_with = sprintf( | |
| 82 | + ' title="%s">%s<', | |
| 83 | + esc_attr__( 'This comment was received from the fediverse and your reply will be sent to the original author', 'activitypub' ), | |
| 84 | + esc_html__( 'Reply with federation', 'activitypub' ) | |
| 153 | 85 | ); |
| 154 | - | |
| 155 | - $block = \do_blocks( \sprintf( '<!-- wp:activitypub/remote-reply %s /-->', \wp_json_encode( $attributes ) ) ); | |
| 156 | - | |
| 157 | - /** | |
| 158 | - * Filters the HTML markup for the ActivityPub remote comment reply container. | |
| 159 | - * | |
| 160 | - * @param string $block The HTML markup for the remote reply container. | |
| 161 | - */ | |
| 162 | - return \apply_filters( 'activitypub_comment_reply_link', $block ); | |
| 86 | + return str_replace( $str_to_replace, $replace_with, $link ); | |
| 163 | 87 | } |
| 164 | 88 | |
| 165 | 89 | /** |
| 166 | 90 | * Check if it is allowed to comment to a comment. |
| @@ -177,21 +101,26 @@ | ||
| 177 | 101 | if ( ! self::was_received( $comment ) ) { |
| 178 | 102 | return true; |
| 179 | 103 | } |
| 180 | 104 | |
| 181 | - $current_user = \get_current_user_id(); | |
| 105 | + $current_user = get_current_user_id(); | |
| 182 | 106 | |
| 183 | 107 | if ( ! $current_user ) { |
| 184 | 108 | return false; |
| 185 | 109 | } |
| 186 | 110 | |
| 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. | |
| 189 | - $current_user = Actors::BLOG_USER_ID; | |
| 111 | + if ( is_single_user() && \user_can( $current_user, 'publish_posts' ) ) { | |
| 112 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user | |
| 113 | + $current_user = Users::BLOG_USER_ID; | |
| 190 | 114 | } |
| 191 | 115 | |
| 192 | - // User is not allowed to federate comments. | |
| 193 | - return user_can_activitypub( $current_user ); | |
| 116 | + $is_user_disabled = is_user_disabled( $current_user ); | |
| 117 | + | |
| 118 | + if ( $is_user_disabled ) { | |
| 119 | + return false; | |
| 120 | + } | |
| 121 | + | |
| 122 | + return true; | |
| 194 | 123 | } |
| 195 | 124 | |
| 196 | 125 | /** |
| 197 | 126 | * Check if a comment is federated. |
| @@ -275,9 +204,9 @@ | ||
| 275 | 204 | * |
| 276 | 205 | * @return boolean True if the comment should be federated, false otherwise. |
| 277 | 206 | */ |
| 278 | 207 | public static function should_be_federated( $comment ) { |
| 279 | - // We should not federate federated comments. | |
| 208 | + // we should not federate federated comments | |
| 280 | 209 | if ( self::was_received( $comment ) ) { |
| 281 | 210 | return false; |
| 282 | 211 | } |
| 283 | 212 | |
| @@ -283,40 +212,31 @@ | ||
| 283 | 212 | |
| 284 | 213 | $comment = \get_comment( $comment ); |
| 285 | 214 | $user_id = $comment->user_id; |
| 286 | 215 | |
| 287 | - // Comments without user can't be federated. | |
| 216 | + // comments without user can't be federated | |
| 288 | 217 | if ( ! $user_id ) { |
| 289 | 218 | return false; |
| 290 | 219 | } |
| 291 | 220 | |
| 292 | - 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. | |
| 294 | - $user_id = Actors::BLOG_USER_ID; | |
| 221 | + if ( is_single_user() && \user_can( $user_id, 'publish_posts' ) ) { | |
| 222 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user | |
| 223 | + $user_id = Users::BLOG_USER_ID; | |
| 295 | 224 | } |
| 296 | 225 | |
| 297 | - // User is not allowed to federate comments. | |
| 298 | - if ( ! user_can_activitypub( $user_id ) ) { | |
| 299 | - return false; | |
| 300 | - } | |
| 226 | + $is_user_disabled = is_user_disabled( $user_id ); | |
| 301 | 227 | |
| 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 ) ) { | |
| 228 | + // user is disabled for federation | |
| 229 | + if ( $is_user_disabled ) { | |
| 310 | 230 | return false; |
| 311 | 231 | } |
| 312 | 232 | |
| 313 | - // It is a comment to the post and can be federated. | |
| 233 | + // it is a comment to the post and can be federated | |
| 314 | 234 | if ( empty( $comment->comment_parent ) ) { |
| 315 | 235 | return true; |
| 316 | 236 | } |
| 317 | 237 | |
| 318 | - // Check if parent comment is federated. | |
| 238 | + // check if parent comment is federated | |
| 319 | 239 | $parent_comment = \get_comment( $comment->comment_parent ); |
| 320 | 240 | |
| 321 | 241 | return ! self::is_local( $parent_comment ); |
| 322 | 242 | } |
| @@ -323,34 +243,25 @@ | ||
| 323 | 243 | |
| 324 | 244 | /** |
| 325 | 245 | * Examine a comment ID and look up an existing comment it represents. |
| 326 | 246 | * |
| 327 | - * @since 9.1.0 Added the `$args` parameter. | |
| 247 | + * @param string $id ActivityPub object ID (usually a URL) to check. | |
| 328 | 248 | * |
| 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 | 249 | * @return \WP_Comment|false Comment object, or false on failure. |
| 334 | 250 | */ |
| 335 | - public static function object_id_to_comment( $id, $args = array() ) { | |
| 336 | - $args = \wp_parse_args( | |
| 337 | - $args, | |
| 251 | + public static function object_id_to_comment( $id ) { | |
| 252 | + $comment_query = new WP_Comment_Query( | |
| 338 | 253 | array( |
| 339 | - 'number' => 1, | |
| 340 | - 'orderby' => 'comment_date', | |
| 341 | - 'order' => 'DESC', | |
| 254 | + 'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 255 | + 'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value | |
| 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 | |
| 259 | + if ( ! $comment_query->comments ) { | |
| 260 | + return false; | |
| 261 | + } | |
| 349 | 262 | |
| 350 | - $comment_query = new \WP_Comment_Query( $args ); | |
| 351 | - | |
| 352 | - if ( ! $comment_query->comments ) { | |
| 263 | + if ( count( $comment_query->comments ) > 1 ) { | |
| 353 | 264 | return false; |
| 354 | 265 | } |
| 355 | 266 | |
| 356 | 267 | return $comment_query->comments[0]; |
| @@ -357,25 +268,25 @@ | ||
| 357 | 268 | } |
| 358 | 269 | |
| 359 | 270 | /** |
| 360 | 271 | * Verify if URL is a local comment, or if it is a previously received |
| 361 | - * remote comment (For threading comments locally). | |
| 272 | + * remote comment (For threading comments locally) | |
| 362 | 273 | * |
| 363 | 274 | * @param string $url The URL to check. |
| 364 | 275 | * |
| 365 | - * @return string|null Comment ID or null if not found. | |
| 276 | + * @return int comment_ID or null if not found | |
| 366 | 277 | */ |
| 367 | 278 | public static function url_to_commentid( $url ) { |
| 368 | - if ( ! $url || ! \filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 279 | + if ( ! $url || ! filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 369 | 280 | return null; |
| 370 | 281 | } |
| 371 | 282 | |
| 372 | - // Check for local comment. | |
| 373 | - if ( is_same_domain( $url ) ) { | |
| 283 | + // check for local comment | |
| 284 | + if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) === \wp_parse_url( $url, \PHP_URL_HOST ) ) { | |
| 374 | 285 | $query = \wp_parse_url( $url, \PHP_URL_QUERY ); |
| 375 | 286 | |
| 376 | 287 | if ( $query ) { |
| 377 | - \parse_str( $query, $params ); | |
| 288 | + parse_str( $query, $params ); | |
| 378 | 289 | |
| 379 | 290 | if ( ! empty( $params['c'] ) ) { |
| 380 | 291 | $comment = \get_comment( $params['c'] ); |
| 381 | 292 | |
| @@ -400,12 +311,12 @@ | ||
| 400 | 311 | ), |
| 401 | 312 | ), |
| 402 | 313 | ); |
| 403 | 314 | |
| 404 | - $query = new \WP_Comment_Query(); | |
| 315 | + $query = new WP_Comment_Query(); | |
| 405 | 316 | $comments = $query->query( $args ); |
| 406 | 317 | |
| 407 | - if ( $comments && \is_array( $comments ) ) { | |
| 318 | + if ( $comments && is_array( $comments ) ) { | |
| 408 | 319 | return $comments[0]->comment_ID; |
| 409 | 320 | } |
| 410 | 321 | |
| 411 | 322 | return null; |
| @@ -420,10 +331,10 @@ | ||
| 420 | 331 | * |
| 421 | 332 | * @return string[] An array of classes. |
| 422 | 333 | */ |
| 423 | 334 | public static function comment_class( $classes, $css_class, $comment_id ) { |
| 424 | - // Check if ActivityPub comment. | |
| 425 | - if ( 'activitypub' === \get_comment_meta( $comment_id, 'protocol', true ) ) { | |
| 335 | + // check if ActivityPub comment | |
| 336 | + if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) { | |
| 426 | 337 | $classes[] = 'activitypub-comment'; |
| 427 | 338 | } |
| 428 | 339 | |
| 429 | 340 | return $classes; |
| @@ -429,120 +340,52 @@ | ||
| 429 | 340 | return $classes; |
| 430 | 341 | } |
| 431 | 342 | |
| 432 | 343 | /** |
| 433 | - * Makes the comment feed filterable by comment type. | |
| 344 | + * Link remote comments to source url. | |
| 434 | 345 | * |
| 435 | - * Also excludes ActivityPub comment types from the feed when no type is specified. | |
| 346 | + * @param string $comment_link | |
| 347 | + * @param object|WP_Comment $comment | |
| 436 | 348 | * |
| 437 | - * @param string $where The `WHERE` clause for the comment feed query. | |
| 438 | - * | |
| 439 | - * @return string The modified `WHERE` clause. | |
| 349 | + * @return string $url | |
| 440 | 350 | */ |
| 441 | - public static function comment_feed_where( $where ) { | |
| 442 | - global $wpdb; | |
| 443 | - | |
| 444 | - $comment_type = \get_query_var( 'type' ); | |
| 445 | - | |
| 446 | - if ( 'all' === $comment_type ) { | |
| 447 | - return $where; | |
| 351 | + public static function remote_comment_link( $comment_link, $comment ) { | |
| 352 | + if ( ! $comment || is_admin() ) { | |
| 353 | + return $comment_link; | |
| 448 | 354 | } |
| 449 | 355 | |
| 450 | - $comment_types = self::get_comment_type_slugs(); | |
| 356 | + $comment_meta = \get_comment_meta( $comment->comment_ID ); | |
| 451 | 357 | |
| 452 | - if ( \in_array( $comment_type, $comment_types, true ) ) { | |
| 453 | - $where .= $wpdb->prepare( ' AND comment_type = %s', $comment_type ); | |
| 454 | - } else { | |
| 455 | - $comment_types = \array_map( 'esc_sql', $comment_types ); | |
| 456 | - $placeholders = \implode( ', ', \array_fill( 0, \count( $comment_types ), '%s' ) ); | |
| 457 | - // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQL.NotPrepared | |
| 458 | - $where .= $wpdb->prepare( \sprintf( ' AND comment_type NOT IN (%s)', $placeholders ), ...$comment_types ); | |
| 459 | - } | |
| 460 | - | |
| 461 | - return $where; | |
| 462 | - } | |
| 463 | - | |
| 464 | - /** | |
| 465 | - * Gets the public comment id via the WordPress comments meta. | |
| 466 | - * | |
| 467 | - * @param int $wp_comment_id The internal WordPress comment ID. | |
| 468 | - * @param bool $fallback Whether the code should fall back to `source_url` if `source_id` is not set. | |
| 469 | - * | |
| 470 | - * @return string|null The ActivityPub id/url of the comment. | |
| 471 | - */ | |
| 472 | - public static function get_source_id( $wp_comment_id, $fallback = true ) { | |
| 473 | - $comment_meta = \get_comment_meta( $wp_comment_id ); | |
| 474 | - | |
| 475 | - if ( ! empty( $comment_meta['source_id'][0] ) ) { | |
| 476 | - return $comment_meta['source_id'][0]; | |
| 477 | - } elseif ( ! empty( $comment_meta['source_url'][0] ) && $fallback ) { | |
| 478 | - return $comment_meta['source_url'][0]; | |
| 479 | - } | |
| 480 | - | |
| 481 | - return null; | |
| 482 | - } | |
| 483 | - | |
| 484 | - /** | |
| 485 | - * Gets the public comment url via the WordPress comments meta. | |
| 486 | - * | |
| 487 | - * @param int $wp_comment_id The internal WordPress comment ID. | |
| 488 | - * @param bool $fallback Whether the code should fall back to `source_id` if `source_url` is not set. | |
| 489 | - * | |
| 490 | - * @return string|null The ActivityPub id/url of the comment. | |
| 491 | - */ | |
| 492 | - public static function get_source_url( $wp_comment_id, $fallback = true ) { | |
| 493 | - $comment_meta = \get_comment_meta( $wp_comment_id ); | |
| 494 | - | |
| 495 | 358 | if ( ! empty( $comment_meta['source_url'][0] ) ) { |
| 496 | 359 | return $comment_meta['source_url'][0]; |
| 497 | - } elseif ( ! empty( $comment_meta['source_id'][0] ) && $fallback ) { | |
| 360 | + } elseif ( ! empty( $comment_meta['source_id'][0] ) ) { | |
| 498 | 361 | return $comment_meta['source_id'][0]; |
| 499 | 362 | } |
| 500 | 363 | |
| 501 | - return null; | |
| 364 | + return $comment_link; | |
| 502 | 365 | } |
| 503 | 366 | |
| 504 | - /** | |
| 505 | - * Link remote comments to source url. | |
| 506 | - * | |
| 507 | - * @param string $comment_link The comment link. | |
| 508 | - * @param object|\WP_Comment $comment The comment object. | |
| 509 | - * | |
| 510 | - * @return string $url | |
| 511 | - */ | |
| 512 | - public static function remote_comment_link( $comment_link, $comment ) { | |
| 513 | - if ( ! $comment || \is_admin() || \is_search() ) { | |
| 514 | - return $comment_link; | |
| 515 | - } | |
| 516 | 367 | |
| 517 | - $remote_comment_link = null; | |
| 518 | - if ( 'comment' === $comment->comment_type ) { | |
| 519 | - $remote_comment_link = self::get_source_url( $comment->comment_ID ); | |
| 520 | - } | |
| 521 | - | |
| 522 | - return $remote_comment_link ?? $comment_link; | |
| 523 | - } | |
| 524 | - | |
| 525 | - | |
| 526 | 368 | /** |
| 527 | 369 | * Generates an ActivityPub URI for a comment |
| 528 | 370 | * |
| 529 | - * @param \WP_Comment|int $comment A comment object or comment ID. | |
| 371 | + * @param WP_Comment|int $comment A comment object or comment ID | |
| 530 | 372 | * |
| 531 | 373 | * @return string ActivityPub URI for comment |
| 532 | 374 | */ |
| 533 | 375 | public static function generate_id( $comment ) { |
| 534 | - $comment = \get_comment( $comment ); | |
| 376 | + $comment = \get_comment( $comment ); | |
| 377 | + $comment_meta = \get_comment_meta( $comment->comment_ID ); | |
| 535 | 378 | |
| 536 | - // Show external comment ID if it exists. | |
| 537 | - $public_comment_link = self::get_source_id( $comment->comment_ID ); | |
| 538 | - | |
| 539 | - if ( $public_comment_link ) { | |
| 540 | - return $public_comment_link; | |
| 379 | + // show external comment ID if it exists | |
| 380 | + if ( ! empty( $comment_meta['source_id'][0] ) ) { | |
| 381 | + return $comment_meta['source_id'][0]; | |
| 382 | + } elseif ( ! empty( $comment_meta['source_url'][0] ) ) { | |
| 383 | + return $comment_meta['source_url'][0]; | |
| 541 | 384 | } |
| 542 | 385 | |
| 543 | - // Generate URI based on comment ID. | |
| 544 | - return \add_query_arg( 'c', $comment->comment_ID, \home_url( '/' ) ); | |
| 386 | + // generate URI based on comment ID | |
| 387 | + return \add_query_arg( 'c', $comment->comment_ID, \trailingslashit( \home_url() ) ); | |
| 545 | 388 | } |
| 546 | 389 | |
| 547 | 390 | /** |
| 548 | 391 | * Check if a post has remote comments |
| @@ -553,9 +396,9 @@ | ||
| 553 | 396 | */ |
| 554 | 397 | private static function post_has_remote_comments( $post_id ) { |
| 555 | 398 | $comments = \get_comments( |
| 556 | 399 | array( |
| 557 | - 'post_id' => $post_id, | |
| 400 | + 'post_id' => $post_id, | |
| 558 | 401 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 559 | 402 | 'meta_query' => array( |
| 560 | 403 | 'relation' => 'AND', |
| 561 | 404 | array( |
| @@ -574,26 +417,57 @@ | ||
| 574 | 417 | return ! empty( $comments ); |
| 575 | 418 | } |
| 576 | 419 | |
| 577 | 420 | /** |
| 578 | - * Get the comment type by activity type. | |
| 579 | - * | |
| 580 | - * @param string $activity_type The activity type. | |
| 581 | - * | |
| 582 | - * @return array|null The comment type. | |
| 421 | + * Enqueue scripts for remote comments | |
| 583 | 422 | */ |
| 584 | - public static function get_comment_type_by_activity_type( $activity_type ) { | |
| 585 | - $activity_type = \strtolower( $activity_type ); | |
| 586 | - $activity_type = \sanitize_key( $activity_type ); | |
| 587 | - $comment_types = self::get_comment_types(); | |
| 423 | + public static function enqueue_scripts() { | |
| 424 | + if ( ! \is_singular() || \is_user_logged_in() ) { | |
| 425 | + // only on single pages, only for logged out users | |
| 426 | + return; | |
| 427 | + } | |
| 588 | 428 | |
| 589 | - foreach ( $comment_types as $comment_type ) { | |
| 590 | - if ( \in_array( $activity_type, $comment_type['activity_types'], true ) ) { | |
| 591 | - return $comment_type; | |
| 592 | - } | |
| 429 | + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) { | |
| 430 | + // post type does not support ActivityPub | |
| 431 | + return; | |
| 593 | 432 | } |
| 594 | 433 | |
| 595 | - return null; | |
| 434 | + if ( ! \comments_open() || ! \get_comments_number() ) { | |
| 435 | + // no comments, no need to load the script | |
| 436 | + return; | |
| 437 | + } | |
| 438 | + | |
| 439 | + if ( ! self::post_has_remote_comments( \get_the_ID() ) ) { | |
| 440 | + // no remote comments, no need to load the script | |
| 441 | + return; | |
| 442 | + } | |
| 443 | + | |
| 444 | + $handle = 'activitypub-remote-reply'; | |
| 445 | + $data = array( | |
| 446 | + 'namespace' => ACTIVITYPUB_REST_NAMESPACE, | |
| 447 | + ); | |
| 448 | + $js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) ); | |
| 449 | + $asset_file = ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply/index.asset.php'; | |
| 450 | + | |
| 451 | + if ( \file_exists( $asset_file ) ) { | |
| 452 | + $assets = require_once $asset_file; | |
| 453 | + | |
| 454 | + \wp_enqueue_script( | |
| 455 | + $handle, | |
| 456 | + \plugins_url( 'build/remote-reply/index.js', __DIR__ ), | |
| 457 | + $assets['dependencies'], | |
| 458 | + $assets['version'], | |
| 459 | + true | |
| 460 | + ); | |
| 461 | + \wp_add_inline_script( $handle, $js, 'before' ); | |
| 462 | + | |
| 463 | + \wp_enqueue_style( | |
| 464 | + $handle, | |
| 465 | + \plugins_url( 'build/remote-reply/style-index.css', __DIR__ ), | |
| 466 | + [ 'wp-components' ], | |
| 467 | + $assets['version'] | |
| 468 | + ); | |
| 469 | + } | |
| 596 | 470 | } |
| 597 | 471 | |
| 598 | 472 | /** |
| 599 | 473 | * Return the registered custom comment types. |
| @@ -602,81 +476,61 @@ | ||
| 602 | 476 | */ |
| 603 | 477 | public static function get_comment_types() { |
| 604 | 478 | global $activitypub_comment_types; |
| 605 | 479 | |
| 606 | - return (array) $activitypub_comment_types; | |
| 480 | + return $activitypub_comment_types; | |
| 607 | 481 | } |
| 608 | 482 | |
| 609 | 483 | /** |
| 610 | - * Is this a registered comment type. | |
| 484 | + * Is this a registered comment type | |
| 611 | 485 | * |
| 612 | - * @param string $slug The slug of the type. | |
| 613 | - * | |
| 486 | + * @param string $slug The name of the type | |
| 614 | 487 | * @return boolean True if registered. |
| 615 | 488 | */ |
| 616 | 489 | public static function is_registered_comment_type( $slug ) { |
| 617 | - $slug = \strtolower( $slug ); | |
| 618 | - $slug = \sanitize_key( $slug ); | |
| 490 | + $slug = strtolower( $slug ); | |
| 491 | + $slug = sanitize_key( $slug ); | |
| 619 | 492 | |
| 620 | - $comment_types = self::get_comment_types(); | |
| 621 | - | |
| 622 | - return isset( $comment_types[ $slug ] ); | |
| 493 | + return in_array( $slug, array_keys( self::get_comment_types() ), true ); | |
| 623 | 494 | } |
| 624 | 495 | |
| 625 | 496 | /** |
| 626 | - * Return the registered custom comment type slugs. | |
| 497 | + * Return the registered custom comment types names. | |
| 627 | 498 | * |
| 628 | - * @return array The registered custom comment type slugs. | |
| 499 | + * @return array The registered custom comment type names | |
| 629 | 500 | */ |
| 630 | - public static function get_comment_type_slugs() { | |
| 631 | - if ( ! \did_action( 'init' ) ) { | |
| 632 | - \_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' ); | |
| 633 | - | |
| 634 | - return array(); | |
| 635 | - } | |
| 636 | - | |
| 637 | - return \array_keys( self::get_comment_types() ); | |
| 501 | + public static function get_comment_type_names() { | |
| 502 | + return array_values( wp_list_pluck( self::get_comment_types(), 'type' ) ); | |
| 638 | 503 | } |
| 639 | 504 | |
| 640 | 505 | /** |
| 641 | - * Get the custom comment type. | |
| 506 | + * Get a comment type | |
| 642 | 507 | * |
| 643 | - * Check if the type is registered, if not, check if it is a custom type. | |
| 508 | + * @param string $type The comment type | |
| 644 | 509 | * |
| 645 | - * It looks for the array key in the registered types and returns the array. | |
| 646 | - * If it is not found, it looks for the type in the custom types and returns the array. | |
| 647 | - * | |
| 648 | - * @param string $type The comment type. | |
| 649 | - * | |
| 650 | - * @return array The comment type. | |
| 510 | + * @return array The comment type | |
| 651 | 511 | */ |
| 652 | 512 | public static function get_comment_type( $type ) { |
| 653 | - $type = \strtolower( $type ); | |
| 654 | - $type = \sanitize_key( $type ); | |
| 513 | + $type = strtolower( $type ); | |
| 514 | + $type = sanitize_key( $type ); | |
| 515 | + $types = self::get_comment_types(); | |
| 655 | 516 | |
| 656 | - $comment_types = self::get_comment_types(); | |
| 657 | - $type_array = array(); | |
| 658 | - | |
| 659 | - // Check array keys. | |
| 660 | - if ( \in_array( $type, \array_keys( $comment_types ), true ) ) { | |
| 661 | - $type_array = $comment_types[ $type ]; | |
| 517 | + if ( in_array( $type, array_keys( $types ), true ) ) { | |
| 518 | + $type_array = $types[ $type ]; | |
| 519 | + } else { | |
| 520 | + $type_array = array(); | |
| 662 | 521 | } |
| 663 | 522 | |
| 664 | - /** | |
| 665 | - * Filter the comment type. | |
| 666 | - * | |
| 667 | - * @param array $type_array The comment type. | |
| 668 | - */ | |
| 669 | - return \apply_filters( "activitypub_comment_type_{$type}", $type_array ); | |
| 523 | + return apply_filters( "activitypub_comment_type_{$type}", $type_array ); | |
| 670 | 524 | } |
| 671 | 525 | |
| 672 | 526 | /** |
| 673 | - * Get a comment type attribute. | |
| 527 | + * Get a comment type attribute | |
| 674 | 528 | * |
| 675 | - * @param string $type The comment type. | |
| 676 | - * @param string $attr The attribute to get. | |
| 529 | + * @param string $type The comment type | |
| 530 | + * @param string $attr The attribute to get | |
| 677 | 531 | * |
| 678 | - * @return mixed The value of the attribute. | |
| 532 | + * @return mixed The value of the attribute | |
| 679 | 533 | */ |
| 680 | 534 | public static function get_comment_type_attr( $type, $attr ) { |
| 681 | 535 | $type_array = self::get_comment_type( $type ); |
| 682 | 536 | |
| @@ -685,37 +539,30 @@ | ||
| 685 | 539 | } else { |
| 686 | 540 | $value = ''; |
| 687 | 541 | } |
| 688 | 542 | |
| 689 | - /** | |
| 690 | - * Filter the comment type attribute. | |
| 691 | - * | |
| 692 | - * @param mixed $value The value of the attribute. | |
| 693 | - * @param string $type The comment type. | |
| 694 | - */ | |
| 695 | - return \apply_filters( "activitypub_comment_type_{$attr}", $value, $type ); | |
| 543 | + return apply_filters( "activitypub_comment_type_{$attr}", $value, $type ); | |
| 696 | 544 | } |
| 697 | 545 | |
| 546 | + | |
| 547 | + | |
| 698 | 548 | /** |
| 699 | - * Register the comment types used by the ActivityPub plugin. | |
| 549 | + * Register the comment types used by the ActivityPub plugin | |
| 550 | + * | |
| 551 | + * @return void | |
| 700 | 552 | */ |
| 701 | 553 | public static function register_comment_types() { |
| 702 | 554 | register_comment_type( |
| 703 | - 'repost', | |
| 555 | + 'announce', | |
| 704 | 556 | array( |
| 705 | - 'label' => \__( 'Reposts', 'activitypub' ), | |
| 706 | - 'singular' => \__( 'Repost', 'activitypub' ), | |
| 707 | - '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.', | |
| 708 | - 'icon' => '♻️', | |
| 709 | - 'class' => 'p-repost', | |
| 710 | - 'type' => 'repost', | |
| 711 | - 'collection' => 'reposts', | |
| 712 | - 'activity_types' => array( 'announce' ), | |
| 713 | - 'excerpt' => \html_entity_decode( \__( '… reposted this!', 'activitypub' ) ), | |
| 714 | - /* translators: %d: Number of reposts */ | |
| 715 | - 'count_single' => \_x( '%d repost', 'number of reposts', 'activitypub' ), | |
| 716 | - /* translators: %d: Number of reposts */ | |
| 717 | - 'count_plural' => \_x( '%d reposts', 'number of reposts', 'activitypub' ), | |
| 557 | + 'label' => __( 'Reposts', 'activitypub' ), | |
| 558 | + 'singular' => __( 'Repost', 'activitypub' ), | |
| 559 | + 'description' => __( 'A repost on the indieweb is a post that is purely a 100% re-publication of another (typically someone else\'s) post.', 'activitypub' ), | |
| 560 | + 'icon' => '♻️', | |
| 561 | + 'class' => 'p-repost', | |
| 562 | + 'type' => 'repost', | |
| 563 | + // translators: %1$s username, %2$s opject format (post, audio, ...), %3$s URL, %4$s domain | |
| 564 | + 'excerpt' => __( '… reposted this!', 'activitypub' ), | |
| 718 | 565 | ) |
| 719 | 566 | ); |
| 720 | 567 | |
| 721 | 568 | register_comment_type( |
| @@ -720,56 +567,32 @@ | ||
| 720 | 567 | |
| 721 | 568 | register_comment_type( |
| 722 | 569 | 'like', |
| 723 | 570 | array( |
| 724 | - 'label' => \__( 'Likes', 'activitypub' ), | |
| 725 | - 'singular' => \__( 'Like', 'activitypub' ), | |
| 726 | - 'description' => 'A like is a small positive reaction that shows appreciation for a post without sharing it further.', | |
| 727 | - 'icon' => '👍', | |
| 728 | - 'class' => 'p-like', | |
| 729 | - 'type' => 'like', | |
| 730 | - 'collection' => 'likes', | |
| 731 | - 'activity_types' => array( 'like' ), | |
| 732 | - 'excerpt' => \html_entity_decode( \__( '… liked this!', 'activitypub' ) ), | |
| 733 | - /* translators: %d: Number of likes */ | |
| 734 | - 'count_single' => \_x( '%d like', 'number of likes', 'activitypub' ), | |
| 735 | - /* translators: %d: Number of likes */ | |
| 736 | - 'count_plural' => \_x( '%d likes', 'number of likes', 'activitypub' ), | |
| 571 | + 'label' => __( 'Likes', 'activitypub' ), | |
| 572 | + 'singular' => __( 'Like', 'activitypub' ), | |
| 573 | + 'description' => __( 'A like is a popular webaction button and in some cases post type on various silos such as Facebook and Instagram.', 'activitypub' ), | |
| 574 | + 'icon' => '👍', | |
| 575 | + 'class' => 'p-like', | |
| 576 | + 'type' => 'like', | |
| 577 | + // translators: %1$s username, %2$s opject format (post, audio, ...), %3$s URL, %4$s domain | |
| 578 | + 'excerpt' => __( '… liked this!', 'activitypub' ), | |
| 737 | 579 | ) |
| 738 | 580 | ); |
| 739 | - | |
| 740 | - register_comment_type( | |
| 741 | - 'quote', | |
| 742 | - array( | |
| 743 | - 'label' => \__( 'Quotes', 'activitypub' ), | |
| 744 | - 'singular' => \__( 'Quote', 'activitypub' ), | |
| 745 | - '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.', | |
| 746 | - 'icon' => '❞', | |
| 747 | - 'class' => 'p-quote', | |
| 748 | - 'type' => 'quote', | |
| 749 | - 'collection' => 'quotes', | |
| 750 | - 'activity_types' => array( 'quote' ), | |
| 751 | - 'excerpt' => \html_entity_decode( \__( '… quoted this!', 'activitypub' ) ), | |
| 752 | - /* translators: %d: Number of quotes */ | |
| 753 | - 'count_single' => \_x( '%d quote', 'number of quotes', 'activitypub' ), | |
| 754 | - /* translators: %d: Number of quotes */ | |
| 755 | - 'count_plural' => \_x( '%d quotes', 'number of quotes', 'activitypub' ), | |
| 756 | - ) | |
| 757 | - ); | |
| 758 | 581 | } |
| 759 | 582 | |
| 760 | 583 | /** |
| 761 | - * Show avatars on Activities if set. | |
| 584 | + * Show avatars on Activities if set | |
| 762 | 585 | * |
| 763 | - * @param array $types List of avatar enabled comment types. | |
| 586 | + * @param array $types list of avatar enabled comment types | |
| 764 | 587 | * |
| 765 | 588 | * @return array show avatars on Activities |
| 766 | 589 | */ |
| 767 | 590 | public static function get_avatar_comment_types( $types ) { |
| 768 | - $comment_types = self::get_comment_type_slugs(); | |
| 769 | - $types = \array_merge( $types, $comment_types ); | |
| 591 | + $comment_types = self::get_comment_type_names(); | |
| 592 | + $types = array_merge( $types, $comment_types ); | |
| 770 | 593 | |
| 771 | - return \array_unique( $types ); | |
| 594 | + return array_unique( $types ); | |
| 772 | 595 | } |
| 773 | 596 | |
| 774 | 597 | /** |
| 775 | 598 | * Excludes likes and reposts from comment queries. |
| @@ -777,284 +600,27 @@ | ||
| 777 | 600 | * @author Jan Boddez |
| 778 | 601 | * |
| 779 | 602 | * @see https://github.com/janboddez/indieblocks/blob/a2d59de358031056a649ee47a1332ce9e39d4ce2/includes/functions.php#L423-L432 |
| 780 | 603 | * |
| 781 | - * @param \WP_Comment_Query $query Comment count. | |
| 604 | + * @param WP_Comment_Query $query Comment count. | |
| 782 | 605 | */ |
| 783 | 606 | public static function comment_query( $query ) { |
| 784 | - if ( ! $query instanceof \WP_Comment_Query ) { | |
| 607 | + if ( ! $query instanceof WP_Comment_Query ) { | |
| 785 | 608 | return; |
| 786 | 609 | } |
| 787 | 610 | |
| 788 | - // Do not exclude likes and reposts on ActivityPub requests. | |
| 789 | - if ( \defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) { | |
| 611 | + if ( is_admin() || ! is_singular() ) { | |
| 790 | 612 | return; |
| 791 | 613 | } |
| 792 | 614 | |
| 793 | - // Do not exclude likes and reposts on REST requests (handled by rest_comment_query). | |
| 794 | - if ( \wp_is_serving_rest_request() ) { | |
| 615 | + if ( ! empty( $query->query_vars['type__in'] ) ) { | |
| 795 | 616 | return; |
| 796 | 617 | } |
| 797 | 618 | |
| 798 | - // Filter post types for admin requests. | |
| 799 | - if ( \is_admin() ) { | |
| 800 | - $query->query_vars['post_type'] = self::get_allowed_comment_post_types(); | |
| 619 | + if ( isset( $query->query_vars['count'] ) && true === $query->query_vars['count'] ) { | |
| 801 | 620 | return; |
| 802 | 621 | } |
| 803 | 622 | |
| 804 | - // Do not exclude likes and reposts on non-singular pages. | |
| 805 | - if ( ! \is_singular() ) { | |
| 806 | - return; | |
| 807 | - } | |
| 808 | - | |
| 809 | - // Do not exclude likes and reposts if the query is for specific types. | |
| 810 | - if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) { | |
| 811 | - return; | |
| 812 | - } | |
| 813 | - | |
| 814 | - // Do not exclude likes and reposts if the query is already excluding other comment types. | |
| 815 | - if ( ! empty( $query->query_vars['type__not_in'] ) ) { | |
| 816 | - return; | |
| 817 | - } | |
| 818 | - | |
| 819 | - // Exclude likes and reposts by the ActivityPub plugin. | |
| 820 | - $query->query_vars['type__not_in'] = self::get_comment_type_slugs(); | |
| 821 | - } | |
| 822 | - | |
| 823 | - /** | |
| 824 | - * Filters comments in REST API requests. | |
| 825 | - * | |
| 826 | - * Excludes comments on ActivityPub post types and ActivityPub comment | |
| 827 | - * types (likes, reposts) from the REST API. | |
| 828 | - * | |
| 829 | - * @param array $prepared_args Array of arguments for WP_Comment_Query. | |
| 830 | - * | |
| 831 | - * @return array Modified array of arguments. | |
| 832 | - */ | |
| 833 | - public static function rest_comment_query( $prepared_args ) { | |
| 834 | - // Exclude comments on ActivityPub post types. | |
| 835 | - $prepared_args['post_type'] = self::get_allowed_comment_post_types(); | |
| 836 | - | |
| 837 | - // Exclude ActivityPub comment types (likes, reposts) unless explicitly requested. | |
| 838 | - if ( empty( $prepared_args['type'] ) && empty( $prepared_args['type__in'] ) ) { | |
| 839 | - $prepared_args['type__not_in'] = self::get_comment_type_slugs(); | |
| 840 | - } | |
| 841 | - | |
| 842 | - return $prepared_args; | |
| 843 | - } | |
| 844 | - | |
| 845 | - /** | |
| 846 | - * Returns post types that should show comments (excluding hidden post types). | |
| 847 | - * | |
| 848 | - * @return array Array of post type names. | |
| 849 | - */ | |
| 850 | - private static function get_allowed_comment_post_types() { | |
| 851 | - $hide_for = self::hide_for(); | |
| 852 | - | |
| 853 | - if ( empty( $hide_for ) ) { | |
| 854 | - return \get_post_types_by_support( 'comments' ); | |
| 855 | - } | |
| 856 | - | |
| 857 | - return \array_diff( \get_post_types_by_support( 'comments' ), $hide_for ); | |
| 858 | - } | |
| 859 | - | |
| 860 | - /** | |
| 861 | - * Filter the comment status before it is set. | |
| 862 | - * | |
| 863 | - * @param int|string|\WP_Error $approved The approved comment status. | |
| 864 | - * @param array $comment_data The comment data. | |
| 865 | - * | |
| 866 | - * @return int|string|\WP_Error The approval status. 1, 0, 'spam', 'trash', or WP_Error. | |
| 867 | - */ | |
| 868 | - public static function pre_comment_approved( $approved, $comment_data ) { | |
| 869 | - /* | |
| 870 | - * Only return early for already-approved comments, trash, or errors. | |
| 871 | - * Don't short-circuit on 'spam' - we may want to override Akismet. | |
| 872 | - * Respect 'trash' since it comes from the WordPress disallowed list. | |
| 873 | - */ | |
| 874 | - if ( 1 === $approved || '1' === $approved || 'trash' === $approved || \is_wp_error( $approved ) ) { | |
| 875 | - return $approved; | |
| 876 | - } | |
| 877 | - | |
| 878 | - // Maybe auto-approve likes and reposts. | |
| 879 | - if ( | |
| 880 | - \in_array( $comment_data['comment_type'], self::get_comment_type_slugs(), true ) && | |
| 881 | - '1' === \get_option( 'activitypub_auto_approve_reactions' ) | |
| 882 | - ) { | |
| 883 | - return 1; | |
| 884 | - } | |
| 885 | - | |
| 886 | - /* | |
| 887 | - * Always auto-approve comments on remote posts (ap_post) since | |
| 888 | - * they are not visible in the WP admin comment moderation screen. | |
| 889 | - */ | |
| 890 | - $post_id = $comment_data['comment_post_ID']; | |
| 891 | - $post = \get_post( $post_id ); | |
| 892 | - | |
| 893 | - if ( $post && \in_array( $post->post_type, self::hide_for(), true ) ) { | |
| 894 | - return 1; | |
| 895 | - } | |
| 896 | - | |
| 897 | - if ( '1' !== \get_option( 'comment_previously_approved' ) ) { | |
| 898 | - return $approved; | |
| 899 | - } | |
| 900 | - | |
| 901 | - if ( | |
| 902 | - empty( $comment_data['comment_meta']['protocol'] ) || | |
| 903 | - 'activitypub' !== $comment_data['comment_meta']['protocol'] | |
| 904 | - ) { | |
| 905 | - return $approved; | |
| 906 | - } | |
| 907 | - | |
| 908 | - global $wpdb; | |
| 909 | - | |
| 910 | - $author = $comment_data['comment_author']; | |
| 911 | - $author_url = $comment_data['comment_author_url']; | |
| 912 | - // phpcs:ignore | |
| 913 | - $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 ) ); | |
| 914 | - | |
| 915 | - if ( 1 === (int) $ok_to_comment ) { | |
| 916 | - return 1; | |
| 917 | - } | |
| 918 | - | |
| 919 | - return $approved; | |
| 920 | - } | |
| 921 | - | |
| 922 | - /** | |
| 923 | - * Update comment counts when interaction settings are disabled. | |
| 924 | - * | |
| 925 | - * Triggers a recount when likes or reposts are disabled to ensure accurate comment counts. | |
| 926 | - * | |
| 927 | - * @param mixed $old_value The old option value. | |
| 928 | - * @param mixed $value The new option value. | |
| 929 | - */ | |
| 930 | - public static function maybe_update_comment_counts( $old_value, $value ) { | |
| 931 | - if ( '1' === $old_value && '1' !== $value ) { | |
| 932 | - Migration::update_comment_counts(); | |
| 933 | - } | |
| 934 | - } | |
| 935 | - | |
| 936 | - /** | |
| 937 | - * Filters the comment count to exclude ActivityPub comment types. | |
| 938 | - * | |
| 939 | - * @param int|null $new_count The new comment count. Default null. | |
| 940 | - * @param int $old_count The old comment count. | |
| 941 | - * @param int $post_id Post ID. | |
| 942 | - * | |
| 943 | - * @return int|null The updated comment count, or null to use the default query. | |
| 944 | - */ | |
| 945 | - public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) { | |
| 946 | - if ( null === $new_count ) { | |
| 947 | - $excluded_types = \array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) ); | |
| 948 | - | |
| 949 | - if ( ! empty( $excluded_types ) ) { | |
| 950 | - /* | |
| 951 | - * Include 'note' type when Gutenberg's filter is registered, so a | |
| 952 | - * single query excludes both ActivityPub and Gutenberg types. | |
| 953 | - */ | |
| 954 | - if ( \has_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count' ) ) { | |
| 955 | - $excluded_types[] = 'note'; | |
| 956 | - } | |
| 957 | - | |
| 958 | - /** | |
| 959 | - * Filters the comment types excluded from the comment count. | |
| 960 | - * | |
| 961 | - * Runs at priority 5 on `pre_wp_update_comment_count_now` so that | |
| 962 | - * a single query can exclude types from multiple plugins. Other | |
| 963 | - * plugins can hook here to add their own comment types. | |
| 964 | - * | |
| 965 | - * @since 8.0.0 | |
| 966 | - * | |
| 967 | - * @param string[] $excluded_types The comment type slugs to exclude. | |
| 968 | - * @param int $post_id The post ID. | |
| 969 | - */ | |
| 970 | - $excluded_types = \apply_filters( 'activitypub_excluded_comment_types', $excluded_types, $post_id ); | |
| 971 | - $excluded_types = \array_unique( \array_filter( $excluded_types ) ); | |
| 972 | - | |
| 973 | - global $wpdb; | |
| 974 | - | |
| 975 | - // phpcs:ignore WordPress.DB | |
| 976 | - $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 ) ); | |
| 977 | - } | |
| 978 | - } | |
| 979 | - | |
| 980 | - return $new_count; | |
| 981 | - } | |
| 982 | - | |
| 983 | - /** | |
| 984 | - * Check if a comment type is enabled. | |
| 985 | - * | |
| 986 | - * @param string $comment_type The comment type. | |
| 987 | - * @return bool True if the comment type is enabled. | |
| 988 | - */ | |
| 989 | - public static function is_comment_type_enabled( $comment_type ) { | |
| 990 | - return '1' === \get_option( "activitypub_allow_{$comment_type}s", '1' ); | |
| 991 | - } | |
| 992 | - | |
| 993 | - /** | |
| 994 | - * Get post types to hide comments for in admin. | |
| 995 | - * | |
| 996 | - * These are non-public post types whose comments should not appear | |
| 997 | - * in the main comments list in the WordPress admin. | |
| 998 | - * | |
| 999 | - * @return string[] Array of post type names to hide comments for. | |
| 1000 | - */ | |
| 1001 | - public static function hide_for() { | |
| 1002 | - $post_types = array( Remote_Posts::POST_TYPE ); | |
| 1003 | - | |
| 1004 | - /** | |
| 1005 | - * Filters the list of post types to hide comments for. | |
| 1006 | - * | |
| 1007 | - * @param string[] $post_types Array of post type names to hide comments for. | |
| 1008 | - */ | |
| 1009 | - return \apply_filters( 'activitypub_hide_comments_for', $post_types ); | |
| 1010 | - } | |
| 1011 | - | |
| 1012 | - /** | |
| 1013 | - * Render emoji in comment author name. | |
| 1014 | - * | |
| 1015 | - * Replaces emoji shortcodes with img tags on the get_comment_author filter. | |
| 1016 | - * Emoji data is retrieved from the linked remote actor. | |
| 1017 | - * | |
| 1018 | - * @param string $author The comment author name. | |
| 1019 | - * @param string $comment_id The comment ID as a numeric string. | |
| 1020 | - * | |
| 1021 | - * @return string The comment author name with rendered emoji. | |
| 1022 | - */ | |
| 1023 | - public static function render_emoji( $author, $comment_id ) { | |
| 1024 | - $remote_actor_id = \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ); | |
| 1025 | - | |
| 1026 | - if ( empty( $remote_actor_id ) ) { | |
| 1027 | - return $author; | |
| 1028 | - } | |
| 1029 | - | |
| 1030 | - $emoji_data = \get_post_meta( $remote_actor_id, '_activitypub_emoji', true ); | |
| 1031 | - | |
| 1032 | - if ( empty( $emoji_data ) ) { | |
| 1033 | - return $author; | |
| 1034 | - } | |
| 1035 | - | |
| 1036 | - return Emoji::replace_from_json( $author, $emoji_data ); | |
| 1037 | - } | |
| 1038 | - | |
| 1039 | - /** | |
| 1040 | - * Selectively unescape emoji images in comment author. | |
| 1041 | - * | |
| 1042 | - * This runs at priority 20 after WordPress's esc_html() filter on comment_author. | |
| 1043 | - * | |
| 1044 | - * @param string $author The comment author name (already escaped by WordPress). | |
| 1045 | - * | |
| 1046 | - * @return string The comment author name with emoji images unescaped. | |
| 1047 | - */ | |
| 1048 | - public static function unescape_emoji( $author ) { | |
| 1049 | - // Only attempt to unescape if there are emoji images present in the escaped string. | |
| 1050 | - if ( false === \strpos( $author, 'class="emoji"' ) ) { | |
| 1051 | - return $author; | |
| 1052 | - } | |
| 1053 | - | |
| 1054 | - // Decode entities so we can selectively restore emoji <img> tags. | |
| 1055 | - $decoded = \html_entity_decode( $author, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); | |
| 1056 | - | |
| 1057 | - // Use strict KSES validation to only allow valid emoji img tags. | |
| 1058 | - return \wp_kses( $decoded, Emoji::get_kses_allowed_html() ); | |
| 623 | + // Exclude likes and reposts by the Webmention plugin. | |
| 624 | + $query->query_vars['type__not_in'] = self::get_comment_type_names(); | |
| 1059 | 625 | } |
| 1060 | 626 | } |