Making WordPress.org


Ignore:
Timestamp:
01/12/2021 09:30:56 PM (6 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

File:
1 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.
Note: See TracChangeset for help on using the changeset viewer.