Make WordPress Core

Changeset 49103


Ignore:
Timestamp:
10/08/2020 01:30:25 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Introduce search term handler.

This allows for clients to search the available terms via the /wp/v2/search endpoint by using a type=term query parameter.

Fixes #51458.
Props andraganescu, zieladam, noisysocks, TimothyBlynJacobs.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r49082 r49103  
    269269        $controller->register_routes();
    270270
     271        $search_handlers = array(
     272                new WP_REST_Post_Search_Handler(),
     273                new WP_REST_Term_Search_Handler(),
     274        );
     275
    271276        /**
    272277         * Filters the search handlers to use in the REST search controller.
     
    278283         *                               Default is only a handler for posts.
    279284         */
    280         $search_handlers = apply_filters( 'wp_rest_search_handlers', array( new WP_REST_Post_Search_Handler() ) );
     285        $search_handlers = apply_filters( 'wp_rest_search_handlers', $search_handlers );
    281286
    282287        $controller = new WP_REST_Search_Controller( $search_handlers );
  • trunk/src/wp-settings.php

    r48334 r49103  
    267267require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-search-handler.php';
    268268require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php';
     269require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-term-search-handler.php';
    269270require ABSPATH . WPINC . '/sitemaps.php';
    270271require ABSPATH . WPINC . '/sitemaps/class-wp-sitemaps.php';
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r48939 r49103  
    3636
    3737        /**
     38         * Categories.
     39         *
     40         * @var int
     41         */
     42        private static $my_category_id;
     43
     44        /**
     45         * Tags.
     46         *
     47         * @var int
     48         */
     49        private static $my_tag_id;
     50
     51        /**
    3852         * Create fake data before our tests run.
    3953         *
     
    6175                        array(
    6276                                'post_content' => 'my-foocontent',
     77                        )
     78                );
     79
     80                self::$my_category_id = $factory->term->create(
     81                        array(
     82                                'taxonomy' => 'category',
     83                                'name'     => 'Test Category',
     84                        )
     85                );
     86
     87                self::$my_tag_id = $factory->term->create(
     88                        array(
     89                                'taxonomy' => 'post_tag',
     90                                'name'     => 'Test Tag',
    6391                        )
    6492                );
     
    77105                foreach ( $post_ids as $post_id ) {
    78106                        wp_delete_post( $post_id, true );
     107                }
     108
     109                $term_ids = array(
     110                        self::$my_category_id,
     111                        self::$my_tag_id,
     112                );
     113
     114                foreach ( $term_ids as $term_id ) {
     115                        wp_delete_term( $term_id, true );
    79116                }
    80117        }
     
    516553
    517554        /**
     555         * Search through terms of any type.
     556         *
     557         * @ticket 51458
     558         */
     559        public function test_get_items_search_type_term() {
     560                $response = $this->do_request_with_params(
     561                        array(
     562                                'per_page' => 100,
     563                                'type'     => 'term',
     564                        )
     565                );
     566                $this->assertEquals( 200, $response->get_status() );
     567                $this->assertEqualSets(
     568                        array(
     569                                0 => 1, // That is the default category.
     570                                self::$my_category_id,
     571                                self::$my_tag_id,
     572                        ),
     573                        wp_list_pluck( $response->get_data(), 'id' )
     574                );
     575        }
     576
     577        /**
     578         * Search through terms of subtype 'category'.
     579         *
     580         * @ticket 51458
     581         */
     582        public function test_get_items_search_type_term_subtype_category() {
     583                $response = $this->do_request_with_params(
     584                        array(
     585                                'per_page' => 100,
     586                                'type'     => 'term',
     587                                'subtype'  => 'category',
     588                        )
     589                );
     590
     591                $this->assertEquals( 200, $response->get_status() );
     592                $this->assertEqualSets(
     593                        array(
     594                                0 => 1, // That is the default category.
     595                                self::$my_category_id,
     596                        ),
     597                        wp_list_pluck( $response->get_data(), 'id' )
     598                );
     599        }
     600
     601        /**
     602         * Search through posts of an invalid post type.
     603         *
     604         * @ticket 51458
     605         */
     606        public function test_get_items_search_term_subtype_invalid() {
     607                $response = $this->do_request_with_params(
     608                        array(
     609                                'per_page' => 100,
     610                                'type'     => 'term',
     611                                'subtype'  => 'invalid',
     612                        )
     613                );
     614
     615                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     616        }
     617
     618        /**
     619         * Search through posts and pages.
     620         *
     621         * @ticket 51458
     622         */
     623        public function test_get_items_search_categories_and_tags() {
     624                $response = $this->do_request_with_params(
     625                        array(
     626                                'per_page' => 100,
     627                                'type'     => 'term',
     628                                'subtype'  => 'category,post_tag',
     629                        )
     630                );
     631                $this->assertEquals( 200, $response->get_status() );
     632                $this->assertEqualSets(
     633                        array(
     634                                0 => 1, // This is the default category.
     635                                self::$my_category_id,
     636                                self::$my_tag_id,
     637                        ),
     638                        wp_list_pluck( $response->get_data(), 'id' )
     639                );
     640        }
     641
     642        /**
     643         * Search through all that matches a 'Test Category' search.
     644         *
     645         * @ticket 51458
     646         */
     647        public function test_get_items_search_for_test_category() {
     648                $response = $this->do_request_with_params(
     649                        array(
     650                                'per_page' => 100,
     651                                'search'   => 'Test Category',
     652                                'type'     => 'term',
     653                        )
     654                );
     655
     656                $this->assertEquals( 200, $response->get_status() );
     657                $this->assertEqualSets(
     658                        array(
     659                                self::$my_category_id,
     660                        ),
     661                        wp_list_pluck( $response->get_data(), 'id' )
     662                );
     663        }
     664
     665        /**
     666         * Search through all that matches a 'Test Tag' search.
     667         *
     668         * @ticket 51458
     669         */
     670        public function test_get_items_search_for_test_tag() {
     671                $response = $this->do_request_with_params(
     672                        array(
     673                                'per_page' => 100,
     674                                'search'   => 'Test Tag',
     675                                'type'     => 'term',
     676                        )
     677                );
     678
     679                $this->assertEquals( 200, $response->get_status() );
     680                $this->assertEqualSets(
     681                        array(
     682                                self::$my_tag_id,
     683                        ),
     684                        wp_list_pluck( $response->get_data(), 'id' )
     685                );
     686        }
     687
     688        /**
     689         * Searching for a term that doesn't exist should return an empty result.
     690         *
     691         * @ticket 51458
     692         */
     693        public function test_get_items_search_for_missing_term() {
     694                $response = $this->do_request_with_params(
     695                        array(
     696                                'per_page' => 100,
     697                                'search'   => 'Doesn\'t exist',
     698                                'type'     => 'term',
     699                        )
     700                );
     701
     702                $this->assertEquals( 200, $response->get_status() );
     703                $this->assertEmpty( $response->get_data() );
     704        }
     705
     706        /**
    518707         * Perform a REST request to our search endpoint with given parameters.
    519708         */
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r49031 r49103  
    44144414                            "default": "post",
    44154415                            "enum": [
    4416                                 "post"
     4416                                "post",
     4417                                "term"
    44174418                            ],
    44184419                            "description": "Limit results to items of an object type.",
     
    44284429                                    "post",
    44294430                                    "page",
     4431                                    "category",
     4432                                    "post_tag",
    44304433                                    "any"
    44314434                                ],
Note: See TracChangeset for help on using the changeset viewer.