WordPress.org

Make WordPress Core

Changeset 42832


Ignore:
Timestamp:
03/12/2018 04:42:11 PM (2 years ago)
Author:
flixos90
Message:

General: Introduce dashboard widget to inform administrators about outdated PHP versions.

This new dashboard widget is shown on WordPress sites which are powered by a PHP version which WordPress considers outdated, in order to inform site owners about the resulting problems and to explain how to upgrade to a supported version. An education page for that purpose has been previously created that the widget links to. The link is translatable so that localized versions of the page can be referred to as they become available.

The nag follows the example of the Browse Happy dashboard widget and is only visible for administrators, or network administrators when using multisite. To determine whether it needs to be displayed, a new wordpress.org API introduced prior is called that handles the version logic in a centralized location.

Props flixos90, hedgefield, schlessera.
Fixes #41191.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/dashboard.css

    r41978 r42832  
    11171117}
    11181118
     1119/* PHP Nag */
     1120#dashboard_php_nag h2.hndle {
     1121    border-left: 4px solid #dc3232;
     1122}
     1123
     1124#dashboard_php_nag h3 {
     1125    font-weight: 600;
     1126}
     1127
     1128#dashboard_php_nag .button.button-hero {
     1129    display: block;
     1130    text-align: center;
     1131}
     1132
    11191133/* =Media Queries
    11201134-------------------------------------------------------------- */
  • trunk/src/wp-admin/includes/dashboard.php

    r42781 r42832  
    3434            wp_add_dashboard_widget( 'dashboard_browser_nag', __( 'Your browser is out of date!' ), 'wp_dashboard_browser_nag' );
    3535        }
     36    }
     37
     38    // PHP Version
     39    $response = wp_check_php_version();
     40    if ( $response && ! $response['is_acceptable'] && current_user_can( 'upgrade_php' ) ) {
     41        $title = $response['is_secure'] ? __( 'Your site could be much faster!' ) : __( 'Your site could be much faster and more secure!' );
     42        wp_add_dashboard_widget( 'dashboard_php_nag', $title, 'wp_dashboard_php_nag' );
    3643    }
    3744
     
    179186    }
    180187
     188    $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
     189
    181190    $priority = 'core';
    182     if ( 'dashboard_browser_nag' === $widget_id ) {
     191    if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
    183192        $priority = 'high';
    184193    }
     
    15951604
    15961605        set_site_transient( 'browser_' . $key, $response, WEEK_IN_SECONDS );
     1606    }
     1607
     1608    return $response;
     1609}
     1610
     1611/**
     1612 * Displays the PHP upgrade nag.
     1613 *
     1614 * @since 5.0.0
     1615 */
     1616function wp_dashboard_php_nag() {
     1617    $response = wp_check_php_version();
     1618
     1619    if ( ! $response ) {
     1620        return;
     1621    }
     1622
     1623    $information_url = _x( 'https://wordpress.org/support/upgrade-php/', 'localized PHP upgrade information page' );
     1624
     1625    $msg = __( 'Hi, it’s your friends at WordPress here.' );
     1626    if ( ! $response['is_secure'] ) {
     1627        $msg .= ' ' . __( 'We noticed that your site is running on an insecure version of PHP, which is why we’re showing you this notice.' );
     1628    } else {
     1629        $msg .= ' ' . __( 'We noticed that your site is running on an outdated version of PHP, which is why we’re showing you this notice.' );
     1630    }
     1631
     1632    ?>
     1633    <p><?php echo $msg; ?></p>
     1634
     1635    <h3><?php _e( 'What is PHP and why should I care?' ); ?></h3>
     1636    <p><?php _e( 'PHP is the programming language that WordPress is built on. Newer versions of PHP are both faster and more secure, so upgrading is better for your site, and better for the people who are building WordPress.' ); ?></p>
     1637    <p><?php _e( 'If you want to know exactly how PHP works and why it is important, continue reading.' ); ?></p>
     1638
     1639    <h3><?php _e( 'Okay, how do I update?' ); ?></h3>
     1640    <p><?php _e( 'The button below will take you to a page with more details on what PHP is, how to upgrade your PHP version, and what to do if it turns out you can&#8217;t.' ); ?></p>
     1641    <p>
     1642        <a class="button button-primary button-hero" href="<?php echo esc_url( $information_url ); ?>"><?php _e( 'Show me how to upgrade my PHP' ); ?></a>
     1643    </p>
     1644
     1645    <h3><?php _e( 'Thank you for taking the time to read this!' ); ?></h3>
     1646    <p><?php _e( 'If you carefully follow the instructions we&#8217;ve provided, upgrading shouldn&#8217;t take more than a few minutes, and it is generally very safe to do.' ); ?></p>
     1647    <p><?php _e( 'Good luck and happy blogging!' ); ?></p>
     1648    <?php
     1649}
     1650
     1651/**
     1652 * Checks if the user needs to upgrade PHP.
     1653 *
     1654 * @since 5.0.0
     1655 *
     1656 * @return array Array of PHP version data.
     1657 */
     1658function wp_check_php_version() {
     1659    $version = phpversion();
     1660    $key     = md5( $version );
     1661
     1662    $response = get_site_transient( 'php_check_' . $key );
     1663    if ( false === $response ) {
     1664        $url = 'http://api.wordpress.org/core/serve-happy/1.0/';
     1665        if ( wp_http_supports( array( 'ssl' ) ) ) {
     1666            $url = set_url_scheme( $url, 'https' );
     1667        }
     1668
     1669        $url = add_query_arg( 'php_version', $version, $url );
     1670
     1671        $response = wp_remote_get( $url );
     1672
     1673        if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
     1674            return false;
     1675        }
     1676
     1677        /**
     1678         * Response should be an array with:
     1679         *  'recommended_version' - string - The PHP version recommended by WordPress
     1680         *  'is_supported' - boolean - Whether the PHP version is actively supported
     1681         *  'is_secure' - boolean - Whether the PHP version receives security updates
     1682         *  'is_acceptable' - boolean - Whether the PHP version is still acceptable for WordPress
     1683         */
     1684        $response = json_decode( wp_remote_retrieve_body( $response ), true );
     1685
     1686        if ( ! is_array( $response ) ) {
     1687            return false;
     1688        }
     1689
     1690        set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
    15971691    }
    15981692
  • trunk/src/wp-includes/capabilities.php

    r42809 r42832  
    549549            }
    550550            break;
     551        case 'upgrade_php':
     552            if ( is_multisite() && ! is_super_admin( $user_id ) ) {
     553                $caps[] = 'do_not_allow';
     554            } else {
     555                $caps[] = 'update_core';
     556            }
     557            break;
    551558        default:
    552559            // Handle meta capabilities for custom post types.
  • trunk/tests/phpunit/tests/user/capabilities.php

    r42343 r42832  
    237237            'update_languages'       => array( 'administrator' ),
    238238            'deactivate_plugins'     => array( 'administrator' ),
     239            'upgrade_php'            => array( 'administrator' ),
    239240
    240241            'edit_categories'        => array( 'administrator', 'editor' ),
     
    268269            'update_languages'       => array(),
    269270            'deactivate_plugins'     => array(),
     271            'upgrade_php'            => array(),
    270272
    271273            'customize'              => array( 'administrator' ),
Note: See TracChangeset for help on using the changeset viewer.