Make WordPress Core

Ticket #49334: 49334.diff

File 49334.diff, 2.9 KB (added by afragen, 5 years ago)

compatibility testing for theme activation

  • wp-includes/theme.php

    diff --git a/wp-includes/theme.php b/wp-includes/theme.php
    index c7c4c3ca63..56165033ec 100644
    a b function switch_theme( $stylesheet ) { 
    764764        $new_theme = wp_get_theme( $stylesheet );
    765765        $template  = $new_theme->get_template();
    766766
     767        // Test for theme compatibility, die if incompatibility exists.
     768        $requirements = validate_theme_requirements( $new_theme );
     769        if ( is_wp_error( $requirements ) ) {
     770                wp_die( $requirements );
     771        }
     772
    767773        if ( wp_is_recovery_mode() ) {
    768774                $paused_themes = wp_paused_themes();
    769775                $paused_themes->delete( $old_theme->get_stylesheet() );
    function validate_current_theme() { 
    880886        return false;
    881887}
    882888
     889/**
     890 * Validate the theme requirements for WP version and PHP version.
     891 *
     892 * @since 5.5.0
     893 *
     894 * @param  \WP_Theme $theme Theme object.
     895 * @return true|WP_Error True if requirements are met, WP_Error on failure.
     896 */
     897function validate_theme_requirements( $theme ) {
     898        $response         = true;
     899        $theme_dir        = "$theme->theme_root/$theme->stylesheet";
     900        $theme_data       = array(
     901                'requires'     => '',
     902                'requires_php' => '',
     903        );
     904        $requirements_arr = array(
     905                'requires'     => 'Requires at least',
     906                'requires_php' => 'Requires PHP',
     907        );
     908
     909        // Check for headers in theme readme.
     910        if ( file_exists( "$theme_dir/readme.txt" ) ) {
     911                $theme_data = get_file_data( "$theme_dir/readme.txt", $requirements_arr, 'theme' );
     912        }
     913
     914        // Check for headers in the themes's style.css file.
     915        $stylesheet_data = get_file_data( "$theme_dir/style.css", $requirements_arr, 'theme' );
     916        // Give precedence to the stylesheet headers.
     917        $theme_data = array_merge( $theme_data, $stylesheet_data );
     918
     919        $theme_data['wp_compatible']  = is_wp_version_compatible( $theme_data['requires'] );
     920        $theme_data['php_compatible'] = is_php_version_compatible( $theme_data['requires_php'] );
     921        $theme_data['Name']           = $theme->get( 'Name' );
     922
     923        if ( ! $theme_data['wp_compatible'] && ! $theme_data['php_compatible'] ) {
     924                $response = new WP_Error(
     925                        'theme_wp_php_incompatible',
     926                        sprintf(
     927                                /* translators: %s: Theme name. */
     928                                __( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ),
     929                                $theme_data['Name']
     930                        )
     931                );
     932        } elseif ( ! $theme_data['php_compatible'] ) {
     933                $response = new WP_Error(
     934                        'theme_php_incompatible',
     935                        sprintf(
     936                                /* translators: %s: Theme name. */
     937                                __( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ),
     938                                $theme_data['Name']
     939                        )
     940                );
     941        } elseif ( ! $theme_data['wp_compatible'] ) {
     942                $response = new WP_Error(
     943                        'theme_wp_incompatible',
     944                        sprintf(
     945                                /* translators: %s: Theme name. */
     946                                __( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ),
     947                                $theme_data['Name']
     948                        )
     949                );
     950        }
     951
     952        return $response;
     953}
     954
    883955/**
    884956 * Retrieve all theme modifications.
    885957 *