From 094fc5b845837293412e9d79cc2dee0b9a8c8cb8 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 6 Apr 2023 14:15:07 -0300 Subject: [PATCH 1/3] Replace theme slug in templates after getting media urls from them to avoid non-existing urls. If we replace the slug before getting the media URLs and those urls contain the old slug name, we get non-existing URLs. Co-authored-by: vcanales --- admin/create-theme/theme-media.php | 11 +++++++---- admin/create-theme/theme-templates.php | 12 +++++++++++- admin/create-theme/theme-zip.php | 17 +++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/admin/create-theme/theme-media.php b/admin/create-theme/theme-media.php index 7ddd6e64..5d7279b2 100644 --- a/admin/create-theme/theme-media.php +++ b/admin/create-theme/theme-media.php @@ -138,11 +138,14 @@ public static function make_relative_media_url( $absolute_url ) { public static function add_media_to_local( $media ) { foreach ( $media as $url ) { $download_file = download_url( $url ); - $media_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . self::get_media_folder_path_from_url( $url ); - if ( ! is_dir( $media_path ) ) { - wp_mkdir_p( $media_path ); + // TODO: implement a warning if the file is missing + if ( is_wp_error( $download_file ) ) { + $media_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . self::get_media_folder_path_from_url( $url ); + if ( ! is_dir( $media_path ) ) { + wp_mkdir_p( $media_path ); + } + rename( $download_file, $media_path . basename( $url ) ); } - rename( $download_file, $media_path . basename( $url ) ); } } } diff --git a/admin/create-theme/theme-templates.php b/admin/create-theme/theme-templates.php index 9d176678..3e426a37 100644 --- a/admin/create-theme/theme-templates.php +++ b/admin/create-theme/theme-templates.php @@ -75,10 +75,18 @@ static function filter_theme_template( $template, $export_type, $path, $old_slug // This replaces that with dashes again. We should consider decoding the entire string but that is proving difficult. $template->content = str_replace( '\u002d', '-', $template->content ); + // if ( $new_slug ) { + // $template->content = str_replace( $old_slug, $new_slug, $template->content ); + // } + + return $template; + } + + public static function replace_template_namespace( $template, $new_slug ) { + $old_slug = wp_get_theme()->get( 'TextDomain' ); if ( $new_slug ) { $template->content = str_replace( $old_slug, $new_slug, $template->content ); } - return $template; } @@ -113,6 +121,7 @@ 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_Templates::replace_template_namespace( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( ! empty( $template_data->media ) ) { @@ -153,6 +162,7 @@ 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_Templates::replace_template_namespace( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( ! empty( $template_data->media ) ) { diff --git a/admin/create-theme/theme-zip.php b/admin/create-theme/theme-zip.php index 293ed880..200c2673 100644 --- a/admin/create-theme/theme-zip.php +++ b/admin/create-theme/theme-zip.php @@ -101,6 +101,7 @@ 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_Templates::replace_template_namespace( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( count( $template_data->media ) > 0 ) { @@ -130,6 +131,7 @@ 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_Templates::replace_template_namespace( $template_data, $new_slug ); // If there are images in the template, add it as a pattern if ( count( $template_data->media ) > 0 ) { @@ -162,12 +164,15 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) { static function add_media_to_zip( $zip, $media ) { $media = array_unique( $media ); foreach ( $media as $url ) { - $folder_path = Theme_Media::get_media_folder_path_from_url( $url ); - $download_file = download_url( $url ); - $content_array = file( $download_file ); - $file_as_string = implode( '', $content_array ); - - $zip->addFromString( $folder_path . basename( $url ), $file_as_string ); + $folder_path = Theme_Media::get_media_folder_path_from_url( $url ); + $download_file = download_url( $url ); + // If there was an error downloading the file, skip it. + // TODO: Implement a warning if the file is missing + if ( ! is_wp_error( $download_file ) ) { + $content_array = file( $download_file ); + $file_as_string = implode( '', $content_array ); + $zip->addFromString( $folder_path . basename( $url ), $file_as_string ); + } } } From 39819b5d8cafe77a444ab4b1004c284b625234d3 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 6 Apr 2023 14:21:49 -0300 Subject: [PATCH 2/3] Removing commented code Co-authored-by: vcanales --- admin/create-theme/theme-templates.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/admin/create-theme/theme-templates.php b/admin/create-theme/theme-templates.php index 3e426a37..7da13aa5 100644 --- a/admin/create-theme/theme-templates.php +++ b/admin/create-theme/theme-templates.php @@ -75,10 +75,6 @@ static function filter_theme_template( $template, $export_type, $path, $old_slug // This replaces that with dashes again. We should consider decoding the entire string but that is proving difficult. $template->content = str_replace( '\u002d', '-', $template->content ); - // if ( $new_slug ) { - // $template->content = str_replace( $old_slug, $new_slug, $template->content ); - // } - return $template; } From 448b6627726831dabbf49f37b3af9a7004c2c395 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 6 Apr 2023 14:35:36 -0300 Subject: [PATCH 3/3] Removing parameters no longer used Co-authored-by: vcanales --- admin/create-theme/theme-templates.php | 16 +++++----------- admin/create-theme/theme-zip.php | 2 +- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/admin/create-theme/theme-templates.php b/admin/create-theme/theme-templates.php index 7da13aa5..cecab40d 100644 --- a/admin/create-theme/theme-templates.php +++ b/admin/create-theme/theme-templates.php @@ -8,9 +8,7 @@ class Theme_Templates { /* * Build a collection of templates and template-parts that should be exported (and modified) based on the given export_type and new slug */ - public static function get_theme_templates( $export_type, $new_slug ) { - - $old_slug = wp_get_theme()->get( 'TextDomain' ); + public static function get_theme_templates( $export_type ) { $templates = get_block_templates(); $template_parts = get_block_templates( array(), 'wp_template_part' ); $exported_templates = array(); @@ -25,9 +23,7 @@ public static function get_theme_templates( $export_type, $new_slug ) { $template = self::filter_theme_template( $template, $export_type, - $templates_path, - $old_slug, - $new_slug + $templates_path ); if ( $template ) { $exported_templates[] = $template; @@ -38,9 +34,7 @@ public static function get_theme_templates( $export_type, $new_slug ) { $template = self::filter_theme_template( $template, $export_type, - $parts_path, - $old_slug, - $new_slug + $parts_path ); if ( $template ) { $exported_parts[] = $template; @@ -59,7 +53,7 @@ public static function get_theme_templates( $export_type, $new_slug ) { * Templates not filtered out are modified based on the slug information provided and cleaned up * to have the expected exported value. */ - static function filter_theme_template( $template, $export_type, $path, $old_slug, $new_slug ) { + static function filter_theme_template( $template, $export_type, $path ) { if ( 'theme' === $template->source && 'user' === $export_type ) { return false; } @@ -107,7 +101,7 @@ public static function clear_user_templates_customizations() { public static function add_templates_to_local( $export_type ) { - $theme_templates = self::get_theme_templates( $export_type, null ); + $theme_templates = self::get_theme_templates( $export_type ); $template_folders = get_block_theme_folders(); // If there is no templates folder, create it. diff --git a/admin/create-theme/theme-zip.php b/admin/create-theme/theme-zip.php index 200c2673..29b6efca 100644 --- a/admin/create-theme/theme-zip.php +++ b/admin/create-theme/theme-zip.php @@ -89,7 +89,7 @@ public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) { * all = all templates no matter what */ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) { - $theme_templates = Theme_Templates::get_theme_templates( $export_type, $new_slug ); + $theme_templates = Theme_Templates::get_theme_templates( $export_type ); if ( $theme_templates->templates ) { $zip->addEmptyDir( 'templates' );