wp/wp-includes/blocks/post-template.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     2 /**
     2 /**
     3  * Server-side rendering of the `core/post-template` block.
     3  * Server-side rendering of the `core/post-template` block.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  */
     6  */
       
     7 
       
     8 /**
       
     9  * Determines whether a block list contains a block that uses the featured image.
       
    10  *
       
    11  * @param WP_Block_List $inner_blocks Inner block instance.
       
    12  *
       
    13  * @return bool Whether the block list contains a block that uses the featured image.
       
    14  */
       
    15 function block_core_post_template_uses_featured_image( $inner_blocks ) {
       
    16 	foreach ( $inner_blocks as $block ) {
       
    17 		if ( 'core/post-featured-image' === $block->name ) {
       
    18 			return true;
       
    19 		}
       
    20 		if (
       
    21 			'core/cover' === $block->name &&
       
    22 			! empty( $block->attributes['useFeaturedImage'] )
       
    23 		) {
       
    24 			return true;
       
    25 		}
       
    26 		if ( $block->inner_blocks && block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
       
    27 			return true;
       
    28 		}
       
    29 	}
       
    30 
       
    31 	return false;
       
    32 }
     7 
    33 
     8 /**
    34 /**
     9  * Renders the `core/post-template` block on the server.
    35  * Renders the `core/post-template` block on the server.
    10  *
    36  *
    11  * @param array    $attributes Block attributes.
    37  * @param array    $attributes Block attributes.
    16  */
    42  */
    17 function render_block_core_post_template( $attributes, $content, $block ) {
    43 function render_block_core_post_template( $attributes, $content, $block ) {
    18 	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    44 	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    19 	$page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    45 	$page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    20 
    46 
    21 	$query_args = build_query_vars_from_query_block( $block, $page );
    47 	// Use global query if needed.
    22 	// Override the custom query with the global query if needed.
       
    23 	$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
    48 	$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
    24 	if ( $use_global_query ) {
    49 	if ( $use_global_query ) {
    25 		global $wp_query;
    50 		global $wp_query;
    26 		if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
    51 		$query = clone $wp_query;
    27 			// Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
    52 	} else {
    28 			unset( $query_args['offset'] );
    53 		$query_args = build_query_vars_from_query_block( $block, $page );
    29 			$query_args = wp_parse_args( $wp_query->query_vars, $query_args );
    54 		$query      = new WP_Query( $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 	}
    55 	}
    36 
       
    37 	$query = new WP_Query( $query_args );
       
    38 
    56 
    39 	if ( ! $query->have_posts() ) {
    57 	if ( ! $query->have_posts() ) {
    40 		return '';
    58 		return '';
       
    59 	}
       
    60 
       
    61 	if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
       
    62 		update_post_thumbnail_cache( $query );
    41 	}
    63 	}
    42 
    64 
    43 	$classnames = '';
    65 	$classnames = '';
    44 	if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
    66 	if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
    45 		if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
    67 		if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
    50 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
    72 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
    51 
    73 
    52 	$content = '';
    74 	$content = '';
    53 	while ( $query->have_posts() ) {
    75 	while ( $query->have_posts() ) {
    54 		$query->the_post();
    76 		$query->the_post();
       
    77 
       
    78 		// Get an instance of the current Post Template block.
       
    79 		$block_instance = $block->parsed_block;
       
    80 
       
    81 		// 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.
       
    83 		$block_instance['blockName'] = 'core/null';
       
    84 
       
    85 		// 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.
    55 		$block_content = (
    87 		$block_content = (
    56 			new WP_Block(
    88 			new WP_Block(
    57 				$block->parsed_block,
    89 				$block_instance,
    58 				array(
    90 				array(
    59 					'postType' => get_post_type(),
    91 					'postType' => get_post_type(),
    60 					'postId'   => get_the_ID(),
    92 					'postId'   => get_the_ID(),
    61 				)
    93 				)
    62 			)
    94 			)
    63 		)->render( array( 'dynamic' => false ) );
    95 		)->render( array( 'dynamic' => false ) );
    64 		$content      .= "<li>{$block_content}</li>";
    96 
       
    97 		// Wrap the render inner blocks in a `li` element with the appropriate post classes.
       
    98 		$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
       
    99 		$content     .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
    65 	}
   100 	}
    66 
   101 
       
   102 	/*
       
   103 	 * Use this function to restore the context of the template tags
       
   104 	 * from a secondary query loop back to the main query loop.
       
   105 	 * Since we use two custom loops, it's safest to always restore.
       
   106 	*/
    67 	wp_reset_postdata();
   107 	wp_reset_postdata();
    68 
   108 
    69 	return sprintf(
   109 	return sprintf(
    70 		'<ul %1$s>%2$s</ul>',
   110 		'<ul %1$s>%2$s</ul>',
    71 		$wrapper_attributes,
   111 		$wrapper_attributes,