[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> post-content.php (source)

   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 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      // Check is needed for backward compatibility with third-party plugins
  40      // that might rely on the `in_the_loop` check; calling `the_post` sets it to true.
  41      if ( ! in_the_loop() && have_posts() ) {
  42          the_post();
  43      }
  44  
  45      // When inside the main loop, we want to use queried object
  46      // so that `the_preview` for the current post can apply.
  47      // We force this behavior by omitting the third argument (post ID) from the `get_the_content`.
  48      $content = get_the_content();
  49      // Check for nextpage to display page links for paginated posts.
  50      if ( has_block( 'core/nextpage' ) ) {
  51          $content .= wp_link_pages( array( 'echo' => 0 ) );
  52      }
  53  
  54      /** This filter is documented in wp-includes/post-template.php */
  55      $content = apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );
  56      unset( $seen_ids[ $post_id ] );
  57  
  58      if ( empty( $content ) ) {
  59          return '';
  60      }
  61  
  62      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) );
  63  
  64      return (
  65          '<div ' . $wrapper_attributes . '>' .
  66              $content .
  67          '</div>'
  68      );
  69  }
  70  
  71  /**
  72   * Registers the `core/post-content` block on the server.
  73   */
  74  function register_block_core_post_content() {
  75      register_block_type_from_metadata(
  76          __DIR__ . '/post-content',
  77          array(
  78              'render_callback' => 'render_block_core_post_content',
  79          )
  80      );
  81  }
  82  add_action( 'init', 'register_block_core_post_content' );


Generated: Fri Dec 6 01:00:02 2024 Cross-referenced by PHPXref 0.7.1