Edit File by line
/home/zeestwma/richards.../wp-inclu.../blocks
File: comments.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/comments` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Renders the `core/comments` block on the server.
[8] Fix | Delete
*
[9] Fix | Delete
* This render callback is mainly for rendering a dynamic, legacy version of
[10] Fix | Delete
* this block (the old `core/post-comments`). It uses the `comments_template()`
[11] Fix | Delete
* function to generate the output, in the same way as classic PHP themes.
[12] Fix | Delete
*
[13] Fix | Delete
* As this callback will always run during SSR, first we need to check whether
[14] Fix | Delete
* the block is in legacy mode. If not, the HTML generated in the editor is
[15] Fix | Delete
* returned instead.
[16] Fix | Delete
*
[17] Fix | Delete
* @since 6.1.0
[18] Fix | Delete
*
[19] Fix | Delete
* @global WP_Post $post Global post object.
[20] Fix | Delete
*
[21] Fix | Delete
* @param array $attributes Block attributes.
[22] Fix | Delete
* @param string $content Block default content.
[23] Fix | Delete
* @param WP_Block $block Block instance.
[24] Fix | Delete
* @return string Returns the filtered post comments for the current post wrapped inside "p" tags.
[25] Fix | Delete
*/
[26] Fix | Delete
function render_block_core_comments( $attributes, $content, $block ) {
[27] Fix | Delete
global $post;
[28] Fix | Delete
[29] Fix | Delete
$post_id = $block->context['postId'];
[30] Fix | Delete
if ( ! isset( $post_id ) ) {
[31] Fix | Delete
return '';
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
// Return early if there are no comments and comments are closed.
[35] Fix | Delete
if ( ! comments_open( $post_id ) && (int) get_comments_number( $post_id ) === 0 ) {
[36] Fix | Delete
return '';
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
// If this isn't the legacy block, we need to render the static version of this block.
[40] Fix | Delete
$is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] );
[41] Fix | Delete
if ( ! $is_legacy ) {
[42] Fix | Delete
return $block->render( array( 'dynamic' => false ) );
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
$post_before = $post;
[46] Fix | Delete
$post = get_post( $post_id );
[47] Fix | Delete
setup_postdata( $post );
[48] Fix | Delete
[49] Fix | Delete
ob_start();
[50] Fix | Delete
[51] Fix | Delete
/*
[52] Fix | Delete
* There's a deprecation warning generated by WP Core.
[53] Fix | Delete
* Ideally this deprecation is removed from Core.
[54] Fix | Delete
* In the meantime, this removes it from the output.
[55] Fix | Delete
*/
[56] Fix | Delete
add_filter( 'deprecated_file_trigger_error', '__return_false' );
[57] Fix | Delete
comments_template();
[58] Fix | Delete
remove_filter( 'deprecated_file_trigger_error', '__return_false' );
[59] Fix | Delete
[60] Fix | Delete
$output = ob_get_clean();
[61] Fix | Delete
$post = $post_before;
[62] Fix | Delete
[63] Fix | Delete
$classnames = array();
[64] Fix | Delete
// Adds the old class name for styles' backwards compatibility.
[65] Fix | Delete
if ( isset( $attributes['legacy'] ) ) {
[66] Fix | Delete
$classnames[] = 'wp-block-post-comments';
[67] Fix | Delete
}
[68] Fix | Delete
if ( isset( $attributes['textAlign'] ) ) {
[69] Fix | Delete
$classnames[] = 'has-text-align-' . $attributes['textAlign'];
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes(
[73] Fix | Delete
array( 'class' => implode( ' ', $classnames ) )
[74] Fix | Delete
);
[75] Fix | Delete
[76] Fix | Delete
/*
[77] Fix | Delete
* Enqueues scripts and styles required only for the legacy version. That is
[78] Fix | Delete
* why they are not defined in `block.json`.
[79] Fix | Delete
*/
[80] Fix | Delete
wp_enqueue_script( 'comment-reply' );
[81] Fix | Delete
enqueue_legacy_post_comments_block_styles( $block->name );
[82] Fix | Delete
[83] Fix | Delete
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Registers the `core/comments` block on the server.
[88] Fix | Delete
*
[89] Fix | Delete
* @since 6.1.0
[90] Fix | Delete
*/
[91] Fix | Delete
function register_block_core_comments() {
[92] Fix | Delete
register_block_type_from_metadata(
[93] Fix | Delete
__DIR__ . '/comments',
[94] Fix | Delete
array(
[95] Fix | Delete
'render_callback' => 'render_block_core_comments',
[96] Fix | Delete
'skip_inner_blocks' => true,
[97] Fix | Delete
)
[98] Fix | Delete
);
[99] Fix | Delete
}
[100] Fix | Delete
add_action( 'init', 'register_block_core_comments' );
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Use the button block classes for the form-submit button.
[104] Fix | Delete
*
[105] Fix | Delete
* @since 6.1.0
[106] Fix | Delete
*
[107] Fix | Delete
* @param array $fields The default comment form arguments.
[108] Fix | Delete
*
[109] Fix | Delete
* @return array Returns the modified fields.
[110] Fix | Delete
*/
[111] Fix | Delete
function comments_block_form_defaults( $fields ) {
[112] Fix | Delete
if ( wp_is_block_theme() ) {
[113] Fix | Delete
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link ' . wp_theme_get_element_class_name( 'button' ) . '" value="%4$s" />';
[114] Fix | Delete
$fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
return $fields;
[118] Fix | Delete
}
[119] Fix | Delete
add_filter( 'comment_form_defaults', 'comments_block_form_defaults' );
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* Enqueues styles from the legacy `core/post-comments` block. These styles are
[123] Fix | Delete
* required only by the block's fallback.
[124] Fix | Delete
*
[125] Fix | Delete
* @since 6.1.0
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $block_name Name of the new block type.
[128] Fix | Delete
*/
[129] Fix | Delete
function enqueue_legacy_post_comments_block_styles( $block_name ) {
[130] Fix | Delete
static $are_styles_enqueued = false;
[131] Fix | Delete
[132] Fix | Delete
if ( ! $are_styles_enqueued ) {
[133] Fix | Delete
$handles = array(
[134] Fix | Delete
'wp-block-post-comments',
[135] Fix | Delete
'wp-block-buttons',
[136] Fix | Delete
'wp-block-button',
[137] Fix | Delete
);
[138] Fix | Delete
foreach ( $handles as $handle ) {
[139] Fix | Delete
wp_enqueue_block_style( $block_name, array( 'handle' => $handle ) );
[140] Fix | Delete
}
[141] Fix | Delete
$are_styles_enqueued = true;
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Ensures backwards compatibility for any users running the Gutenberg plugin
[147] Fix | Delete
* who have used Post Comments before it was merged into Comments Query Loop.
[148] Fix | Delete
*
[149] Fix | Delete
* The same approach was followed when core/query-loop was renamed to
[150] Fix | Delete
* core/post-template.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 6.1.0
[153] Fix | Delete
*
[154] Fix | Delete
* @see https://github.com/WordPress/gutenberg/pull/41807
[155] Fix | Delete
* @see https://github.com/WordPress/gutenberg/pull/32514
[156] Fix | Delete
*/
[157] Fix | Delete
function register_legacy_post_comments_block() {
[158] Fix | Delete
$registry = WP_Block_Type_Registry::get_instance();
[159] Fix | Delete
[160] Fix | Delete
/*
[161] Fix | Delete
* Remove the old `post-comments` block if it was already registered, as it
[162] Fix | Delete
* is about to be replaced by the type defined below.
[163] Fix | Delete
*/
[164] Fix | Delete
if ( $registry->is_registered( 'core/post-comments' ) ) {
[165] Fix | Delete
unregister_block_type( 'core/post-comments' );
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
// Recreate the legacy block metadata.
[169] Fix | Delete
$metadata = array(
[170] Fix | Delete
'name' => 'core/post-comments',
[171] Fix | Delete
'category' => 'theme',
[172] Fix | Delete
'attributes' => array(
[173] Fix | Delete
'textAlign' => array(
[174] Fix | Delete
'type' => 'string',
[175] Fix | Delete
),
[176] Fix | Delete
),
[177] Fix | Delete
'uses_context' => array(
[178] Fix | Delete
'postId',
[179] Fix | Delete
'postType',
[180] Fix | Delete
),
[181] Fix | Delete
'supports' => array(
[182] Fix | Delete
'html' => false,
[183] Fix | Delete
'align' => array( 'wide', 'full' ),
[184] Fix | Delete
'typography' => array(
[185] Fix | Delete
'fontSize' => true,
[186] Fix | Delete
'lineHeight' => true,
[187] Fix | Delete
'__experimentalFontStyle' => true,
[188] Fix | Delete
'__experimentalFontWeight' => true,
[189] Fix | Delete
'__experimentalLetterSpacing' => true,
[190] Fix | Delete
'__experimentalTextTransform' => true,
[191] Fix | Delete
'__experimentalDefaultControls' => array(
[192] Fix | Delete
'fontSize' => true,
[193] Fix | Delete
),
[194] Fix | Delete
),
[195] Fix | Delete
'color' => array(
[196] Fix | Delete
'gradients' => true,
[197] Fix | Delete
'link' => true,
[198] Fix | Delete
'__experimentalDefaultControls' => array(
[199] Fix | Delete
'background' => true,
[200] Fix | Delete
'text' => true,
[201] Fix | Delete
),
[202] Fix | Delete
),
[203] Fix | Delete
'inserter' => false,
[204] Fix | Delete
),
[205] Fix | Delete
'style' => array(
[206] Fix | Delete
'wp-block-post-comments',
[207] Fix | Delete
'wp-block-buttons',
[208] Fix | Delete
'wp-block-button',
[209] Fix | Delete
),
[210] Fix | Delete
'render_callback' => 'render_block_core_comments',
[211] Fix | Delete
'skip_inner_blocks' => true,
[212] Fix | Delete
);
[213] Fix | Delete
[214] Fix | Delete
/*
[215] Fix | Delete
* Filters the metadata object, the same way it's done inside
[216] Fix | Delete
* `register_block_type_from_metadata()`. This applies some default filters,
[217] Fix | Delete
* like `_wp_multiple_block_styles`, which is required in this case because
[218] Fix | Delete
* the block has multiple styles.
[219] Fix | Delete
*/
[220] Fix | Delete
/** This filter is documented in wp-includes/blocks.php */
[221] Fix | Delete
$metadata = apply_filters( 'block_type_metadata', $metadata );
[222] Fix | Delete
[223] Fix | Delete
register_block_type( 'core/post-comments', $metadata );
[224] Fix | Delete
}
[225] Fix | Delete
add_action( 'init', 'register_legacy_post_comments_block', 21 );
[226] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function