<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[42832] trunk: General: Introduce dashboard widget to inform administrators about outdated PHP versions.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/42832">42832</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/42832","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-03-12 16:42:11 +0000 (Mon, 12 Mar 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/41191">#41191</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmincssdashboardcss">trunk/src/wp-admin/css/dashboard.css</a></li>
<li><a href="#trunksrcwpadminincludesdashboardphp">trunk/src/wp-admin/includes/dashboard.php</a></li>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadmincssdashboardcss"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/css/dashboard.css</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/css/dashboard.css      2018-03-12 10:58:24 UTC (rev 42831)
+++ trunk/src/wp-admin/css/dashboard.css        2018-03-12 16:42:11 UTC (rev 42832)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1116,6 +1116,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        font-size: 16px;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* 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;
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /* =Media Queries
</span><span class="cx" style="display: block; padding: 0 10px"> -------------------------------------------------------------- */
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpadminincludesdashboardphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/dashboard.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/dashboard.php 2018-03-12 10:58:24 UTC (rev 42831)
+++ trunk/src/wp-admin/includes/dashboard.php   2018-03-12 16:42:11 UTC (rev 42832)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,6 +35,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // 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' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Right Now
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_blog_admin() && current_user_can( 'edit_posts' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_add_dashboard_widget( 'dashboard_right_now', __( 'At a Glance' ), 'wp_dashboard_right_now' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,8 +185,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $location = 'side';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $priority = 'core';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 'dashboard_browser_nag' === $widget_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $priority = 'high';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1600,6 +1609,91 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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&#8217;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&#8217;re showing you this notice.' );
+       } else {
+               $msg .= ' ' . __( 'We noticed that your site is running on an outdated version of PHP, which is why we&#8217;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;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Empty function usable by plugins to output empty dashboard widget (to be populated later by JS).
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_dashboard_empty() {}
</span></span></pre></div>
<a id="trunksrcwpincludescapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/capabilities.php    2018-03-12 10:58:24 UTC (rev 42831)
+++ trunk/src/wp-includes/capabilities.php      2018-03-12 16:42:11 UTC (rev 42832)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -548,6 +548,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $caps[] = 'manage_options';
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        break;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                case 'upgrade_php':
+                       if ( is_multisite() && ! is_super_admin( $user_id ) ) {
+                               $caps[] = 'do_not_allow';
+                       } else {
+                               $caps[] = 'update_core';
+                       }
+                       break;
</ins><span class="cx" style="display: block; padding: 0 10px">                 default:
</span><span class="cx" style="display: block; padding: 0 10px">                        // Handle meta capabilities for custom post types.
</span><span class="cx" style="display: block; padding: 0 10px">                        global $post_type_meta_caps;
</span></span></pre></div>
<a id="trunktestsphpunittestsusercapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/capabilities.php   2018-03-12 10:58:24 UTC (rev 42831)
+++ trunk/tests/phpunit/tests/user/capabilities.php     2018-03-12 16:42:11 UTC (rev 42832)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -236,6 +236,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'install_languages'      => array( 'administrator' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_languages'       => array( 'administrator' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'deactivate_plugins'     => array( 'administrator' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'upgrade_php'            => array( 'administrator' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        'edit_categories'        => array( 'administrator', 'editor' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'delete_categories'      => array( 'administrator', 'editor' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -267,6 +268,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'install_languages'      => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_languages'       => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'deactivate_plugins'     => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'upgrade_php'            => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        'customize'              => array( 'administrator' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'delete_site'            => array( 'administrator' ),
</span></span></pre>
</div>
</div>

</body>
</html>