Making WordPress.org

Changeset 10572


Ignore:
Timestamp:
01/12/2021 09:30:56 PM (4 years ago)
Author:
iandunn
Message:

Pattern API: Use search param instead of /search endpoint.

Only one post type is being searched, so this is much simpler.

See https://github.com/WordPress/gutenberg/pull/26578#issuecomment-745032557

Location:
sites/trunk/api.wordpress.org/public_html/patterns/1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/patterns/1.0/index.php

    r10545 r10572  
    88 *
    99 * todo
    10  *  publish caching sysreq once query args settled, etc -- https://make.wordpress.org/systems/wp-admin/post.php?post=1788&action=edit
    11  *
    12  */
    13 
    14 
    15 /*
    16  * @todo
    17  *
     10 * - publish caching sysreq once query args settled, etc -- https://make.wordpress.org/systems/wp-admin/post.php?post=1788&action=edit
     11 * - add docs to codex
    1812 */
    1913
     
    2418 * Proxy w.org/patterns API endpoints for reliability.
    2519 *
    26  * Core needs to send requests to api.w.org, because it has more resources and better caching than w.org.
     20 * Core clients need to send requests to api.w.org, because it has more resources and better caching than w.org.
    2721 *
    2822 * @param string $query_string
     
    3125    $api_url_base  = 'https://wordpress.org/patterns/wp-json';
    3226    $wp_init_query = true;
     27    $endpoint      = '/wp/v2/wporg-pattern';
    3328
     29    /*
     30     * Core clients should pass params for the desired action:
     31     *
     32     * @example Browse all:        `/patterns/1.0/`
     33     * @example Browse a category: `/patterns/1.0/?pattern-categories={id}`
     34     * @example Search:            `/patterns/1.0/?search={query}`
     35     */
    3436    parse_str( $query_string, $query_args );
    3537
    36     switch ( $query_args['action'] ) {
    37         // List all patterns, or all with in category.
    38         // To restrict to a category, the client needs to provide `pattern-categories={id}` param.
    39         default:
    40         case 'get_patterns':
    41             $endpoint = '/wp/v2/wporg-pattern';
     38    /*
     39     * Filter the returned fields down to only the ones Core uses.
     40     *
     41     * `_links` is necessary for `wp:term` to be embedded, see https://core.trac.wordpress.org/ticket/49985.
     42     * Related https://core.trac.wordpress.org/ticket/49538.
     43     */
     44    $query_args['_fields'] = 'id,title,content,meta,_links';
     45    $query_args['_embed']  = 'wp:term';
    4246
    43             // `_links` is a workaround for https://core.trac.wordpress.org/ticket/49985. Related https://core.trac.wordpress.org/ticket/49538.
    44             $query_args['_fields'] = 'id,title,content,meta,_links';
    45             $query_args['_embed']  = 'wp:term';
    46 
    47             break;
    48 
    49         // Search patterns.
    50         // Client needs to provide `search={string}` param.
    51         case 'query_patterns':
    52             $endpoint              = '/wp/v2/search';
    53             $query_args['subtype'] = 'wporg-pattern';
    54 
    55             // `_links` is a workaround for https://core.trac.wordpress.org/ticket/49985. Related https://core.trac.wordpress.org/ticket/49538.
    56             $query_args['_fields'] = '_links';
    57             $query_args['_embed']  = 'self';
    58 
    59             break;
    60     }
    61 
    62     unset( $query_args['action'] );
    63 
    64     $wp_init_host = $api_url_base . $endpoint;
    65 
    66     if ( $query_args ) {
    67         $wp_init_host .= '?' . urldecode( http_build_query( $query_args ) );
    68     }
     47    $wp_init_host = $api_url_base . $endpoint . '?' . urldecode( http_build_query( $query_args ) );
    6948
    7049    // Load WordPress to process the request and output the response.
  • sites/trunk/api.wordpress.org/public_html/patterns/1.0/tests/test-index.php

    r10545 r10572  
    4040
    4141    /**
     42     * Asserts that an HTTP response is valid and contains a pattern.
     43     *
     44     * @param Requests_Response $response
     45     */
     46    public function assertResponseHasPattern( $response ) {
     47        $patterns = json_decode( $response->body );
     48
     49        $this->assertSame( 200, $response->status_code );
     50        $this->assertIsString( $patterns[0]->title->rendered );
     51        $this->assertIsInt( $patterns[0]->meta->wpop_viewport_width );
     52    }
     53
     54    /**
     55     * Pluck term IDs from a list of patterns.
     56     *
     57     * @param object[] $patterns
     58     *
     59     * @return int[]
     60     */
     61    public function get_term_ids( $patterns ) {
     62        $term_ids = array();
     63
     64        foreach ( $patterns as $pattern ) {
     65            $term_ids = array_merge(
     66                $term_ids,
     67                array_column( $pattern->_embedded->{'wp:term'}[0], 'id' )
     68            );
     69        }
     70
     71        return array_unique( $term_ids );
     72    }
     73
     74    /**
    4275     * @covers ::main()
    4376     *
    4477     * @group e2e
    4578     */
    46     public function test_get_all_patterns() : void {
    47         $response = $this->send_request( '/?action=get_patterns' );
    48         $body     = json_decode( $response->body );
     79    public function test_browse_all_patterns() : void {
     80        $response = $this->send_request( '/' );
     81        $patterns = json_decode( $response->body );
     82        $term_ids = $this->get_term_ids( $patterns );
    4983
    50         $this->assertSame( 200, $response->status_code );
    51         $this->assertIsString( $body[0]->title->rendered );
    52         $this->assertIsInt( $body[0]->meta->wpop_viewport_width );
     84        $this->assertResponseHasPattern( $response );
     85        $this->assertGreaterThan( 1, count( $term_ids ) );
     86    }
     87
     88    /**
     89     * @covers ::main()
     90     *
     91     * @group e2e
     92     */
     93    public function test_browse_category() : void {
     94        $query_term_id = 2;
     95        $response      = $this->send_request( '/?pattern-categories=' . $query_term_id );
     96        $patterns      = json_decode( $response->body );
     97        $term_ids      = $this->get_term_ids( $patterns );
     98
     99        $this->assertResponseHasPattern( $response );
     100        $this->assertSame( array( $query_term_id ), $term_ids );
     101    }
     102
     103    /**
     104     * @covers ::main()
     105     *
     106     * @dataProvider data_search_patterns
     107     *
     108     * @group e2e
     109     *
     110     * @param string $search_query
     111     */
     112    public function test_search_patterns( $search_term, $match_expected ) : void {
     113        $response = $this->send_request( '/?search=' . $search_term );
     114        $patterns = json_decode( $response->body );
     115
     116        if ( $match_expected ) {
     117            $this->assertResponseHasPattern( $response );
     118
     119            $all_patterns_include_query = true;
     120
     121            foreach ( $patterns as $pattern ) {
     122                $match_in_title       = stripos( $pattern->title->rendered, $search_term );
     123                $match_in_description = stripos( $pattern->meta->wpop_description, $search_term );;
     124
     125                if ( ! $match_in_title && ! $match_in_description ) {
     126                    $all_patterns_include_query = false;
     127                    break;
     128                }
     129            }
     130
     131            $this->assertTrue( $all_patterns_include_query );
     132
     133        } else {
     134            $this->assertSame( 200, $response->status_code );
     135            $this->assertSame( '[]', $response->body );
     136        }
     137    }
     138
     139    public function data_search_patterns() {
     140        return array(
     141            'match title' => array(
     142                'search_term'    => 'side by side',
     143                'match_expected' => true,
     144            ),
     145
     146            // todo Enable this once https://github.com/WordPress/pattern-directory/issues/28 is done
     147//          'match description' => array(
     148//              'search_term'    => 'bright gradient background',
     149//              'match_expected' => true,
     150//          ),
     151
     152            'no matches' => array(
     153                'search_term'    => 'Supercalifragilisticexpialidocious',
     154                'match_expected' => false,
     155            ),
     156        );
    53157    }
    54158}
Note: See TracChangeset for help on using the changeset viewer.