PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.5
Elementor Website Builder – more than just a page builder v4.1.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / wp-rest / classes / term-query.php

term-query.php in Elementor Website Builder – more than just a page builder 4.1.5, at modules/wp-rest/classes/term-query.php

225 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\WpRest\Classes;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Modules\WpRest\Base\Query as Base;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Term_Query extends Base {
13 const ENDPOINT = 'term';
14 const SEARCH_FILTER_ACCEPTED_ARGS = 3;
15
16 /**
17 * @param array $clauses Associative array of the clauses for the query.
18 * @param array $taxonomies Array of taxonomy names.
19 * @param array $args The args passed to 'get_terms()'.
20 * @return array Modified clauses.
21 */
22 public function customize_terms_query( $clauses, $taxonomies, $args ) {
23 if ( ! $args['custom_search'] ) {
24 return $clauses;
25 }
26
27 if ( is_numeric( $args['name__like'] ) ) {
28 $clauses['where'] = '(' . $clauses['where'] . ' OR t.term_id = ' . $args['name__like'] . ')';
29 }
30
31 if ( empty( $args['excluded_taxonomies'] ) ) {
32 return $clauses;
33 }
34
35 $excluded_taxonomies = $args['excluded_taxonomies'];
36 $escaped = array_map( 'esc_sql', $excluded_taxonomies );
37 $list = "'" . implode( "','", $escaped ) . "'";
38 $clauses['where'] .= " AND tt.taxonomy NOT IN ({$list})";
39
40 return $clauses;
41 }
42
43 /**
44 * @param \WP_REST_Request $request
45 * @return \WP_REST_Response
46 */
47 protected function get( \WP_REST_Request $request ) {
48 $params = $request->get_params();
49 $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
50
51 if ( empty( $term ) ) {
52 return new \WP_REST_Response( [
53 'success' => true,
54 'data' => [
55 'value' => [],
56 ],
57 ], 200 );
58 }
59
60 $included_taxonomies = $params[ self::INCLUDED_TYPE_KEY ];
61 $excluded_taxonomies = $params[ self::EXCLUDED_TYPE_KEY ];
62
63 $keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ];
64
65 $requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
66 $validated_count = max( $requested_count, 1 );
67 $count = min( $validated_count, self::MAX_RESPONSE_COUNT );
68
69 $should_hide_empty = $params[ self::HIDE_EMPTY_KEY ] ?? false;
70
71 $query_args = [
72 'number' => $count,
73 'name__like' => $term,
74 'hide_empty' => $should_hide_empty,
75 'taxonomy' => ! empty( $included_taxonomies ) ? $included_taxonomies : null,
76 'excluded_taxonomies' => $excluded_taxonomies ?? [],
77 'suppress_filter' => false,
78 'custom_search' => true,
79 ];
80
81 if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
82 $query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
83 }
84
85 $this->add_filter_to_customize_query();
86 $terms = new Collection( get_terms( $query_args ) );
87 $this->remove_filter_to_customize_query();
88
89 $term_group_labels = $terms
90 ->reduce( function ( $term_types, $term ) {
91 if ( ! isset( $term_types[ $term->taxonomy ] ) ) {
92 $taxonomy = get_taxonomy( $term->taxonomy );
93 $term_types[ $term->taxonomy ] = $taxonomy->labels->name ?? $term->labels;
94 }
95
96 return $term_types;
97 }, [] );
98
99 return new \WP_REST_Response( [
100 'success' => true,
101 'data' => [
102 'value' => $terms
103 ->map( function ( $term ) use ( $keys_format_map, $term_group_labels ) {
104 $term_object = (array) $term;
105
106 if ( isset( $term_object['taxonomy'] ) ) {
107 $group_name = $term_object['taxonomy'];
108
109 if ( isset( $term_group_labels[ $group_name ] ) ) {
110 $term_object['taxonomy'] = $term_group_labels[ $group_name ];
111 }
112 }
113
114 return $this->translate_keys( $term_object, $keys_format_map );
115 } )
116 ->all(),
117 ],
118 ], 200 );
119 }
120
121 /**
122 * @return void
123 */
124 private function add_filter_to_customize_query() {
125 $priority = self::SEARCH_FILTER_PRIORITY;
126 $accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
127
128 add_filter( 'terms_clauses', [ $this, 'customize_terms_query' ], $priority, $accepted_args );
129 }
130
131 /**
132 * @return void
133 */
134 private function remove_filter_to_customize_query() {
135 $priority = self::SEARCH_FILTER_PRIORITY;
136 $accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
137
138 remove_filter( 'terms_clauses', [ $this, 'customize_terms_query' ], $priority, $accepted_args );
139 }
140
141 protected function permission_check( \WP_REST_Request $request ): bool {
142 return current_user_can( 'edit_posts' );
143 }
144
145 /**
146 * @return array
147 */
148 protected function get_endpoint_registration_args(): array {
149 return [
150 self::INCLUDED_TYPE_KEY => [
151 'description' => 'Included taxonomy containing terms (categories, tags, etc...)',
152 'type' => 'array',
153 'required' => false,
154 'default' => null,
155 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
156 ],
157 self::EXCLUDED_TYPE_KEY => [
158 'description' => 'Excluded taxonomy containing terms (categories, tags, etc...)',
159 'type' => 'array',
160 'required' => false,
161 'default' => null,
162 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
163 ],
164 self::SEARCH_TERM_KEY => [
165 'description' => 'Terms to search',
166 'type' => 'string',
167 'required' => false,
168 'default' => '',
169 'sanitize_callback' => 'sanitize_text_field',
170 ],
171 self::KEYS_CONVERSION_MAP_KEY => [
172 'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].',
173 'type' => 'array',
174 'required' => false,
175 'default' => [
176 'term_id' => 'id',
177 'name' => 'label',
178 'taxonomy' => 'groupLabel',
179 ],
180 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
181 ],
182 self::ITEMS_COUNT_KEY => [
183 'description' => 'Terms per request',
184 'type' => 'integer',
185 'required' => false,
186 'default' => self::MAX_RESPONSE_COUNT,
187 ],
188 self::HIDE_EMPTY_KEY => [
189 'description' => 'Whether to include only public terms',
190 'type' => 'boolean',
191 'required' => false,
192 'default' => false,
193 ],
194 self::META_QUERY_KEY => [
195 'description' => 'WP_Query meta_query array',
196 'type' => 'array',
197 'required' => false,
198 'default' => null,
199 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
200 ],
201 ];
202 }
203
204 protected static function get_allowed_param_keys(): array {
205 return [
206 self::EXCLUDED_TYPE_KEY,
207 self::INCLUDED_TYPE_KEY,
208 self::KEYS_CONVERSION_MAP_KEY,
209 self::META_QUERY_KEY,
210 self::TAX_QUERY_KEY,
211 self::ITEMS_COUNT_KEY,
212 ];
213 }
214
215 protected static function get_keys_to_encode(): array {
216 return [
217 self::EXCLUDED_TYPE_KEY,
218 self::INCLUDED_TYPE_KEY,
219 self::KEYS_CONVERSION_MAP_KEY,
220 self::META_QUERY_KEY,
221 self::TAX_QUERY_KEY,
222 ];
223 }
224 }
225