From b16aa77bfe3143e11dae90f2d3521f52aa80c7b1 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 14 May 2024 07:36:07 -0400 Subject: [PATCH 1/7] Add version to sanitized theme data so it isn't wiped when updating --- includes/class-create-block-theme-api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index d4186935..8fb31891 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -489,6 +489,7 @@ private function sanitize_theme_data( $theme ) { $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'] ); From 01c8ce4a1d48a3923544e7e3f22f2eace0d5f0bc Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 14 May 2024 07:39:12 -0400 Subject: [PATCH 2/7] Pull license data from style.css --- admin/create-theme/theme-styles.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/admin/create-theme/theme-styles.php b/admin/create-theme/theme-styles.php index f45b00c6..bf5ab845 100644 --- a/admin/create-theme/theme-styles.php +++ b/admin/create-theme/theme-styles.php @@ -21,11 +21,19 @@ public static function update_style_css( $style_css, $theme ) { $requires_php = $current_theme->get( 'RequiresPHP' ); $template = $current_theme->get( 'Template' ); $text_domain = $theme['slug']; + $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 ); + + preg_match( '/License: (.+)/', $style_css, $matches ); + if ( isset( $matches[1] ) ) { + $license = $matches[1]; + } + preg_match( '/License URI: (.+)/', $style_css, $matches ); + if ( isset( $matches[1] ) ) { + $license_uri = $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 = "/* Theme Name: {$name} Theme URI: {$uri} From 7391bc9425d839a50dc263f65e9f32f860b2d240 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 14 May 2024 07:42:19 -0400 Subject: [PATCH 3/7] Don't overwrite existing copyright in style.css --- admin/create-theme/theme-styles.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/admin/create-theme/theme-styles.php b/admin/create-theme/theme-styles.php index bf5ab845..1f0e1033 100644 --- a/admin/create-theme/theme-styles.php +++ b/admin/create-theme/theme-styles.php @@ -24,6 +24,7 @@ public static function update_style_css( $style_css, $theme ) { $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 ); + $copyright = ''; preg_match( '/License: (.+)/', $style_css, $matches ); if ( isset( $matches[1] ) ) { @@ -33,6 +34,10 @@ public static function update_style_css( $style_css, $theme ) { if ( isset( $matches[1] ) ) { $license_uri = $matches[1]; } + preg_match( '/^\s*\n((?s).*?)\*\/\s*$/m', $style_css, $matches ); + if ( isset( $matches[1] ) ) { + $copyright = $matches[1]; + } $css_metadata = "/* Theme Name: {$name} @@ -54,7 +59,8 @@ public static function update_style_css( $style_css, $theme ) { $css_metadata .= "Text Domain: {$text_domain} Tags: {$tags} -*/ + +{$copyright}*/ "; return $css_metadata . $css_contents; From c3af5126090a5632fb64ad7e89aba196c2931abb Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 14 May 2024 07:45:29 -0400 Subject: [PATCH 4/7] Use WordPress minimum version from style.css --- admin/create-theme/theme-styles.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/admin/create-theme/theme-styles.php b/admin/create-theme/theme-styles.php index 1f0e1033..41b1bda8 100644 --- a/admin/create-theme/theme-styles.php +++ b/admin/create-theme/theme-styles.php @@ -17,6 +17,7 @@ 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' ); @@ -45,7 +46,7 @@ public static function update_style_css( $style_css, $theme ) { 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} From 2e03baf5b933cafa4412d1d3391b3fbfc4a88a78 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 14 May 2024 08:10:49 -0400 Subject: [PATCH 5/7] allow blank fields when sanitizing theme data --- includes/class-create-block-theme-api.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index 8fb31891..7af9d3b7 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -483,14 +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['version'] = sanitize_text_field( $theme['version'] ); - $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']; From c596e200fcecf961a1a58eda8032962859425fda Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Thu, 16 May 2024 07:18:32 -0400 Subject: [PATCH 6/7] Use get_file_data for license parsing --- admin/create-theme/theme-styles.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/admin/create-theme/theme-styles.php b/admin/create-theme/theme-styles.php index 41b1bda8..9a189fc6 100644 --- a/admin/create-theme/theme-styles.php +++ b/admin/create-theme/theme-styles.php @@ -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'] ); @@ -22,19 +30,11 @@ public static function update_style_css( $style_css, $theme ) { $requires_php = $current_theme->get( 'RequiresPHP' ); $template = $current_theme->get( 'Template' ); $text_domain = $theme['slug']; - $license = 'GNU General Public License v2 or later'; - $license_uri = 'http://www.gnu.org/licenses/gpl-2.0.html'; + $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'; $tags = CBT_Theme_Tags::theme_tags_list( $theme ); $copyright = ''; - preg_match( '/License: (.+)/', $style_css, $matches ); - if ( isset( $matches[1] ) ) { - $license = $matches[1]; - } - preg_match( '/License URI: (.+)/', $style_css, $matches ); - if ( isset( $matches[1] ) ) { - $license_uri = $matches[1]; - } preg_match( '/^\s*\n((?s).*?)\*\/\s*$/m', $style_css, $matches ); if ( isset( $matches[1] ) ) { $copyright = $matches[1]; From f73355128344ef82ebbd232be07a9e451e93bdf9 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Thu, 16 May 2024 07:42:59 -0400 Subject: [PATCH 7/7] Refactored style string generation slightly to eliminate logic once string is being built (put it before), ensure that blank lines don't follow metadata if there is no copyright information, that there is only a single blank line following copyright and css_content is correctly positioned. --- admin/create-theme/theme-styles.php | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/admin/create-theme/theme-styles.php b/admin/create-theme/theme-styles.php index 9a189fc6..52b52ffb 100644 --- a/admin/create-theme/theme-styles.php +++ b/admin/create-theme/theme-styles.php @@ -28,19 +28,19 @@ public static function update_style_css( $style_css, $theme ) { $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'; $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 = $matches[1]; + $copyright = "\n" . $matches[1]; } - $css_metadata = "/* + return "/* Theme Name: {$name} Theme URI: {$uri} Author: {$author} @@ -51,20 +51,11 @@ public static function update_style_css( $style_css, $theme ) { 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}*/ - +{$copyright}*/{$css_contents} "; - return $css_metadata . $css_contents; } /**