From d6ca550787d513c1d7849735bd288f40a648b3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Wed, 16 Feb 2022 09:26:47 +0100 Subject: [PATCH 1/2] automatically register block patterns located in directory This is using the work done by @mcsf in WordPress/gutenberg#36751 and applying it to the usecase of the wp-scaffold theme --- themes/10up-theme/includes/blocks.php | 129 ++++++++++++++++++ .../patterns/example-html-pattern.html | 7 + .../patterns/example-php-pattern.php | 12 ++ 3 files changed, 148 insertions(+) create mode 100644 themes/10up-theme/patterns/example-html-pattern.html create mode 100644 themes/10up-theme/patterns/example-php-pattern.php diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index b6a33a27..f04be376 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -25,6 +25,7 @@ function setup() { add_filter( 'block_categories_all', $n( 'blocks_categories' ), 10, 2 ); add_action( 'init', $n( 'register_theme_blocks' ) ); + add_action( 'init', $n( 'register_theme_block_patterns' ) ); add_action( 'init', $n( 'block_patterns_and_categories' ) ); @@ -143,3 +144,131 @@ function block_patterns_and_categories() { */ } + +/** + * Register theme block patterns + */ +function register_theme_block_patterns() { + $default_headers = array( + 'title' => 'Pattern Name', + 'description' => 'Description', + 'viewportWidth' => 'Viewport Width', + 'categories' => 'Categories', + 'keywords' => 'Keywords', + 'blockTypes' => 'Block Types', + ); + + // Register patterns for the active theme, for both parent and child theme, + // if applicable. + foreach ( wp_get_active_and_valid_themes() as $theme ) { + $pattern_directory_path = $theme . '/patterns/'; + if ( file_exists( $pattern_directory_path ) ) { + $files = glob( $pattern_directory_path . '*.html' ); + if ( $files ) { + foreach ( $files as $file ) { + // Parse pattern slug from file name. + if ( ! preg_match( '#/(?P[A-z0-9_-]+)\.html$#', $file, $matches ) ) { + continue; + } + // Example name: 10up-theme/example-pattern. + $pattern_name = get_stylesheet() . '/' . $matches['slug']; + + $pattern_options = get_file_data( $file, $default_headers ); + + // Title is a required property. + if ( ! $pattern_options['title'] ) { + continue; + } + + // For properties of type array, parse data as comma-separated. + foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { + $pattern_options[ $property ] = array_filter( + preg_split( + '/[\s,]+/', + (string) $pattern_options[ $property ] + ) + ); + } + + // Parse properties of type int. + foreach ( array( 'viewportWidth' ) as $property ) { + $pattern_options[ $property ] = (int) $pattern_options[ $property ]; + } + + // Remove up empty values, so as not to override defaults. + foreach ( array_keys( $default_headers ) as $property ) { + if ( empty( $pattern_options[ $property ] ) ) { + unset( $pattern_options[ $property ] ); + } + } + + // The actual pattern is everything following the leading comment. + $raw_content = file_get_contents( $file ); + $token = '-->'; + $pattern_options['content'] = substr( + $raw_content, + strpos( $raw_content, $token ) + strlen( $token ) + ); + if ( ! $pattern_options['content'] ) { + continue; + } + + register_block_pattern( $pattern_name, $pattern_options ); + } + } + } + if ( file_exists( $pattern_directory_path ) ) { + $files = glob( $pattern_directory_path . '*.php' ); + if ( $files ) { + foreach ( $files as $file ) { + + // Parse pattern slug from file name. + if ( ! preg_match( '#/(?P[A-z0-9_-]+)\.php$#', $file, $matches ) ) { + continue; + } + // Example name: 10up-theme/example-pattern. + $pattern_name = get_stylesheet() . '/' . $matches['slug']; + + $pattern_options = get_file_data( $file, $default_headers ); + + // Title is a required property. + if ( ! $pattern_options['title'] ) { + continue; + } + + // For properties of type array, parse data as comma-separated. + foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { + $pattern_options[ $property ] = array_filter( + preg_split( + '/[\s,]+/', + (string) $pattern_options[ $property ] + ) + ); + } + + // Parse properties of type int. + foreach ( array( 'viewportWidth' ) as $property ) { + $pattern_options[ $property ] = (int) $pattern_options[ $property ]; + } + + // Remove up empty values, so as not to override defaults. + foreach ( array_keys( $default_headers ) as $property ) { + if ( empty( $pattern_options[ $property ] ) ) { + unset( $pattern_options[ $property ] ); + } + } + + // The actual pattern content is the output of the file. + ob_start(); + include $file; + $pattern_options['content'] = ob_get_clean(); + if ( ! $pattern_options['content'] ) { + continue; + } + + register_block_pattern( $pattern_name, $pattern_options ); + } + } + } + } +} diff --git a/themes/10up-theme/patterns/example-html-pattern.html b/themes/10up-theme/patterns/example-html-pattern.html new file mode 100644 index 00000000..d1c1dfdf --- /dev/null +++ b/themes/10up-theme/patterns/example-html-pattern.html @@ -0,0 +1,7 @@ + + +

Example HTML Pattern

+ diff --git a/themes/10up-theme/patterns/example-php-pattern.php b/themes/10up-theme/patterns/example-php-pattern.php new file mode 100644 index 00000000..5bd5d219 --- /dev/null +++ b/themes/10up-theme/patterns/example-php-pattern.php @@ -0,0 +1,12 @@ + + +

Example PHP Pattern

+ From 6422a2586dc45a9b75fb010fc6c1643a5425901a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Wed, 16 Feb 2022 09:31:24 +0100 Subject: [PATCH 2/2] update existing example of how to include patterns --- themes/10up-theme/includes/blocks.php | 31 +++---------------- .../patterns/example-html-pattern.html | 2 +- .../patterns/example-php-pattern.php | 2 +- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index f04be376..c92af16c 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -26,8 +26,7 @@ function setup() { add_action( 'init', $n( 'register_theme_blocks' ) ); add_action( 'init', $n( 'register_theme_block_patterns' ) ); - - add_action( 'init', $n( 'block_patterns_and_categories' ) ); + add_action( 'init', $n( 'register_block_pattern_categories' ) ); /* If you are using the block library, remove the blocks you don't need. @@ -110,39 +109,19 @@ function blocks_categories( $categories ) { } /** - * Manage block patterns and block pattern categories + * Manage block pattern categories * * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns/ * * @return void */ -function block_patterns_and_categories() { - /* - ## Examples - - // Register block pattern - register_block_pattern( - 'tenup/block-pattern', - array( - 'title' => __( 'Two buttons', 'tenup' ), - 'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ), - 'content' => "\n\n", - ) - ); - - // Unregister a block pattern - unregister_block_pattern( 'tenup/block-pattern' ); +function register_block_pattern_categories() { // Register a block pattern category register_block_pattern_category( - 'client-name', - array( 'label' => __( 'Client Name', 'tenup' ) ) + '10up-theme', + array( 'label' => __( '10up Theme', 'tenup' ) ) ); - - // Unregister a block pattern category - unregister_block_pattern('client-name') - - */ } /** diff --git a/themes/10up-theme/patterns/example-html-pattern.html b/themes/10up-theme/patterns/example-html-pattern.html index d1c1dfdf..54126515 100644 --- a/themes/10up-theme/patterns/example-html-pattern.html +++ b/themes/10up-theme/patterns/example-html-pattern.html @@ -1,6 +1,6 @@

Example HTML Pattern

diff --git a/themes/10up-theme/patterns/example-php-pattern.php b/themes/10up-theme/patterns/example-php-pattern.php index 5bd5d219..26727997 100644 --- a/themes/10up-theme/patterns/example-php-pattern.php +++ b/themes/10up-theme/patterns/example-php-pattern.php @@ -1,7 +1,7 @@