PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / post-template.php

post-template.php in Gutenberg 12.1.0, at build/block-library/blocks/post-template.php

90 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 $post_classes = esc_attr( implode( ' ', get_post_class( 'wp-block-post' ) ) );
65 $content .= '<li class="' . $post_classes . '">' . $block_content . '</li>';
66 }
67
68 wp_reset_postdata();
69
70 return sprintf(
71 '<ul %1$s>%2$s</ul>',
72 $wrapper_attributes,
73 $content
74 );
75 }
76
77 /**
78 * Registers the `core/post-template` block on the server.
79 */
80 function gutenberg_register_block_core_post_template() {
81 register_block_type_from_metadata(
82 __DIR__ . '/post-template',
83 array(
84 'render_callback' => 'gutenberg_render_block_core_post_template',
85 'skip_inner_blocks' => true,
86 )
87 );
88 }
89 add_action( 'init', 'gutenberg_register_block_core_post_template', 20 );
90