Edit File by line
/home/barbar84/www/wp-inclu.../blocks
File: latest-comments.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/latest-comments` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Get the post title.
[8] Fix | Delete
*
[9] Fix | Delete
* The post title is fetched and if it is blank then a default string is
[10] Fix | Delete
* returned.
[11] Fix | Delete
*
[12] Fix | Delete
* Copied from `wp-admin/includes/template.php`, but we can't include that
[13] Fix | Delete
* file because:
[14] Fix | Delete
*
[15] Fix | Delete
* 1. It causes bugs with test fixture generation and strange Docker 255 error
[16] Fix | Delete
* codes.
[17] Fix | Delete
* 2. It's in the admin; ideally we *shouldn't* be including files from the
[18] Fix | Delete
* admin for a block's output. It's a very small/simple function as well,
[19] Fix | Delete
* so duplicating it isn't too terrible.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 3.3.0
[22] Fix | Delete
*
[23] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
[24] Fix | Delete
* @return string The post title if set; "(no title)" if no title is set.
[25] Fix | Delete
*/
[26] Fix | Delete
function wp_latest_comments_draft_or_post_title( $post = 0 ) {
[27] Fix | Delete
$title = get_the_title( $post );
[28] Fix | Delete
if ( empty( $title ) ) {
[29] Fix | Delete
$title = __( '(no title)' );
[30] Fix | Delete
}
[31] Fix | Delete
return esc_html( $title );
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Renders the `core/latest-comments` block on server.
[36] Fix | Delete
*
[37] Fix | Delete
* @param array $attributes The block attributes.
[38] Fix | Delete
*
[39] Fix | Delete
* @return string Returns the post content with latest comments added.
[40] Fix | Delete
*/
[41] Fix | Delete
function render_block_core_latest_comments( $attributes = array() ) {
[42] Fix | Delete
$comments = get_comments(
[43] Fix | Delete
// This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php.
[44] Fix | Delete
apply_filters(
[45] Fix | Delete
'widget_comments_args',
[46] Fix | Delete
array(
[47] Fix | Delete
'number' => $attributes['commentsToShow'],
[48] Fix | Delete
'status' => 'approve',
[49] Fix | Delete
'post_status' => 'publish',
[50] Fix | Delete
)
[51] Fix | Delete
)
[52] Fix | Delete
);
[53] Fix | Delete
[54] Fix | Delete
$list_items_markup = '';
[55] Fix | Delete
if ( ! empty( $comments ) ) {
[56] Fix | Delete
// Prime the cache for associated posts. This is copied from \WP_Widget_Recent_Comments::widget().
[57] Fix | Delete
$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
[58] Fix | Delete
_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
[59] Fix | Delete
[60] Fix | Delete
foreach ( $comments as $comment ) {
[61] Fix | Delete
$list_items_markup .= '<li class="wp-block-latest-comments__comment">';
[62] Fix | Delete
if ( $attributes['displayAvatar'] ) {
[63] Fix | Delete
$avatar = get_avatar(
[64] Fix | Delete
$comment,
[65] Fix | Delete
48,
[66] Fix | Delete
'',
[67] Fix | Delete
'',
[68] Fix | Delete
array(
[69] Fix | Delete
'class' => 'wp-block-latest-comments__comment-avatar',
[70] Fix | Delete
)
[71] Fix | Delete
);
[72] Fix | Delete
if ( $avatar ) {
[73] Fix | Delete
$list_items_markup .= $avatar;
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$list_items_markup .= '<article>';
[78] Fix | Delete
$list_items_markup .= '<footer class="wp-block-latest-comments__comment-meta">';
[79] Fix | Delete
$author_url = get_comment_author_url( $comment );
[80] Fix | Delete
if ( empty( $author_url ) && ! empty( $comment->user_id ) ) {
[81] Fix | Delete
$author_url = get_author_posts_url( $comment->user_id );
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
$author_markup = '';
[85] Fix | Delete
if ( $author_url ) {
[86] Fix | Delete
$author_markup .= '<a class="wp-block-latest-comments__comment-author" href="' . esc_url( $author_url ) . '">' . get_comment_author( $comment ) . '</a>';
[87] Fix | Delete
} else {
[88] Fix | Delete
$author_markup .= '<span class="wp-block-latest-comments__comment-author">' . get_comment_author( $comment ) . '</span>';
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
// `_draft_or_post_title` calls `esc_html()` so we don't need to wrap that call in
[92] Fix | Delete
// `esc_html`.
[93] Fix | Delete
$post_title = '<a class="wp-block-latest-comments__comment-link" href="' . esc_url( get_comment_link( $comment ) ) . '">' . wp_latest_comments_draft_or_post_title( $comment->comment_post_ID ) . '</a>';
[94] Fix | Delete
[95] Fix | Delete
$list_items_markup .= sprintf(
[96] Fix | Delete
/* translators: 1: author name (inside <a> or <span> tag, based on if they have a URL), 2: post title related to this comment */
[97] Fix | Delete
__( '%1$s on %2$s' ),
[98] Fix | Delete
$author_markup,
[99] Fix | Delete
$post_title
[100] Fix | Delete
);
[101] Fix | Delete
[102] Fix | Delete
if ( $attributes['displayDate'] ) {
[103] Fix | Delete
$list_items_markup .= sprintf(
[104] Fix | Delete
'<time datetime="%1$s" class="wp-block-latest-comments__comment-date">%2$s</time>',
[105] Fix | Delete
esc_attr( get_comment_date( 'c', $comment ) ),
[106] Fix | Delete
date_i18n( get_option( 'date_format' ), get_comment_date( 'U', $comment ) )
[107] Fix | Delete
);
[108] Fix | Delete
}
[109] Fix | Delete
$list_items_markup .= '</footer>';
[110] Fix | Delete
if ( $attributes['displayExcerpt'] ) {
[111] Fix | Delete
$list_items_markup .= '<div class="wp-block-latest-comments__comment-excerpt">' . wpautop( get_comment_excerpt( $comment ) ) . '</div>';
[112] Fix | Delete
}
[113] Fix | Delete
$list_items_markup .= '</article></li>';
[114] Fix | Delete
}
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
$classnames = array();
[118] Fix | Delete
if ( $attributes['displayAvatar'] ) {
[119] Fix | Delete
$classnames[] = 'has-avatars';
[120] Fix | Delete
}
[121] Fix | Delete
if ( $attributes['displayDate'] ) {
[122] Fix | Delete
$classnames[] = 'has-dates';
[123] Fix | Delete
}
[124] Fix | Delete
if ( $attributes['displayExcerpt'] ) {
[125] Fix | Delete
$classnames[] = 'has-excerpts';
[126] Fix | Delete
}
[127] Fix | Delete
if ( empty( $comments ) ) {
[128] Fix | Delete
$classnames[] = 'no-comments';
[129] Fix | Delete
}
[130] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
[131] Fix | Delete
[132] Fix | Delete
return ! empty( $comments ) ? sprintf(
[133] Fix | Delete
'<ol %1$s>%2$s</ol>',
[134] Fix | Delete
$wrapper_attributes,
[135] Fix | Delete
$list_items_markup
[136] Fix | Delete
) : sprintf(
[137] Fix | Delete
'<div %1$s>%2$s</div>',
[138] Fix | Delete
$wrapper_attributes,
[139] Fix | Delete
__( 'No comments to show.' )
[140] Fix | Delete
);
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Registers the `core/latest-comments` block.
[145] Fix | Delete
*/
[146] Fix | Delete
function register_block_core_latest_comments() {
[147] Fix | Delete
register_block_type_from_metadata(
[148] Fix | Delete
__DIR__ . '/latest-comments',
[149] Fix | Delete
array(
[150] Fix | Delete
'render_callback' => 'render_block_core_latest_comments',
[151] Fix | Delete
)
[152] Fix | Delete
);
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
add_action( 'init', 'register_block_core_latest_comments' );
[156] Fix | Delete
[157] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function