Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow spaces in theme slugs #622

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
Loading