| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-content` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-content` block on the server. |
| 10 |
* |
| 11 |
* @param array $attributes Block attributes. |
| 12 |
* @param string $content Block default content. |
| 13 |
* @param WP_Block $block Block instance. |
| 14 |
* @return string Returns the filtered post content of the current post. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_post_content( $attributes, $content, $block ) { |
| 17 |
static $seen_ids = array(); |
| 18 |
|
| 19 |
if ( ! isset( $block->context['postId'] ) ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
$post_id = $block->context['postId']; |
| 24 |
|
| 25 |
if ( isset( $seen_ids[ $post_id ] ) ) { |
| 26 |
// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 27 |
// is set in `wp_debug_mode()`. |
| 28 |
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && |
| 29 |
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; |
| 30 |
|
| 31 |
return $is_debug ? |
| 32 |
// translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 33 |
__( '[block rendering halted]' ) : |
| 34 |
''; |
| 35 |
} |
| 36 |
|
| 37 |
$seen_ids[ $post_id ] = true; |
| 38 |
|
| 39 |
if ( ! in_the_loop() && have_posts() ) { |
| 40 |
the_post(); |
| 41 |
} |
| 42 |
|
| 43 |
$content = get_the_content( null, false, $post_id ); |
| 44 |
/** This filter is documented in wp-includes/post-template.php */ |
| 45 |
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); |
| 46 |
unset( $seen_ids[ $post_id ] ); |
| 47 |
|
| 48 |
if ( empty( $content ) ) { |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); |
| 53 |
|
| 54 |
return ( |
| 55 |
'<div ' . $wrapper_attributes . '>' . |
| 56 |
$content . |
| 57 |
'</div>' |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Registers the `core/post-content` block on the server. |
| 63 |
*/ |
| 64 |
function gutenberg_register_block_core_post_content() { |
| 65 |
register_block_type_from_metadata( |
| 66 |
__DIR__ . '/post-content', |
| 67 |
array( |
| 68 |
'render_callback' => 'gutenberg_render_block_core_post_content', |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
add_action( 'init', 'gutenberg_register_block_core_post_content', 20 ); |
| 73 |
|