PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.1
Elementor Website Builder – more than just a page builder v4.2.1
4.3.0-beta3 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 All 452 releases
← All changes | modules/wp-rest/classes/post-query.php +54 -28 4.1.04.2.1 View file →
@@ -12,8 +12,14 @@
12 12 class Post_Query extends Base {
13 13 const ENDPOINT = 'post';
14 14 const SEARCH_FILTER_ACCEPTED_ARGS = 2;
15 15 const DEFAULT_FORBIDDEN_POST_TYPES = [ 'e-floating-buttons', 'e-landing-page', 'elementor_library', 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset' ];
16 + const SEARCH_IN_CONTENT_KEY = 'search_in_content';
17 + const ALLOWED_KEYS_CONVERSION_MAP = [
18 + 'ID' => 'id',
19 + 'post_title' => 'label',
20 + 'post_type' => 'groupLabel',
21 + ];
16 22
17 23 /**
18 24 * @param string $search_term The original search query.
19 25 * @param \WP_Query $wp_query The WP_Query instance.
@@ -24,10 +30,15 @@
24 30 $is_custom_search = $wp_query->get( 'custom_search' ) ?? false;
25 31
26 32 if ( $is_custom_search && ! empty( $term ) ) {
27 33 $escaped = esc_sql( $term );
34 + $search_in_content = $wp_query->get( self::SEARCH_IN_CONTENT_KEY ) ?? false;
28 35 $search_term .= ' AND (';
29 36 $search_term .= "post_title LIKE '%{$escaped}%'";
37 + if ( $search_in_content ) {
38 + $search_term .= " OR post_content LIKE '%{$escaped}%'";
39 + $search_term .= " OR post_excerpt LIKE '%{$escaped}%'";
40 + }
30 41 if ( ctype_digit( $term ) ) {
31 42 $search_term .= ' OR ID = ' . intval( $term );
32 43 } else {
33 44 $search_term .= " OR ID LIKE '%{$escaped}%'";
@@ -45,35 +56,33 @@
45 56 protected function get( \WP_REST_Request $request ) {
46 57 $params = $request->get_params();
47 58 $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' );
48 59
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 ];
60 + $keys_format_map = $this->filter_keys_conversion_map(
61 + $params[ self::KEYS_CONVERSION_MAP_KEY ] ?? self::ALLOWED_KEYS_CONVERSION_MAP,
62 + self::ALLOWED_KEYS_CONVERSION_MAP
63 + );
59 64 $requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0;
60 65 $validated_count = max( $requested_count, 1 );
61 66 $post_count = min( $validated_count, self::MAX_RESPONSE_COUNT );
62 67 $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true;
63 - $post_types = $this->get_post_types_from_params( $params );
68 + $post_types = $this->get_post_types_from_params( $request );
64 69
65 70 $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',
71 + 'post_type' => array_keys( $post_types ),
72 + 'numberposts' => $post_count,
73 + 'suppress_filters' => false,
74 + 'custom_search' => true,
75 + 'post_status' => $is_public_only ? 'publish' : 'any',
76 + 'orderby' => 'modified',
77 + 'order' => 'DESC',
74 78 ];
75 79
80 + if ( ! empty( $term ) ) {
81 + $query_args['search_term'] = $term;
82 + $query_args[ self::SEARCH_IN_CONTENT_KEY ] = $params[ self::SEARCH_IN_CONTENT_KEY ] ?? false;
83 + }
84 +
76 85 if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
77 86 $query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
78 87 }
79 88
@@ -94,18 +103,24 @@
94 103 return new \WP_REST_Response( [
95 104 'success' => true,
96 105 'data' => [
97 106 'value' => $posts
107 + ->filter( function ( $post ) {
108 + return current_user_can( 'read_post', $post->ID );
109 + } )
98 110 ->map( function ( $post ) use ( $keys_format_map, $post_type_labels ) {
99 - $post_object = (array) $post;
111 + $post_type_label = $post->post_type;
100 112
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 - }
113 + if ( isset( $post_type_labels[ $post->post_type ] ) ) {
114 + $post_type_label = $post_type_labels[ $post->post_type ];
106 115 }
107 116
117 + $post_object = [
118 + 'ID' => $post->ID,
119 + 'post_title' => $post->post_title,
120 + 'post_type' => $post_type_label,
121 + ];
122 +
108 123 return $this->translate_keys( $post_object, $keys_format_map );
109 124 } )
110 125 ->all(),
111 126 ],
@@ -131,8 +146,12 @@
131 146
132 147 remove_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
133 148 }
134 149
150 + protected function permission_check( \WP_REST_Request $request ): bool {
151 + return current_user_can( 'edit_posts' );
152 + }
153 +
135 154 protected function get_endpoint_registration_args(): array {
136 155 return [
137 156 self::INCLUDED_TYPE_KEY => [
138 157 'description' => 'Included post types',
@@ -191,8 +210,14 @@
191 210 'required' => false,
192 211 'default' => null,
193 212 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
194 213 ],
214 + self::SEARCH_IN_CONTENT_KEY => [
215 + 'description' => 'Whether to search within post content and excerpt in addition to title',
216 + 'type' => 'boolean',
217 + 'required' => false,
218 + 'default' => false,
219 + ],
195 220 ];
196 221 }
197 222
198 223 protected static function get_allowed_param_keys(): array {
@@ -203,8 +228,9 @@
203 228 self::META_QUERY_KEY,
204 229 self::TAX_QUERY_KEY,
205 230 self::IS_PUBLIC_KEY,
206 231 self::ITEMS_COUNT_KEY,
232 + self::SEARCH_IN_CONTENT_KEY,
207 233 ];
208 234 }
209 235
210 236 protected static function get_keys_to_encode(): array {
@@ -216,11 +242,11 @@
216 242 self::TAX_QUERY_KEY,
217 243 ];
218 244 }
219 245
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 ];
246 + private function get_post_types_from_params( \WP_REST_Request $request ) {
247 + $included_types = $request->get_param( self::INCLUDED_TYPE_KEY );
248 + $excluded_types = $request->get_param( self::EXCLUDED_TYPE_KEY );
223 249 $post_type_query_args = [
224 250 'public' => true,
225 251 ];
226 252