| 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 |
* @since 5.8.0 |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block default content. |
| 15 |
* @param WP_Block $block Block instance. |
| 16 |
* @return string Returns the filtered post content of the current post. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_post_content( $attributes, $content, $block ) { |
| 19 |
static $seen_ids = array(); |
| 20 |
|
| 21 |
if ( ! isset( $block->context['postId'] ) ) { |
| 22 |
return ''; |
| 23 |
} |
| 24 |
|
| 25 |
$post_id = $block->context['postId']; |
| 26 |
|
| 27 |
if ( isset( $seen_ids[ $post_id ] ) ) { |
| 28 |
// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 29 |
// is set in `wp_debug_mode()`. |
| 30 |
$is_debug = WP_DEBUG && WP_DEBUG_DISPLAY; |
| 31 |
|
| 32 |
return $is_debug ? |
| 33 |
// translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 34 |
__( '[block rendering halted]' ) : |
| 35 |
''; |
| 36 |
} |
| 37 |
|
| 38 |
$seen_ids[ $post_id ] = true; |
| 39 |
|
| 40 |
// When inside the main loop, we want to use queried object |
| 41 |
// so that `the_preview` for the current post can apply. |
| 42 |
// We force this behavior by omitting the third argument (post ID) from the `get_the_content`. |
| 43 |
$content = get_the_content(); |
| 44 |
// Check for nextpage to display page links for paginated posts. |
| 45 |
if ( has_block( 'core/nextpage' ) ) { |
| 46 |
$content .= wp_link_pages( array( 'echo' => 0 ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** This filter is documented in wp-includes/post-template.php */ |
| 50 |
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); |
| 51 |
unset( $seen_ids[ $post_id ] ); |
| 52 |
|
| 53 |
if ( empty( $content ) ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
$tag_name = 'div'; |
| 58 |
|
| 59 |
if ( isset( $attributes['tagName'] ) && is_string( $attributes['tagName'] ) ) { |
| 60 |
/** |
| 61 |
* The allowed tag names match the options offered in the editor. |
| 62 |
* |
| 63 |
* @see packages/block-library/src/post-content/edit.js |
| 64 |
*/ |
| 65 |
$allowed_tag_names = array( 'div', 'main', 'section', 'article' ); |
| 66 |
$normalized_tag_name = strtolower( $attributes['tagName'] ); |
| 67 |
|
| 68 |
if ( in_array( $normalized_tag_name, $allowed_tag_names, true ) ) { |
| 69 |
$tag_name = $normalized_tag_name; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); |
| 74 |
|
| 75 |
return sprintf( |
| 76 |
'<%1$s %2$s>%3$s</%1$s>', |
| 77 |
$tag_name, |
| 78 |
$wrapper_attributes, |
| 79 |
$content |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Registers the `core/post-content` block on the server. |
| 85 |
* |
| 86 |
* @since 5.8.0 |
| 87 |
*/ |
| 88 |
function gutenberg_register_block_core_post_content() { |
| 89 |
register_block_type_from_metadata( |
| 90 |
__DIR__ . '/post-content', |
| 91 |
array( |
| 92 |
'render_callback' => 'gutenberg_render_block_core_post_content', |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
add_action( 'init', 'gutenberg_register_block_core_post_content', 20 ); |
| 97 |
|