164 lines
4.5 KiB
PHP
164 lines
4.5 KiB
PHP
<?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;
|
|
}
|