comment_ID ) ) { $text = '

' . esc_html__( 'Official response', 'feature-request-system' ) . '

' . $text; } $link = self::link_for( $comment ); return '' === $link || self::has_reply_line( $comment ) ? $text : $text . '

' . $link . '

'; } /** * Puts the moderators' link next to a comment's Reply link, on the same line. * * Classic themes and the Comments block both build the Reply link through this filter. * * @param mixed $html HTML of the Reply link, with what the theme puts around it. * @param mixed $args Arguments of the Reply link. "after" holds the markup that closes it. * @param mixed $comment Comment. * @return mixed */ public static function add_to_reply_line( $html, $args = [], $comment = null ) { if ( ! is_string( $html ) || ! $comment instanceof WP_Comment || ! self::has_reply_line( $comment ) ) { return $html; } $link = self::link_for( $comment ); if ( '' === $link ) { return $html; } $link = ' ' . $link . ''; $after = is_array( $args ) && isset( $args['after'] ) ? (string) $args['after'] : ''; // Inside what the theme wraps around the Reply link, so the two stay on one line. return '' !== $after && substr( $html, -strlen( $after ) ) === $after ? substr( $html, 0, -strlen( $after ) ) . $link . $after : $html . $link; } /** * Returns a comment's formatted text without the label and the link, for the pinned * response, which has its own. * * @param WP_Comment $comment Comment. */ public static function text_without_label( WP_Comment $comment ): string { remove_filter( 'comment_text', [ self::class, 'add_label' ], self::LABEL_PRIORITY ); $text = (string) apply_filters( 'comment_text', get_comment_text( $comment ), $comment, [] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress's own filter, applied so the text is formatted like in the thread. add_filter( 'comment_text', [ self::class, 'add_label' ], self::LABEL_PRIORITY, 2 ); return $text; } /** * Returns the link that changes a comment's mark, for moderators on a request's page. * Elsewhere it returns '': the Comments screen has its own action, and the REST API shows text only. * * @param WP_Comment $comment Comment. */ private static function link_for( WP_Comment $comment ): string { return is_singular( RequestPostType::NAME ) ? OfficialResponseLink::for_comment( $comment, get_comment_link( $comment ) ) : ''; } /** * Whether WordPress shows a Reply link for a comment: threaded comments are on, the * request still takes comments, and the comment is not at the deepest level. * * @param WP_Comment $comment Comment. */ private static function has_reply_line( WP_Comment $comment ): bool { if ( ! get_option( 'thread_comments' ) || ! comments_open( (int) $comment->comment_post_ID ) ) { return false; } $depth = 1; $parent = (int) $comment->comment_parent; while ( $parent ) { $ancestor = get_comment( $parent ); $parent = $ancestor ? (int) $ancestor->comment_parent : 0; ++$depth; } return $depth < (int) get_option( 'thread_comments_depth' ); } }