From 9961cedfeb6f3e2bf11f1cd6177ef0f120cd6736 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Aug 2024 12:49:38 +0100 Subject: [PATCH] Update templates that reference the pattern --- includes/create-theme/theme-patterns.php | 22 ++++++++++++++++++++++ includes/create-theme/theme-templates.php | 13 ++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/includes/create-theme/theme-patterns.php b/includes/create-theme/theme-patterns.php index 493bdb35..707885f7 100644 --- a/includes/create-theme/theme-patterns.php +++ b/includes/create-theme/theme-patterns.php @@ -73,6 +73,25 @@ public static function create_pattern_link( $attributes ) { } public static function replace_local_pattern_references( $pattern ) { + // Find any references to pattern in templates + $templates_to_update = array(); + $args = array( + 'post_type' => array( 'wp_template', 'wp_template_part' ), + 'posts_per_page' => -1, + 's' => 'wp:block {"ref":' . $pattern->id . '}', + ); + $find_pattern_refs = new WP_Query( $args ); + if ( $find_pattern_refs->have_posts() ) { + foreach ( $find_pattern_refs->posts as $post ) { + $slug = $post->post_name; + array_push( $templates_to_update, $slug ); + } + } + $templates_to_update = array_unique( $templates_to_update ); + + // Only update templates that reference the pattern + CBT_Theme_Templates::add_templates_to_local( 'all', null, null, $options, $templates_to_update ); + // List all template and pattern files in the theme $base_dir = get_stylesheet_directory(); $patterns = glob( $base_dir . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . '*.php' ); @@ -85,6 +104,9 @@ public static function replace_local_pattern_references( $pattern ) { $file_content = str_replace( 'wp:block {"ref":' . $pattern->id . '}', 'wp:pattern {"slug":"' . $pattern->slug . '"}', $file_content ); file_put_contents( $file, $file_content ); } + + CBT_Theme_Templates::clear_user_templates_customizations(); + CBT_Theme_Templates::clear_user_template_parts_customizations(); } public static function prepare_pattern_for_export( $pattern, $options = null ) { diff --git a/includes/create-theme/theme-templates.php b/includes/create-theme/theme-templates.php index 7b1ea1bf..7837b124 100644 --- a/includes/create-theme/theme-templates.php +++ b/includes/create-theme/theme-templates.php @@ -10,12 +10,13 @@ class CBT_Theme_Templates { * based on the given export_type. * * @param string $export_type The type of export to perform. 'all', 'current', or 'user'. + * @param array $templates_to_export List of specific templates to export. * @return object An object containing the templates and parts that should be exported. */ - public static function get_theme_templates( $export_type ) { + public static function get_theme_templates( $export_type, $templates_to_export = null ) { - $templates = get_block_templates(); - $template_parts = get_block_templates( array(), 'wp_template_part' ); + $templates = get_block_templates( array( 'slug__in' => $templates_to_export ) ); + $template_parts = get_block_templates( array( 'slug__in' => $templates_to_export ), 'wp_template_part' ); $exported_templates = array(); $exported_parts = array(); @@ -195,10 +196,12 @@ public static function prepare_template_for_export( $template, $slug = null, $op * @param string $export_type The type of export to perform. 'all', 'current', or 'user'. * @param string $path The path to the theme folder. If null it is assumed to be the current theme. * @param string $slug The slug of the theme. If null it is assumed to be the current theme. + * @param array $options An array of options to use when exporting the templates. + * @param array $templates_to_export List of specific templates to export. If null it will be fetched. */ - public static function add_templates_to_local( $export_type, $path = null, $slug = null, $options = null ) { + public static function add_templates_to_local( $export_type, $path = null, $slug = null, $options = null, $templates_to_export = null ) { - $theme_templates = self::get_theme_templates( $export_type ); + $theme_templates = self::get_theme_templates( $export_type, $templates_to_export ); $template_folders = get_block_theme_folders(); $base_dir = $path ? $path : get_stylesheet_directory();