diff --git a/lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php b/lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php index 5f639bd62baa5f..e92b25f675abbd 100644 --- a/lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php +++ b/lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php @@ -57,16 +57,7 @@ public function get_items( $request ) { $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 @@ -75,11 +66,7 @@ public function get_items( $request ) { $raw_patterns = get_site_transient( $transient_key ); if ( ! $raw_patterns ) { - $api_url = add_query_arg( - array_map( 'rawurlencode', $query_args ), - 'http://api.wordpress.org/patterns/1.0/' - ); - + $api_url = 'http://api.wordpress.org/patterns/1.0/?' . build_query( $query_args ); if ( wp_http_supports( array( 'ssl' ) ) ) { $api_url = set_url_scheme( $api_url, 'https' ); } @@ -138,4 +125,40 @@ public function get_items( $request ) { return new WP_REST_Response( $response ); } + + /** + * 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 + * @todo This should be removed when the minimum required WordPress version is >= 6.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 ( method_exists( get_parent_class( $this ), __FUNCTION__ ) ) { + return parent::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 ) ); + } }