| 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 |
/** |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
protected function get_endpoint_registration_args(): array { |
| 145 |
return [ |
| 146 |
self::INCLUDED_TYPE_KEY => [ |
| 147 |
'description' => 'Included taxonomy containing terms (categories, tags, etc...)', |
| 148 |
'type' => 'array', |
| 149 |
'required' => false, |
| 150 |
'default' => null, |
| 151 |
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 152 |
], |
| 153 |
self::EXCLUDED_TYPE_KEY => [ |
| 154 |
'description' => 'Excluded taxonomy containing terms (categories, tags, etc...)', |
| 155 |
'type' => 'array', |
| 156 |
'required' => false, |
| 157 |
'default' => null, |
| 158 |
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 159 |
], |
| 160 |
self::SEARCH_TERM_KEY => [ |
| 161 |
'description' => 'Terms to search', |
| 162 |
'type' => 'string', |
| 163 |
'required' => false, |
| 164 |
'default' => '', |
| 165 |
'sanitize_callback' => 'sanitize_text_field', |
| 166 |
], |
| 167 |
self::KEYS_CONVERSION_MAP_KEY => [ |
| 168 |
'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].', |
| 169 |
'type' => 'array', |
| 170 |
'required' => false, |
| 171 |
'default' => [ |
| 172 |
'term_id' => 'id', |
| 173 |
'name' => 'label', |
| 174 |
'taxonomy' => 'groupLabel', |
| 175 |
], |
| 176 |
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 177 |
], |
| 178 |
self::ITEMS_COUNT_KEY => [ |
| 179 |
'description' => 'Terms per request', |
| 180 |
'type' => 'integer', |
| 181 |
'required' => false, |
| 182 |
'default' => self::MAX_RESPONSE_COUNT, |
| 183 |
], |
| 184 |
self::HIDE_EMPTY_KEY => [ |
| 185 |
'description' => 'Whether to include only public terms', |
| 186 |
'type' => 'boolean', |
| 187 |
'required' => false, |
| 188 |
'default' => false, |
| 189 |
], |
| 190 |
self::META_QUERY_KEY => [ |
| 191 |
'description' => 'WP_Query meta_query array', |
| 192 |
'type' => 'array', |
| 193 |
'required' => false, |
| 194 |
'default' => null, |
| 195 |
'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 196 |
], |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
protected static function get_allowed_param_keys(): array { |
| 201 |
return [ |
| 202 |
self::EXCLUDED_TYPE_KEY, |
| 203 |
self::INCLUDED_TYPE_KEY, |
| 204 |
self::KEYS_CONVERSION_MAP_KEY, |
| 205 |
self::META_QUERY_KEY, |
| 206 |
self::TAX_QUERY_KEY, |
| 207 |
self::ITEMS_COUNT_KEY, |
| 208 |
]; |
| 209 |
} |
| 210 |
|
| 211 |
protected static function get_keys_to_encode(): array { |
| 212 |
return [ |
| 213 |
self::EXCLUDED_TYPE_KEY, |
| 214 |
self::INCLUDED_TYPE_KEY, |
| 215 |
self::KEYS_CONVERSION_MAP_KEY, |
| 216 |
self::META_QUERY_KEY, |
| 217 |
self::TAX_QUERY_KEY, |
| 218 |
]; |
| 219 |
} |
| 220 |
} |
| 221 |
|