Skip to content

Commit

Permalink
Use serialize instead of implode because transient keys must depend o…
Browse files Browse the repository at this point in the history
…n request keys.
  • Loading branch information
anton-vlasenko committed Apr 27, 2022
1 parent 1257482 commit 2c08eea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,12 @@ protected function get_transient_key( $query_args ) {
// 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'] ) ) {
// Empty arrays should not affect the transient key.
if ( 0 === count( $query_args['slug'] ) ) {
unset( $query_args['slug'] );
}
}

return 'wp_remote_block_patterns_' . md5( implode( '-', $query_args ) );
return 'wp_remote_block_patterns_' . md5( serialize( $query_args ) );
}
}
20 changes: 20 additions & 0 deletions tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@ public function data_get_query_parameters() {
),
'message' => 'The order of slugs should not affect transient key.',
),
array(
array(
'parameter_1' => 1,
'slug' => array(),
),
array(
'parameter_1' => 1,
),
'message' => 'Transient keys must match.',
),
array(
array(
'parameter_1' => 1,
Expand All @@ -485,6 +495,16 @@ public function data_get_query_parameters() {
'message' => 'Transient keys must not match.',
false,
),
array(
array(
'parameter_1' => 1,
),
array(
'parameter_2' => 1,
),
'message' => 'Transient keys must depend on array keys.',
false,
),
);
}

Expand Down

0 comments on commit 2c08eea

Please sign in to comment.