wp/wp-includes/blocks/post-template.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     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 		$content      .= "<li>{$block_content}</li>";
       
    65 	}
       
    66 
       
    67 	wp_reset_postdata();
       
    68 
       
    69 	return sprintf(
       
    70 		'<ul %1$s>%2$s</ul>',
       
    71 		$wrapper_attributes,
       
    72 		$content
       
    73 	);
       
    74 }
       
    75 
       
    76 /**
       
    77  * Registers the `core/post-template` block on the server.
       
    78  */
       
    79 function register_block_core_post_template() {
       
    80 	register_block_type_from_metadata(
       
    81 		__DIR__ . '/post-template',
       
    82 		array(
       
    83 			'render_callback'   => 'render_block_core_post_template',
       
    84 			'skip_inner_blocks' => true,
       
    85 		)
       
    86 	);
       
    87 }
       
    88 add_action( 'init', 'register_block_core_post_template' );