-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pattern Directory API: Add support for pagination parameters (#45293)
* Pattern Directory API: Add support for pagination parameters * Fix linter & php compat issues * Remove the 6.0 filter * Mirror GB 6.0 to also pass through the gutenberg version * Add 'per_page', 'page', 'offset', 'order', and 'orderby' to collection params * Add initial tests for new query parameters * Bump the default per_page to 100 to match w.org API * Update function name * Fix linter issues * remove obsolete `get_items` Co-authored-by: ntsekouras <[email protected]>
- Loading branch information
1 parent
1386ec3
commit b14837c
Showing
6 changed files
with
375 additions
and
79 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
lib/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller-6-0.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* REST API: Gutenberg_REST_Pattern_Directory_Controller_6_0 class | ||
* | ||
* @package Gutenberg | ||
* @subpackage REST_API | ||
*/ | ||
|
||
/** | ||
* Controller which provides REST endpoint for block patterns from wordpress.org/patterns. | ||
*/ | ||
class Gutenberg_REST_Pattern_Directory_Controller_6_0 extends WP_REST_Pattern_Directory_Controller { | ||
/** | ||
* 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 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.