[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-template` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Determines whether a block list contains a block that uses the featured image.
  10   *
  11   * @since 6.0.0
  12   *
  13   * @param WP_Block_List $inner_blocks Inner block instance.
  14   *
  15   * @return bool Whether the block list contains a block that uses the featured image.
  16   */
  17  function block_core_post_template_uses_featured_image( $inner_blocks ) {
  18      foreach ( $inner_blocks as $block ) {
  19          if ( 'core/post-featured-image' === $block->name ) {
  20              return true;
  21          }
  22          if (
  23              'core/cover' === $block->name &&
  24              ! empty( $block->attributes['useFeaturedImage'] )
  25          ) {
  26              return true;
  27          }
  28          if ( $block->inner_blocks && block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
  29              return true;
  30          }
  31      }
  32  
  33      return false;
  34  }
  35  
  36  /**
  37   * Renders the `core/post-template` block on the server.
  38   *
  39   * @since 6.3.0 Changed render_block_context priority to `1`.
  40   *
  41   * @global WP_Query $wp_query WordPress Query object.
  42   *
  43   * @param array    $attributes Block attributes.
  44   * @param string   $content    Block default content.
  45   * @param WP_Block $block      Block instance.
  46   *
  47   * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
  48   */
  49  function render_block_core_post_template( $attributes, $content, $block ) {
  50      $page_key            = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  51      $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
  52      $page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  53  
  54      // Use global query if needed.
  55      $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  56      if ( $use_global_query ) {
  57          global $wp_query;
  58  
  59          /*
  60           * If already in the main query loop, duplicate the query instance to not tamper with the main instance.
  61           * Since this is a nested query, it should start at the beginning, therefore rewind posts.
  62           * Otherwise, the main query loop has not started yet and this block is responsible for doing so.
  63           */
  64          if ( in_the_loop() ) {
  65              $query = clone $wp_query;
  66              $query->rewind_posts();
  67          } else {
  68              $query = $wp_query;
  69          }
  70      } else {
  71          $query_args = build_query_vars_from_query_block( $block, $page );
  72          $query      = new WP_Query( $query_args );
  73      }
  74  
  75      if ( ! $query->have_posts() ) {
  76          return '';
  77      }
  78  
  79      if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
  80          update_post_thumbnail_cache( $query );
  81      }
  82  
  83      $classnames = '';
  84      if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
  85          if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
  86              $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
  87          }
  88      }
  89      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
  90          $classnames .= ' has-link-color';
  91      }
  92  
  93      // Ensure backwards compatibility by flagging the number of columns via classname when using grid layout.
  94      if ( isset( $attributes['layout']['type'] ) && 'grid' === $attributes['layout']['type'] && ! empty( $attributes['layout']['columnCount'] ) ) {
  95          $classnames .= ' ' . sanitize_title( 'columns-' . $attributes['layout']['columnCount'] );
  96      }
  97  
  98      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) );
  99  
 100      $content = '';
 101      while ( $query->have_posts() ) {
 102          $query->the_post();
 103  
 104          // Get an instance of the current Post Template block.
 105          $block_instance = $block->parsed_block;
 106  
 107          // Set the block name to one that does not correspond to an existing registered block.
 108          // This ensures that for the inner instances of the Post Template block, we do not render any block supports.
 109          $block_instance['blockName'] = 'core/null';
 110  
 111          $post_id              = get_the_ID();
 112          $post_type            = get_post_type();
 113          $filter_block_context = static function ( $context ) use ( $post_id, $post_type ) {
 114              $context['postType'] = $post_type;
 115              $context['postId']   = $post_id;
 116              return $context;
 117          };
 118  
 119          // Use an early priority to so that other 'render_block_context' filters have access to the values.
 120          add_filter( 'render_block_context', $filter_block_context, 1 );
 121          // Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
 122          // `render_callback` and ensure that no wrapper markup is included.
 123          $block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) );
 124          remove_filter( 'render_block_context', $filter_block_context, 1 );
 125  
 126          // Wrap the render inner blocks in a `li` element with the appropriate post classes.
 127          $post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
 128  
 129          $inner_block_directives = $enhanced_pagination ? ' data-wp-key="post-template-item-' . $post_id . '"' : '';
 130  
 131          $content .= '<li' . $inner_block_directives . ' class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
 132      }
 133  
 134      /*
 135       * Use this function to restore the context of the template tags
 136       * from a secondary query loop back to the main query loop.
 137       * Since we use two custom loops, it's safest to always restore.
 138      */
 139      wp_reset_postdata();
 140  
 141      return sprintf(
 142          '<ul %1$s>%2$s</ul>',
 143          $wrapper_attributes,
 144          $content
 145      );
 146  }
 147  
 148  /**
 149   * Registers the `core/post-template` block on the server.
 150   *
 151   * @since 5.8.0
 152   */
 153  function register_block_core_post_template() {
 154      register_block_type_from_metadata(
 155          __DIR__ . '/post-template',
 156          array(
 157              'render_callback'   => 'render_block_core_post_template',
 158              'skip_inner_blocks' => true,
 159          )
 160      );
 161  }
 162  add_action( 'init', 'register_block_core_post_template' );


Generated : Sun Dec 22 08:20:01 2024 Cross-referenced by PHPXref