diff --git a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php index deb2e67379a2d..eecf3c4c5654e 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php @@ -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 ); } } @@ -253,4 +259,3 @@ private function update_core_patterns_with_wpcom_categories() { } } } - diff --git a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php index 093cfe4dbfac4..ddb3e5586220c 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php @@ -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; + } }