Skip to content

Commit

Permalink
Block Patterns: Load patterns from wordpress.org API
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Feb 5, 2021
1 parent 66d46b3 commit 4571c1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Block Pattern functionality.
*
* @package gutenberg
*/

/**
* Register "core" patterns from the WordPress.org pattern directory.
*/
function gutenberg_register_remote_block_patterns() {
$should_register_core_patterns = get_theme_support( 'core-block-patterns' );

// Unregister the bundled core patterns.
$patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
foreach ( $patterns as $pattern ) {
if ( 'core/' === substr( $pattern['name'], 0, 5 ) ) {
unregister_block_pattern( $pattern['name'] );
}
}

if ( $should_register_core_patterns ) {
$request = new WP_REST_Request( 'GET', '/__experimental/pattern-directory/patterns' );
$request->set_param( 'keyword', 11 ); // 11 is the ID for "core".
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
foreach ( $patterns as $settings ) {
$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
}
}
}
add_action( 'init', 'gutenberg_register_remote_block_patterns', 11 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/full-site-editing/edit-site-page.php';
require __DIR__ . '/full-site-editing/edit-site-export.php';

require __DIR__ . '/block-patterns.php';
require __DIR__ . '/blocks.php';
require __DIR__ . '/client-assets.php';
require __DIR__ . '/demo.php';
Expand Down

0 comments on commit 4571c1b

Please sign in to comment.