From 1cca8be49f22161a6294be0084bae84d525140b1 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Mon, 2 May 2022 14:00:09 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@53333 git-svn-id: http://core.svn.wordpress.org/trunk@52922 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- ...s-wp-rest-pattern-directory-controller.php | 48 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php index 28f42fd5a4..64b4ce28f1 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php @@ -119,16 +119,7 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { $query_args['slug'] = $slug; } - /* - * Include a hash of the query args, so that different requests are stored in - * separate caches. - * - * MD5 is chosen for its speed, low-collision rate, universal availability, and to stay - * under the character limit for `_site_transient_timeout_{...}` keys. - * - * @link https://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses - */ - $transient_key = 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) ); + $transient_key = $this->get_transient_key( $query_args ); /* * Use network-wide transient to improve performance. The locale is the only site @@ -337,6 +328,11 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { 'minimum' => 1, ); + $query_params['slug'] = array( + 'description' => __( 'Limit results to those matching a pattern (slug).' ), + 'type' => 'array', + ); + /** * Filter collection parameters for the block pattern directory controller. * @@ -346,4 +342,36 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { */ return apply_filters( 'rest_pattern_directory_collection_params', $query_params ); } + + /* + * Include a hash of the query args, so that different requests are stored in + * separate caches. + * + * MD5 is chosen for its speed, low-collision rate, universal availability, and to stay + * under the character limit for `_site_transient_timeout_{...}` keys. + * + * @link https://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses + * + * @since 6.0.0 + * + * @param array $query_args Query arguments to generate a transient key from. + * @return string Transient key. + */ + protected function get_transient_key( $query_args ) { + + if ( isset( $query_args['slug'] ) ) { + // This is an additional precaution because the "sort" function expects an array. + $query_args['slug'] = wp_parse_list( $query_args['slug'] ); + + // Empty arrays should not affect the transient key. + if ( empty( $query_args['slug'] ) ) { + unset( $query_args['slug'] ); + } else { + // Sort the array so that the transient key doesn't depend on the order of slugs. + sort( $query_args['slug'] ); + } + } + + return 'wp_remote_block_patterns_' . md5( serialize( $query_args ) ); + } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 09ca872c49..1ddc115ce4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-beta3-53332'; +$wp_version = '6.0-beta3-53333'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.