PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-dev1
Elementor Website Builder – more than just a page builder v4.1.0-dev1
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 / post-query.php

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

236 lines 6.9 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 Post_Query extends Base {
13 const ENDPOINT = 'post';
14 const SEARCH_FILTER_ACCEPTED_ARGS = 2;
15 const DEFAULT_FORBIDDEN_POST_TYPES = [ 'e-floating-buttons', 'e-landing-page', 'elementor_library', 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset' ];
16
17 /**
18 * @param string $search_term The original search query.
19 * @param \WP_Query $wp_query The WP_Query instance.
20 * @return string Modified search query.
21 */
22 public function customize_post_query( string $search_term, \WP_Query $wp_query ) {
23 $term = $wp_query->get( 'search_term' ) ?? '';
24 $is_custom_search = $wp_query->get( 'custom_search' ) ?? false;
25
26 if ( $is_custom_search && ! empty( $term ) ) {
27 $escaped = esc_sql( $term );
28 $search_term .= ' AND (';
29 $search_term .= "post_title LIKE '%{$escaped}%'";
30 if ( ctype_digit( $term ) ) {
31 $search_term .= ' OR ID = ' . intval( $term );
32 } else {
33 $search_term .= " OR ID LIKE '%{$escaped}%'";
34 }
35 $search_term .= ')';
36 }
37
38 return $search_term;
39 }
40
41 /**
42 * @param \WP_REST_Request $request
43 * @return \WP_REST_Response
44 */
45 protected function get( \WP_REST_Request $request ) {
46 $params = $request->get_params();
47 $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
48
49 if ( empty( $term ) ) {
50 return new \WP_REST_Response( [
51 'success' => true,
52 'data' => [
53 'value' => [],
54 ],
55 ], 200 );
56 }
57
58 $keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ];
59 $requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
60 $validated_count = max( $requested_count, 1 );
61 $post_count = min( $validated_count, self::MAX_RESPONSE_COUNT );
62 $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true;
63 $post_types = $this->get_post_types_from_params( $params );
64
65 $query_args = [
66 'post_type' => array_keys( $post_types ),
67 'numberposts' => $post_count,
68 'suppress_filters' => false,
69 'custom_search' => true,
70 'search_term' => $term,
71 'post_status' => $is_public_only ? 'publish' : 'any',
72 'orderby' => 'ID',
73 'order' => 'ASC',
74 ];
75
76 if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
77 $query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
78 }
79
80 if ( ! empty( $params[ self::TAX_QUERY_KEY ] ) && is_array( $params[ self::TAX_QUERY_KEY ] ) ) {
81 $query_args['tax_query'] = $params[ self::TAX_QUERY_KEY ];
82 }
83
84 $this->add_filter_to_customize_query();
85 $posts = new Collection( get_posts( $query_args ) );
86 $this->remove_filter_to_customize_query();
87
88 $post_type_labels = ( new Collection( $post_types ) )
89 ->map( function ( $pt ) {
90 return $pt->label;
91 } )
92 ->all();
93
94 return new \WP_REST_Response( [
95 'success' => true,
96 'data' => [
97 'value' => $posts
98 ->map( function ( $post ) use ( $keys_format_map, $post_type_labels ) {
99 $post_object = (array) $post;
100
101 if ( isset( $post_object['post_type'] ) ) {
102 $pt_name = $post_object['post_type'];
103 if ( isset( $post_type_labels[ $pt_name ] ) ) {
104 $post_object['post_type'] = $post_type_labels[ $pt_name ];
105 }
106 }
107
108 return $this->translate_keys( $post_object, $keys_format_map );
109 } )
110 ->all(),
111 ],
112 ], 200 );
113 }
114
115 /**
116 * @return void
117 */
118 private function add_filter_to_customize_query() {
119 $priority = self::SEARCH_FILTER_PRIORITY;
120 $accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
121
122 add_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
123 }
124
125 /**
126 * @return void
127 */
128 private function remove_filter_to_customize_query() {
129 $priority = self::SEARCH_FILTER_PRIORITY;
130 $accepted_args = self::SEARCH_FILTER_ACCEPTED_ARGS;
131
132 remove_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
133 }
134
135 protected function get_endpoint_registration_args(): array {
136 return [
137 self::INCLUDED_TYPE_KEY => [
138 'description' => 'Included post types',
139 'type' => 'array',
140 'required' => false,
141 'default' => null,
142 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
143 ],
144 self::EXCLUDED_TYPE_KEY => [
145 'description' => 'Post type to exclude',
146 'type' => 'array',
147 'required' => false,
148 'default' => self::DEFAULT_FORBIDDEN_POST_TYPES,
149 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
150 ],
151 self::SEARCH_TERM_KEY => [
152 'description' => 'Posts to search',
153 'type' => 'string',
154 'required' => false,
155 'default' => '',
156 'sanitize_callback' => 'sanitize_text_field',
157 ],
158 self::KEYS_CONVERSION_MAP_KEY => [
159 'description' => 'Specify keys to extract and convert, i.e. ["key_1" => "new_key_1"].',
160 'type' => 'array',
161 'required' => false,
162 'default' => [
163 'ID' => 'id',
164 'post_title' => 'label',
165 'post_type' => 'groupLabel',
166 ],
167 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
168 ],
169 self::ITEMS_COUNT_KEY => [
170 'description' => 'Posts per page',
171 'type' => 'integer',
172 'required' => false,
173 'default' => self::MAX_RESPONSE_COUNT,
174 ],
175 self::IS_PUBLIC_KEY => [
176 'description' => 'Whether to include only public post types',
177 'type' => 'boolean',
178 'required' => false,
179 'default' => true,
180 ],
181 self::META_QUERY_KEY => [
182 'description' => 'WP_Query meta_query array',
183 'type' => 'array',
184 'required' => false,
185 'default' => null,
186 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
187 ],
188 self::TAX_QUERY_KEY => [
189 'description' => 'WP_Query tax_query array',
190 'type' => 'array',
191 'required' => false,
192 'default' => null,
193 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
194 ],
195 ];
196 }
197
198 protected static function get_allowed_param_keys(): array {
199 return [
200 self::EXCLUDED_TYPE_KEY,
201 self::INCLUDED_TYPE_KEY,
202 self::KEYS_CONVERSION_MAP_KEY,
203 self::META_QUERY_KEY,
204 self::TAX_QUERY_KEY,
205 self::IS_PUBLIC_KEY,
206 self::ITEMS_COUNT_KEY,
207 ];
208 }
209
210 protected static function get_keys_to_encode(): array {
211 return [
212 self::EXCLUDED_TYPE_KEY,
213 self::INCLUDED_TYPE_KEY,
214 self::KEYS_CONVERSION_MAP_KEY,
215 self::META_QUERY_KEY,
216 self::TAX_QUERY_KEY,
217 ];
218 }
219
220 private function get_post_types_from_params( $params ) {
221 $included_types = $params[ self::INCLUDED_TYPE_KEY ];
222 $excluded_types = $params[ self::EXCLUDED_TYPE_KEY ];
223 $post_type_query_args = [
224 'public' => true,
225 ];
226
227 $post_types = get_post_types( $post_type_query_args, 'objects' );
228
229 return Collection::make( $post_types )
230 ->filter( function ( $slug, $post_type ) use ( $included_types, $excluded_types ) {
231 return ( empty( $included_types ) || in_array( $post_type, $included_types ) ) &&
232 ( empty( $excluded_types ) || ! in_array( $post_type, $excluded_types ) );
233 } )->all();
234 }
235 }
236