PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
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 +58 -27 4.1.0-dev14.3.0-beta3 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,38 @@
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,
71 + 'post_type' => array_keys( $post_types ),
72 + 'numberposts' => $post_count,
68 73 '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 + 'custom_search' => true,
75 + 'post_status' => $is_public_only ? 'publish' : 'any',
76 + 'orderby' => 'modified',
77 + 'order' => 'DESC',
74 78 ];
75 79
80 + // for non-admins (contributors), filter by author for private posts
81 + if ( ! $is_public_only && ! current_user_can( 'read_private_posts' ) ) {
82 + $query_args['author'] = get_current_user_id();
83 + }
84 +
85 + if ( ! empty( $term ) ) {
86 + $query_args['search_term'] = $term;
87 + $query_args[ self::SEARCH_IN_CONTENT_KEY ] = $params[ self::SEARCH_IN_CONTENT_KEY ] ?? false;
88 + }
89 +
76 90 if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) {
77 91 $query_args['meta_query'] = $params[ self::META_QUERY_KEY ];
78 92 }
79 93
@@ -94,18 +108,24 @@
94 108 return new \WP_REST_Response( [
95 109 'success' => true,
96 110 'data' => [
97 111 'value' => $posts
112 + ->filter( function ( $post ) {
113 + return current_user_can( 'read_post', $post->ID );
114 + } )
98 115 ->map( function ( $post ) use ( $keys_format_map, $post_type_labels ) {
99 - $post_object = (array) $post;
116 + $post_type_label = $post->post_type;
100 117
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 - }
118 + if ( isset( $post_type_labels[ $post->post_type ] ) ) {
119 + $post_type_label = $post_type_labels[ $post->post_type ];
106 120 }
107 121
122 + $post_object = [
123 + 'ID' => $post->ID,
124 + 'post_title' => $post->post_title,
125 + 'post_type' => $post_type_label,
126 + ];
127 +
108 128 return $this->translate_keys( $post_object, $keys_format_map );
109 129 } )
110 130 ->all(),
111 131 ],
@@ -131,8 +151,12 @@
131 151
132 152 remove_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args );
133 153 }
134 154
155 + protected function permission_check( \WP_REST_Request $request ): bool {
156 + return current_user_can( 'edit_posts' );
157 + }
158 +
135 159 protected function get_endpoint_registration_args(): array {
136 160 return [
137 161 self::INCLUDED_TYPE_KEY => [
138 162 'description' => 'Included post types',
@@ -191,8 +215,14 @@
191 215 'required' => false,
192 216 'default' => null,
193 217 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ),
194 218 ],
219 + self::SEARCH_IN_CONTENT_KEY => [
220 + 'description' => 'Whether to search within post content and excerpt in addition to title',
221 + 'type' => 'boolean',
222 + 'required' => false,
223 + 'default' => false,
224 + ],
195 225 ];
196 226 }
197 227
198 228 protected static function get_allowed_param_keys(): array {
@@ -203,8 +233,9 @@
203 233 self::META_QUERY_KEY,
204 234 self::TAX_QUERY_KEY,
205 235 self::IS_PUBLIC_KEY,
206 236 self::ITEMS_COUNT_KEY,
237 + self::SEARCH_IN_CONTENT_KEY,
207 238 ];
208 239 }
209 240
210 241 protected static function get_keys_to_encode(): array {
@@ -216,11 +247,11 @@
216 247 self::TAX_QUERY_KEY,
217 248 ];
218 249 }
219 250
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 ];
251 + private function get_post_types_from_params( \WP_REST_Request $request ) {
252 + $included_types = $request->get_param( self::INCLUDED_TYPE_KEY );
253 + $excluded_types = $request->get_param( self::EXCLUDED_TYPE_KEY );
223 254 $post_type_query_args = [
224 255 'public' => true,
225 256 ];
226 257