Skip to content

Commit

Permalink
Register wp_block patterns from Dotcompatterns with blockTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
miksansegundo committed Jan 30, 2024
1 parent 1f61063 commit b194b92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ function ( $a, $b ) {
$viewport_width = $viewport_width < 320 ? 320 : $viewport_width;
$pattern_name = self::PATTERN_NAMESPACE . $pattern['name'];
$block_types = $this->utils->maybe_get_pattern_block_types_from_pattern_meta( $pattern );
if ( empty( $block_types ) ) {
// For wp_block patterns because don't use pattern meta for block types.
$block_types = $this->utils->get_block_types_from_categories( $pattern );
}

$results[ $pattern_name ] = register_block_pattern(
$pattern_name,
Expand Down Expand Up @@ -180,7 +184,7 @@ private function get_patterns( $patterns_cache_key, $patterns_source ) {
if ( $enable_testing_v2_patterns || false === $block_patterns || ( defined( 'WP_DISABLE_PATTERN_CACHE' ) && WP_DISABLE_PATTERN_CACHE ) ) {
if ( $enable_testing_v2_patterns ) {
$request_params = array(
'site' => 'assemblerv2patterns.wordpress.com',
'site' => 'dotcompatterns.wordpress.com',
'post_type' => 'wp_block',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ public function maybe_get_pattern_block_types_from_pattern_meta( $pattern ) {
return $block_types;
}

/**
* Using the pattern categories, generate the `blockTypes` property for
* registering the pattern via `register_block_pattern`.
*
* @param array $pattern A pattern with categories.
*
* @return array An array of block types.
*/
public function get_block_types_from_categories( $pattern ) {
$block_types = array();

if ( ! isset( $pattern['categories'] ) || empty( $pattern['categories'] ) ) {
return $block_types;
}

foreach ( $pattern['categories'] as $key => $value ) {
switch ( $key ) {
case 'header':
$block_types[] = 'core/template-part/header';
break;
case 'footer':
$block_types[] = 'core/template-part/footer';
break;
case 'posts':
$block_types[] = 'core/query';
break;
}
}

return $block_types;
}

/**
* Return pattern post types based on the pattern's blockTypes.
*
Expand Down

0 comments on commit b194b92

Please sign in to comment.