wp/wp-includes/blocks/post-template.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     5  * @package WordPress
     5  * @package WordPress
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Determines whether a block list contains a block that uses the featured image.
     9  * Determines whether a block list contains a block that uses the featured image.
       
    10  *
       
    11  * @since 6.0.0
    10  *
    12  *
    11  * @param WP_Block_List $inner_blocks Inner block instance.
    13  * @param WP_Block_List $inner_blocks Inner block instance.
    12  *
    14  *
    13  * @return bool Whether the block list contains a block that uses the featured image.
    15  * @return bool Whether the block list contains a block that uses the featured image.
    14  */
    16  */
    32 }
    34 }
    33 
    35 
    34 /**
    36 /**
    35  * Renders the `core/post-template` block on the server.
    37  * Renders the `core/post-template` block on the server.
    36  *
    38  *
       
    39  * @since 6.3.0 Changed render_block_context priority to `1`.
       
    40  *
       
    41  * @global WP_Query $wp_query WordPress Query object.
       
    42  *
    37  * @param array    $attributes Block attributes.
    43  * @param array    $attributes Block attributes.
    38  * @param string   $content    Block default content.
    44  * @param string   $content    Block default content.
    39  * @param WP_Block $block      Block instance.
    45  * @param WP_Block $block      Block instance.
    40  *
    46  *
    41  * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
    47  * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
    42  */
    48  */
    43 function render_block_core_post_template( $attributes, $content, $block ) {
    49 function render_block_core_post_template( $attributes, $content, $block ) {
    44 	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    50 	$page_key            = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    45 	$page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    51 	$enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
       
    52 	$page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    46 
    53 
    47 	// Use global query if needed.
    54 	// Use global query if needed.
    48 	$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
    55 	$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
    49 	if ( $use_global_query ) {
    56 	if ( $use_global_query ) {
    50 		global $wp_query;
    57 		global $wp_query;
    51 		$query = clone $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 		}
    52 	} else {
    70 	} else {
    53 		$query_args = build_query_vars_from_query_block( $block, $page );
    71 		$query_args = build_query_vars_from_query_block( $block, $page );
    54 		$query      = new WP_Query( $query_args );
    72 		$query      = new WP_Query( $query_args );
    55 	}
    73 	}
    56 
    74 
    66 	if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
    84 	if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
    67 		if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
    85 		if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
    68 			$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
    86 			$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
    69 		}
    87 		}
    70 	}
    88 	}
       
    89 	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
       
    90 		$classnames .= ' has-link-color';
       
    91 	}
    71 
    92 
    72 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
    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 ) ) );
    73 
    99 
    74 	$content = '';
   100 	$content = '';
    75 	while ( $query->have_posts() ) {
   101 	while ( $query->have_posts() ) {
    76 		$query->the_post();
   102 		$query->the_post();
    77 
   103 
    80 
   106 
    81 		// Set the block name to one that does not correspond to an existing registered block.
   107 		// Set the block name to one that does not correspond to an existing registered block.
    82 		// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
   108 		// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
    83 		$block_instance['blockName'] = 'core/null';
   109 		$block_instance['blockName'] = 'core/null';
    84 
   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 );
    85 		// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
   121 		// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
    86 		// `render_callback` and ensure that no wrapper markup is included.
   122 		// `render_callback` and ensure that no wrapper markup is included.
    87 		$block_content = (
   123 		$block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) );
    88 			new WP_Block(
   124 		remove_filter( 'render_block_context', $filter_block_context, 1 );
    89 				$block_instance,
       
    90 				array(
       
    91 					'postType' => get_post_type(),
       
    92 					'postId'   => get_the_ID(),
       
    93 				)
       
    94 			)
       
    95 		)->render( array( 'dynamic' => false ) );
       
    96 
   125 
    97 		// Wrap the render inner blocks in a `li` element with the appropriate post classes.
   126 		// Wrap the render inner blocks in a `li` element with the appropriate post classes.
    98 		$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
   127 		$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
    99 		$content     .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
   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>';
   100 	}
   132 	}
   101 
   133 
   102 	/*
   134 	/*
   103 	 * Use this function to restore the context of the template tags
   135 	 * Use this function to restore the context of the template tags
   104 	 * from a secondary query loop back to the main query loop.
   136 	 * from a secondary query loop back to the main query loop.
   113 	);
   145 	);
   114 }
   146 }
   115 
   147 
   116 /**
   148 /**
   117  * Registers the `core/post-template` block on the server.
   149  * Registers the `core/post-template` block on the server.
       
   150  *
       
   151  * @since 5.8.0
   118  */
   152  */
   119 function register_block_core_post_template() {
   153 function register_block_core_post_template() {
   120 	register_block_type_from_metadata(
   154 	register_block_type_from_metadata(
   121 		__DIR__ . '/post-template',
   155 		__DIR__ . '/post-template',
   122 		array(
   156 		array(