| @@ -12,14 +12,8 @@ | ||
| 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 | - ]; | |
| 22 | 16 | |
| 23 | 17 | /** |
| 24 | 18 | * @param string $search_term The original search query. |
| 25 | 19 | * @param \WP_Query $wp_query The WP_Query instance. |
| @@ -30,15 +24,10 @@ | ||
| 30 | 24 | $is_custom_search = $wp_query->get( 'custom_search' ) ?? false; |
| 31 | 25 | |
| 32 | 26 | if ( $is_custom_search && ! empty( $term ) ) { |
| 33 | 27 | $escaped = esc_sql( $term ); |
| 34 | - $search_in_content = $wp_query->get( self::SEARCH_IN_CONTENT_KEY ) ?? false; | |
| 35 | 28 | $search_term .= ' AND ('; |
| 36 | 29 | $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 | - } | |
| 41 | 30 | if ( ctype_digit( $term ) ) { |
| 42 | 31 | $search_term .= ' OR ID = ' . intval( $term ); |
| 43 | 32 | } else { |
| 44 | 33 | $search_term .= " OR ID LIKE '%{$escaped}%'"; |
| @@ -56,33 +45,35 @@ | ||
| 56 | 45 | protected function get( \WP_REST_Request $request ) { |
| 57 | 46 | $params = $request->get_params(); |
| 58 | 47 | $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' ); |
| 59 | 48 | |
| 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 | - ); | |
| 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 ]; | |
| 64 | 59 | $requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0; |
| 65 | 60 | $validated_count = max( $requested_count, 1 ); |
| 66 | 61 | $post_count = min( $validated_count, self::MAX_RESPONSE_COUNT ); |
| 67 | 62 | $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true; |
| 68 | - $post_types = $this->get_post_types_from_params( $request ); | |
| 63 | + $post_types = $this->get_post_types_from_params( $params ); | |
| 69 | 64 | |
| 70 | 65 | $query_args = [ |
| 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', | |
| 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', | |
| 78 | 74 | ]; |
| 79 | 75 | |
| 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 | - | |
| 85 | 76 | if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) { |
| 86 | 77 | $query_args['meta_query'] = $params[ self::META_QUERY_KEY ]; |
| 87 | 78 | } |
| 88 | 79 | |
| @@ -103,24 +94,18 @@ | ||
| 103 | 94 | return new \WP_REST_Response( [ |
| 104 | 95 | 'success' => true, |
| 105 | 96 | 'data' => [ |
| 106 | 97 | 'value' => $posts |
| 107 | - ->filter( function ( $post ) { | |
| 108 | - return current_user_can( 'read_post', $post->ID ); | |
| 109 | - } ) | |
| 110 | 98 | ->map( function ( $post ) use ( $keys_format_map, $post_type_labels ) { |
| 111 | - $post_type_label = $post->post_type; | |
| 99 | + $post_object = (array) $post; | |
| 112 | 100 | |
| 113 | - if ( isset( $post_type_labels[ $post->post_type ] ) ) { | |
| 114 | - $post_type_label = $post_type_labels[ $post->post_type ]; | |
| 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 | + } | |
| 115 | 106 | } |
| 116 | 107 | |
| 117 | - $post_object = [ | |
| 118 | - 'ID' => $post->ID, | |
| 119 | - 'post_title' => $post->post_title, | |
| 120 | - 'post_type' => $post_type_label, | |
| 121 | - ]; | |
| 122 | - | |
| 123 | 108 | return $this->translate_keys( $post_object, $keys_format_map ); |
| 124 | 109 | } ) |
| 125 | 110 | ->all(), |
| 126 | 111 | ], |
| @@ -146,12 +131,8 @@ | ||
| 146 | 131 | |
| 147 | 132 | remove_filter( 'posts_search', [ $this, 'customize_post_query' ], $priority, $accepted_args ); |
| 148 | 133 | } |
| 149 | 134 | |
| 150 | - protected function permission_check( \WP_REST_Request $request ): bool { | |
| 151 | - return current_user_can( 'edit_posts' ); | |
| 152 | - } | |
| 153 | - | |
| 154 | 135 | protected function get_endpoint_registration_args(): array { |
| 155 | 136 | return [ |
| 156 | 137 | self::INCLUDED_TYPE_KEY => [ |
| 157 | 138 | 'description' => 'Included post types', |
| @@ -210,14 +191,8 @@ | ||
| 210 | 191 | 'required' => false, |
| 211 | 192 | 'default' => null, |
| 212 | 193 | 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 213 | 194 | ], |
| 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 | - ], | |
| 220 | 195 | ]; |
| 221 | 196 | } |
| 222 | 197 | |
| 223 | 198 | protected static function get_allowed_param_keys(): array { |
| @@ -228,9 +203,8 @@ | ||
| 228 | 203 | self::META_QUERY_KEY, |
| 229 | 204 | self::TAX_QUERY_KEY, |
| 230 | 205 | self::IS_PUBLIC_KEY, |
| 231 | 206 | self::ITEMS_COUNT_KEY, |
| 232 | - self::SEARCH_IN_CONTENT_KEY, | |
| 233 | 207 | ]; |
| 234 | 208 | } |
| 235 | 209 | |
| 236 | 210 | protected static function get_keys_to_encode(): array { |
| @@ -242,11 +216,11 @@ | ||
| 242 | 216 | self::TAX_QUERY_KEY, |
| 243 | 217 | ]; |
| 244 | 218 | } |
| 245 | 219 | |
| 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 ); | |
| 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 ]; | |
| 249 | 223 | $post_type_query_args = [ |
| 250 | 224 | 'public' => true, |
| 251 | 225 | ]; |
| 252 | 226 | |