Make WordPress Core

Changeset 54123


Ignore:
Timestamp:
09/11/2022 09:10:31 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add support for searching resources by id.

This brings support for the include and exclude collection parameters to the Search Controller. This can be used to find an item by id when it's subtype is unknown.

Props kadamwhite.
Fixes #56546.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r53760 r54123  
    332332                );
    333333
     334                $query_params['exclude'] = array(
     335                        'description' => __( 'Ensure result set excludes specific IDs.' ),
     336                        'type'        => 'array',
     337                        'items'       => array(
     338                                'type' => 'integer',
     339                        ),
     340                        'default'     => array(),
     341                );
     342
     343                $query_params['include'] = array(
     344                        'description' => __( 'Limit result set to specific IDs.' ),
     345                        'type'        => 'array',
     346                        'items'       => array(
     347                                'type' => 'integer',
     348                        ),
     349                        'default'     => array(),
     350                );
     351
    334352                return $query_params;
    335353        }
  • trunk/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php

    r53676 r54123  
    7070                }
    7171
     72                if ( ! empty( $request['exclude'] ) ) {
     73                        $query_args['post__not_in'] = $request['exclude'];
     74                }
     75
     76                if ( ! empty( $request['include'] ) ) {
     77                        $query_args['post__in'] = $request['include'];
     78                }
     79
    7280                /**
    7381                 * Filters the query arguments for a REST API search request.
  • trunk/src/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php

    r53877 r54123  
    6969                if ( ! empty( $request['search'] ) ) {
    7070                        $query_args['search'] = $request['search'];
     71                }
     72
     73                if ( ! empty( $request['exclude'] ) ) {
     74                        $query_args['exclude'] = $request['exclude'];
     75                }
     76
     77                if ( ! empty( $request['include'] ) ) {
     78                        $query_args['include'] = $request['include'];
    7179                }
    7280
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r53760 r54123  
    820820        }
    821821
     822        /**
     823         * @ticket 56546
     824         */
     825        public function test_get_items_search_posts_include_ids() {
     826                $response = $this->do_request_with_params(
     827                        array(
     828                                'include' => array_slice( self::$my_title_post_ids, 1, 2 ),
     829                        )
     830                );
     831
     832                $this->assertSame( 200, $response->get_status() );
     833                $this->assertSameSets(
     834                        array( self::$my_title_post_ids[1], self::$my_title_post_ids[2] ),
     835                        wp_list_pluck( $response->get_data(), 'id' )
     836                );
     837        }
     838
     839        /**
     840         * @ticket 56546
     841         */
     842        public function test_get_items_search_posts_exclude_ids() {
     843                $response = $this->do_request_with_params(
     844                        array(
     845                                'exclude' => self::$my_title_page_ids,
     846                        )
     847                );
     848
     849                $this->assertSame( 200, $response->get_status() );
     850                $this->assertSameSets(
     851                        array_merge(
     852                                self::$my_title_post_ids,
     853                                self::$my_content_post_ids
     854                        ),
     855                        wp_list_pluck( $response->get_data(), 'id' )
     856                );
     857        }
     858
     859        /**
     860         * @ticket 56546
     861         */
     862        public function test_get_items_search_terms_include_ids() {
     863                $response = $this->do_request_with_params(
     864                        array(
     865                                'include' => self::$my_tag_id,
     866                                'type'    => 'term',
     867                        )
     868                );
     869
     870                $this->assertSame( 200, $response->get_status() );
     871                $this->assertSameSets(
     872                        array( self::$my_tag_id ),
     873                        wp_list_pluck( $response->get_data(), 'id' )
     874                );
     875        }
     876
     877        /**
     878         * @ticket 56546
     879         */
     880        public function test_get_items_search_terms_exclude_ids() {
     881                $response = $this->do_request_with_params(
     882                        array(
     883                                // "1" is the default category.
     884                                'exclude' => array( 1, self::$my_tag_id ),
     885                                'type'    => 'term',
     886                        )
     887                );
     888
     889                $this->assertSame( 200, $response->get_status() );
     890                $this->assertSameSets(
     891                        array( self::$my_category_id ),
     892                        wp_list_pluck( $response->get_data(), 'id' )
     893                );
     894        }
     895
    822896}
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r54083 r54123  
    92709270                            },
    92719271                            "required": false
     9272                        },
     9273                        "exclude": {
     9274                            "description": "Ensure result set excludes specific IDs.",
     9275                            "type": "array",
     9276                            "items": {
     9277                                "type": "integer"
     9278                            },
     9279                            "default": [],
     9280                            "required": false
     9281                        },
     9282                        "include": {
     9283                            "description": "Limit result set to specific IDs.",
     9284                            "type": "array",
     9285                            "items": {
     9286                                "type": "integer"
     9287                            },
     9288                            "default": [],
     9289                            "required": false
    92729290                        }
    92739291                    }
Note: See TracChangeset for help on using the changeset viewer.