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

Don't Clobber Metadata #634

Merged
merged 8 commits into from
May 16, 2024
42 changes: 24 additions & 18 deletions admin/create-theme/theme-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class CBT_Theme_Styles {
*/
public static function update_style_css( $style_css, $theme ) {

$style_data = get_file_data(
path_join( get_stylesheet_directory(), 'style.css' ),
array(
'License' => 'License',
'LicenseURI' => 'License URI',
)
);

$current_theme = wp_get_theme();
$css_contents = trim( substr( $style_css, strpos( $style_css, '*/' ) + 2 ) );
$name = stripslashes( $theme['name'] );
Expand All @@ -17,39 +25,37 @@ public static function update_style_css( $style_css, $theme ) {
$author = stripslashes( $theme['author'] );
$author_uri = $theme['author_uri'];
$wp_version = get_bloginfo( 'version' );
$wp_min = $current_theme->get( 'RequiresWP' );
$version = $theme['version'];
$requires_php = $current_theme->get( 'RequiresPHP' );
$template = $current_theme->get( 'Template' );
$text_domain = $theme['slug'];
$template = $current_theme->get( 'Template' ) ? "\n" . 'Template: ' . $current_theme->get( 'Template' ) : '';
$license = $style_data['License'] ? $style_data['License'] : 'GNU General Public License v2 or later';
$license_uri = $style_data['LicenseURI'] ? $style_data['LicenseURI'] : 'http://www.gnu.org/licenses/gpl-2.0.html';
pbking marked this conversation as resolved.
Show resolved Hide resolved
$tags = CBT_Theme_Tags::theme_tags_list( $theme );
$css_contents = $css_contents ? "\n\n" . $css_contents : '';
$copyright = '';
preg_match( '/^\s*\n((?s).*?)\*\/\s*$/m', $style_css, $matches );
if ( isset( $matches[1] ) ) {
$copyright = "\n" . $matches[1];
}

//TODO: These items don't seem to be available via ->get('License') calls
$license = 'GNU General Public License v2 or later';
$license_uri = 'http://www.gnu.org/licenses/gpl-2.0.html';
$tags = CBT_Theme_Tags::theme_tags_list( $theme );
$css_metadata = "/*
return "/*
Theme Name: {$name}
Theme URI: {$uri}
Author: {$author}
Author URI: {$author_uri}
Description: {$description}
Requires at least: 6.0
Requires at least: {$wp_min}
Tested up to: {$wp_version}
Requires PHP: {$requires_php}
Version: {$version}
License: {$license}
License URI: {$license_uri}
";

if ( ! empty( $template ) ) {
$css_metadata .= "Template: {$template}\n";
}

$css_metadata .= "Text Domain: {$text_domain}
License URI: {$license_uri}{$template}
Text Domain: {$text_domain}
Tags: {$tags}
*/

{$copyright}*/{$css_contents}
";
return $css_metadata . $css_contents;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,14 @@ function rest_save_theme( $request ) {

private function sanitize_theme_data( $theme ) {
$sanitized_theme['name'] = sanitize_text_field( $theme['name'] );
$sanitized_theme['description'] = sanitize_text_field( $theme['description'] );
$sanitized_theme['uri'] = sanitize_text_field( $theme['uri'] );
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] );
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] );
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] );
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] );
$sanitized_theme['description'] = sanitize_text_field( $theme['description'] ?? '' );
$sanitized_theme['uri'] = sanitize_text_field( $theme['uri'] ?? '' );
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] ?? '' );
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ?? '' );
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] ?? '' );
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ?? '' );
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
$sanitized_theme['template'] = '';
$sanitized_theme['slug'] = sanitize_title( $theme['name'] );
$sanitized_theme['text_domain'] = $sanitized_theme['slug'];
Expand Down
Loading