Skip to content

Commit

Permalink
Cache the remote patterns for an hour
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle authored and youknowriad committed May 19, 2021
1 parent 81f84d7 commit 492f873
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,19 @@ function remove_core_patterns() {
* Import patterns from wordpress.org/patterns.
*/
function load_remote_patterns() {
$request = new WP_REST_Request( 'GET', '/__experimental/pattern-directory/patterns' );
$core_keyword_id = 11; // 11 is the ID for "core".
$request->set_param( 'keyword', $core_keyword_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
$patterns = get_transient( 'gutenberg_remote_block_patterns' );
if ( ! $patterns ) {
$request = new WP_REST_Request( 'GET', '/__experimental/pattern-directory/patterns' );
$core_keyword_id = 11; // 11 is the ID for "core".
$request->set_param( 'keyword', $core_keyword_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
set_transient( 'gutenberg_remote_block_patterns', $patterns, HOUR_IN_SECONDS );
}
$patterns = $response->get_data();

foreach ( $patterns as $settings ) {
$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
Expand Down

0 comments on commit 492f873

Please sign in to comment.