PluginProbe
ElasticPress / 5.0.0
ElasticPress v5.0.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / REST / MetaRange.php

MetaRange.php in ElasticPress 5.0.0, at includes/classes/REST/MetaRange.php

115 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_filter( 'ep_is_facetable', '__return_true' );
75
76 $search = Features::factory()->get_registered_feature( 'search' );
77 $facets = Features::factory()->get_registered_feature( 'facets' );
78
79 $facet = $request->get_param( 'facet' );
80
81 add_filter(
82 'ep_facet_meta_range_fields',
83 function ( $meta_fields ) use ( $facet ) {
84 $meta_fields = [ $facet ];
85
86 return $meta_fields;
87 }
88 );
89
90 $args = [
91 'post_type' => $search->get_searchable_post_types(),
92 'posts_per_page' => 1,
93 ];
94 $wp_query->query( $args );
95
96 $min_field_name = $facets->types['meta-range']->get_filter_name() . $facet . '_min';
97 $max_field_name = $facets->types['meta-range']->get_filter_name() . $facet . '_max';
98
99 if ( empty( $GLOBALS['ep_facet_aggs'][ $min_field_name ] ) || empty( $GLOBALS['ep_facet_aggs'][ $max_field_name ] ) ) {
100 wp_send_json_error();
101 return;
102 }
103
104 $min = $GLOBALS['ep_facet_aggs'][ $min_field_name ];
105 $max = $GLOBALS['ep_facet_aggs'][ $max_field_name ];
106
107 wp_send_json_success(
108 [
109 'min' => $min,
110 'max' => $max,
111 ]
112 );
113 }
114 }
115