PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
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 2 years 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
190 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 * Get the query data based on the type.
29 *
30 * @param string $query_type The type of query (WP_Query, post_meta, etc).
31 * @param array $attributes Block attributes.
32 * @param object $block The block instance.
33 * @param int $page The current query page.
34 * @return array Array of query data including the data for looping and no_results.
35 */
36 public static function get_query_data( $query_type, $attributes, $block, $page ) {
37 $original_args = $attributes['query'] ?? [];
38 $query_data = [
39 'data' => [],
40 'no_results' => true,
41 'args' => $original_args,
42 ];
43
44 if ( self::TYPE_WP_QUERY === $query_type ) {
45 // Override the custom query with the global query if needed.
46 $use_global_query = ( isset( $attributes['inheritQuery'] ) && $attributes['inheritQuery'] );
47
48 if ( $use_global_query ) {
49 global $wp_query;
50
51 /*
52 * If already in the main query loop, duplicate the query instance to not tamper with the main instance.
53 * Since this is a nested query, it should start at the beginning, therefore rewind posts.
54 * Otherwise, the main query loop has not started yet and this block is responsible for doing so.
55 */
56 if ( in_the_loop() ) {
57 $data = clone $wp_query;
58 $data->rewind_posts();
59 } else {
60 $data = $wp_query;
61 }
62
63 $query_args = $data->query_vars;
64 } else {
65 $query_args = GenerateBlocks_Query_Utils::get_wp_query_args(
66 $query_data['args'],
67 $page,
68 $attributes,
69 $block
70 );
71
72 // Make the new WP_Query with filtered args.
73 $data = new WP_Query( $query_args );
74 }
75
76 $query_data = [
77 'data' => $data,
78 'no_results' => 0 === $data->found_posts,
79 'args' => $query_args,
80 ];
81 }
82
83 /**
84 * Modify the Query block's query data.
85 *
86 * @param array $query_data The current query data.
87 * @param string $query_type The type of query.
88 * @param array $attributes An array of block attributes.
89 * @param object $object The block instance.
90 * @param int $page The current page number.
91 *
92 * @return array An array of query data.
93 */
94 return apply_filters( 'generateblocks_query_data', $query_data, $query_type, $attributes, $block, $page );
95 }
96
97 /**
98 * Render the Query block.
99 *
100 * @param array $attributes The block attributes.
101 * @param string $block_content The block content.
102 * @param object $block The block.
103 */
104 public static function render_block( $attributes, $block_content, $block ) {
105 $query_id = isset( $attributes['uniqueId'] ) ? 'query-' . $attributes['uniqueId'] : 'query';
106 $page_key = $query_id . '-page';
107 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening.
108 $pagination_type = $attributes['paginationType'] ?? '';
109 $instant_pagination = self::TYPE_INSTANT_PAGINATION === $pagination_type;
110 $query_type = $attributes['queryType'] ?? self::TYPE_WP_QUERY;
111 $query_data = self::get_query_data(
112 $query_type,
113 $attributes,
114 $block,
115 $page
116 );
117
118 if ( $instant_pagination ) {
119 if ( ! wp_script_is( 'generateblocks-looper', 'enqueued' ) ) {
120 self::enqueue_assets();
121 }
122 }
123
124 $max_pages = $query_data['max_num_pages'] ?? $query_data['data']->max_num_pages ?? 0;
125
126 $parsed_content = (
127 new WP_Block(
128 $block->parsed_block,
129 array(
130 'generateblocks/queryData' => [
131 'id' => $attributes['uniqueId'] ?? '',
132 'noResults' => $query_data['no_results'],
133 'data' => $query_data['data'],
134 'args' => $query_data['args'],
135 'type' => $query_type,
136 'maxPages' => $max_pages,
137 'inherit' => $attributes['inheritQuery'] ?? false,
138 'paginationType' => $pagination_type,
139 ],
140 )
141 )
142 )->render( array( 'dynamic' => false ) );
143
144 if ( $instant_pagination && class_exists( 'WP_HTML_Tag_Processor' ) ) {
145 $processor = new WP_HTML_Tag_Processor( $parsed_content );
146
147 if ( $processor->next_tag( $attributes['tagName'] ) ) {
148 $processor->set_attribute( 'data-gb-router-region', $query_id );
149 $parsed_content = $processor->get_updated_html();
150 }
151 }
152
153 // Add styles to this block if needed.
154 $output = generateblocks_maybe_add_block_css(
155 '',
156 [
157 'class_name' => __CLASS__,
158 'attributes' => $attributes,
159 'block_ids' => self::$block_ids,
160 ]
161 );
162
163 $output .= $parsed_content;
164
165 return $output;
166 }
167
168 /**
169 * Enqueue block scripts.
170 */
171 private static function enqueue_scripts() {
172 $asset_info = generateblocks_get_enqueue_assets( 'generateblocks-looper' );
173
174 wp_enqueue_script(
175 'generateblocks-looper',
176 GENERATEBLOCKS_DIR_URL . 'dist/looper.js',
177 $asset_info['dependencies'],
178 $asset_info['version'],
179 true
180 );
181 }
182
183 /**
184 * Enqueue block assets.
185 */
186 public static function enqueue_assets() {
187 self::enqueue_scripts();
188 }
189 }
190