Skip to content

Commit

Permalink
update all remote calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Feb 2, 2023
1 parent 3e2d823 commit 06f6b16
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
114 changes: 102 additions & 12 deletions lib/compat/wordpress-6.2/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,33 @@ function gutenberg_register_theme_block_patterns() {
remove_action( 'init', '_register_theme_block_patterns' );
add_action( 'init', 'gutenberg_register_theme_block_patterns' );

/**
* Normalize the pattern from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
*
* @since 6.2.0
*
* @param array $pattern Pattern as returned from the Pattern Directory API.
*/
function gutenberg_normalize_remote_pattern( $pattern ) {
if ( isset( $pattern['block_types'] ) ) {
$pattern['blockTypes'] = $pattern['block_types'];
unset( $pattern['block_types'] );
}

if ( isset( $pattern['viewport_width'] ) ) {
$pattern['viewportWidth'] = $pattern['viewport_width'];
unset( $pattern['viewport_width'] );
}

return (array) $pattern;
}

/**
* Register Core's official patterns from wordpress.org/patterns.
*
* @since 5.8.0
* @since 5.9.0 The $current_screen argument was removed.
* @since 6.2.0 Normalize the settings from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
*
* @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
*/
Expand Down Expand Up @@ -351,19 +371,89 @@ function gutenberg_load_remote_block_patterns( $deprecated = null ) {
}
$patterns = $response->get_data();

foreach ( $patterns as $settings ) {
if ( isset( $settings['block_types'] ) ) {
$settings['blockTypes'] = $settings['block_types'];
unset( $settings['block_types'] );
}
foreach ( $patterns as $pattern ) {
$normalized_pattern = gutenberg_normalize_remote_pattern( $pattern );
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
register_block_pattern( $pattern_name, (array) $normalized_pattern );
}
}
}

if ( isset( $settings['viewport_width'] ) ) {
$settings['viewportWidth'] = $settings['viewport_width'];
unset( $settings['viewport_width'] );
}
/**
* Register `Featured` (category) patterns from wordpress.org/patterns.
*
* @since 5.9.0
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
*/
function gutenberg_load_remote_featured_patterns() {
$supports_core_patterns = get_theme_support( 'core-block-patterns' );

/** This filter is documented in wp-includes/block-patterns.php */
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );

$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
if ( ! $should_load_remote || ! $supports_core_patterns ) {
return;
}

$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
$request->set_param( 'category', $featured_cat_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();

foreach ( $patterns as $pattern ) {
$normalized_pattern = gutenberg_normalize_remote_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] );
$registry = WP_Block_Patterns_Registry::get_instance();

This comment was marked as duplicate.

Copy link
@Mamaduka

Mamaduka Feb 2, 2023

Member

I had this somewhere on my to-do list, so let's do it here 😄

We should move this like outside the loop; no need to get the same registry instance on each iteration.

// Some patterns might be already registered as core patterns with the `core` prefix.
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
if ( ! $is_registered ) {
register_block_pattern( $pattern_name, (array) $normalized_pattern );
}
}
}

/**
* Registers patterns from Pattern Directory provided by a theme's
* `theme.json` file.
*
* @since 6.0.0
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
* @access private
*/
function gutenberg_register_remote_theme_patterns() {
/** This filter is documented in wp-includes/block-patterns.php */
if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
return;
}

if ( ! wp_theme_has_theme_json() ) {
return;
}

$pattern_settings = WP_Theme_JSON_Resolver::get_theme_data()->get_patterns();
if ( empty( $pattern_settings ) ) {
return;
}

$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$request['slug'] = $pattern_settings;
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
$patterns_registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) {
$normalized_pattern = gutenberg_normalize_remote_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix.
$is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
if ( ! $is_registered ) {
register_block_pattern( $pattern_name, (array) $normalized_pattern );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public function get_items( $request ) {
if ( ! $this->remote_patterns_loaded ) {
// Load block patterns from w.org.
gutenberg_load_remote_block_patterns(); // Patterns with the `core` keyword.
_load_remote_featured_patterns(); // Patterns in the `featured` category.
_register_remote_theme_patterns(); // Patterns requested by current theme.
gutenberg_load_remote_featured_patterns(); // Patterns in the `featured` category.
gutenberg_register_remote_theme_patterns(); // Patterns requested by current theme.

$this->remote_patterns_loaded = true;
}
Expand Down

0 comments on commit 06f6b16

Please sign in to comment.