Skip to content

Commit

Permalink
Merge pull request #301 from WordPress/fix/child-themes-slugs
Browse files Browse the repository at this point in the history
Fix theme slug, textdomain, and template for cloned, child and sibling themes.
  • Loading branch information
matiasbenedetto authored Apr 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 0374c69 + b6b2d86 commit 9db048f
Showing 8 changed files with 57 additions and 22 deletions.
33 changes: 24 additions & 9 deletions admin/class-create-block-theme-admin.php
Original file line number Diff line number Diff line change
@@ -71,14 +71,14 @@ function save_variation( $export_type, $theme ) {
* Export activated child theme
*/
function export_child_theme( $theme ) {
$theme['slug'] = wp_get_theme()->get( 'TextDomain' );
$theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] );

// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), $theme['slug'] );
$zip = Theme_Zip::create_zip( $filename );

$zip = Theme_Zip::copy_theme_to_zip( $zip, null, null );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', null );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' );

$zip->close();
@@ -95,15 +95,18 @@ function export_child_theme( $theme ) {
* Create a sibling theme of the activated theme
*/
function create_sibling_theme( $theme, $screenshot ) {
$theme_slug = Theme_Utils::get_theme_slug( $theme['name'] );

// Sanitize inputs.
$theme['name'] = sanitize_text_field( $theme['name'] );
$theme['description'] = sanitize_text_field( $theme['description'] );
$theme['uri'] = sanitize_text_field( $theme['uri'] );
$theme['author'] = sanitize_text_field( $theme['author'] );
$theme['author_uri'] = sanitize_text_field( $theme['author_uri'] );
$theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] );
$theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] );
$theme['slug'] = $theme_slug;
$theme['template'] = wp_get_theme()->get( 'Template' );
$theme['text_domain'] = $theme_slug;

// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), $theme['slug'] );
@@ -152,16 +155,19 @@ function create_sibling_theme( $theme, $screenshot ) {
* Clone the activated theme to create a new theme
*/
function clone_theme( $theme, $screenshot ) {
$theme_slug = Theme_Utils::get_theme_slug( $theme['name'] );

// Sanitize inputs.
$theme['name'] = sanitize_text_field( $theme['name'] );
$theme['description'] = sanitize_text_field( $theme['description'] );
$theme['uri'] = sanitize_text_field( $theme['uri'] );
$theme['author'] = sanitize_text_field( $theme['author'] );
$theme['author_uri'] = sanitize_text_field( $theme['author_uri'] );
$theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] );
$theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] );
$theme['template'] = wp_get_theme()->get( 'Template' );
$theme['slug'] = $theme_slug;
$theme['template'] = '';
$theme['original_theme'] = wp_get_theme()->get( 'Name' );
$theme['text_domain'] = $theme_slug;

// Use previous theme's tags if custom tags are empty.
if ( empty( $theme['tags_custom'] ) ) {
@@ -236,21 +242,27 @@ public function register_theme_export() {
* Create a child theme of the activated theme
*/
function create_child_theme( $theme, $screenshot ) {

$parent_theme_slug = Theme_Utils::get_theme_slug( wp_get_theme()->get( 'Name' ) );
$child_theme_slug = Theme_Utils::get_theme_slug( $theme['name'] );

// Sanitize inputs.
$theme['name'] = sanitize_text_field( $theme['name'] );
$theme['description'] = sanitize_text_field( $theme['description'] );
$theme['uri'] = sanitize_text_field( $theme['uri'] );
$theme['author'] = sanitize_text_field( $theme['author'] );
$theme['author_uri'] = sanitize_text_field( $theme['author_uri'] );
$theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] );
$theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] );
$theme['template'] = wp_get_theme()->get( 'TextDomain' );

$theme['text_domain'] = $child_theme_slug;
$theme['template'] = $parent_theme_slug;
$theme['slug'] = $child_theme_slug;

// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), $theme['slug'] );
$zip = Theme_Zip::create_zip( $filename );

$zip = Theme_Zip::add_templates_to_zip( $zip, 'user', null );
$zip = Theme_Zip::add_templates_to_zip( $zip, 'user', $theme['slug'] );
$zip = Theme_Zip::add_theme_json_to_zip( $zip, 'user' );

// Add readme.txt.
@@ -308,6 +320,8 @@ function export_theme( $theme ) {
}

function create_blank_theme( $theme, $screenshot ) {
$theme_slug = Theme_Utils::get_theme_slug( $theme['name'] );

// Sanitize inputs.
$theme['name'] = sanitize_text_field( $theme['name'] );
$theme['description'] = sanitize_text_field( $theme['description'] );
@@ -316,7 +330,8 @@ function create_blank_theme( $theme, $screenshot ) {
$theme['author_uri'] = sanitize_text_field( $theme['author_uri'] );
$theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] );
$theme['template'] = '';
$theme['slug'] = Theme_Utils::get_theme_slug( $theme['name'] );
$theme['slug'] = $theme_slug;
$theme['text_domain'] = $theme_slug;

// Create theme directory.
$source = plugin_dir_path( __DIR__ ) . 'assets/boilerplate';
1 change: 1 addition & 0 deletions admin/create-theme/theme-blocks.php
Original file line number Diff line number Diff line change
@@ -188,4 +188,5 @@ static function clean_serialized_markup( $markup ) {
$markup = html_entity_decode( $markup, ENT_QUOTES | ENT_XML1, 'UTF-8' );
return $markup;
}

}
3 changes: 1 addition & 2 deletions admin/create-theme/theme-form.php
Original file line number Diff line number Diff line change
@@ -53,8 +53,7 @@ public static function create_admin_form_page() {
</label>
<br />
<?php _e( '[Create a new theme cloning the activated child theme. The parent theme will be the same as the parent of the currently activated theme. The resulting theme will have all of the assets of the activated theme, none of the assets provided by the parent theme, as well as user changes.]', 'create-block-theme' ); ?>
<p><b><?php _e( 'NOTE: Sibling themes created from this theme will have the original namespacing. This should be changed manually once the theme has been created.', 'create-block-theme' ); ?></b></p>
<br />
<br /><br />
<?php else : ?>
<label>
<input value="child" type="radio" name="theme[type]" class="regular-text code" onchange="toggleForm( this );"/>
10 changes: 8 additions & 2 deletions admin/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

class Theme_Patterns {
public static function pattern_from_template( $template ) {
$theme_slug = wp_get_theme()->get( 'TextDomain' );
public static function pattern_from_template( $template, $new_slug = null ) {
$theme_slug = $new_slug ? $new_slug : wp_get_theme()->get( 'TextDomain' );
$pattern_slug = $theme_slug . '/' . $template->slug;
$pattern_content = (
'<?php
@@ -61,4 +61,10 @@ static function escape_text_for_pattern( $text ) {
return "<?php echo esc_attr_e( '" . $text . "', '" . wp_get_theme()->get( 'Name' ) . "' ); ?>";
}
}

public static function create_pattern_link( $attributes ) {
$block_attributes = array_filter( $attributes );
$attributes_json = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES );
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
}
}
3 changes: 2 additions & 1 deletion admin/create-theme/theme-styles.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public static function build_child_style_css( $theme ) {
$author_uri = $theme['author_uri'];
$wp_version = get_bloginfo( 'version' );
$template = $theme['template'];
$text_domain = $theme['text_domain'];
$tags = Theme_Tags::theme_tags_list( $theme );
return "/*
Theme Name: {$name}
@@ -29,7 +30,7 @@ public static function build_child_style_css( $theme ) {
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Template: {$template}
Text Domain: {$slug}
Text Domain: {$text_domain}
Tags: {$tags}
*/";
}
14 changes: 10 additions & 4 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
@@ -122,8 +122,11 @@ public static function add_templates_to_local( $export_type ) {
}

// If there are external images, add it as a pattern
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"' . $pattern['slug'] . '"} /-->';
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Write the pattern
file_put_contents(
@@ -159,8 +162,11 @@ public static function add_templates_to_local( $export_type ) {
}

// If there are external images, add it as a pattern
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"' . $pattern['slug'] . '"} /-->';
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Write the pattern
file_put_contents(
14 changes: 10 additions & 4 deletions admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
@@ -104,8 +104,11 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {

// If there are images in the template, add it as a pattern
if ( count( $template_data->media ) > 0 ) {
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"' . $pattern['slug'] . '"} /-->';
$pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Add pattern to zip
$zip->addFromString(
@@ -130,8 +133,11 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {

// If there are images in the template, add it as a pattern
if ( count( $template_data->media ) > 0 ) {
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$template_data->content = '<!-- wp:pattern {"slug":"' . $pattern['slug'] . '"} /-->';
$pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

// Add pattern to zip
$zip->addFromString(
1 change: 1 addition & 0 deletions admin/js/form-script.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ function toggleForm( element ) {
case 'child':
case 'clone':
case 'blank':
case 'sibling':
// Show New Theme form
document
.getElementById( 'new_theme_metadata_form' )

0 comments on commit 9db048f

Please sign in to comment.