Skip to content

Commit

Permalink
Allow spaces in slugs. Changed logic to correctly replace functions. …
Browse files Browse the repository at this point in the history
…Updated tests. (#622)
  • Loading branch information
pbking authored May 10, 2024
1 parent b496fc0 commit f3dc5c9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
26 changes: 5 additions & 21 deletions admin/create-theme/theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,7 @@ public static function is_absolute_url( $url ) {
}

public static function get_theme_slug( $new_theme_name ) {
$theme = wp_get_theme();

// 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 = $theme->get( 'TextDomain' );
$new_slug = sanitize_title( $new_theme_name );
$new_slug = preg_replace( '/\s+/', '', $new_slug ); // Remove spaces

if ( ! str_contains( $old_slug, '-' ) && str_contains( $new_slug, '-' ) ) {
return str_replace( '-', '', $new_slug );
}

return $new_slug;
return sanitize_title( $new_theme_name );
}

public static function get_file_extension_from_url( $url ) {
Expand All @@ -31,22 +15,22 @@ public static function get_file_extension_from_url( $url ) {
}

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 );
$new_slug_underscore = str_replace( '-', '_', $new_slug ) . '_';
$old_slug_underscore = str_replace( '-', '_', $old_slug ) . '_';

// Generate placeholders
$placeholder_slug = md5( $old_slug );
$placeholder_slug_underscore = md5( $old_slug_underscore );
$placeholder_name = md5( $old_name );

// Replace old values with placeholders
$content = str_replace( $old_slug, $placeholder_slug, $content );
$content = str_replace( $old_slug_underscore, $placeholder_slug_underscore, $content );
$content = str_replace( $old_slug, $placeholder_slug, $content );
$content = str_replace( $old_name, $placeholder_name, $content );

// Replace placeholders with new values
$content = str_replace( $placeholder_slug, $new_slug, $content );
$content = str_replace( $placeholder_slug_underscore, $new_slug_underscore, $content );
$content = str_replace( $placeholder_slug, $new_slug, $content );
$content = str_replace( $placeholder_name, $new_name, $content );

return $content;
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 @@ -484,7 +484,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'] = Theme_Utils::get_theme_slug( $theme['name'] );
$sanitized_theme['slug'] = sanitize_title( $theme['name'] );
$sanitized_theme['text_domain'] = $sanitized_theme['slug'];
return $sanitized_theme;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test-theme-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function test_replace_namespace_in_code_with_single_word_slug() {
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 );
$updated_code_string = Theme_Utils::replace_namespace( $code_string, 'oldslug', sanitize_title( 'New Slug' ), 'OldSlug', 'New Slug' );
$this->assertStringContainsString( '@package new-slug', $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 );
$this->assertStringContainsString( 'function new_slug_support', $updated_code_string );
$this->assertStringContainsString( "function_exists( 'new_slug_support' )", $updated_code_string );
}
}

0 comments on commit f3dc5c9

Please sign in to comment.