diff --git a/admin/create-theme/theme-templates.php b/admin/create-theme/theme-templates.php index 437a12e2..668ff60c 100644 --- a/admin/create-theme/theme-templates.php +++ b/admin/create-theme/theme-templates.php @@ -131,10 +131,10 @@ public static function clear_user_templates_customizations() { * @param object $template The template to extract content from. * @return object The template with the patternized content. */ - public static function paternize_template( $template ) { + public static function paternize_template( $template, $slug = null ) { // If there is any PHP in the template then paternize if ( str_contains( $template->content, ' $pattern['slug'], ); @@ -173,12 +173,12 @@ public static function prepare_template_for_export( $template, $slug = null, $op $template = Theme_Media::make_template_images_local( $template ); } - $template = self::paternize_template( $template ); - if ( $slug ) { $template = self::replace_template_namespace( $template, $slug ); } + $template = self::paternize_template( $template, $slug ); + return $template; } diff --git a/admin/create-theme/theme-utils.php b/admin/create-theme/theme-utils.php index 3ebec180..45be322d 100644 --- a/admin/create-theme/theme-utils.php +++ b/admin/create-theme/theme-utils.php @@ -30,12 +30,9 @@ public static function get_file_extension_from_url( $url ) { return $extension; } - public static function replace_namespace( $content, $new_slug, $new_name ) { - $theme = wp_get_theme(); - $old_slug = $theme->get( 'TextDomain' ); + public static function replace_namespace( $content, $old_slug, $new_slug, $old_name, $new_name ) { $new_slug_underscore = str_replace( '-', '_', $new_slug ); $old_slug_underscore = str_replace( '-', '_', $old_slug ); - $old_name = $theme->get( 'Name' ); // Generate placeholders $placeholder_slug = md5( $old_slug ); @@ -57,6 +54,10 @@ public static function replace_namespace( $content, $new_slug, $new_name ) { public static function clone_theme_to_folder( $location, $new_slug, $new_name ) { + $theme = wp_get_theme(); + $old_slug = $theme->get( 'TextDomain' ); + $old_name = $theme->get( 'Name' ); + // Get real path for our folder $theme_path = get_stylesheet_directory(); @@ -97,7 +98,7 @@ public static function clone_theme_to_folder( $location, $new_slug, $new_name ) if ( preg_match( "/\.({$valid_extensions_regex})$/", $relative_path ) ) { // Replace namespace values if provided if ( $new_slug ) { - $contents = self::replace_namespace( $contents, $new_slug, $new_name ); + $contents = self::replace_namespace( $contents, $old_slug, $new_slug, $old_name, $new_name ); } } diff --git a/admin/create-theme/theme-zip.php b/admin/create-theme/theme-zip.php index 34087371..b7c39891 100644 --- a/admin/create-theme/theme-zip.php +++ b/admin/create-theme/theme-zip.php @@ -31,6 +31,10 @@ public static function add_theme_json_to_zip( $zip, $export_type ) { public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) { + $theme = wp_get_theme(); + $old_slug = $theme->get( 'TextDomain' ); + $old_name = $theme->get( 'Name' ); + // Get real path for our folder $theme_path = get_stylesheet_directory(); @@ -72,7 +76,7 @@ public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) { // Replace namespace values if provided if ( $new_slug ) { - $contents = Theme_Utils::replace_namespace( $contents, $new_slug, $new_name ); + $contents = Theme_Utils::replace_namespace( $contents, $old_slug, $new_slug, $old_name, $new_name ); } // Add current file to archive diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index 361281a5..1868a194 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -462,7 +462,7 @@ private function sanitize_theme_data( $theme ) { $sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ); $sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ); $sanitized_theme['template'] = ''; - $sanitized_theme['slug'] = sanitize_title( $theme['name'] ); + $sanitized_theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] ); $sanitized_theme['text_domain'] = $sanitized_theme['slug']; return $sanitized_theme; } diff --git a/tests/test-theme-utils.php b/tests/test-theme-utils.php new file mode 100644 index 00000000..6a7ec214 --- /dev/null +++ b/tests/test-theme-utils.php @@ -0,0 +1,66 @@ + + +'; + + $updated_pattern_string = Theme_Utils::replace_namespace( $pattern_string, 'old-slug', 'new-slug', 'Old Name', 'New Name' ); + $this->assertStringContainsString( 'Slug: new-slug/index', $updated_pattern_string ); + $this->assertStringNotContainsString( 'old-slug', $updated_pattern_string ); + + } + + public function test_replace_namespace_in_code() { + $code_string = "assertStringContainsString( '@package new-slug', $updated_code_string ); + $this->assertStringNotContainsString( 'old-slug', $updated_code_string ); + $this->assertStringContainsString( 'function new_slug_support', $updated_code_string ); + $this->assertStringContainsString( "function_exists( 'new_slug_support' )", $updated_code_string ); + } + + public function test_replace_namespace_in_code_with_single_word_slug() { + $code_string = "assertStringContainsString( '@package newslug', $updated_code_string ); + $this->assertStringNotContainsString( 'old-slug', $updated_code_string ); + $this->assertStringContainsString( 'function newslug_support', $updated_code_string ); + $this->assertStringContainsString( "function_exists( 'newslug_support' )", $updated_code_string ); + } +}