Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add mandatory "Namespace" field
… as opposed to implicitly tying a pattern to the stylesheet as obtained
via `get_stylesheet`. This may be heavy-handed, but it leaves it up to
pattern and theme authors to define their naming schema. See discussion
in #36751
  • Loading branch information
mcsf committed Mar 17, 2022
commit bc8471f23397c6f5b0a88107412626e606a268cb
13 changes: 10 additions & 3 deletions lib/compat/wordpress-6.0/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function( $current_screen ) {
*
* /**
* * Pattern Name: My Pattern
* * Namespace: my-theme
* *
*
* The output of the PHP source corresponds to the content of the pattern, e.g.:
Expand All @@ -83,6 +84,7 @@ function( $current_screen ) {
function gutenberg_register_theme_block_patterns() {
$default_headers = array(
'title' => 'Pattern Name',
'namespace' => 'Namespace',
'description' => 'Description',
'viewportWidth' => 'Viewport Width',
'categories' => 'Categories',
Expand All @@ -104,15 +106,20 @@ function gutenberg_register_theme_block_patterns() {
if ( ! preg_match( '#/(?P<slug>[A-z0-9_-]+)\.php$#', $file, $matches ) ) {
continue;
}

$pattern_data = get_file_data( $file, $default_headers );

if ( empty( $pattern_data[ 'namespace' ] ) ) {
continue;
}

// Example name: twentytwentytwo/query-grid-posts.
$pattern_name = get_stylesheet() . '/' . $matches['slug'];
$pattern_name = $pattern_data[ 'namespace' ] . "/" . $matches['slug'];

if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_name ) ) {
continue;
}

$pattern_data = get_file_data( $file, $default_headers );

// Title is a required property.
if ( ! $pattern_data['title'] ) {
continue;
Expand Down