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