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

Fix theme slug and textdomain for cloned, child and sibling themes. #301

Merged
merged 5 commits into from
Apr 5, 2023
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
33 changes: 24 additions & 9 deletions admin/class-create-block-theme-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'] );
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'] );
Expand All @@ -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';
Expand Down
1 change: 1 addition & 0 deletions admin/create-theme/theme-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 );"/>
Expand Down
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
Expand Down Expand Up @@ -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
Expand Up @@ -14,6 +14,7 @@ public static function build_child_style_css( $theme ) {
$author = $theme['author'];
$author_uri = $theme['author_uri'];
$template = $theme['template'];
$text_domain = $theme['text_domain'];
$tags = Theme_Tags::theme_tags_list( $theme );
return "/*
Theme Name: {$name}
Expand All @@ -28,7 +29,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}
*/";
}
Expand Down
14 changes: 10 additions & 4 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions admin/js/form-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down