From 4f33d0a0b876bab309f46ff4ea6e0b822ad68c72 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Thu, 17 Mar 2022 09:36:54 -0400 Subject: [PATCH] Refactor all of the namespaced items during cloning (#52) Co-authored-by: Maggie --- admin/class-create-block-theme-admin.php | 111 +++++++++++++++++------ 1 file changed, 84 insertions(+), 27 deletions(-) diff --git a/admin/class-create-block-theme-admin.php b/admin/class-create-block-theme-admin.php index 38f66e75..50e9b18c 100644 --- a/admin/class-create-block-theme-admin.php +++ b/admin/class-create-block-theme-admin.php @@ -38,8 +38,8 @@ function export_child_theme( $theme ) { $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = $this->create_zip( $filename ); - $zip = $this->copy_theme_to_zip( $zip ); - $zip = $this->add_templates_to_zip( $zip, 'current' ); + $zip = $this->copy_theme_to_zip( $zip, null, null ); + $zip = $this->add_templates_to_zip( $zip, 'current', null ); $zip = $this->add_theme_json_to_zip( $zip, 'current' ); $zip->close(); @@ -62,17 +62,15 @@ function create_sibling_theme( $theme ) { $theme['uri'] = sanitize_text_field( $theme['uri'] ); $theme['author'] = sanitize_text_field( $theme['author'] ); $theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ); - $theme['slug'] = sanitize_title( $theme['name'] ); + $theme['slug'] = $this->get_theme_slug( $theme['name'] ); $theme['template'] = wp_get_theme()->get( 'Template' ); // Create ZIP file in the temporary directory. $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = $this->create_zip( $filename ); - // TODO: This must have namespaces changed to be complete - $zip = $this->copy_theme_to_zip( $zip ); - - $zip = $this->add_templates_to_zip( $zip, 'current' ); + $zip = $this->copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] ); + $zip = $this->add_templates_to_zip( $zip, 'current', $theme['slug'] ); $zip = $this->add_theme_json_to_zip( $zip, 'current' ); // Add readme.txt. @@ -118,17 +116,16 @@ function clone_theme( $theme ) { $theme['uri'] = sanitize_text_field( $theme['uri'] ); $theme['author'] = sanitize_text_field( $theme['author'] ); $theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ); - $theme['slug'] = sanitize_title( $theme['name'] ); + $theme['slug'] = $this->get_theme_slug( $theme['name'] ); $theme['template'] = wp_get_theme()->get( 'Template' ); // Create ZIP file in the temporary directory. $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = $this->create_zip( $filename ); - // TODO: This must have namespaces changed to be complete - $zip = $this->copy_theme_to_zip( $zip ); + $zip = $this->copy_theme_to_zip( $zip, $theme['slug'], $theme['name']); - $zip = $this->add_templates_to_zip( $zip, 'all' ); + $zip = $this->add_templates_to_zip( $zip, 'all', $theme['slug'] ); $zip = $this->add_theme_json_to_zip( $zip, 'all' ); // Add readme.txt. @@ -173,14 +170,14 @@ function create_child_theme( $theme ) { $theme['uri'] = sanitize_text_field( $theme['uri'] ); $theme['author'] = sanitize_text_field( $theme['author'] ); $theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ); - $theme['slug'] = sanitize_title( $theme['name'] ); + $theme['slug'] = $this->get_theme_slug( $theme['name'] ); $theme['template'] = wp_get_theme()->get( 'TextDomain' ); // Create ZIP file in the temporary directory. $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = $this->create_zip( $filename ); - $zip = $this->add_templates_to_zip( $zip, 'user' ); + $zip = $this->add_templates_to_zip( $zip, 'user', null ); $zip = $this->add_theme_json_to_zip( $zip, 'user' ); // Add readme.txt. @@ -221,8 +218,8 @@ function export_theme( $theme ) { $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = $this->create_zip( $filename ); - $zip = $this->copy_theme_to_zip( $zip ); - $zip = $this->add_templates_to_zip( $zip, 'all' ); + $zip = $this->copy_theme_to_zip( $zip, null, null ); + $zip = $this->add_templates_to_zip( $zip, 'all', null ); $zip = $this->add_theme_json_to_zip( $zip, 'all' ); $zip->close(); @@ -244,7 +241,7 @@ function add_theme_json_to_zip ( $zip, $export_type ) { return $zip; } - function copy_theme_to_zip( $zip ) { + function copy_theme_to_zip( $zip, $new_slug, $new_name ) { // Get real path for our folder $theme_path = get_stylesheet_directory(); @@ -257,13 +254,13 @@ function copy_theme_to_zip( $zip ) { ); // Add all the files (except for templates) - foreach ( $files as $name => $file ) - { + foreach ( $files as $name => $file ) { + // Skip directories (they would be added automatically) - if ( ! $file->isDir() ) - { + if ( ! $file->isDir() ) { + // Get real and relative path for current file - $file_path = $file->getRealPath(); + $file_path = wp_normalize_path( $file ); // If the path is for templates/parts ignore it if ( @@ -275,15 +272,65 @@ function copy_theme_to_zip( $zip ) { continue; } - // Add current file to archive $relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); - $zip->addFile( $file_path, $relative_path ); + + // Replace only text files, skip png's and other stuff. + $valid_extensions = array( 'php', 'css', 'scss', 'js', 'txt', 'html' ); + $valid_extensions_regex = implode( '|', $valid_extensions ); + if ( ! preg_match( "/\.({$valid_extensions_regex})$/", $relative_path ) ) { + $zip->addFile( $file_path, $relative_path ); + } + + else { + $contents = file_get_contents( $file_path ); + + // Replace namespace values if provided + if ( $new_slug ) { + $contents = $this->replace_namespace( $contents, $new_slug, $new_name ); + } + + // Add current file to archive + $zip->addFromString( $relative_path, $contents ); + } + } } return $zip; } + function replace_namespace( $content, $new_slug, $new_name ) { + + $old_slug = wp_get_theme()->get( 'TextDomain' ); + $new_slug_underscore = str_replace( '-', '_', $new_slug ); + $old_slug_underscore = str_replace( '-', '_', $old_slug ); + $old_name = wp_get_theme()->get( 'Name' ); + + $content = str_replace( $old_slug, $new_slug, $content ); + $content = str_replace( $old_slug_underscore, $new_slug_underscore, $content ); + $content = str_replace( $old_name, $new_name, $content ); + + return $content; + } + + function get_theme_slug( $new_theme_name ) { + + // If the source theme has a single-word slug but the new theme has a multi-word slug + // then function will look like: function apple-bumpkin_support() and that won't work. + // There are no issues if it is multi-word>single-word or multi>multi or single>single. + // Due to the complexity of this situation (compared to the simplicity of the others) + // this will enforce the usage of a singleword slug for those themes. + + $old_slug = wp_get_theme()->get( 'TextDomain' ); + $new_slug = sanitize_title( $new_theme_name ); + + if( ! str_contains( $old_slug , '-') && str_contains( $new_slug, '-' ) ) { + return str_replace( '-', '', $new_slug ); + } + + return $new_slug; + } + /** * Add block templates and parts to the zip. * @@ -294,11 +341,12 @@ function copy_theme_to_zip( $zip ) { * user = only user edited templates * all = all templates no matter what */ - function add_templates_to_zip( $zip, $export_type ) { + function add_templates_to_zip( $zip, $export_type, $new_slug ) { $templates = gutenberg_get_block_templates(); $template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); - + $old_slug = wp_get_theme()->get( 'TextDomain' ); + if ( $templates ) { $zip->addEmptyDir( 'templates' ); } @@ -327,6 +375,11 @@ function add_templates_to_zip( $zip, $export_type ) { } $template->content = _remove_theme_attribute_in_block_template_content( $template->content ); + + if ( $new_slug ) { + $template->content = str_replace( $old_slug, $new_slug, $template->content ); + } + $zip->addFromString( 'templates/' . $template->slug . '.html', $template->content @@ -347,6 +400,11 @@ function add_templates_to_zip( $zip, $export_type ) { } $template_part->content = _remove_theme_attribute_in_block_template_content( $template_part->content ); + + if ( $new_slug ) { + $template_part->content = str_replace( $old_slug, $new_slug, $template_part->content ); + } + $zip->addFromString( 'parts/' . $template_part->slug . '.html', $template_part->content @@ -465,8 +523,7 @@ function create_admin_form_page() {



+