Make WordPress Core


Ignore:
Timestamp:
04/13/2020 03:26:37 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Themes: Block theme activation if it requires a higher version of PHP or WordPress.

Introduce validate_theme_requirements() for validating a theme's WordPress and PHP version requirements.

Follow-up to [44978] and [45546] for plugins.

Props afragen, audrasjb, SergeyBiryukov.
See #43992.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme.php

    r47550 r47573  
    734734        global $wp_theme_directories, $wp_customize, $sidebars_widgets;
    735735
     736        $requirements = validate_theme_requirements( $stylesheet );
     737        if ( is_wp_error( $requirements ) ) {
     738                wp_die( $requirements );
     739        }
     740
    736741        $_sidebars_widgets = null;
    737742        if ( 'wp_ajax_customize_save' === current_action() ) {
     
    879884        switch_theme( $default->get_stylesheet() );
    880885        return false;
     886}
     887
     888/**
     889 * Validates the theme requirements for WordPress version and PHP version.
     890 *
     891 * Uses the information from `Requires at least` and `Requires PHP` headers
     892 * defined in the theme's `style.css` file.
     893 *
     894 * If the headers are not present in the theme's stylesheet file,
     895 * `readme.txt` is also checked as a fallback.
     896 *
     897 * @since 5.5.0
     898 *
     899 * @param string $stylesheet Directory name for the theme.
     900 * @return true|WP_Error True if requirements are met, WP_Error on failure.
     901 */
     902function validate_theme_requirements( $stylesheet ) {
     903        $theme = wp_get_theme( $stylesheet );
     904
     905        $requirements = array(
     906                'requires'     => ! empty( $theme->get( 'RequiresWP' ) ) ? $theme->get( 'RequiresWP' ) : '',
     907                'requires_php' => ! empty( $theme->get( 'RequiresPHP' ) ) ? $theme->get( 'RequiresPHP' ) : '',
     908        );
     909
     910        $readme_file = $theme->theme_root . '/' . $stylesheet . '/readme.txt';
     911
     912        if ( file_exists( $readme_file ) ) {
     913                $readme_headers = get_file_data(
     914                        $readme_file,
     915                        array(
     916                                'requires'     => 'Requires at least',
     917                                'requires_php' => 'Requires PHP',
     918                        ),
     919                        'theme'
     920                );
     921
     922                $requirements = array_merge( $readme_headers, $requirements );
     923        }
     924
     925        $compatible_wp  = is_wp_version_compatible( $requirements['requires'] );
     926        $compatible_php = is_php_version_compatible( $requirements['requires_php'] );
     927
     928        if ( ! $compatible_wp && ! $compatible_php ) {
     929                return new WP_Error(
     930                        'theme_wp_php_incompatible',
     931                        sprintf(
     932                                /* translators: %s: Theme name. */
     933                                _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'theme' ),
     934                                $theme->display( 'Name' )
     935                        )
     936                );
     937        } elseif ( ! $compatible_php ) {
     938                return new WP_Error(
     939                        'theme_php_incompatible',
     940                        sprintf(
     941                                /* translators: %s: Theme name. */
     942                                _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'theme' ),
     943                                $theme->display( 'Name' )
     944                        )
     945                );
     946        } elseif ( ! $compatible_wp ) {
     947                return new WP_Error(
     948                        'theme_wp_incompatible',
     949                        sprintf(
     950                                /* translators: %s: Theme name. */
     951                                _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'theme' ),
     952                                $theme->display( 'Name' )
     953                        )
     954                );
     955        }
     956
     957        return true;
    881958}
    882959
Note: See TracChangeset for help on using the changeset viewer.