-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/wp/v2/pattern-directory/patterns endpoint: slug parameter has no effect on the response
#2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
7571960
f06d3d0
b504030
b1598ab
e284e1e
7fb829d
07aa848
fd1642d
5c9a336
5e923bc
c2122fb
005de4a
0a2c2eb
7d1c512
1257482
2c08eea
0411a81
5653f25
0879fe0
8ec23a0
1d8f893
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -119,16 +119,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 | ||||||
|
|
@@ -337,6 +328,11 @@ public function get_collection_params() { | |||||
| '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,39 @@ public function get_collection_params() { | |||||
| */ | ||||||
| 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'] = (array) $query_args['slug']; | ||||||
hellofromtonya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // Sort the array so that the transient key doesn't depend on the order of slugs. | ||||||
| sort( $query_args['slug'] ); | ||||||
|
|
||||||
| // Slugs have to be imploded separately as implode doesn't work with recursive arrays. | ||||||
| $query_args['slug'] = implode( ',', $query_args['slug'] ); | ||||||
|
|
||||||
| if ( '' === trim( $query_args['slug'] ) ) { | ||||||
hellofromtonya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| unset( $query_args['slug'] ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) ); | ||||||
|
||||||
| return 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) ); | |
| return 'wp_remote_block_patterns_' . md5( serialize( $query_args ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I've been thinking about replacing implode with wp_json_encode (json_encode is faster than serialize) in the scope of another PR, but serialize will do too.
The result produced by implode doesn't depend on the keys of the $query_args array. Therefore, this increases the chance of collisions.
Fixed in 2c08eea
P.S. I didn't change it because I was afraid that it would cause a sharp increase in the number of requests to the wp/v2/pattern-directory/patterns endpoint once WordPress 6.0 is released. The old values stored in the cache will be invalidated because this PR replaces implode with serialize.
I hope it will not happen since not all WordPress sites are updated simultaneously.
Uh oh!
There was an error while loading. Please reload this page.