forked from ryanwelcher/advanced-query-loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-loop.php
164 lines (145 loc) · 4.46 KB
/
query-loop.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
<?php
/**
* Handles the filters we need to add to the query.
*
* @package AdvancedQueryLoop
*/
namespace AdvancedQueryLoop;
// Bail on unit tests.
if ( ! function_exists( 'add_filter' ) ) {
return;
}
/**
* Updates the query on the front end based on custom query attributes.
*/
\add_filter(
'pre_render_block',
function ( $pre_render, $parsed_block ) {
if ( isset( $parsed_block['attrs']['namespace'] ) && 'advanced-query-loop' === $parsed_block['attrs']['namespace'] ) {
// Hijack the global query. It's a hack, but it works.
if ( isset( $parsed_block['attrs']['query']['inherit'] ) && 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'],
)
);
/**
* Filter the query vars.
*
* Allows filtering query params when the query is being inherited.
*
* @since 1.5
*
* @param array $query_args Arguments to be passed to WP_Query.
* @param array $block_query The query attribute retrieved from the block.
* @param boolean $inherited Whether the query is being inherited.
*
* @param array $filtered_query_args Final arguments list.
*/
$filtered_query_args = \apply_filters(
'aql_query_vars',
$query_args,
$parsed_block['attrs']['query'],
true,
);
$wp_query = new \WP_Query( array_filter( $filtered_query_args ) );
} else {
\add_filter(
'query_loop_block_query_vars',
function ( $default_query, $block ) {
// Retrieve the query from the passed block context.
$block_query = $block->context['query'] ?? array();
// Process all of the params
$qpg = new Query_Params_Generator( $default_query, $block_query );
$qpg->process_all();
$query_args = $qpg->get_query_args();
/** This filter is documented in includes/query-loop.php */
$filtered_query_args = \apply_filters(
'aql_query_vars',
$query_args,
$block_query,
false
);
// Return the merged query.
return array_merge(
$default_query,
$filtered_query_args
);
},
10,
2
);
}
}
return $pre_render;
},
10,
2
);
/**
* Updates the query vars for the Query Loop block in the block editor
*/
// Add a filter to each rest endpoint to add our custom query params.
\add_action(
'init',
function () {
$registered_post_types = \get_post_types( array( 'public' => true ) );
foreach ( $registered_post_types as $registered_post_type ) {
\add_filter( 'rest_' . $registered_post_type . '_query', __NAMESPACE__ . '\add_custom_query_params', 10, 2 );
// We need more sortBy options.
\add_filter( 'rest_' . $registered_post_type . '_collection_params', __NAMESPACE__ . '\add_more_sort_by', 10, 2 );
}
},
PHP_INT_MAX
);
/**
* Override the allowed items
*
* @see https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/get_collection_params/
*
* @param array $query_params The query params.
* @param array $post_type The post type.
*
* @return array
*/
function add_more_sort_by( $query_params ) {
$query_params['orderby']['enum'][] = 'menu_order';
$query_params['orderby']['enum'][] = 'meta_value';
$query_params['orderby']['enum'][] = 'meta_value_num';
$query_params['orderby']['enum'][] = 'rand';
$query_params['orderby']['enum'][] = 'post__in';
$query_params['orderby']['enum'][] = 'comment_count';
$query_params['orderby']['enum'][] = 'name';
// die( '<pre>' .print_r( $query_params , 1 ) .'</pre>' );
return $query_params;
}
/**
* Callback to handle the custom query params. Updates the block editor.
*
* @param array $args The query args.
* @param WP_REST_Request $request The request object.
*/
function add_custom_query_params( $args, $request ) {
// Process all of the params
$qpg = new Query_Params_Generator( $args, $request->get_params() );
$qpg->process_all();
$query_args = $qpg->get_query_args();
/** This filter is documented in includes/query-loop.php */
$filtered_query_args = \apply_filters(
'aql_query_vars',
$query_args,
$request->get_params(),
false,
);
// Merge all queries.
$merged = array_merge(
$args,
array_filter( $filtered_query_args )
);
// die( var_dump( $request->get_params() ) );
return $merged;
}