Skip to content

Commit

Permalink
Block Patterns: Load patterns from wordpress.org API (#28800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle authored May 20, 2021
1 parent 7b39dd7 commit 57f7791
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 543 deletions.
68 changes: 38 additions & 30 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ function register_gutenberg_patterns() {
}

/**
* Deactivate the legacy patterns bundled with WordPress, and add new block patterns for testing.
* More details in the trac issue (https://core.trac.wordpress.org/ticket/52846).
* Deactivate the legacy patterns bundled with WordPress.
*/
function update_core_patterns() {
function remove_core_patterns() {
$core_block_patterns = array(
'text-two-columns',
'two-buttons',
Expand All @@ -202,36 +201,31 @@ function update_core_patterns() {
'quote',
);

$new_core_block_patterns = array(
'media-text-nature',
'two-images-gallery',
'three-columns-media-text',
'quote',
'large-header-left',
'large-header-text-button',
'media-text-art',
'text-two-columns-title',
'three-columns-text',
'text-two-columns-title-offset',
'heading',
'three-images-gallery',
'text-two-columns',
'media-text-arquitecture',
'two-buttons',
);

foreach ( $core_block_patterns as $core_block_pattern ) {
$name = 'core/' . $core_block_pattern;
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) {
unregister_block_pattern( $name );
unregister_block_pattern( 'core/' . $core_block_pattern );
}
}

/**
* Import patterns from wordpress.org/patterns.
*/
function load_remote_patterns() {
$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 );
}

foreach ( $new_core_block_patterns as $core_block_pattern ) {
register_block_pattern(
'core/' . $core_block_pattern,
require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'
);
foreach ( $patterns as $settings ) {
$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
}
}

Expand All @@ -242,7 +236,21 @@ function() {
if ( ! get_theme_support( 'core-block-patterns' ) || ! function_exists( 'unregister_block_pattern' ) ) {
return;
}
remove_core_patterns();
register_gutenberg_patterns();
update_core_patterns();
}
);

add_action(
'current_screen',
function( $current_screen ) {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}

$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) );
if ( $current_screen->is_block_editor || $is_site_editor ) {
load_remote_patterns();
}
}
);
16 changes: 0 additions & 16 deletions lib/block-patterns/heading.php

This file was deleted.

33 changes: 0 additions & 33 deletions lib/block-patterns/large-header-left.php

This file was deleted.

35 changes: 0 additions & 35 deletions lib/block-patterns/large-header-text-button.php

This file was deleted.

21 changes: 0 additions & 21 deletions lib/block-patterns/media-text-arquitecture.php

This file was deleted.

21 changes: 0 additions & 21 deletions lib/block-patterns/media-text-art.php

This file was deleted.

28 changes: 0 additions & 28 deletions lib/block-patterns/media-text-nature.php

This file was deleted.

30 changes: 0 additions & 30 deletions lib/block-patterns/quote.php

This file was deleted.

53 changes: 0 additions & 53 deletions lib/block-patterns/text-two-columns-title-offset.php

This file was deleted.

Loading

0 comments on commit 57f7791

Please sign in to comment.