| @@ -5,8 +5,34 @@ | ||
| 5 | 5 | * @package WordPress |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 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 gutenberg_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 && gutenberg_block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { | |
| 27 | + return true; | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + return false; | |
| 32 | +} | |
| 33 | + | |
| 34 | +/** | |
| 9 | 35 | * Renders the `core/post-template` block on the server. |
| 10 | 36 | * |
| 11 | 37 | * @param array $attributes Block attributes. |
| 12 | 38 | * @param string $content Block default content. |
| @@ -17,30 +43,26 @@ | ||
| 17 | 43 | function gutenberg_render_block_core_post_template( $attributes, $content, $block ) { |
| 18 | 44 | $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
| 19 | 45 | $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
| 20 | 46 | |
| 21 | - $query_args = gutenberg_build_query_vars_from_query_block( $block, $page ); | |
| 22 | - // Override the custom query with the global query if needed. | |
| 47 | + // Use global query if needed. | |
| 23 | 48 | $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); |
| 24 | 49 | if ( $use_global_query ) { |
| 25 | 50 | 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 | - } | |
| 51 | + $query = clone $wp_query; | |
| 52 | + } else { | |
| 53 | + $query_args = gutenberg_build_query_vars_from_query_block( $block, $page ); | |
| 54 | + $query = new WP_Query( $query_args ); | |
| 35 | 55 | } |
| 36 | 56 | |
| 37 | - $query = new WP_Query( $query_args ); | |
| 38 | - | |
| 39 | 57 | if ( ! $query->have_posts() ) { |
| 40 | 58 | return ''; |
| 41 | 59 | } |
| 42 | 60 | |
| 61 | + if ( gutenberg_block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { | |
| 62 | + update_post_thumbnail_cache( $query ); | |
| 63 | + } | |
| 64 | + | |
| 43 | 65 | $classnames = ''; |
| 44 | 66 | if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { |
| 45 | 67 | if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { |
| 46 | 68 | $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; |
| @@ -51,11 +73,21 @@ | ||
| 51 | 73 | |
| 52 | 74 | $content = ''; |
| 53 | 75 | while ( $query->have_posts() ) { |
| 54 | 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 | 87 | $block_content = ( |
| 56 | 88 | new WP_Block( |
| 57 | - $block->parsed_block, | |
| 89 | + $block_instance, | |
| 58 | 90 | array( |
| 59 | 91 | 'postType' => get_post_type(), |
| 60 | 92 | 'postId' => get_the_ID(), |
| 61 | 93 | ) |
| @@ -60,12 +92,19 @@ | ||
| 60 | 92 | 'postId' => get_the_ID(), |
| 61 | 93 | ) |
| 62 | 94 | ) |
| 63 | 95 | )->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>'; | |
| 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>'; | |
| 66 | 100 | } |
| 67 | 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 | + */ | |
| 68 | 107 | wp_reset_postdata(); |
| 69 | 108 | |
| 70 | 109 | return sprintf( |
| 71 | 110 | '<ul %1$s>%2$s</ul>', |