Skip to content

Commit

Permalink
Fix/un transposed patterns (#567)
Browse files Browse the repository at this point in the history
* Added tests to validate replacing theme namespace and refactored to support it
* Pass slug to patternize content when cloning theme
* Fixed creating of theme slug from name using no -'s to fix namespace swapping issue
  • Loading branch information
pbking authored Apr 18, 2024
1 parent 6ab7656 commit 2fec309
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
8 changes: 4 additions & 4 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<?php' ) ) {
$pattern = Theme_Patterns::pattern_from_template( $template );
$pattern = Theme_Patterns::pattern_from_template( $template, $slug );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
);
Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 6 additions & 5 deletions admin/create-theme/theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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();

Expand Down Expand Up @@ -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 );
}
}

Expand Down
6 changes: 5 additions & 1 deletion admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
66 changes: 66 additions & 0 deletions tests/test-theme-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @package Create_Block_Theme
*/
class Test_Create_Block_Theme_Utils extends WP_UnitTestCase {

public function test_replace_namespace_in_pattern() {
$pattern_string = '<?php
/**
* Title: index
* Slug: old-slug/index
* Categories: hidden
* Inserter: no
*/
?>
<!-- wp:template-part {"slug":"header-minimal","tagName":"header"} /-->
';

$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 = "<?php
/**
* old-slug functions and definitions
*
* @package old-slug
* @since old-slug 1.0
*/
if ( ! function_exists( 'old_slug_support' ) ) :
function old_slug_support() {
";

$updated_code_string = Theme_Utils::replace_namespace( $code_string, 'old-slug', 'new-slug', 'Old Name', 'New Name' );
$this->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 = "<?php
/**
* oldslug functions and definitions
*
* @package oldslug
* @since oldslug 1.0
*/
if ( ! function_exists( 'oldslug_support' ) ) :
function oldslug_support() {
";

$updated_code_string = Theme_Utils::replace_namespace( $code_string, 'oldslug', Theme_Utils::get_theme_slug( 'New Slug' ), 'OldSlug', 'New Slug' );
$this->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 );
}
}

0 comments on commit 2fec309

Please sign in to comment.