| @@ -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. |
| @@ -54,14 +60,17 @@ | ||
| 54 | 60 | ], |
| 55 | 61 | ], 200 ); |
| 56 | 62 | } |
| 57 | 63 | |
| 58 | - $keys_format_map = $params[ self::KEYS_CONVERSION_MAP_KEY ]; | |
| 64 | + $keys_format_map = $this->filter_keys_conversion_map( | |
| 65 | + $params[ self::KEYS_CONVERSION_MAP_KEY ] ?? self::ALLOWED_KEYS_CONVERSION_MAP, | |
| 66 | + self::ALLOWED_KEYS_CONVERSION_MAP | |
| 67 | + ); | |
| 59 | 68 | $requested_count = $params[ self::ITEMS_COUNT_KEY ] ?? 0; |
| 60 | 69 | $validated_count = max( $requested_count, 1 ); |
| 61 | 70 | $post_count = min( $validated_count, self::MAX_RESPONSE_COUNT ); |
| 62 | 71 | $is_public_only = $params[ self::IS_PUBLIC_KEY ] ?? true; |
| 63 | - $post_types = $this->get_post_types_from_params( $params ); | |
| 72 | + $post_types = $this->get_post_types_from_params( $request ); | |
| 64 | 73 | |
| 65 | 74 | $query_args = [ |
| 66 | 75 | 'post_type' => array_keys( $post_types ), |
| 67 | 76 | 'numberposts' => $post_count, |
| @@ -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', |
| @@ -216,11 +235,11 @@ | ||
| 216 | 235 | self::TAX_QUERY_KEY, |
| 217 | 236 | ]; |
| 218 | 237 | } |
| 219 | 238 | |
| 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 ]; | |
| 239 | + private function get_post_types_from_params( \WP_REST_Request $request ) { | |
| 240 | + $included_types = $request->get_param( self::INCLUDED_TYPE_KEY ); | |
| 241 | + $excluded_types = $request->get_param( self::EXCLUDED_TYPE_KEY ); | |
| 223 | 242 | $post_type_query_args = [ |
| 224 | 243 | 'public' => true, |
| 225 | 244 | ]; |
| 226 | 245 | |