| 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 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 |
/** |
| 35 |
* Renders the `core/post-template` block on the server. |
| 36 |
* |
| 37 |
* @param array $attributes Block attributes. |
| 38 |
* @param string $content Block default content. |
| 39 |
* @param WP_Block $block Block instance. |
| 40 |
* |
| 41 |
* @return string Returns the output of the query, structured using the layout defined by the block's inner blocks. |
| 42 |
*/ |
| 43 |
function gutenberg_render_block_core_post_template( $attributes, $content, $block ) { |
| 44 |
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
| 45 |
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
| 46 |
|
| 47 |
$query_args = gutenberg_build_query_vars_from_query_block( $block, $page ); |
| 48 |
// Override the custom query with the global query if needed. |
| 49 |
$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); |
| 50 |
if ( $use_global_query ) { |
| 51 |
global $wp_query; |
| 52 |
if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { |
| 53 |
// Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. |
| 54 |
unset( $query_args['offset'] ); |
| 55 |
$query_args = wp_parse_args( $wp_query->query_vars, $query_args ); |
| 56 |
|
| 57 |
if ( empty( $query_args['post_type'] ) && is_singular() ) { |
| 58 |
$query_args['post_type'] = get_post_type( get_the_ID() ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$query = new WP_Query( $query_args ); |
| 64 |
|
| 65 |
if ( ! $query->have_posts() ) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
if ( gutenberg_block_core_post_template_uses_featured_image( $block->inner_blocks ) ) { |
| 70 |
update_post_thumbnail_cache( $query ); |
| 71 |
} |
| 72 |
|
| 73 |
$classnames = ''; |
| 74 |
if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { |
| 75 |
if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { |
| 76 |
$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); |
| 81 |
|
| 82 |
$content = ''; |
| 83 |
while ( $query->have_posts() ) { |
| 84 |
$query->the_post(); |
| 85 |
$block_content = ( |
| 86 |
new WP_Block( |
| 87 |
$block->parsed_block, |
| 88 |
array( |
| 89 |
'postType' => get_post_type(), |
| 90 |
'postId' => get_the_ID(), |
| 91 |
) |
| 92 |
) |
| 93 |
)->render( array( 'dynamic' => false ) ); |
| 94 |
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) ); |
| 95 |
$content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>'; |
| 96 |
} |
| 97 |
|
| 98 |
wp_reset_postdata(); |
| 99 |
|
| 100 |
return sprintf( |
| 101 |
'<ul %1$s>%2$s</ul>', |
| 102 |
$wrapper_attributes, |
| 103 |
$content |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Registers the `core/post-template` block on the server. |
| 109 |
*/ |
| 110 |
function gutenberg_register_block_core_post_template() { |
| 111 |
register_block_type_from_metadata( |
| 112 |
__DIR__ . '/post-template', |
| 113 |
array( |
| 114 |
'render_callback' => 'gutenberg_render_block_core_post_template', |
| 115 |
'skip_inner_blocks' => true, |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
add_action( 'init', 'gutenberg_register_block_core_post_template', 20 ); |
| 120 |
|