[ Index ]

PHP Cross Reference of WordPress

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   * Renders the `core/post-template` 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   *
  15   * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
  16   */
  17  function render_block_core_post_template( $attributes, $content, $block ) {
  18      $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  19      $page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  20  
  21      $query_args = build_query_vars_from_query_block( $block, $page );
  22      // Override the custom query with the global query if needed.
  23      $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  24      if ( $use_global_query ) {
  25          global $wp_query;
  26          if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
  27              // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
  28              unset( $query_args['offset'] );
  29              $query_args = wp_parse_args( $wp_query->query_vars, $query_args );
  30  
  31              if ( empty( $query_args['post_type'] ) && is_singular() ) {
  32                  $query_args['post_type'] = get_post_type( get_the_ID() );
  33              }
  34          }
  35      }
  36  
  37      $query = new WP_Query( $query_args );
  38  
  39      if ( ! $query->have_posts() ) {
  40          return '';
  41      }
  42  
  43      $classnames = '';
  44      if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
  45          if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
  46              $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
  47          }
  48      }
  49  
  50      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  51  
  52      $content = '';
  53      while ( $query->have_posts() ) {
  54          $query->the_post();
  55          $block_content = (
  56              new WP_Block(
  57                  $block->parsed_block,
  58                  array(
  59                      'postType' => get_post_type(),
  60                      'postId'   => get_the_ID(),
  61                  )
  62              )
  63          )->render( array( 'dynamic' => false ) );
  64          $post_classes  = implode( ' ', get_post_class( 'wp-block-post' ) );
  65          $content      .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
  66      }
  67  
  68      wp_reset_postdata();
  69  
  70      return sprintf(
  71          '<ul %1$s>%2$s</ul>',
  72          $wrapper_attributes,
  73          $content
  74      );
  75  }
  76  
  77  /**
  78   * Registers the `core/post-template` block on the server.
  79   */
  80  function register_block_core_post_template() {
  81      register_block_type_from_metadata(
  82          __DIR__ . '/post-template',
  83          array(
  84              'render_callback'   => 'render_block_core_post_template',
  85              'skip_inner_blocks' => true,
  86          )
  87      );
  88  }
  89  add_action( 'init', 'register_block_core_post_template' );


Generated: Sun Oct 13 01:00:02 2024 Cross-referenced by PHPXref 0.7.1