| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Collection\Actors; |
| 11 | -use Activitypub\Collection\Remote_Posts; | |
| 11 | +use WP_Comment_Query; | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * ActivityPub Comment Class. |
| 15 | 15 | * |
| @@ -22,82 +22,23 @@ | ||
| 22 | 22 | */ |
| 23 | 23 | public static function init() { |
| 24 | 24 | self::register_comment_types(); |
| 25 | 25 | |
| 26 | - \add_filter( 'map_meta_cap', array( self::class, 'map_meta_cap' ), 10, 4 ); | |
| 27 | 26 | \add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 3 ); |
| 28 | 27 | \add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 ); |
| 29 | - \add_filter( 'comment_feed_where', array( static::class, 'comment_feed_where' ) ); | |
| 30 | - \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 ); | |
| 28 | + \add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 ); | |
| 29 | + \add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); | |
| 31 | 30 | \add_action( 'pre_get_comments', array( static::class, 'comment_query' ) ); |
| 32 | - \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 11, 2 ); | |
| 31 | + \add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 ); | |
| 33 | 32 | \add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 ); |
| 34 | - \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. | |
| 33 | + \add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 ); | |
| 41 | 34 | } |
| 42 | 35 | |
| 43 | 36 | /** |
| 44 | - * Render blocks in comment content. | |
| 45 | - * | |
| 46 | - * Comments don't automatically parse blocks like posts do. | |
| 47 | - * This filter applies do_blocks() to render activitypub/emoji | |
| 48 | - * and activitypub/image blocks in comment content. | |
| 49 | - * | |
| 50 | - * @param string $content The comment content. | |
| 51 | - * | |
| 52 | - * @return string The content with blocks rendered. | |
| 53 | - */ | |
| 54 | - public static function render_blocks( $content ) { | |
| 55 | - if ( empty( $content ) || ! \str_contains( $content, '<!-- wp:activitypub/' ) ) { | |
| 56 | - return $content; | |
| 57 | - } | |
| 58 | - | |
| 59 | - $blocks = \parse_blocks( $content ); | |
| 60 | - $output = ''; | |
| 61 | - | |
| 62 | - foreach ( $blocks as $block ) { | |
| 63 | - if ( ! empty( $block['blockName'] ) && \str_starts_with( $block['blockName'], 'activitypub/' ) ) { | |
| 64 | - $output .= \render_block( $block ); | |
| 65 | - } else { | |
| 66 | - $output .= \serialize_block( $block ); | |
| 67 | - } | |
| 68 | - } | |
| 69 | - | |
| 70 | - return $output; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Remove edit capabilities for comments received via ActivityPub. | |
| 75 | - * | |
| 76 | - * @param array $caps Array of capabilities. | |
| 77 | - * @param string $cap Capability name. | |
| 78 | - * @param int $user_id User ID. | |
| 79 | - * @param array $args Array of arguments. | |
| 80 | - * | |
| 81 | - * @return array Modified array of capabilities. | |
| 82 | - */ | |
| 83 | - public static function map_meta_cap( $caps, $cap, $user_id, $args ) { | |
| 84 | - if ( 'edit_comment' === $cap && self::was_received( $args[0] ) ) { | |
| 85 | - if ( ! \is_admin() || ( isset( $GLOBALS['current_screen'] ) && 'comment' === $GLOBALS['current_screen']->id ) ) { | |
| 86 | - $caps[] = 'do_not_allow'; | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - return $caps; | |
| 91 | - } | |
| 92 | - | |
| 93 | - /** | |
| 94 | 37 | * Filter the comment reply link. |
| 95 | 38 | * |
| 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 | |
| 39 | + * We don't want to show the comment reply link for federated comments | |
| 40 | + * if the user is disabled for federation. | |
| 100 | 41 | * |
| 101 | 42 | * @param string $link The HTML markup for the comment reply link. |
| 102 | 43 | * @param array $args An array of arguments overriding the defaults. |
| 103 | 44 | * @param \WP_Comment $comment The object of the comment being replied. |
| @@ -105,65 +46,59 @@ | ||
| 105 | 46 | * @return string The filtered HTML markup for the comment reply link. |
| 106 | 47 | */ |
| 107 | 48 | public static function comment_reply_link( $link, $args, $comment ) { |
| 108 | 49 | 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 | - ); | |
| 50 | + $user_id = get_current_user_id(); | |
| 51 | + if ( $user_id && self::was_received( $comment ) && \user_can( $user_id, 'activitypub' ) ) { | |
| 52 | + return self::create_fediverse_reply_link( $link, $args ); | |
| 130 | 53 | } |
| 131 | 54 | |
| 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 ); | |
| 55 | + return $link; | |
| 144 | 56 | } |
| 145 | 57 | |
| 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( | |
| 58 | + $attrs = array( | |
| 151 | 59 | 'selectedComment' => self::generate_id( $comment ), |
| 152 | 60 | 'commentId' => $comment->comment_ID, |
| 153 | 61 | ); |
| 154 | 62 | |
| 155 | - $block = \do_blocks( \sprintf( '<!-- wp:activitypub/remote-reply %s /-->', \wp_json_encode( $attributes ) ) ); | |
| 63 | + $div = sprintf( | |
| 64 | + '<div class="activitypub-remote-reply" data-attrs="%s"></div>', | |
| 65 | + esc_attr( wp_json_encode( $attrs ) ) | |
| 66 | + ); | |
| 156 | 67 | |
| 157 | 68 | /** |
| 158 | 69 | * Filters the HTML markup for the ActivityPub remote comment reply container. |
| 159 | 70 | * |
| 160 | - * @param string $block The HTML markup for the remote reply container. | |
| 71 | + * @param string $div The HTML markup for the remote reply container. Default is a div | |
| 72 | + * with class 'activitypub-remote-reply' and data attributes for | |
| 73 | + * the selected comment ID and internal comment ID. | |
| 161 | 74 | */ |
| 162 | - return \apply_filters( 'activitypub_comment_reply_link', $block ); | |
| 75 | + return apply_filters( 'activitypub_comment_reply_link', $div ); | |
| 163 | 76 | } |
| 164 | 77 | |
| 165 | 78 | /** |
| 79 | + * Create a link to reply to a federated comment. | |
| 80 | + * | |
| 81 | + * This function adds a title attribute to the reply link to inform the user | |
| 82 | + * that the comment was received from the fediverse and the reply will be sent | |
| 83 | + * to the original author. | |
| 84 | + * | |
| 85 | + * @param string $link The HTML markup for the comment reply link. | |
| 86 | + * @param array $args The args provided by the `comment_reply_link` filter. | |
| 87 | + * | |
| 88 | + * @return string The modified HTML markup for the comment reply link. | |
| 89 | + */ | |
| 90 | + private static function create_fediverse_reply_link( $link, $args ) { | |
| 91 | + $str_to_replace = sprintf( '>%s<', $args['reply_text'] ); | |
| 92 | + $replace_with = sprintf( | |
| 93 | + ' title="%s">%s<', | |
| 94 | + esc_attr__( 'This comment was received from the fediverse and your reply will be sent to the original author', 'activitypub' ), | |
| 95 | + esc_html__( 'Reply with federation', 'activitypub' ) | |
| 96 | + ); | |
| 97 | + return str_replace( $str_to_replace, $replace_with, $link ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 166 | 101 | * Check if it is allowed to comment to a comment. |
| 167 | 102 | * |
| 168 | 103 | * Checks if the comment is local only or if the user can comment federated comments. |
| 169 | 104 | * |
| @@ -177,21 +112,26 @@ | ||
| 177 | 112 | if ( ! self::was_received( $comment ) ) { |
| 178 | 113 | return true; |
| 179 | 114 | } |
| 180 | 115 | |
| 181 | - $current_user = \get_current_user_id(); | |
| 116 | + $current_user = get_current_user_id(); | |
| 182 | 117 | |
| 183 | 118 | if ( ! $current_user ) { |
| 184 | 119 | return false; |
| 185 | 120 | } |
| 186 | 121 | |
| 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. | |
| 122 | + if ( is_single_user() && \user_can( $current_user, 'publish_posts' ) ) { | |
| 123 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user. | |
| 189 | 124 | $current_user = Actors::BLOG_USER_ID; |
| 190 | 125 | } |
| 191 | 126 | |
| 192 | - // User is not allowed to federate comments. | |
| 193 | - return user_can_activitypub( $current_user ); | |
| 127 | + $is_user_disabled = is_user_disabled( $current_user ); | |
| 128 | + | |
| 129 | + if ( $is_user_disabled ) { | |
| 130 | + return false; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return true; | |
| 194 | 134 | } |
| 195 | 135 | |
| 196 | 136 | /** |
| 197 | 137 | * Check if a comment is federated. |
| @@ -289,25 +229,16 @@ | ||
| 289 | 229 | return false; |
| 290 | 230 | } |
| 291 | 231 | |
| 292 | 232 | 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. | |
| 233 | + // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user. | |
| 294 | 234 | $user_id = Actors::BLOG_USER_ID; |
| 295 | 235 | } |
| 296 | 236 | |
| 297 | - // User is not allowed to federate comments. | |
| 298 | - if ( ! user_can_activitypub( $user_id ) ) { | |
| 299 | - return false; | |
| 300 | - } | |
| 237 | + $is_user_disabled = is_user_disabled( $user_id ); | |
| 301 | 238 | |
| 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 ) ) { | |
| 239 | + // User is disabled for federation. | |
| 240 | + if ( $is_user_disabled ) { | |
| 310 | 241 | return false; |
| 311 | 242 | } |
| 312 | 243 | |
| 313 | 244 | // It is a comment to the post and can be federated. |
| @@ -323,34 +254,25 @@ | ||
| 323 | 254 | |
| 324 | 255 | /** |
| 325 | 256 | * Examine a comment ID and look up an existing comment it represents. |
| 326 | 257 | * |
| 327 | - * @since 9.1.0 Added the `$args` parameter. | |
| 258 | + * @param string $id ActivityPub object ID (usually a URL) to check. | |
| 328 | 259 | * |
| 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 | 260 | * @return \WP_Comment|false Comment object, or false on failure. |
| 334 | 261 | */ |
| 335 | - public static function object_id_to_comment( $id, $args = array() ) { | |
| 336 | - $args = \wp_parse_args( | |
| 337 | - $args, | |
| 262 | + public static function object_id_to_comment( $id ) { | |
| 263 | + $comment_query = new WP_Comment_Query( | |
| 338 | 264 | array( |
| 339 | - 'number' => 1, | |
| 340 | - 'orderby' => 'comment_date', | |
| 341 | - 'order' => 'DESC', | |
| 265 | + 'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 266 | + 'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value | |
| 342 | 267 | ) |
| 343 | 268 | ); |
| 344 | 269 | |
| 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 | |
| 270 | + if ( ! $comment_query->comments ) { | |
| 271 | + return false; | |
| 272 | + } | |
| 349 | 273 | |
| 350 | - $comment_query = new \WP_Comment_Query( $args ); | |
| 351 | - | |
| 352 | - if ( ! $comment_query->comments ) { | |
| 274 | + if ( count( $comment_query->comments ) > 1 ) { | |
| 353 | 275 | return false; |
| 354 | 276 | } |
| 355 | 277 | |
| 356 | 278 | return $comment_query->comments[0]; |
| @@ -364,18 +286,18 @@ | ||
| 364 | 286 | * |
| 365 | 287 | * @return string|null Comment ID or null if not found. |
| 366 | 288 | */ |
| 367 | 289 | public static function url_to_commentid( $url ) { |
| 368 | - if ( ! $url || ! \filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 290 | + if ( ! $url || ! filter_var( $url, \FILTER_VALIDATE_URL ) ) { | |
| 369 | 291 | return null; |
| 370 | 292 | } |
| 371 | 293 | |
| 372 | 294 | // Check for local comment. |
| 373 | - if ( is_same_domain( $url ) ) { | |
| 295 | + if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) === \wp_parse_url( $url, \PHP_URL_HOST ) ) { | |
| 374 | 296 | $query = \wp_parse_url( $url, \PHP_URL_QUERY ); |
| 375 | 297 | |
| 376 | 298 | if ( $query ) { |
| 377 | - \parse_str( $query, $params ); | |
| 299 | + parse_str( $query, $params ); | |
| 378 | 300 | |
| 379 | 301 | if ( ! empty( $params['c'] ) ) { |
| 380 | 302 | $comment = \get_comment( $params['c'] ); |
| 381 | 303 | |
| @@ -400,12 +322,12 @@ | ||
| 400 | 322 | ), |
| 401 | 323 | ), |
| 402 | 324 | ); |
| 403 | 325 | |
| 404 | - $query = new \WP_Comment_Query(); | |
| 326 | + $query = new WP_Comment_Query(); | |
| 405 | 327 | $comments = $query->query( $args ); |
| 406 | 328 | |
| 407 | - if ( $comments && \is_array( $comments ) ) { | |
| 329 | + if ( $comments && is_array( $comments ) ) { | |
| 408 | 330 | return $comments[0]->comment_ID; |
| 409 | 331 | } |
| 410 | 332 | |
| 411 | 333 | return null; |
| @@ -421,9 +343,9 @@ | ||
| 421 | 343 | * @return string[] An array of classes. |
| 422 | 344 | */ |
| 423 | 345 | public static function comment_class( $classes, $css_class, $comment_id ) { |
| 424 | 346 | // Check if ActivityPub comment. |
| 425 | - if ( 'activitypub' === \get_comment_meta( $comment_id, 'protocol', true ) ) { | |
| 347 | + if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) { | |
| 426 | 348 | $classes[] = 'activitypub-comment'; |
| 427 | 349 | } |
| 428 | 350 | |
| 429 | 351 | return $classes; |
| @@ -429,39 +351,8 @@ | ||
| 429 | 351 | return $classes; |
| 430 | 352 | } |
| 431 | 353 | |
| 432 | 354 | /** |
| 433 | - * Makes the comment feed filterable by comment type. | |
| 434 | - * | |
| 435 | - * Also excludes ActivityPub comment types from the feed when no type is specified. | |
| 436 | - * | |
| 437 | - * @param string $where The `WHERE` clause for the comment feed query. | |
| 438 | - * | |
| 439 | - * @return string The modified `WHERE` clause. | |
| 440 | - */ | |
| 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; | |
| 448 | - } | |
| 449 | - | |
| 450 | - $comment_types = self::get_comment_type_slugs(); | |
| 451 | - | |
| 452 | - if ( \in_array( $comment_type, $comment_types, true ) ) { | |
| 453 | - $where .= $wpdb->prepare( ' AND comment_type = %s', $comment_type ); | |
| 454 | - } else { | |
| 455 | - $placeholders = \implode( ', ', \array_fill( 0, \count( $comment_types ), '%s' ) ); | |
| 456 | - // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQL.NotPrepared | |
| 457 | - $where .= $wpdb->prepare( \sprintf( ' AND comment_type NOT IN (%s)', $placeholders ), ...$comment_types ); | |
| 458 | - } | |
| 459 | - | |
| 460 | - return $where; | |
| 461 | - } | |
| 462 | - | |
| 463 | - /** | |
| 464 | 355 | * Gets the public comment id via the WordPress comments meta. |
| 465 | 356 | * |
| 466 | 357 | * @param int $wp_comment_id The internal WordPress comment ID. |
| 467 | 358 | * @param bool $fallback Whether the code should fall back to `source_url` if `source_id` is not set. |
| @@ -508,18 +399,15 @@ | ||
| 508 | 399 | * |
| 509 | 400 | * @return string $url |
| 510 | 401 | */ |
| 511 | 402 | public static function remote_comment_link( $comment_link, $comment ) { |
| 512 | - if ( ! $comment || \is_admin() || \is_search() ) { | |
| 403 | + if ( ! $comment || is_admin() ) { | |
| 513 | 404 | return $comment_link; |
| 514 | 405 | } |
| 515 | 406 | |
| 516 | - $remote_comment_link = null; | |
| 517 | - if ( 'comment' === $comment->comment_type ) { | |
| 518 | - $remote_comment_link = self::get_source_url( $comment->comment_ID ); | |
| 519 | - } | |
| 407 | + $public_comment_link = self::get_source_url( $comment->comment_ID ); | |
| 520 | 408 | |
| 521 | - return $remote_comment_link ?? $comment_link; | |
| 409 | + return $public_comment_link ?? $comment_link; | |
| 522 | 410 | } |
| 523 | 411 | |
| 524 | 412 | |
| 525 | 413 | /** |
| @@ -539,9 +427,9 @@ | ||
| 539 | 427 | return $public_comment_link; |
| 540 | 428 | } |
| 541 | 429 | |
| 542 | 430 | // Generate URI based on comment ID. |
| 543 | - return \add_query_arg( 'c', $comment->comment_ID, \home_url( '/' ) ); | |
| 431 | + return \add_query_arg( 'c', $comment->comment_ID, \trailingslashit( \home_url() ) ); | |
| 544 | 432 | } |
| 545 | 433 | |
| 546 | 434 | /** |
| 547 | 435 | * Check if a post has remote comments |
| @@ -573,8 +461,61 @@ | ||
| 573 | 461 | return ! empty( $comments ); |
| 574 | 462 | } |
| 575 | 463 | |
| 576 | 464 | /** |
| 465 | + * Enqueue scripts for remote comments | |
| 466 | + */ | |
| 467 | + public static function enqueue_scripts() { | |
| 468 | + if ( ! \is_singular() || \is_user_logged_in() ) { | |
| 469 | + // Only on single pages, only for logged-out users. | |
| 470 | + return; | |
| 471 | + } | |
| 472 | + | |
| 473 | + if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) { | |
| 474 | + // Post type does not support ActivityPub. | |
| 475 | + return; | |
| 476 | + } | |
| 477 | + | |
| 478 | + if ( ! \comments_open() || ! \get_comments_number() ) { | |
| 479 | + // No comments, no need to load the script. | |
| 480 | + return; | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( ! self::post_has_remote_comments( \get_the_ID() ) ) { | |
| 484 | + // No remote comments, no need to load the script. | |
| 485 | + return; | |
| 486 | + } | |
| 487 | + | |
| 488 | + $handle = 'activitypub-remote-reply'; | |
| 489 | + $data = array( | |
| 490 | + 'namespace' => ACTIVITYPUB_REST_NAMESPACE, | |
| 491 | + 'defaultAvatarUrl' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg', | |
| 492 | + ); | |
| 493 | + $js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) ); | |
| 494 | + $asset_file = ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply/index.asset.php'; | |
| 495 | + | |
| 496 | + if ( \file_exists( $asset_file ) ) { | |
| 497 | + $assets = require_once $asset_file; | |
| 498 | + | |
| 499 | + \wp_enqueue_script( | |
| 500 | + $handle, | |
| 501 | + \plugins_url( 'build/remote-reply/index.js', __DIR__ ), | |
| 502 | + $assets['dependencies'], | |
| 503 | + $assets['version'], | |
| 504 | + true | |
| 505 | + ); | |
| 506 | + \wp_add_inline_script( $handle, $js, 'before' ); | |
| 507 | + | |
| 508 | + \wp_enqueue_style( | |
| 509 | + $handle, | |
| 510 | + \plugins_url( 'build/remote-reply/style-index.css', __DIR__ ), | |
| 511 | + array( 'wp-components' ), | |
| 512 | + $assets['version'] | |
| 513 | + ); | |
| 514 | + } | |
| 515 | + } | |
| 516 | + | |
| 517 | + /** | |
| 577 | 518 | * Get the comment type by activity type. |
| 578 | 519 | * |
| 579 | 520 | * @param string $activity_type The activity type. |
| 580 | 521 | * |
| @@ -585,9 +526,9 @@ | ||
| 585 | 526 | $activity_type = \sanitize_key( $activity_type ); |
| 586 | 527 | $comment_types = self::get_comment_types(); |
| 587 | 528 | |
| 588 | 529 | foreach ( $comment_types as $comment_type ) { |
| 589 | - if ( \in_array( $activity_type, $comment_type['activity_types'], true ) ) { | |
| 530 | + if ( in_array( $activity_type, $comment_type['activity_types'], true ) ) { | |
| 590 | 531 | return $comment_type; |
| 591 | 532 | } |
| 592 | 533 | } |
| 593 | 534 | |
| @@ -601,9 +542,9 @@ | ||
| 601 | 542 | */ |
| 602 | 543 | public static function get_comment_types() { |
| 603 | 544 | global $activitypub_comment_types; |
| 604 | 545 | |
| 605 | - return (array) $activitypub_comment_types; | |
| 546 | + return $activitypub_comment_types; | |
| 606 | 547 | } |
| 607 | 548 | |
| 608 | 549 | /** |
| 609 | 550 | * Is this a registered comment type. |
| @@ -626,15 +567,22 @@ | ||
| 626 | 567 | * |
| 627 | 568 | * @return array The registered custom comment type slugs. |
| 628 | 569 | */ |
| 629 | 570 | 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' ); | |
| 571 | + return array_keys( self::get_comment_types() ); | |
| 572 | + } | |
| 632 | 573 | |
| 633 | - return array(); | |
| 634 | - } | |
| 574 | + /** | |
| 575 | + * Return the registered custom comment type slugs. | |
| 576 | + * | |
| 577 | + * @deprecated 4.5.0 Use get_comment_type_slugs instead. | |
| 578 | + * | |
| 579 | + * @return array The registered custom comment type slugs. | |
| 580 | + */ | |
| 581 | + public static function get_comment_type_names() { | |
| 582 | + _deprecated_function( __METHOD__, '4.5.0', 'get_comment_type_slugs' ); | |
| 635 | 583 | |
| 636 | - return \array_keys( self::get_comment_types() ); | |
| 584 | + return self::get_comment_type_slugs(); | |
| 637 | 585 | } |
| 638 | 586 | |
| 639 | 587 | /** |
| 640 | 588 | * Get the custom comment type. |
| @@ -648,16 +596,16 @@ | ||
| 648 | 596 | * |
| 649 | 597 | * @return array The comment type. |
| 650 | 598 | */ |
| 651 | 599 | public static function get_comment_type( $type ) { |
| 652 | - $type = \strtolower( $type ); | |
| 653 | - $type = \sanitize_key( $type ); | |
| 600 | + $type = strtolower( $type ); | |
| 601 | + $type = sanitize_key( $type ); | |
| 654 | 602 | |
| 655 | 603 | $comment_types = self::get_comment_types(); |
| 656 | 604 | $type_array = array(); |
| 657 | 605 | |
| 658 | 606 | // Check array keys. |
| 659 | - if ( \in_array( $type, \array_keys( $comment_types ), true ) ) { | |
| 607 | + if ( in_array( $type, array_keys( $comment_types ), true ) ) { | |
| 660 | 608 | $type_array = $comment_types[ $type ]; |
| 661 | 609 | } |
| 662 | 610 | |
| 663 | 611 | /** |
| @@ -664,9 +612,9 @@ | ||
| 664 | 612 | * Filter the comment type. |
| 665 | 613 | * |
| 666 | 614 | * @param array $type_array The comment type. |
| 667 | 615 | */ |
| 668 | - return \apply_filters( "activitypub_comment_type_{$type}", $type_array ); | |
| 616 | + return apply_filters( "activitypub_comment_type_{$type}", $type_array ); | |
| 669 | 617 | } |
| 670 | 618 | |
| 671 | 619 | /** |
| 672 | 620 | * Get a comment type attribute. |
| @@ -690,9 +638,9 @@ | ||
| 690 | 638 | * |
| 691 | 639 | * @param mixed $value The value of the attribute. |
| 692 | 640 | * @param string $type The comment type. |
| 693 | 641 | */ |
| 694 | - return \apply_filters( "activitypub_comment_type_{$attr}", $value, $type ); | |
| 642 | + return apply_filters( "activitypub_comment_type_{$attr}", $value, $type ); | |
| 695 | 643 | } |
| 696 | 644 | |
| 697 | 645 | /** |
| 698 | 646 | * Register the comment types used by the ActivityPub plugin. |
| @@ -700,21 +648,21 @@ | ||
| 700 | 648 | public static function register_comment_types() { |
| 701 | 649 | register_comment_type( |
| 702 | 650 | 'repost', |
| 703 | 651 | array( |
| 704 | - 'label' => \__( 'Reposts', 'activitypub' ), | |
| 705 | - 'singular' => \__( 'Repost', 'activitypub' ), | |
| 706 | - '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.', | |
| 652 | + 'label' => __( 'Reposts', 'activitypub' ), | |
| 653 | + 'singular' => __( 'Repost', 'activitypub' ), | |
| 654 | + 'description' => __( 'A repost on the indieweb is a post that is purely a 100% re-publication of another (typically someone else\'s) post.', 'activitypub' ), | |
| 707 | 655 | 'icon' => '♻️', |
| 708 | 656 | 'class' => 'p-repost', |
| 709 | 657 | 'type' => 'repost', |
| 710 | 658 | 'collection' => 'reposts', |
| 711 | 659 | 'activity_types' => array( 'announce' ), |
| 712 | - 'excerpt' => \html_entity_decode( \__( '… reposted this!', 'activitypub' ) ), | |
| 660 | + 'excerpt' => __( '… reposted this!', 'activitypub' ), | |
| 713 | 661 | /* translators: %d: Number of reposts */ |
| 714 | - 'count_single' => \_x( '%d repost', 'number of reposts', 'activitypub' ), | |
| 662 | + 'count_single' => _x( '%d repost', 'number of reposts', 'activitypub' ), | |
| 715 | 663 | /* translators: %d: Number of reposts */ |
| 716 | - 'count_plural' => \_x( '%d reposts', 'number of reposts', 'activitypub' ), | |
| 664 | + 'count_plural' => _x( '%d reposts', 'number of reposts', 'activitypub' ), | |
| 717 | 665 | ) |
| 718 | 666 | ); |
| 719 | 667 | |
| 720 | 668 | register_comment_type( |
| @@ -719,42 +667,23 @@ | ||
| 719 | 667 | |
| 720 | 668 | register_comment_type( |
| 721 | 669 | 'like', |
| 722 | 670 | array( |
| 723 | - 'label' => \__( 'Likes', 'activitypub' ), | |
| 724 | - 'singular' => \__( 'Like', 'activitypub' ), | |
| 725 | - 'description' => 'A like is a small positive reaction that shows appreciation for a post without sharing it further.', | |
| 671 | + 'label' => __( 'Likes', 'activitypub' ), | |
| 672 | + 'singular' => __( 'Like', 'activitypub' ), | |
| 673 | + 'description' => __( 'A like is a popular webaction button and in some cases post type on various silos such as Facebook and Instagram.', 'activitypub' ), | |
| 726 | 674 | 'icon' => '👍', |
| 727 | 675 | 'class' => 'p-like', |
| 728 | 676 | 'type' => 'like', |
| 729 | 677 | 'collection' => 'likes', |
| 730 | 678 | 'activity_types' => array( 'like' ), |
| 731 | - 'excerpt' => \html_entity_decode( \__( '… liked this!', 'activitypub' ) ), | |
| 679 | + 'excerpt' => __( '… liked this!', 'activitypub' ), | |
| 732 | 680 | /* translators: %d: Number of likes */ |
| 733 | - 'count_single' => \_x( '%d like', 'number of likes', 'activitypub' ), | |
| 681 | + 'count_single' => _x( '%d like', 'number of likes', 'activitypub' ), | |
| 734 | 682 | /* translators: %d: Number of likes */ |
| 735 | - 'count_plural' => \_x( '%d likes', 'number of likes', 'activitypub' ), | |
| 683 | + 'count_plural' => _x( '%d likes', 'number of likes', 'activitypub' ), | |
| 736 | 684 | ) |
| 737 | 685 | ); |
| 738 | - | |
| 739 | - register_comment_type( | |
| 740 | - 'quote', | |
| 741 | - array( | |
| 742 | - 'label' => \__( 'Quotes', 'activitypub' ), | |
| 743 | - 'singular' => \__( 'Quote', 'activitypub' ), | |
| 744 | - '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.', | |
| 745 | - 'icon' => '❞', | |
| 746 | - 'class' => 'p-quote', | |
| 747 | - 'type' => 'quote', | |
| 748 | - 'collection' => 'quotes', | |
| 749 | - 'activity_types' => array( 'quote' ), | |
| 750 | - 'excerpt' => \html_entity_decode( \__( '… quoted this!', 'activitypub' ) ), | |
| 751 | - /* translators: %d: Number of quotes */ | |
| 752 | - 'count_single' => \_x( '%d quote', 'number of quotes', 'activitypub' ), | |
| 753 | - /* translators: %d: Number of quotes */ | |
| 754 | - 'count_plural' => \_x( '%d quotes', 'number of quotes', 'activitypub' ), | |
| 755 | - ) | |
| 756 | - ); | |
| 757 | 686 | } |
| 758 | 687 | |
| 759 | 688 | /** |
| 760 | 689 | * Show avatars on Activities if set. |
| @@ -764,11 +693,11 @@ | ||
| 764 | 693 | * @return array show avatars on Activities |
| 765 | 694 | */ |
| 766 | 695 | public static function get_avatar_comment_types( $types ) { |
| 767 | 696 | $comment_types = self::get_comment_type_slugs(); |
| 768 | - $types = \array_merge( $types, $comment_types ); | |
| 697 | + $types = array_merge( $types, $comment_types ); | |
| 769 | 698 | |
| 770 | - return \array_unique( $types ); | |
| 699 | + return array_unique( $types ); | |
| 771 | 700 | } |
| 772 | 701 | |
| 773 | 702 | /** |
| 774 | 703 | * Excludes likes and reposts from comment queries. |
| @@ -776,131 +705,47 @@ | ||
| 776 | 705 | * @author Jan Boddez |
| 777 | 706 | * |
| 778 | 707 | * @see https://github.com/janboddez/indieblocks/blob/a2d59de358031056a649ee47a1332ce9e39d4ce2/includes/functions.php#L423-L432 |
| 779 | 708 | * |
| 780 | - * @param \WP_Comment_Query $query Comment count. | |
| 709 | + * @param WP_Comment_Query $query Comment count. | |
| 781 | 710 | */ |
| 782 | 711 | public static function comment_query( $query ) { |
| 783 | - if ( ! $query instanceof \WP_Comment_Query ) { | |
| 712 | + if ( ! $query instanceof WP_Comment_Query ) { | |
| 784 | 713 | return; |
| 785 | 714 | } |
| 786 | 715 | |
| 787 | - // Do not exclude likes and reposts on ActivityPub requests. | |
| 788 | - if ( \defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) { | |
| 716 | + if ( is_admin() || ! is_singular() ) { | |
| 789 | 717 | return; |
| 790 | 718 | } |
| 791 | 719 | |
| 792 | - // Do not exclude likes and reposts on REST requests (handled by rest_comment_query). | |
| 793 | - if ( \wp_is_serving_rest_request() ) { | |
| 720 | + if ( ! empty( $query->query_vars['type__in'] ) ) { | |
| 794 | 721 | return; |
| 795 | 722 | } |
| 796 | 723 | |
| 797 | - // Filter post types for admin requests. | |
| 798 | - if ( \is_admin() ) { | |
| 799 | - $query->query_vars['post_type'] = self::get_allowed_comment_post_types(); | |
| 800 | - return; | |
| 801 | - } | |
| 802 | - | |
| 803 | - // Do not exclude likes and reposts on non-singular pages. | |
| 804 | - if ( ! \is_singular() ) { | |
| 805 | - return; | |
| 806 | - } | |
| 807 | - | |
| 808 | - // Do not exclude likes and reposts if the query is for specific types. | |
| 809 | - if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) { | |
| 810 | - return; | |
| 811 | - } | |
| 812 | - | |
| 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 | 724 | // Exclude likes and reposts by the ActivityPub plugin. |
| 819 | 725 | $query->query_vars['type__not_in'] = self::get_comment_type_slugs(); |
| 820 | 726 | } |
| 821 | 727 | |
| 822 | 728 | /** |
| 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 | 729 | * Filter the comment status before it is set. |
| 861 | 730 | * |
| 862 | - * @param int|string|\WP_Error $approved The approved comment status. | |
| 863 | - * @param array $comment_data The comment data. | |
| 731 | + * @param string $approved The approved comment status. | |
| 732 | + * @param array $commentdata The comment data. | |
| 864 | 733 | * |
| 865 | - * @return int|string|\WP_Error The approval status. 1, 0, 'spam', 'trash', or WP_Error. | |
| 734 | + * @return boolean `true` if the comment is approved, `false` otherwise. | |
| 866 | 735 | */ |
| 867 | - 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 | + public static function pre_comment_approved( $approved, $commentdata ) { | |
| 737 | + if ( $approved || \is_wp_error( $approved ) ) { | |
| 874 | 738 | return $approved; |
| 875 | 739 | } |
| 876 | 740 | |
| 877 | - // Maybe auto-approve likes and reposts. | |
| 878 | - if ( | |
| 879 | - \in_array( $comment_data['comment_type'], self::get_comment_type_slugs(), true ) && | |
| 880 | - '1' === \get_option( 'activitypub_auto_approve_reactions' ) | |
| 881 | - ) { | |
| 882 | - return 1; | |
| 883 | - } | |
| 884 | - | |
| 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 | 741 | if ( '1' !== \get_option( 'comment_previously_approved' ) ) { |
| 897 | 742 | return $approved; |
| 898 | 743 | } |
| 899 | 744 | |
| 900 | 745 | if ( |
| 901 | - empty( $comment_data['comment_meta']['protocol'] ) || | |
| 902 | - 'activitypub' !== $comment_data['comment_meta']['protocol'] | |
| 746 | + empty( $commentdata['comment_meta']['protocol'] ) || | |
| 747 | + 'activitypub' !== $commentdata['comment_meta']['protocol'] | |
| 903 | 748 | ) { |
| 904 | 749 | return $approved; |
| 905 | 750 | } |
| 906 | 751 | |
| @@ -905,10 +750,10 @@ | ||
| 905 | 750 | } |
| 906 | 751 | |
| 907 | 752 | global $wpdb; |
| 908 | 753 | |
| 909 | - $author = $comment_data['comment_author']; | |
| 910 | - $author_url = $comment_data['comment_author_url']; | |
| 754 | + $author = $commentdata['comment_author']; | |
| 755 | + $author_url = $commentdata['comment_author_url']; | |
| 911 | 756 | // phpcs:ignore |
| 912 | 757 | $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 ) ); |
| 913 | 758 | |
| 914 | 759 | if ( 1 === (int) $ok_to_comment ) { |
| @@ -918,22 +763,8 @@ | ||
| 918 | 763 | return $approved; |
| 919 | 764 | } |
| 920 | 765 | |
| 921 | 766 | /** |
| 922 | - * Update comment counts when interaction settings are disabled. | |
| 923 | - * | |
| 924 | - * Triggers a recount when likes or reposts are disabled to ensure accurate comment counts. | |
| 925 | - * | |
| 926 | - * @param mixed $old_value The old option value. | |
| 927 | - * @param mixed $value The new option value. | |
| 928 | - */ | |
| 929 | - public static function maybe_update_comment_counts( $old_value, $value ) { | |
| 930 | - if ( '1' === $old_value && '1' !== $value ) { | |
| 931 | - Migration::update_comment_counts(); | |
| 932 | - } | |
| 933 | - } | |
| 934 | - | |
| 935 | - /** | |
| 936 | 767 | * Filters the comment count to exclude ActivityPub comment types. |
| 937 | 768 | * |
| 938 | 769 | * @param int|null $new_count The new comment count. Default null. |
| 939 | 770 | * @param int $old_count The old comment count. |
| @@ -942,118 +773,16 @@ | ||
| 942 | 773 | * @return int|null The updated comment count, or null to use the default query. |
| 943 | 774 | */ |
| 944 | 775 | public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) { |
| 945 | 776 | if ( null === $new_count ) { |
| 946 | - $excluded_types = \array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) ); | |
| 777 | + global $wpdb; | |
| 947 | 778 | |
| 948 | - 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 | - } | |
| 779 | + $excluded_types = self::get_comment_type_slugs(); | |
| 956 | 780 | |
| 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 ) ); | |
| 781 | + // phpcs:ignore WordPress.DB | |
| 782 | + $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 ) ); | |
| 971 | 783 | |
| 972 | - global $wpdb; | |
| 973 | - | |
| 974 | - // 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 ) ); | |
| 976 | - } | |
| 977 | 784 | } |
| 978 | 785 | |
| 979 | 786 | return $new_count; |
| 980 | - } | |
| 981 | - | |
| 982 | - /** | |
| 983 | - * Check if a comment type is enabled. | |
| 984 | - * | |
| 985 | - * @param string $comment_type The comment type. | |
| 986 | - * @return bool True if the comment type is enabled. | |
| 987 | - */ | |
| 988 | - 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 | - * @param string $author The comment author name (already escaped by WordPress). | |
| 1044 | - * | |
| 1045 | - * @return string The comment author name with emoji images unescaped. | |
| 1046 | - */ | |
| 1047 | - public static function unescape_emoji( $author ) { | |
| 1048 | - // Only attempt to unescape if there are emoji images present in the escaped string. | |
| 1049 | - if ( false === \strpos( $author, 'class="emoji"' ) ) { | |
| 1050 | - return $author; | |
| 1051 | - } | |
| 1052 | - | |
| 1053 | - // Decode entities so we can selectively restore emoji <img> tags. | |
| 1054 | - $decoded = \html_entity_decode( $author, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); | |
| 1055 | - | |
| 1056 | - // Use strict KSES validation to only allow valid emoji img tags. | |
| 1057 | - return \wp_kses( $decoded, Emoji::get_kses_allowed_html() ); | |
| 1058 | 787 | } |
| 1059 | 788 | } |