-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-post-type-spotlight-block-editor.php
347 lines (271 loc) · 8.73 KB
/
class-post-type-spotlight-block-editor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Post_Type_Spotlight_Block_Editor' ) ) {
/**
* Post_Type_Spotlight class.
*/
class Post_Type_Spotlight_Block_Editor {
public function __construct() {
add_action( 'init', [ $this, 'filter_rest_query' ] );
add_action( 'init', [ $this, 'block_init' ] );
add_action( 'init', [ $this, 'block_scripts' ] );
add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 999, 3 );
// This is used to sort the featured posts first if selected in the block options
add_filter('posts_orderby', [ $this, 'orderby_featured_first' ], 10, 2 );
}
/**
* @since 3.0.0
*
* @return void
*/
public function filter_rest_query() {
$pts_settings = get_option( 'pts_featured_post_types_settings', array() );
if ( ! is_array( $pts_settings ) ) {
$pts_settings = [ $pts_settings ];
}
foreach ( $pts_settings as $post_type ) {
add_filter( "rest_{$post_type}_query", [ $this, 'filter_request_by_query_type' ], 10, 2 );
}
}
/**
* Callback to handle the custom query params. Updates the block editor.
*
* @see https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/extending-the-query-loop-block/
* @see https://github.com/ryanwelcher/advanced-query-loop/blob/trunk/includes/query-loop.php
*
* @param array $args The query args.
* @param WP_REST_Request $request The request object.
*/
public function add_custom_query_params( $args, $request ) {
// Generate a new custom query with all potential query vars.
$custom_args = [];
$queryType = $request->get_param( 'queryType' );
if ( $queryType ) {
$custom_args['queryType'] = $queryType;
}
// Merge all queries.
return array_merge(
$args,
array_filter( $custom_args )
);
}
/**
* Before rendering our block on the front end change it up a bit.
*
* @since 3.0.0
*
* @param $pre_render
* @param $parsed_block
*
* @return mixed
*/
public function pre_render_block( $pre_render, $parsed_block ) {
if ( isset( $parsed_block['attrs']['namespace'] ) && 'post-type-spotlight/featured-list' === $parsed_block['attrs']['namespace'] ) {
// Hijack the global query. It's a hack, but it works.
if ( true === $parsed_block['attrs']['query']['inherit'] ) {
global $wp_query;
$query_args = array_merge(
$wp_query->query_vars,
array(
'posts_per_page' => $parsed_block['attrs']['query']['perPage'],
'order' => $parsed_block['attrs']['query']['order'],
'orderby' => $parsed_block['attrs']['query']['orderBy'],
)
);
$wp_query = new \WP_Query( array_filter( $query_args ) );
} else {
add_filter(
'query_loop_block_query_vars',
function( $default_query ) use ( $parsed_block ) {
$custom_query = $parsed_block['attrs']['query'];
$featured_term = get_term_by('slug', 'featured', 'pts_feature_tax');
// Generate a new custom query with all potential query vars.
$custom_args = [];
$queryType = $parsed_block['attrs']['query']['queryType'];
$custom_args['tax_query'] = [
[
'taxonomy' => 'pts_feature_tax',
'terms' => [ $featured_term->term_id ],
'operator' => 'IN',
'include_children' => false,
],
];
if ( $queryType === 'featured-exclude' ) {
$custom_args['tax_query'][0]['operator'] = 'NOT IN';
}
if ( ! empty( $default_query['tax_query'] ) ) {
foreach ( $default_query['tax_query'] as $index => $tax_query ) {
if ( $tax_query['taxonomy'] === 'pts_feature_tax' ) {
// If somehow they added the pts tax to their query, and it's a block remove
// the default since the block does it already.
unset( $default_query['tax_query'][ $index ] );
}
}
if ( empty( $default_query['tax_query'] ) ) {
unset( $default_query['tax_query'] );
} else {
$custom_tax_query = array_merge(
$default_query['tax_query'],
$custom_args['tax_query']
);
}
}
$data = array_merge(
$default_query,
$custom_args
);
if ( ! empty( $custom_tax_query ) ) {
$data['tax_query'] = $custom_tax_query;
if ( count( $custom_tax_query ) > 1 ) {
$data['tax_query']['relation'] = 'AND';
}
}
return $data;
},
10,
2
);
}
}
return $pre_render;
}
/**
* When using PTS in the block editor, we need to filter the request
* to order the posts by displayType attribute.
*
* Only Show Featured
* Show Featured First
* Exclude Featured
*
* @param $args
* @param $request
*
* @return void
*/
public function filter_request_by_query_type( $args, $request ) {
$queryType = $request->get_param( 'queryType' );
$custom_args = [];
if ( ! empty( $queryType ) ) {
if ( $queryType !== 'featured-first' ) {
$custom_args[ 'tax_query' ] = [
[
'taxonomy' => 'pts_feature_tax',
'field' => 'slug',
'terms' => [ 'featured' ],
'operator' => 'IN',
'include_children' => false,
],
];
}
if ( $queryType === 'featured-exclude' ) {
$custom_args['tax_query'][0]['operator'] = 'NOT IN';
}
}
return array_merge( $args, $custom_args );
}
/**
* Alter the order by clause to sort featured posts first.
*
* @param $orderby
* @param $query
*
* @return mixed|string
*/
public function orderby_featured_first( $orderby, $query ) {
global $wpdb;
if ( ! isset( $_REQUEST['queryType'] ) ) {
return $orderby;
}
if ( $_REQUEST['queryType'] !== 'featured-first' ) {
return $orderby;
}
$term = get_term_by('slug', 'featured', 'pts_feature_tax' );
// If the term doesn't exist or there's an error, just return the original orderby
if ( false === $term || is_wp_error( $term ) ) {
return $orderby;
}
// Add the custom sorting using the CASE statement directly in the ORDER BY clause
$orderby = "
CASE
WHEN EXISTS (
SELECT 1
FROM {$wpdb->term_relationships}
WHERE {$wpdb->term_relationships}.object_id = {$wpdb->posts}.ID
AND {$wpdb->term_relationships}.term_taxonomy_id = {$term->term_taxonomy_id}
)
THEN 1
ELSE 0
END DESC, {$orderby}";
return $orderby;
}
/**
* Register our plugin scripts
*
* @since 0.1.0
*
* @return void
*/
public function block_scripts() {
$post_type = false;
if ( is_admin() ) {
if ( isset($_GET['post'])) {
$post_id = $_GET['post'];
$post_type = get_post_type($post_id);
} elseif ( isset( $_GET['post_type'] ) ) {
$post_type = $_GET['post_type'];
}
if ( $post_type && use_block_editor_for_post_type($post_type)) {
$script_asset_path = POST_TYPE_SPOTLIGHT_PATH . 'blocks/build/index.asset.php';
if ( ! file_exists( $script_asset_path ) ) {
throw new \Error(
$script_asset_path . ' Missing: You need to run `npm start` or `npm run build` for the "post-type-spotlight/blocks" script first.'
);
}
$index_js = POST_TYPE_SPOTLIGHT_PLUGIN_URL . 'blocks/build/index.js';
$script_asset = require $script_asset_path;
$dependencies = array_merge(
$script_asset['dependencies'],
[
'wp-edit-post',
]
);
wp_register_script(
'post-type-spotlight-block-editor',
$index_js,
$dependencies,
$script_asset['version'],
true
);
wp_enqueue_script( 'post-type-spotlight-block-editor' );
}
}
}
/**
* Registers the block using the metadata loaded from the `block-{type}.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* Each block registered within the \Blocks namespace for examples
* @see https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/
*
* @since 0.2.1
*/
public function block_init() {
$blocks = [];
$blocks = apply_filters( 'post_type_spotlight', $blocks );
if ( empty( $blocks ) ) {
return;
}
foreach ( $blocks as $block_key => $block_args ) {
$block_path = trailingslashit( POST_TYPE_SPOTLIGHT_PATH ) . "blocks/build/blocks/{$block_key}/block.json";
if ( file_exists( $block_path ) ) {
register_block_type( $block_path, $block_args );
} else {
wp_die( esc_html( 'could not find file: ' . $block_path ) );
}
}
}
}
}