| 1 |
<?php |
| 2 |
/** |
| 3 |
* Meta Range REST API Controller |
| 4 |
* |
| 5 |
* @since 5.0.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\REST; |
| 10 |
|
| 11 |
use ElasticPress\Features; |
| 12 |
|
| 13 |
/** |
| 14 |
* Meta Range API controller class. |
| 15 |
* |
| 16 |
* @since 5.0.0 |
| 17 |
* @package elasticpress |
| 18 |
*/ |
| 19 |
class MetaRange { |
| 20 |
|
| 21 |
/** |
| 22 |
* Register routes. |
| 23 |
* |
| 24 |
* Registers the route using its own endpoint and the previous facets |
| 25 |
* endpoint, for backwards compatibility. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function register_routes() { |
| 30 |
$args = [ |
| 31 |
'args' => $this->get_args(), |
| 32 |
'callback' => [ $this, 'get_meta_range' ], |
| 33 |
'methods' => 'GET', |
| 34 |
'permission_callback' => [ $this, 'check_permission' ], |
| 35 |
]; |
| 36 |
|
| 37 |
register_rest_route( 'elasticpress/v1', 'meta-range', $args ); |
| 38 |
register_rest_route( 'elasticpress/v1', 'facets/meta-range/block-preview', $args ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check that the request has permission. |
| 43 |
* |
| 44 |
* @return boolean |
| 45 |
*/ |
| 46 |
public function check_permission() { |
| 47 |
return current_user_can( 'edit_theme_options' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get args schema. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function get_args() { |
| 56 |
return [ |
| 57 |
'facet' => [ |
| 58 |
'description' => __( 'Filter to get a value range for.', 'elasticpress' ), |
| 59 |
'required' => true, |
| 60 |
'type' => 'string', |
| 61 |
], |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the range of values for a given filter. |
| 67 |
* |
| 68 |
* @param \WP_REST_Request $request Full details about the request. |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function get_meta_range( \WP_REST_Request $request ) { |
| 72 |
global $wp_query; |
| 73 |
|
| 74 |
$search = Features::factory()->get_registered_feature( 'search' ); |
| 75 |
$facets = Features::factory()->get_registered_feature( 'facets' ); |
| 76 |
|
| 77 |
$facet = $request->get_param( 'facet' ); |
| 78 |
|
| 79 |
add_filter( |
| 80 |
'ep_facet_meta_range_fields', |
| 81 |
function ( $meta_fields ) use ( $facet ) { |
| 82 |
$meta_fields = [ $facet ]; |
| 83 |
|
| 84 |
return $meta_fields; |
| 85 |
} |
| 86 |
); |
| 87 |
|
| 88 |
$args = [ |
| 89 |
'post_type' => $search->get_searchable_post_types(), |
| 90 |
'posts_per_page' => 1, |
| 91 |
'ep_is_facetable' => true, |
| 92 |
]; |
| 93 |
$wp_query->query( $args ); |
| 94 |
|
| 95 |
$min_field_name = $facets->types['meta-range']->get_filter_name() . $facet . '_min'; |
| 96 |
$max_field_name = $facets->types['meta-range']->get_filter_name() . $facet . '_max'; |
| 97 |
|
| 98 |
$aggregations = $facets->get_query_aggregations( $wp_query ); |
| 99 |
|
| 100 |
if ( empty( $aggregations[ $min_field_name ] ) || empty( $aggregations[ $max_field_name ] ) ) { |
| 101 |
wp_send_json_error(); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$min = $aggregations[ $min_field_name ]; |
| 106 |
$max = $aggregations[ $max_field_name ]; |
| 107 |
|
| 108 |
wp_send_json_success( |
| 109 |
[ |
| 110 |
'min' => $min, |
| 111 |
'max' => $max, |
| 112 |
] |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
|