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.


git-svn-id: https://develop.svn.wordpress.org/trunk@42832 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2018-03-12 16:42:11 +00:00
parent a4f6e8dadb
commit 5f56921131
4 changed files with 118 additions and 1 deletions

View File

@ -1116,6 +1116,20 @@ a.rsswidget {
font-size: 16px;
}
/* PHP Nag */
#dashboard_php_nag h2.hndle {
border-left: 4px solid #dc3232;
}
#dashboard_php_nag h3 {
font-weight: 600;
}
#dashboard_php_nag .button.button-hero {
display: block;
text-align: center;
}
/* =Media Queries
-------------------------------------------------------------- */

View File

@ -35,6 +35,13 @@ function wp_dashboard_setup() {
}
}
// PHP Version
$response = wp_check_php_version();
if ( $response && ! $response['is_acceptable'] && current_user_can( 'upgrade_php' ) ) {
$title = $response['is_secure'] ? __( 'Your site could be much faster!' ) : __( 'Your site could be much faster and more secure!' );
wp_add_dashboard_widget( 'dashboard_php_nag', $title, 'wp_dashboard_php_nag' );
}
// Right Now
if ( is_blog_admin() && current_user_can( 'edit_posts' ) ) {
wp_add_dashboard_widget( 'dashboard_right_now', __( 'At a Glance' ), 'wp_dashboard_right_now' );
@ -178,8 +185,10 @@ function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_
$location = 'side';
}
$high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
$priority = 'core';
if ( 'dashboard_browser_nag' === $widget_id ) {
if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
$priority = 'high';
}
@ -1599,6 +1608,91 @@ function wp_check_browser_version() {
return $response;
}
/**
* Displays the PHP upgrade nag.
*
* @since 5.0.0
*/
function wp_dashboard_php_nag() {
$response = wp_check_php_version();
if ( ! $response ) {
return;
}
$information_url = _x( 'https://wordpress.org/support/upgrade-php/', 'localized PHP upgrade information page' );
$msg = __( 'Hi, it’s your friends at WordPress here.' );
if ( ! $response['is_secure'] ) {
$msg .= ' ' . __( 'We noticed that your site is running on an insecure version of PHP, which is why we’re showing you this notice.' );
} else {
$msg .= ' ' . __( 'We noticed that your site is running on an outdated version of PHP, which is why we’re showing you this notice.' );
}
?>
<p><?php echo $msg; ?></p>
<h3><?php _e( 'What is PHP and why should I care?' ); ?></h3>
<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>
<p><?php _e( 'If you want to know exactly how PHP works and why it is important, continue reading.' ); ?></p>
<h3><?php _e( 'Okay, how do I update?' ); ?></h3>
<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>
<p>
<a class="button button-primary button-hero" href="<?php echo esc_url( $information_url ); ?>"><?php _e( 'Show me how to upgrade my PHP' ); ?></a>
</p>
<h3><?php _e( 'Thank you for taking the time to read this!' ); ?></h3>
<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>
<p><?php _e( 'Good luck and happy blogging!' ); ?></p>
<?php
}
/**
* Checks if the user needs to upgrade PHP.
*
* @since 5.0.0
*
* @return array Array of PHP version data.
*/
function wp_check_php_version() {
$version = phpversion();
$key = md5( $version );
$response = get_site_transient( 'php_check_' . $key );
if ( false === $response ) {
$url = 'http://api.wordpress.org/core/serve-happy/1.0/';
if ( wp_http_supports( array( 'ssl' ) ) ) {
$url = set_url_scheme( $url, 'https' );
}
$url = add_query_arg( 'php_version', $version, $url );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
return false;
}
/**
* Response should be an array with:
* 'recommended_version' - string - The PHP version recommended by WordPress
* 'is_supported' - boolean - Whether the PHP version is actively supported
* 'is_secure' - boolean - Whether the PHP version receives security updates
* 'is_acceptable' - boolean - Whether the PHP version is still acceptable for WordPress
*/
$response = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $response ) ) {
return false;
}
set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
}
return $response;
}
/**
* Empty function usable by plugins to output empty dashboard widget (to be populated later by JS).
*/

View File

@ -548,6 +548,13 @@ function map_meta_cap( $cap, $user_id ) {
$caps[] = 'manage_options';
}
break;
case 'upgrade_php':
if ( is_multisite() && ! is_super_admin( $user_id ) ) {
$caps[] = 'do_not_allow';
} else {
$caps[] = 'update_core';
}
break;
default:
// Handle meta capabilities for custom post types.
global $post_type_meta_caps;

View File

@ -236,6 +236,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
'install_languages' => array( 'administrator' ),
'update_languages' => array( 'administrator' ),
'deactivate_plugins' => array( 'administrator' ),
'upgrade_php' => array( 'administrator' ),
'edit_categories' => array( 'administrator', 'editor' ),
'delete_categories' => array( 'administrator', 'editor' ),
@ -267,6 +268,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
'install_languages' => array(),
'update_languages' => array(),
'deactivate_plugins' => array(),
'upgrade_php' => array(),
'customize' => array( 'administrator' ),
'delete_site' => array( 'administrator' ),