Skip to content

Commit

Permalink
When registering patterns include postTypes
Browse files Browse the repository at this point in the history
When registering block patterns from APIs set the `postTypes` pattern
property to `wp_template` and `wp_template_part` for patterns that have
a block_type of `core/template-part/*`.

The practical effect of this right now is that header and footer
patterns from our pattern repositories will only be offered inside the
site editor and not inside the post or page editor

Fixes #66628
  • Loading branch information
dsas committed Jan 10, 2023
1 parent dfbae0c commit ec70e23
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,26 @@ 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 );
$pattern_props = array(
'title' => $pattern['title'],
'description' => $pattern['description'],
'content' => $pattern['html'],
'viewportWidth' => $viewport_width,
'categories' => array_keys(
$pattern['categories']
),
'isPremium' => $is_premium,
'blockTypes' => $block_types,
);

$post_types = $this->utils->get_pattern_post_types_from_pattern( $pattern_props );
if ( $post_types ) {
$pattern_props['postTypes'] = $post_types;
}

$results[ $pattern_name ] = register_block_pattern(
$pattern_name,
array(
'title' => $pattern['title'],
'description' => $pattern['description'],
'content' => $pattern['html'],
'viewportWidth' => $viewport_width,
'categories' => array_keys(
$pattern['categories']
),
'isPremium' => $is_premium,
'blockTypes' => $block_types,
)
$pattern_props
);
}
}
Expand Down Expand Up @@ -253,4 +259,3 @@ private function update_core_patterns_with_wpcom_categories() {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,23 @@ public function maybe_get_pattern_block_types_from_pattern_meta( $pattern ) {

return $block_types;
}

/**
* Check for post type values in a pattern array
*
* @param array $pattern A pattern array such as is passed to `register_block_pattern`.
*
* @return array $postTypes An array of post type names.
*/
public function get_pattern_post_types_from_pattern( $pattern ) {
$post_types = array();
$block_types = $pattern['blockTypes'];
$block_types_count = count( $block_types );
// If all of a patterns blockTypes are template-parts then limit the
// postTypes to just the template related types.
if ( $block_types_count && count( array_filter( $block_types, fn( $block_type) => preg_match( '#core/template-part/#', $block_type ) ) ) === $block_types_count ) {
$post_types = array( 'wp_template', 'wp_template_part' );
}
return $post_types;
}
}

0 comments on commit ec70e23

Please sign in to comment.