PluginProbe
Gutenberg / 9.8.0
Gutenberg v9.8.0
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / query-loop.php

query-loop.php in Gutenberg 9.8.0, at build/block-library/blocks/query-loop.php

121 lines 4.0 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/query-loop` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/query-loop` 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_query_loop( $attributes, $content, $block ) {
18 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
19 $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );
20
21 $query = array(
22 'post_type' => 'post',
23 'offset' => 0,
24 'order' => 'DESC',
25 'orderby' => 'date',
26 'post__not_in' => array(),
27 );
28
29 if ( isset( $block->context['query'] ) ) {
30 if ( isset( $block->context['query']['postType'] ) ) {
31 $query['post_type'] = $block->context['query']['postType'];
32 }
33 if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) {
34 $sticky = get_option( 'sticky_posts' );
35 if ( 'only' === $block->context['query']['sticky'] ) {
36 $query['post__in'] = $sticky;
37 } else {
38 $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
39 }
40 }
41 if ( isset( $block->context['query']['exclude'] ) ) {
42 $query['post__not_in'] = array_merge( $query['post__not_in'], $block->context['query']['exclude'] );
43 }
44 if ( isset( $block->context['query']['perPage'] ) ) {
45 $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset'];
46 $query['posts_per_page'] = $block->context['query']['perPage'];
47 }
48 if ( isset( $block->context['query']['categoryIds'] ) ) {
49 $query['category__in'] = $block->context['query']['categoryIds'];
50 }
51 if ( isset( $block->context['query']['tagIds'] ) ) {
52 $query['tag__in'] = $block->context['query']['tagIds'];
53 }
54 if ( isset( $block->context['query']['order'] ) ) {
55 $query['order'] = strtoupper( $block->context['query']['order'] );
56 }
57 if ( isset( $block->context['query']['orderBy'] ) ) {
58 $query['orderby'] = $block->context['query']['orderBy'];
59 }
60 if ( isset( $block->context['query']['author'] ) ) {
61 $query['author'] = $block->context['query']['author'];
62 }
63 if ( isset( $block->context['query']['search'] ) ) {
64 $query['s'] = $block->context['query']['search'];
65 }
66 }
67
68 // Override the custom query with the global query if needed.
69 $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
70 if ( $use_global_query ) {
71 global $wp_query;
72 if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
73 $query = wp_parse_args( $wp_query->query_vars, $query );
74 }
75 }
76
77 $posts = get_posts( $query );
78
79 $classnames = '';
80 if ( isset( $block->context['layout'] ) && isset( $block->context['query'] ) ) {
81 if ( isset( $block->context['layout']['type'] ) && 'flex' === $block->context['layout']['type'] ) {
82 $classnames = "is-flex-container columns-{$block->context['layout']['columns']}";
83 }
84 }
85
86 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
87
88 $content = '';
89 foreach ( $posts as $post ) {
90 $block_content = (
91 new WP_Block(
92 $block->parsed_block,
93 array(
94 'postType' => $post->post_type,
95 'postId' => $post->ID,
96 )
97 )
98 )->render( array( 'dynamic' => false ) );
99 $content .= "<li>{$block_content}</li>";
100 }
101 return sprintf(
102 '<ul %1$s>%2$s</ul>',
103 $wrapper_attributes,
104 $content
105 );
106 }
107
108 /**
109 * Registers the `core/query-loop` block on the server.
110 */
111 function gutenberg_register_block_core_query_loop() {
112 register_block_type_from_metadata(
113 __DIR__ . '/query-loop',
114 array(
115 'render_callback' => 'gutenberg_render_block_core_query_loop',
116 'skip_inner_blocks' => true,
117 )
118 );
119 }
120 add_action( 'init', 'gutenberg_register_block_core_query_loop', 20 );
121