| @@ -30,10 +30,15 @@ | ||
| 30 | 30 | $is_custom_search = $wp_query->get( 'custom_search' ) ?? false; |
| 31 | 31 | |
| 32 | 32 | if ( $is_custom_search && ! empty( $term ) ) { |
| 33 | 33 | $escaped = esc_sql( $term ); |
| 34 | + $search_in_content = $wp_query->get( self::SEARCH_IN_CONTENT_KEY ) ?? false; | |
| 34 | 35 | $search_term .= ' AND ('; |
| 35 | 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 | + } | |
| 36 | 41 | if ( ctype_digit( $term ) ) { |
| 37 | 42 | $search_term .= ' OR ID = ' . intval( $term ); |
| 38 | 43 | } else { |
| 39 | 44 | $search_term .= " OR ID LIKE '%{$escaped}%'"; |
| @@ -51,17 +56,8 @@ | ||
| 51 | 56 | protected function get( \WP_REST_Request $request ) { |
| 52 | 57 | $params = $request->get_params(); |
| 53 | 58 | $term = trim( $params[ self::SEARCH_TERM_KEY ] ?? '' ); |
| 54 | 59 | |
| 55 | - if ( empty( $term ) ) { | |
| 56 | - return new \WP_REST_Response( [ | |
| 57 | - 'success' => true, | |
| 58 | - 'data' => [ | |
| 59 | - 'value' => [], | |
| 60 | - ], | |
| 61 | - ], 200 ); | |
| 62 | - } | |
| 63 | - | |
| 64 | 60 | $keys_format_map = $this->filter_keys_conversion_map( |
| 65 | 61 | $params[ self::KEYS_CONVERSION_MAP_KEY ] ?? self::ALLOWED_KEYS_CONVERSION_MAP, |
| 66 | 62 | self::ALLOWED_KEYS_CONVERSION_MAP |
| 67 | 63 | ); |
| @@ -71,18 +67,27 @@ | ||
| 71 | 67 | $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true; |
| 72 | 68 | $post_types = $this->get_post_types_from_params( $request ); |
| 73 | 69 | |
| 74 | 70 | $query_args = [ |
| 75 | - 'post_type' => array_keys( $post_types ), | |
| 76 | - 'numberposts' => $post_count, | |
| 71 | + 'post_type' => array_keys( $post_types ), | |
| 72 | + 'numberposts' => $post_count, | |
| 77 | 73 | 'suppress_filters' => false, |
| 78 | - 'custom_search' => true, | |
| 79 | - 'search_term' => $term, | |
| 80 | - 'post_status' => $is_public_only ? 'publish' : 'any', | |
| 81 | - 'orderby' => 'ID', | |
| 82 | - 'order' => 'ASC', | |
| 74 | + 'custom_search' => true, | |
| 75 | + 'post_status' => $is_public_only ? 'publish' : 'any', | |
| 76 | + 'orderby' => 'modified', | |
| 77 | + 'order' => 'DESC', | |
| 83 | 78 | ]; |
| 84 | 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 | + | |
| 85 | 90 | if ( ! empty( $params[ self::META_QUERY_KEY ] ) && is_array( $params[ self::META_QUERY_KEY ] ) ) { |
| 86 | 91 | $query_args['meta_query'] = $params[ self::META_QUERY_KEY ]; |
| 87 | 92 | } |
| 88 | 93 | |
| @@ -210,8 +215,14 @@ | ||
| 210 | 215 | 'required' => false, |
| 211 | 216 | 'default' => null, |
| 212 | 217 | 'sanitize_callback' => fn ( ...$args ) => self::sanitize_string_array( ...$args ), |
| 213 | 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 | + ], | |
| 214 | 225 | ]; |
| 215 | 226 | } |
| 216 | 227 | |
| 217 | 228 | protected static function get_allowed_param_keys(): array { |
| @@ -222,8 +233,9 @@ | ||
| 222 | 233 | self::META_QUERY_KEY, |
| 223 | 234 | self::TAX_QUERY_KEY, |
| 224 | 235 | self::IS_PUBLIC_KEY, |
| 225 | 236 | self::ITEMS_COUNT_KEY, |
| 237 | + self::SEARCH_IN_CONTENT_KEY, | |
| 226 | 238 | ]; |
| 227 | 239 | } |
| 228 | 240 | |
| 229 | 241 | protected static function get_keys_to_encode(): array { |