2021-05-19 11:09:27 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Server-side rendering of the `core/post-content` block.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the `core/post-content` block on the server.
|
|
|
|
*
|
|
|
|
* @param array $attributes Block attributes.
|
|
|
|
* @param string $content Block default content.
|
|
|
|
* @param WP_Block $block Block instance.
|
|
|
|
* @return string Returns the filtered post content of the current post.
|
|
|
|
*/
|
|
|
|
function render_block_core_post_content( $attributes, $content, $block ) {
|
|
|
|
static $seen_ids = array();
|
|
|
|
|
|
|
|
if ( ! isset( $block->context['postId'] ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$post_id = $block->context['postId'];
|
|
|
|
|
|
|
|
if ( isset( $seen_ids[ $post_id ] ) ) {
|
2021-07-06 10:38:40 -04:00
|
|
|
// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
|
|
|
|
// is set in `wp_debug_mode()`.
|
2023-02-07 02:04:52 -05:00
|
|
|
$is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
|
2021-07-06 10:38:40 -04:00
|
|
|
|
2021-05-19 11:09:27 -04:00
|
|
|
return $is_debug ?
|
|
|
|
// translators: Visible only in the front end, this warning takes the place of a faulty block.
|
|
|
|
__( '[block rendering halted]' ) :
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
|
|
|
|
$seen_ids[ $post_id ] = true;
|
|
|
|
|
2022-01-04 00:39:28 -05:00
|
|
|
// Check is needed for backward compatibility with third-party plugins
|
|
|
|
// that might rely on the `in_the_loop` check; calling `the_post` sets it to true.
|
2021-07-06 10:38:40 -04:00
|
|
|
if ( ! in_the_loop() && have_posts() ) {
|
2021-05-19 11:09:27 -04:00
|
|
|
the_post();
|
|
|
|
}
|
|
|
|
|
2022-01-04 00:39:28 -05:00
|
|
|
// When inside the main loop, we want to use queried object
|
|
|
|
// so that `the_preview` for the current post can apply.
|
|
|
|
// We force this behavior by omitting the third argument (post ID) from the `get_the_content`.
|
2022-04-12 11:12:47 -04:00
|
|
|
$content = get_the_content();
|
2022-01-10 22:13:33 -05:00
|
|
|
// Check for nextpage to display page links for paginated posts.
|
|
|
|
if ( has_block( 'core/nextpage' ) ) {
|
|
|
|
$content .= wp_link_pages( array( 'echo' => 0 ) );
|
|
|
|
}
|
|
|
|
|
2021-05-21 06:14:23 -04:00
|
|
|
/** This filter is documented in wp-includes/post-template.php */
|
|
|
|
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) );
|
|
|
|
unset( $seen_ids[ $post_id ] );
|
2021-05-19 11:09:27 -04:00
|
|
|
|
|
|
|
if ( empty( $content ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) );
|
|
|
|
|
|
|
|
return (
|
|
|
|
'<div ' . $wrapper_attributes . '>' .
|
|
|
|
$content .
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `core/post-content` block on the server.
|
|
|
|
*/
|
|
|
|
function register_block_core_post_content() {
|
|
|
|
register_block_type_from_metadata(
|
|
|
|
__DIR__ . '/post-content',
|
|
|
|
array(
|
|
|
|
'render_callback' => 'render_block_core_post_content',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
add_action( 'init', 'register_block_core_post_content' );
|