From b6b2d86debf0e20bc5d3d399cda585ce128509a0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 4 Apr 2023 15:31:42 -0300 Subject: [PATCH] setting the right theme slug for newly created patterns --- admin/create-theme/theme-blocks.php | 41 -------------------------- admin/create-theme/theme-patterns.php | 6 ++-- admin/create-theme/theme-templates.php | 8 ++--- admin/create-theme/theme-zip.php | 12 +++----- 4 files changed, 9 insertions(+), 58 deletions(-) diff --git a/admin/create-theme/theme-blocks.php b/admin/create-theme/theme-blocks.php index 4b8a915b..833daf98 100644 --- a/admin/create-theme/theme-blocks.php +++ b/admin/create-theme/theme-blocks.php @@ -189,45 +189,4 @@ static function clean_serialized_markup( $markup ) { return $markup; } - // update pattern and template-parts theme attribute - public static function update_patterns_and_templates_theme_attribute( $template, $theme_slug ) { - $new_content = $template->content; - $template_blocks = parse_blocks( $template->content ); - - $blocks = self::update_blocks_theme_attribute( $template_blocks, $theme_slug ); - $blocks = serialize_blocks( $blocks ); - - $template->content = self::clean_serialized_markup( $blocks ); - return $template; - } - - static function update_blocks_theme_attribute( $nested_blocks, $theme_slug ) { - $new_blocks = array(); - foreach ( $nested_blocks as $block ) { - $inner_blocks = $block['innerBlocks']; - switch ( $block['blockName'] ) { - case 'core/pattern': - case 'core/template-part': - $block = self::update_block_theme_attribute( $block, $theme_slug ); - break; - } - // recursive call for inner blocks - if ( ! empty( $block['innerBlocks'] ) ) { - $block['innerBlocks'] = self::update_blocks_theme_attribute( $inner_blocks, $theme_slug ); - } - $new_blocks[] = $block; - } - return $new_blocks; - } - - static function update_block_theme_attribute( $block, $theme_slug ) { - if ( 'core/pattern' === $block['blockName'] || 'core/template-part' === $block['blockName'] ) { - // Adds the theme attribute to the block only if is set, otherwise it won't be added - if ( ! empty( $block['attrs']['theme'] ) ) { - $block['attrs']['theme'] = $theme_slug; - } - } - return $block; - } - } diff --git a/admin/create-theme/theme-patterns.php b/admin/create-theme/theme-patterns.php index b6625f2e..dafde8af 100644 --- a/admin/create-theme/theme-patterns.php +++ b/admin/create-theme/theme-patterns.php @@ -1,8 +1,8 @@ get( 'TextDomain' ); + public static function pattern_from_template( $template, $new_slug = null ) { + $theme_slug = $new_slug ? $new_slug : wp_get_theme()->get( 'TextDomain' ); $pattern_slug = $theme_slug . '/' . $template->slug; $pattern_content = ( ''; } } diff --git a/admin/create-theme/theme-templates.php b/admin/create-theme/theme-templates.php index 61a48fb3..9d176678 100644 --- a/admin/create-theme/theme-templates.php +++ b/admin/create-theme/theme-templates.php @@ -113,7 +113,6 @@ public static function add_templates_to_local( $export_type ) { foreach ( $theme_templates->templates as $template ) { $template_data = Theme_Blocks::make_template_images_local( $template ); - $template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $theme_slug ); // If there are images in the template, add it as a pattern if ( ! empty( $template_data->media ) ) { @@ -125,8 +124,7 @@ public static function add_templates_to_local( $export_type ) { // If there are external images, add it as a pattern $pattern = Theme_Patterns::pattern_from_template( $template_data ); $pattern_link_attributes = array( - 'slug' => $pattern['slug'], - 'theme' => $new_slug, + 'slug' => $pattern['slug'], ); $template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes ); @@ -155,7 +153,6 @@ public static function add_templates_to_local( $export_type ) { foreach ( $theme_templates->parts as $template_part ) { $template_data = Theme_Blocks::make_template_images_local( $template_part ); - $template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $theme_slug ); // If there are images in the template, add it as a pattern if ( ! empty( $template_data->media ) ) { @@ -167,8 +164,7 @@ public static function add_templates_to_local( $export_type ) { // If there are external images, add it as a pattern $pattern = Theme_Patterns::pattern_from_template( $template_data ); $pattern_link_attributes = array( - 'slug' => $pattern['slug'], - 'theme' => $new_slug, + 'slug' => $pattern['slug'], ); $template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes ); diff --git a/admin/create-theme/theme-zip.php b/admin/create-theme/theme-zip.php index 02a68565..293ed880 100644 --- a/admin/create-theme/theme-zip.php +++ b/admin/create-theme/theme-zip.php @@ -101,14 +101,12 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) { foreach ( $theme_templates->templates as $template ) { $template_data = Theme_Blocks::make_template_images_local( $template ); - $template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( count( $template_data->media ) > 0 ) { - $pattern = Theme_Patterns::pattern_from_template( $template_data ); + $pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug ); $pattern_link_attributes = array( - 'slug' => $pattern['slug'], - 'theme' => $new_slug, + 'slug' => $pattern['slug'], ); $template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes ); @@ -132,14 +130,12 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) { foreach ( $theme_templates->parts as $template_part ) { $template_data = Theme_Blocks::make_template_images_local( $template_part ); - $template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( count( $template_data->media ) > 0 ) { - $pattern = Theme_Patterns::pattern_from_template( $template_data ); + $pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug ); $pattern_link_attributes = array( - 'slug' => $pattern['slug'], - 'theme' => $new_slug, + 'slug' => $pattern['slug'], ); $template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );