Make WordPress Core


Ignore:
Timestamp:
05/02/2022 01:58:48 PM (4 years ago)
Author:
hellofromTonya
Message:

REST API: Fixes /wp/v2/pattern-directory/patterns endpoint response for slug parameter.

[53218] introduced a bug of a wrong response from the wp/v2/pattern-directory/patterns endpoint with a slug parameter. As the response is cached, it can result in an incorrect list of available patterns supported by the current theme.

This commit resolves by:

  • Limiting the slug to an array in the query parameters.
  • When set, parsing and sorting the slug(s) and then serializing the sorted query args as part of the hashed transient keys.

Props antonvlasenko, timothyblynjacobs, spacedmonkey, costdev, hellofromTonya.

Follow-up to [53218], [53152], [51208].
Fixes #55617.

File:
1 edited

Legend:

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

    r53218 r53333  
    120120        }
    121121
    122         /*
    123          * Include a hash of the query args, so that different requests are stored in
    124          * separate caches.
    125          *
    126          * MD5 is chosen for its speed, low-collision rate, universal availability, and to stay
    127          * under the character limit for `_site_transient_timeout_{...}` keys.
    128          *
    129          * @link https://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses
    130          */
    131         $transient_key = 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) );
     122        $transient_key = $this->get_transient_key( $query_args );
    132123
    133124        /*
     
    338329        );
    339330
     331        $query_params['slug'] = array(
     332            'description' => __( 'Limit results to those matching a pattern (slug).' ),
     333            'type'        => 'array',
     334        );
     335
    340336        /**
    341337         * Filter collection parameters for the block pattern directory controller.
     
    347343        return apply_filters( 'rest_pattern_directory_collection_params', $query_params );
    348344    }
     345
     346    /*
     347     * Include a hash of the query args, so that different requests are stored in
     348     * separate caches.
     349     *
     350     * MD5 is chosen for its speed, low-collision rate, universal availability, and to stay
     351     * under the character limit for `_site_transient_timeout_{...}` keys.
     352     *
     353     * @link https://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses
     354     *
     355     * @since 6.0.0
     356     *
     357     * @param array $query_args Query arguments to generate a transient key from.
     358     * @return string Transient key.
     359     */
     360    protected function get_transient_key( $query_args ) {
     361
     362        if ( isset( $query_args['slug'] ) ) {
     363            // This is an additional precaution because the "sort" function expects an array.
     364            $query_args['slug'] = wp_parse_list( $query_args['slug'] );
     365
     366            // Empty arrays should not affect the transient key.
     367            if ( empty( $query_args['slug'] ) ) {
     368                unset( $query_args['slug'] );
     369            } else {
     370                // Sort the array so that the transient key doesn't depend on the order of slugs.
     371                sort( $query_args['slug'] );
     372            }
     373        }
     374
     375        return 'wp_remote_block_patterns_' . md5( serialize( $query_args ) );
     376    }
    349377}
Note: See TracChangeset for help on using the changeset viewer.