post-content.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-content` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-content` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. * @return string Returns the filtered post content of the current post.
  14. */
  15. function render_block_core_post_content( $attributes, $content, $block ) {
  16. static $seen_ids = array();
  17. if ( ! isset( $block->context['postId'] ) ) {
  18. return '';
  19. }
  20. $post_id = $block->context['postId'];
  21. if ( isset( $seen_ids[ $post_id ] ) ) {
  22. // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
  23. // is set in `wp_debug_mode()`.
  24. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
  25. defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
  26. return $is_debug ?
  27. // translators: Visible only in the front end, this warning takes the place of a faulty block.
  28. __( '[block rendering halted]' ) :
  29. '';
  30. }
  31. $seen_ids[ $post_id ] = true;
  32. // Check is needed for backward compatibility with third-party plugins
  33. // that might rely on the `in_the_loop` check; calling `the_post` sets it to true.
  34. if ( ! in_the_loop() && have_posts() ) {
  35. the_post();
  36. }
  37. // When inside the main loop, we want to use queried object
  38. // so that `the_preview` for the current post can apply.
  39. // We force this behavior by omitting the third argument (post ID) from the `get_the_content`.
  40. $content = get_the_content();
  41. // Check for nextpage to display page links for paginated posts.
  42. if ( has_block( 'core/nextpage' ) ) {
  43. $content .= wp_link_pages( array( 'echo' => 0 ) );
  44. }
  45. /** This filter is documented in wp-includes/post-template.php */
  46. $content = apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );
  47. unset( $seen_ids[ $post_id ] );
  48. if ( empty( $content ) ) {
  49. return '';
  50. }
  51. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) );
  52. return (
  53. '<div ' . $wrapper_attributes . '>' .
  54. $content .
  55. '</div>'
  56. );
  57. }
  58. /**
  59. * Registers the `core/post-content` block on the server.
  60. */
  61. function register_block_core_post_content() {
  62. register_block_type_from_metadata(
  63. __DIR__ . '/post-content',
  64. array(
  65. 'render_callback' => 'render_block_core_post_content',
  66. )
  67. );
  68. }
  69. add_action( 'init', 'register_block_core_post_content' );