-
Notifications
You must be signed in to change notification settings - Fork 3
/
single-post-query-loop-selector.php
75 lines (68 loc) · 2.27 KB
/
single-post-query-loop-selector.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
<?php
/**
* Plugin Name: Single Post Query Loop Selector
* Description: A Query Loop block variation that allows to search and select a single post to be displayed.
* Requires at least: 6.1
* Requires PHP: 7.4
* Version: 0.1.2
* Author: Creative Andrew
* Author URI: https://creativeandrew.me
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: single-post-query-loop-selector
*/
namespace SinglePostQueryLoopSelector;
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_editor_assets' );
/**
* Enqueues block editor assets for a WordPress plugin.
*/
function enqueue_block_editor_assets() {
$assets_file = plugin_dir_path( __FILE__ ) . '/build/index.asset.php';
if ( file_exists( $assets_file ) ) {
$assets = include $assets_file;
wp_enqueue_script(
'single-post-query-loop-selector',
plugin_dir_url( __FILE__ ) . '/build/index.js',
$assets['dependencies'],
$assets['version'],
true
);
}
}
add_filter(
'pre_render_block',
__NAMESPACE__ . '\pre_render_block',
10,
2
);
/**
* Updates the query on the front end based on custom query attributes.
*
* This function is used to modify the query on the front end based on the custom query attributes
* of a specific block. It checks if the block belongs to a particular namespace and, if so,
* applies a filter to adjust the query parameters.
*
* @param array $pre_render The pre-render data for the block.
* @param array $parsed_block The parsed attributes of the block.
*
* @return array The modified pre-render data.
*/
function pre_render_block( $pre_render, $parsed_block ) {
if ( isset( $parsed_block['attrs']['namespace'] )
&& 'creativeandrew/single-post-query-loop-selector' === $parsed_block['attrs']['namespace'] ) {
add_filter(
'query_loop_block_query_vars',
function ( $default_query, $block ) use ( $parsed_block ) {
if ( isset( $parsed_block['attrs']['query']['include'] )
&& isset( $block->context['queryId'] )
&& $block->context['queryId'] === $parsed_block['attrs']['queryId'] ) {
$default_query['post__in'] = $parsed_block['attrs']['query']['include'];
}
return $default_query;
},
10,
2
);
}
return $pre_render;
}