Making WordPress.org

Changeset 12147


Ignore:
Timestamp:
10/25/2022 05:49:03 PM (3 years ago)
Author:
ryelle
Message:

Patterns API: Update pagination & sort order for fetching patterns.

Use the per_page parameter passed in if it exists, otherwise continue defaulting to 100 patterns. Remove the forced alphabetical title order in favor of the value passed in. Patterns will now be returned in the same order they are viewed on the website. orderby=favorite_count can be used to mirror the "popular" sorting.

Fixes https://github.com/WordPress/pattern-directory/issues/529.

File:
1 edited

Legend:

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

    r11169 r12147  
    2020    $old_headers = headers_list();
    2121
    22     // Remove CORS header added by REST API
     22    // Remove CORS header added by REST API.
    2323    header_remove( 'access-control-allow-headers' );
    2424
    2525    $replace = true;
    2626
    27     foreach( headers_list() as $header ) {
     27    foreach ( headers_list() as $header ) {
    2828        if ( 'Link: ' === substr( $header, 0, 6) ) {
    2929            $new_header = str_replace( 'https://wordpress.org/patterns/wp-json/wp/v2/wporg-pattern', 'https://api.wordpress.org/patterns/1.0', $header );
     
    3131            if ( $new_header !== $header ) {
    3232                header( $new_header, $replace );
    33                 $replace = false; // Only replace the first time
     33                $replace = false; // Only replace the first time.
    3434            }
    3535        }
    3636    }
    3737
    38     return false; // original buffer will be output with no changes
     38    return false; // Original buffer will be output with no changes.
    3939}
    4040
     
    7171        $endpoint               = '/wp/v2/wporg-pattern';
    7272        $query_args['_fields']  = 'id,title,content,meta,category_slugs,keyword_slugs,pattern_content';
    73         $query_args['per_page'] = 100;
    74 
    75         // Sort alphabetically so that browsing is intuitive. Search will be sorted by rank.
    76         if ( ! isset( $query_args['search'] ) ) {
    77             $query_args['orderby'] = 'title';
    78             $query_args['order']   = 'asc';
    79         }
     73        $query_args['per_page'] = $query_args['per_page'] ?? 100;
    8074    }
    8175
Note: See TracChangeset for help on using the changeset viewer.