diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php
index b6a33a27..c92af16c 100644
--- a/themes/10up-theme/includes/blocks.php
+++ b/themes/10up-theme/includes/blocks.php
@@ -25,8 +25,8 @@ function setup() {
add_filter( 'block_categories_all', $n( 'blocks_categories' ), 10, 2 );
add_action( 'init', $n( 'register_theme_blocks' ) );
-
- add_action( 'init', $n( 'block_patterns_and_categories' ) );
+ add_action( 'init', $n( 'register_theme_block_patterns' ) );
+ add_action( 'init', $n( 'register_block_pattern_categories' ) );
/*
If you are using the block library, remove the blocks you don't need.
@@ -109,37 +109,145 @@ 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')
+/**
+ * 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..54126515
--- /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..26727997
--- /dev/null
+++ b/themes/10up-theme/patterns/example-php-pattern.php
@@ -0,0 +1,12 @@
+
+
+Example PHP Pattern
+