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

Adds minimum WordPress version to theme metadata #715

Merged
merged 12 commits into from
Sep 20, 2024
Merged
1 change: 1 addition & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private function sanitize_theme_data( $theme ) {
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ?? '' );
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
$sanitized_theme['screenshot'] = sanitize_text_field( $theme['screenshot'] ?? '' );
$sanitized_theme['requires_wp'] = sanitize_text_field( $theme['requires_wp'] ?? '' );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
$sanitized_theme['font_credits'] = sanitize_textarea_field( $theme['font_credits'] ?? '' );
$sanitized_theme['image_credits'] = sanitize_textarea_field( $theme['image_credits'] ?? '' );
Expand Down
7 changes: 7 additions & 0 deletions includes/class-create-block-theme-editor-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ function create_block_theme_sidebar_enqueue() {
'create-block-theme-slot-fill',
);

global $wp_version;
wp_add_inline_script(
'create-block-theme-slot-fill',
'const WP_VERSION = "' . $wp_version . '";',
'before'
);

// Enable localization in the plugin sidebar.
wp_set_script_translations( 'create-block-theme-slot-fill', 'create-block-theme' );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/create-theme/theme-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function update_style_css( $style_css, $theme ) {
$author = stripslashes( $theme['author'] );
$author_uri = $theme['author_uri'];
$wp_version = CBT_Theme_Utils::get_current_wordpress_version();
$wp_min = $current_theme->get( 'RequiresWP' );
$wp_min = isset( $theme['requires_wp'] ) && $theme['requires_wp'] ? $theme['requires_wp'] : '6.0';
jffng marked this conversation as resolved.
Show resolved Hide resolved
$version = $theme['version'];
$requires_php = $current_theme->get( 'RequiresPHP' );
$text_domain = $theme['slug'];
Expand Down
20 changes: 20 additions & 0 deletions src/editor-sidebar/metadata-editor-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FormTokenField,
Modal,
Button,
SelectControl,
TextControl,
TextareaControl,
ExternalLink,
Expand All @@ -29,6 +30,7 @@ import { MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
*/
import { postUpdateThemeMetadata, fetchReadmeData } from '../resolvers';
import { getFontsCreditsText } from '../utils/fonts';
import { generateWpVersions } from '../utils/generate-versions';

const ALLOWED_SCREENSHOT_MEDIA_TYPES = [
'image/png',
Expand All @@ -39,12 +41,15 @@ const ALLOWED_SCREENSHOT_MEDIA_TYPES = [
'image/avif',
];

const WP_MINIMUM_VERSIONS = generateWpVersions( WP_VERSION ); // eslint-disable-line no-undef

export const ThemeMetadataEditorModal = ( { onRequestClose } ) => {
const [ theme, setTheme ] = useState( {
name: '',
description: '',
uri: '',
version: '',
requires_wp: '',
author: '',
author_uri: '',
tags_custom: '',
Expand All @@ -65,6 +70,7 @@ export const ThemeMetadataEditorModal = ( { onRequestClose } ) => {
description: themeData.description.raw,
uri: themeData.theme_uri.raw,
version: themeData.version,
requires_wp: themeData.requires_wp,
author: themeData.author.raw,
author_uri: themeData.author_uri.raw,
tags_custom: themeData.tags.rendered,
Expand Down Expand Up @@ -212,6 +218,20 @@ export const ThemeMetadataEditorModal = ( { onRequestClose } ) => {
'create-block-theme'
) }
/>
<SelectControl
label={ __(
'Minimum WordPress version',
'create-block-theme'
) }
value={ theme.requires_wp }
options={ WP_MINIMUM_VERSIONS.map( ( version ) => ( {
label: version,
value: version,
} ) ) }
onChange={ ( value ) => {
setTheme( { ...theme, requires_wp: value } );
} }
/>
<FormTokenField
label={ __( 'Theme tags', 'create-block-theme' ) }
value={
Expand Down
21 changes: 21 additions & 0 deletions src/utils/generate-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function generateWpVersions( versionString ) {
const version = versionString.split( '-' )[ 0 ];
let [ major, minor ] = version.split( '.' ).slice( 0, 2 ).map( Number );

const versions = [];

// Iterate through the versions from current to 5.9
while ( major > 5 || ( major === 5 && minor >= 9 ) ) {
versions.push( `${ major }.${ minor }` );

// Decrement minor version
if ( minor === 0 ) {
minor = 9; // Wrap around if minor is 0, decrement the major version
major--;
} else {
minor--;
}
}

return versions;
}
Loading