PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / trunk
GenerateBlocks vtrunk
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / blocks / class-query.php
generateblocks / includes / blocks Last commit date
class-block.php 1 year ago class-button-container.php 2 years ago class-button.php 2 years ago class-container.php 2 years ago class-element.php 1 year ago class-grid.php 2 years ago class-headline.php 1 week ago class-image.php 2 years ago class-loop-item.php 1 year ago class-looper.php 1 year ago class-media.php 1 year ago class-query-loop.php 3 years ago class-query-no-results.php 1 year ago class-query-page-numbers.php 1 year ago class-query.php 1 year ago class-shape.php 1 year ago class-text.php 1 year ago
class-query.php
183 lines
1 <?php
2 /**
3 * Handles the Element block.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * The Query block.
14 */
15 class GenerateBlocks_Block_Query extends GenerateBlocks_Block {
16
17 const TYPE_WP_QUERY = 'WP_Query';
18 const TYPE_INSTANT_PAGINATION = 'instant';
19
20 /**
21 * Keep track of all blocks of this type on the page.
22 *
23 * @var array $block_ids The current block id.
24 */
25 protected static $block_ids = [];
26
27 /**
28 * Store our block name.
29 *
30 * @var string $block_name The block name.
31 */
32 public static $block_name = 'generateblocks/query';
33
34 /**
35 * Get the query data based on the type.
36 *
37 * @param string $query_type The type of query (WP_Query, post_meta, etc).
38 * @param array $attributes Block attributes.
39 * @param object $block The block instance.
40 * @param int $page The current query page.
41 * @return array Array of query data including the data for looping and no_results.
42 */
43 public static function get_query_data( $query_type, $attributes, $block, $page ) {
44 $original_args = $attributes['query'] ?? [];
45 $query_data = [
46 'data' => [],
47 'no_results' => true,
48 'args' => $original_args,
49 ];
50
51 if ( self::TYPE_WP_QUERY === $query_type ) {
52 // Override the custom query with the global query if needed.
53 $use_global_query = ( isset( $attributes['inheritQuery'] ) && $attributes['inheritQuery'] );
54
55 if ( $use_global_query ) {
56 global $wp_query;
57
58 /*
59 * If already in the main query loop, duplicate the query instance to not tamper with the main instance.
60 * Since this is a nested query, it should start at the beginning, therefore rewind posts.
61 * Otherwise, the main query loop has not started yet and this block is responsible for doing so.
62 */
63 if ( in_the_loop() ) {
64 $data = clone $wp_query;
65 $data->rewind_posts();
66 } else {
67 $data = $wp_query;
68 }
69
70 $query_args = $data->query_vars;
71 } else {
72 $query_args = GenerateBlocks_Query_Utils::get_wp_query_args(
73 $query_data['args'],
74 $page,
75 $attributes,
76 $block
77 );
78
79 // Make the new WP_Query with filtered args.
80 $data = new WP_Query( $query_args );
81 }
82
83 $query_data = [
84 'data' => $data,
85 'no_results' => 0 === $data->found_posts,
86 'args' => $query_args,
87 ];
88 }
89
90 /**
91 * Modify the Query block's query data.
92 *
93 * @param array $query_data The current query data.
94 * @param string $query_type The type of query.
95 * @param array $attributes An array of block attributes.
96 * @param object $object The block instance.
97 * @param int $page The current page number.
98 *
99 * @return array An array of query data.
100 */
101 return apply_filters( 'generateblocks_query_data', $query_data, $query_type, $attributes, $block, $page );
102 }
103
104 /**
105 * Render the Query block.
106 *
107 * @param array $attributes The block attributes.
108 * @param string $block_content The block content.
109 * @param object $block The block.
110 */
111 public static function render_block( $attributes, $block_content, $block ) {
112 $query_id = isset( $attributes['uniqueId'] ) ? 'query-' . $attributes['uniqueId'] : 'query';
113 $page_key = $query_id . '-page';
114 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
115 $pagination_type = $attributes['paginationType'] ?? '';
116 $instant_pagination = self::TYPE_INSTANT_PAGINATION === $pagination_type;
117 $query_type = $attributes['queryType'] ?? self::TYPE_WP_QUERY;
118 $query_data = self::get_query_data(
119 $query_type,
120 $attributes,
121 $block,
122 $page
123 );
124
125 if ( $instant_pagination ) {
126 if ( ! wp_script_is( 'generateblocks-looper', 'enqueued' ) ) {
127 $asset_info = generateblocks_get_enqueue_assets( 'generateblocks-looper' );
128
129 wp_enqueue_script(
130 'generateblocks-looper',
131 GENERATEBLOCKS_DIR_URL . 'dist/looper.js',
132 $asset_info['dependencies'],
133 $asset_info['version'],
134 true
135 );
136 }
137 }
138
139 $max_pages = $query_data['max_num_pages'] ?? $query_data['data']->max_num_pages ?? 0;
140
141 $parsed_content = (
142 new WP_Block(
143 $block->parsed_block,
144 array(
145 'generateblocks/queryData' => [
146 'id' => $attributes['uniqueId'] ?? '',
147 'noResults' => $query_data['no_results'],
148 'data' => $query_data['data'],
149 'args' => $query_data['args'],
150 'type' => $query_type,
151 'maxPages' => $max_pages,
152 'inherit' => $attributes['inheritQuery'] ?? false,
153 'paginationType' => $pagination_type,
154 ],
155 )
156 )
157 )->render( array( 'dynamic' => false ) );
158
159 if ( $instant_pagination && class_exists( 'WP_HTML_Tag_Processor' ) ) {
160 $processor = new WP_HTML_Tag_Processor( $parsed_content );
161
162 if ( $processor->next_tag( $attributes['tagName'] ) ) {
163 $processor->set_attribute( 'data-gb-router-region', $query_id );
164 $parsed_content = $processor->get_updated_html();
165 }
166 }
167
168 // Add styles to this block if needed.
169 $output = generateblocks_maybe_add_block_css(
170 '',
171 [
172 'class_name' => __CLASS__,
173 'attributes' => $attributes,
174 'block_ids' => self::$block_ids,
175 ]
176 );
177
178 $output .= $parsed_content;
179
180 return $output;
181 }
182 }
183