2021-05-25 21:11:59 -04:00
< ? php
/**
* Block Pattern Directory REST API : WP_REST_Pattern_Directory_Controller class
*
* @ package WordPress
* @ subpackage REST_API
* @ since 5.8 . 0
*/
/**
* Controller which provides REST endpoint for block patterns .
*
* This simply proxies the endpoint at http :// api . wordpress . org / patterns / 1.0 /. That isn ' t necessary for
* functionality , but is desired for privacy . It prevents api . wordpress . org from knowing the user ' s IP address .
*
* @ since 5.8 . 0
*
* @ see WP_REST_Controller
*/
class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
/**
* Constructs the controller .
2021-06-30 09:58:59 -04:00
*
* @ since 5.8 . 0
2021-05-25 21:11:59 -04:00
*/
public function __construct () {
2022-04-19 10:58:09 -04:00
$this -> namespace = 'wp/v2' ;
$this -> rest_base = 'pattern-directory' ;
2021-05-25 21:11:59 -04:00
}
/**
* Registers the necessary REST API routes .
2021-06-30 09:58:59 -04:00
*
* @ since 5.8 . 0
2021-05-25 21:11:59 -04:00
*/
public function register_routes () {
register_rest_route (
$this -> namespace ,
'/' . $this -> rest_base . '/patterns' ,
array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
}
/**
2021-11-23 16:37:01 -05:00
* Checks whether a given request has permission to view the local block pattern directory .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
*
* @ param WP_REST_Request $request Full details about the request .
2021-06-30 13:59:58 -04:00
* @ return true | WP_Error True if the request has permission , WP_Error object otherwise .
2021-05-25 21:11:59 -04:00
*/
public function get_items_permissions_check ( $request ) {
if ( current_user_can ( 'edit_posts' ) ) {
return true ;
}
foreach ( get_post_types ( array ( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can ( $post_type -> cap -> edit_posts ) ) {
return true ;
}
}
return new WP_Error (
'rest_pattern_directory_cannot_view' ,
__ ( 'Sorry, you are not allowed to browse the local block pattern directory.' ),
array ( 'status' => rest_authorization_required_code () )
);
}
/**
* Search and retrieve block patterns metadata
*
* @ since 5.8 . 0
2022-04-12 05:26:16 -04:00
* @ since 6.0 . 0 Added 'slug' to request .
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
* @ since 6.2 . 0 Added 'per_page' , 'page' , 'offset' , 'order' , and 'orderby' to request .
2021-05-25 21:11:59 -04:00
*
* @ param WP_REST_Request $request Full details about the request .
2021-06-30 13:59:58 -04:00
* @ return WP_REST_Response | WP_Error Response object on success , or WP_Error object on failure .
2021-05-25 21:11:59 -04:00
*/
public function get_items ( $request ) {
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$valid_query_args = array (
'offset' => true ,
'order' => true ,
'orderby' => true ,
'page' => true ,
'per_page' => true ,
'search' => true ,
'slug' => true ,
2021-06-22 17:21:57 -04:00
);
2023-02-06 14:40:16 -05:00
$query_args = array_intersect_key ( $request -> get_params (), $valid_query_args );
2021-06-22 17:21:57 -04:00
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$query_args [ 'locale' ] = get_user_locale ();
2024-07-26 20:27:16 -04:00
$query_args [ 'wp-version' ] = wp_get_wp_version ();
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$query_args [ 'pattern-categories' ] = isset ( $request [ 'category' ] ) ? $request [ 'category' ] : false ;
$query_args [ 'pattern-keywords' ] = isset ( $request [ 'keyword' ] ) ? $request [ 'keyword' ] : false ;
2021-05-25 21:11:59 -04:00
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$query_args = array_filter ( $query_args );
2022-04-12 05:26:16 -04:00
2022-05-02 10:00:09 -04:00
$transient_key = $this -> get_transient_key ( $query_args );
2021-05-25 21:11:59 -04:00
2021-06-22 17:24:58 -04:00
/*
* Use network - wide transient to improve performance . The locale is the only site
* configuration that affects the response , and it ' s included in the transient key .
*/
$raw_patterns = get_site_transient ( $transient_key );
if ( ! $raw_patterns ) {
2022-04-19 10:58:09 -04:00
$api_url = 'http://api.wordpress.org/patterns/1.0/?' . build_query ( $query_args );
2021-06-22 17:24:58 -04:00
if ( wp_http_supports ( array ( 'ssl' ) ) ) {
$api_url = set_url_scheme ( $api_url , 'https' );
}
/*
* Default to a short TTL , to mitigate cache stampedes on high - traffic sites .
* This assumes that most errors will be short - lived , e . g . , packet loss that causes the
* first request to fail , but a follow - up one will succeed . The value should be high
* enough to avoid stampedes , but low enough to not interfere with users manually
* re - trying a failed request .
*/
$cache_ttl = 5 ;
$wporg_response = wp_remote_get ( $api_url );
$raw_patterns = json_decode ( wp_remote_retrieve_body ( $wporg_response ) );
if ( is_wp_error ( $wporg_response ) ) {
$raw_patterns = $wporg_response ;
} elseif ( ! is_array ( $raw_patterns ) ) {
// HTTP request succeeded, but response data is invalid.
$raw_patterns = new WP_Error (
'pattern_api_failed' ,
sprintf (
2022-04-12 05:26:16 -04:00
/* translators: %s: Support forums URL. */
2021-06-22 17:24:58 -04:00
__ ( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__ ( 'https://wordpress.org/support/forums/' )
),
array (
'response' => wp_remote_retrieve_body ( $wporg_response ),
)
);
2021-05-25 21:11:59 -04:00
2021-06-22 17:24:58 -04:00
} else {
// Response has valid data.
$cache_ttl = HOUR_IN_SECONDS ;
}
2021-05-25 21:11:59 -04:00
2021-06-22 17:24:58 -04:00
set_site_transient ( $transient_key , $raw_patterns , $cache_ttl );
2021-05-25 21:11:59 -04:00
}
2021-06-22 17:24:58 -04:00
if ( is_wp_error ( $raw_patterns ) ) {
$raw_patterns -> add_data ( array ( 'status' => 500 ) );
return $raw_patterns ;
2021-05-25 21:11:59 -04:00
}
$response = array ();
if ( $raw_patterns ) {
foreach ( $raw_patterns as $pattern ) {
$response [] = $this -> prepare_response_for_collection (
$this -> prepare_item_for_response ( $pattern , $request )
);
}
}
return new WP_REST_Response ( $response );
}
/**
2021-11-23 16:37:01 -05:00
* Prepare a raw block pattern before it gets output in a REST API response .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
* @ since 5.9 . 0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support .
2021-05-25 21:11:59 -04:00
*
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
* @ param object $item Raw pattern from api . wordpress . org , before any changes .
* @ param WP_REST_Request $request Request object .
2021-05-25 21:11:59 -04:00
* @ return WP_REST_Response
*/
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
public function prepare_item_for_response ( $item , $request ) {
// Restores the more descriptive, specific name for use within this method.
2023-09-14 08:46:20 -04:00
$raw_pattern = $item ;
2021-05-25 21:11:59 -04:00
$prepared_pattern = array (
'id' => absint ( $raw_pattern -> id ),
'title' => sanitize_text_field ( $raw_pattern -> title -> rendered ),
'content' => wp_kses_post ( $raw_pattern -> pattern_content ),
'categories' => array_map ( 'sanitize_title' , $raw_pattern -> category_slugs ),
2022-07-05 12:03:13 -04:00
'keywords' => array_map ( 'sanitize_text_field' , explode ( ',' , $raw_pattern -> meta -> wpop_keywords ) ),
2021-05-25 21:11:59 -04:00
'description' => sanitize_text_field ( $raw_pattern -> meta -> wpop_description ),
'viewport_width' => absint ( $raw_pattern -> meta -> wpop_viewport_width ),
2023-02-06 14:40:16 -05:00
'block_types' => array_map ( 'sanitize_text_field' , $raw_pattern -> meta -> wpop_block_types ),
2021-05-25 21:11:59 -04:00
);
$prepared_pattern = $this -> add_additional_fields_to_object ( $prepared_pattern , $request );
$response = new WP_REST_Response ( $prepared_pattern );
/**
2021-11-23 16:37:01 -05:00
* Filters the REST API response for a block pattern .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
*
* @ param WP_REST_Response $response The response object .
2021-11-23 16:37:01 -05:00
* @ param object $raw_pattern The unprepared block pattern .
2021-05-25 21:11:59 -04:00
* @ param WP_REST_Request $request The request object .
*/
return apply_filters ( 'rest_prepare_block_pattern' , $response , $raw_pattern , $request );
}
/**
2021-11-23 16:37:01 -05:00
* Retrieves the block pattern ' s schema , conforming to JSON Schema .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
2023-02-06 14:40:16 -05:00
* @ since 6.2 . 0 Added `'block_types'` to schema .
2021-05-25 21:11:59 -04:00
*
* @ return array Item schema data .
*/
public function get_item_schema () {
if ( $this -> schema ) {
return $this -> add_additional_fields_schema ( $this -> schema );
}
$this -> schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'pattern-directory-item' ,
'type' => 'object' ,
'properties' => array (
'id' => array (
'description' => __ ( 'The pattern ID.' ),
'type' => 'integer' ,
'minimum' => 1 ,
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'title' => array (
'description' => __ ( 'The pattern title, in human readable format.' ),
'type' => 'string' ,
'minLength' => 1 ,
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'content' => array (
'description' => __ ( 'The pattern content.' ),
'type' => 'string' ,
'minLength' => 1 ,
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'categories' => array (
'description' => __ ( " The pattern's category slugs. " ),
'type' => 'array' ,
'uniqueItems' => true ,
'items' => array ( 'type' => 'string' ),
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'keywords' => array (
2022-07-05 12:03:13 -04:00
'description' => __ ( " The pattern's keywords. " ),
2021-05-25 21:11:59 -04:00
'type' => 'array' ,
'uniqueItems' => true ,
'items' => array ( 'type' => 'string' ),
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'description' => array (
'description' => __ ( 'A description of the pattern.' ),
'type' => 'string' ,
'minLength' => 1 ,
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
'viewport_width' => array (
'description' => __ ( 'The preferred width of the viewport when previewing a pattern, in pixels.' ),
'type' => 'integer' ,
2022-04-12 05:26:16 -04:00
'context' => array ( 'view' , 'edit' , 'embed' ),
2021-05-25 21:11:59 -04:00
),
2023-02-06 14:40:16 -05:00
'block_types' => array (
'description' => __ ( 'The block types which can use this pattern.' ),
'type' => 'array' ,
'uniqueItems' => true ,
'items' => array ( 'type' => 'string' ),
'context' => array ( 'view' , 'embed' ),
),
2021-05-25 21:11:59 -04:00
),
);
return $this -> add_additional_fields_schema ( $this -> schema );
}
/**
2021-11-23 16:37:01 -05:00
* Retrieves the search parameters for the block pattern ' s collection .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
* @ since 6.2 . 0 Added 'per_page' , 'page' , 'offset' , 'order' , and 'orderby' to request .
2021-05-25 21:11:59 -04:00
*
* @ return array Collection parameters .
*/
public function get_collection_params () {
$query_params = parent :: get_collection_params ();
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$query_params [ 'per_page' ][ 'default' ] = 100 ;
2021-05-25 21:11:59 -04:00
$query_params [ 'search' ][ 'minLength' ] = 1 ;
$query_params [ 'context' ][ 'default' ] = 'view' ;
$query_params [ 'category' ] = array (
'description' => __ ( 'Limit results to those matching a category ID.' ),
'type' => 'integer' ,
'minimum' => 1 ,
);
$query_params [ 'keyword' ] = array (
'description' => __ ( 'Limit results to those matching a keyword ID.' ),
'type' => 'integer' ,
'minimum' => 1 ,
);
2022-05-02 10:00:09 -04:00
$query_params [ 'slug' ] = array (
'description' => __ ( 'Limit results to those matching a pattern (slug).' ),
'type' => 'array' ,
);
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller`.
Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.
Reference:
* [https://github.com/WordPress/gutenberg/pull/45293 Gutenberg PR 45293]
Follow-up to [55098], [51206], [51021].
Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132
git-svn-id: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-24 11:54:11 -05:00
$query_params [ 'offset' ] = array (
'description' => __ ( 'Offset the result set by a specific number of items.' ),
'type' => 'integer' ,
);
$query_params [ 'order' ] = array (
'description' => __ ( 'Order sort attribute ascending or descending.' ),
'type' => 'string' ,
'default' => 'desc' ,
'enum' => array ( 'asc' , 'desc' ),
);
$query_params [ 'orderby' ] = array (
'description' => __ ( 'Sort collection by post attribute.' ),
'type' => 'string' ,
'default' => 'date' ,
'enum' => array (
'author' ,
'date' ,
'id' ,
'include' ,
'modified' ,
'parent' ,
'relevance' ,
'slug' ,
'include_slugs' ,
'title' ,
'favorite_count' ,
),
);
2021-05-25 21:11:59 -04:00
/**
2021-11-23 16:37:01 -05:00
* Filter collection parameters for the block pattern directory controller .
2021-05-25 21:11:59 -04:00
*
* @ since 5.8 . 0
*
* @ param array $query_params JSON Schema - formatted collection parameters .
*/
return apply_filters ( 'rest_pattern_directory_collection_params' , $query_params );
}
2022-05-02 10:00:09 -04:00
2024-10-10 07:54:15 -04:00
/**
2022-05-02 10:00:09 -04:00
* 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 ) );
}
2021-05-25 21:11:59 -04:00
}