WordPress.org

Make WordPress Core

Ticket #41191: 41191.2.diff

File 41191.2.diff, 10.3 KB (added by flixos90, 4 months ago)
  • src/wp-admin/css/common.css

     
    16051605        }
    16061606}
    16071607
     1608.notice-upgrade-column-container {
     1609        display: -webkit-box;
     1610        display: flex;
     1611        -webkit-box-align: start;
     1612        align-items: flex-start;
     1613        -webkit-box-pack: justify;
     1614        justify-content: space-between;
     1615        flex-wrap: wrap;
     1616        -webkit-box-orient: horizontal;
     1617        -webkit-box-direction: normal;
     1618        flex-flow: row wrap;
     1619}
     1620
     1621.notice-upgrade-column-container .notice-upgrade-column {
     1622        padding: 0 1em 1em 0;
     1623        flex-basis: 300px;
     1624        -webkit-box-flex: 1;
     1625        flex-grow: 1;
     1626}
     1627
     1628.wp-core-ui .button.button-hero.notice-upgrade-button {
     1629        display: block;
     1630        text-align: center;
     1631}
     1632
    16081633
    16091634/* @todo: this does not need its own section anymore */
    16101635/*------------------------------------------------------------------------------
  • src/wp-admin/includes/admin-filters.php

     
    7171// Nav Menu hooks.
    7272add_action( 'admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items' );
    7373
     74// Notices hooks.
     75add_action( 'admin_notices', 'wp_upgrade_php_notice' );
     76
    7477// Plugin hooks.
    7578add_filter( 'whitelist_options', 'option_update_filter' );
    7679
  • src/wp-admin/includes/ms-admin-filters.php

     
    3434// Notices Hooks
    3535add_action( 'admin_notices', 'site_admin_notice' );
    3636add_action( 'network_admin_notices', 'site_admin_notice' );
     37add_action( 'network_admin_notices', 'wp_upgrade_php_notice' );
    3738
    3839// Update Hooks
    3940add_action( 'network_admin_notices', 'update_nag', 3 );
  • src/wp-admin/includes/update.php

     
    809809        </script>
    810810        <?php
    811811}
     812
     813/**
     814 * Determines whether to display an admin notice to inform the user of an unsupported PHP version in use.
     815 *
     816 * @since 5.0.0
     817 *
     818 * @return bool True if the notice should be displayed, false otherwise.
     819 */
     820function wp_should_display_upgrade_php_notice() {
     821        if ( ! wp_is_php_version_outdated() ) {
     822                return false;
     823        }
     824
     825        if ( ! current_user_can( 'upgrade_php' ) ) {
     826                return false;
     827        }
     828
     829        $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
     830
     831        return ! in_array( 'upgrade_php_notice', $dismissed_pointers, true );
     832}
     833
     834/**
     835 * Outputs an admin notice to inform the user of an unsupported PHP version in use.
     836 *
     837 * @since 5.0.0
     838 */
     839function wp_upgrade_php_notice() {
     840        if ( ! wp_should_display_upgrade_php_notice() ) {
     841                return;
     842        }
     843
     844        $information_url = __( 'https://wordpress.org/support/upgrade-php/' );
     845
     846        ?>
     847        <div id="upgrade-php-notice" class="notice-upgrade notice notice-error is-dismissible">
     848                <h2><?php _e( 'Your site could be faster and more secure!' ); ?></h2>
     849
     850                <p><?php _e( 'Hi, it&apos;s your friends at WordPress here. We noticed that your site is running on an outdated version of PHP, which is why we&apos;re showing you this notice.' ); ?></p>
     851
     852                <div class="notice-upgrade-column-container">
     853
     854                        <div class="notice-upgrade-column">
     855                                <h3><?php _e( 'What is PHP and why should I care?' ); ?></h3>
     856
     857                                <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>
     858
     859                                <p><?php _e( 'If you want to know exactly how PHP works and why it is important, continue reading.' ); ?></p>
     860                        </div>
     861
     862                        <div class="notice-upgrade-column">
     863                                <h3><?php _e( 'Okay, how do I update?' ); ?></h3>
     864
     865                                <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&apos;t.' ); ?></p>
     866
     867                                <p><a class="notice-upgrade-button button button-primary button-hero" href="<?php echo esc_url( $information_url ); ?>"><?php _e( 'Show me how to upgrade my PHP' ); ?></a></p>
     868                        </div>
     869
     870                        <div class="notice-upgrade-column">
     871                                <h3><?php _e( 'Thank you for taking the time to read this!' ); ?></h3>
     872
     873                                <p><?php _e( 'If you follow the instructions we&apos;ve provided to the letter, upgrading shouldn&apos;t take more than a few minutes, and it is generally very safe to do.' ); ?></p>
     874
     875                                <p><?php _e( 'Good luck and happy blogging!' ); ?></p>
     876                        </div>
     877
     878                </div>
     879        </div>
     880        <?php
     881
     882        // The following ensures dismissing the notice permanently dismisses it for the current user.
     883        $script = <<<JS
     884        document.getElementById( 'upgrade-php-notice' ).addEventListener( 'click', function( event ) {
     885                if ( ! event.target.classList.contains( 'notice-dismiss' ) ) {
     886                        return;
     887                }
     888
     889                wp.ajax.post( 'dismiss-wp-pointer', {
     890                        pointer: 'upgrade_php_notice'
     891                });
     892        });
     893JS;
     894
     895        wp_enqueue_script( 'wp-util' );
     896        wp_add_inline_script( 'wp-util', $script );
     897}
  • src/wp-includes/capabilities.php

     
    550550                                $caps[] = 'manage_options';
    551551                        }
    552552                        break;
     553                case 'upgrade_php':
     554                        if ( is_multisite() && ! is_super_admin( $user_id ) ) {
     555                                $caps[] = 'do_not_allow';
     556                        } else {
     557                                $caps[] = 'update_core';
     558                        }
     559                        break;
    553560                default:
    554561                        // Handle meta capabilities for custom post types.
    555562                        global $post_type_meta_caps;
  • src/wp-includes/functions.php

     
    60756075                ), $email_change_email['message'], $email_change_email['headers']
    60766076        );
    60776077}
     6078
     6079/**
     6080 * Checks whether a given PHP version requirement is met.
     6081 *
     6082 * @since 5.0.0
     6083 *
     6084 * @param string $requirement Required PHP version to check against.
     6085 * @return bool True if the detected PHP version meets the given requirement, false otherwise.
     6086 */
     6087function wp_satisfies_php_version( $requirement ) {
     6088        /**
     6089         * Filters the detected PHP version.
     6090         *
     6091         * The phpversion() function is used by default. This can be tweaked to account
     6092         * for exotic setups.
     6093         *
     6094         * @since 5.0.0
     6095         *
     6096         * @param string $version Detected PHP version.
     6097         */
     6098        $version = apply_filters( 'wp_detected_php_version', phpversion() );
     6099
     6100        return version_compare( $version, $requirement, '>=' );
     6101}
     6102
     6103/**
     6104 * Determines whether the currently active PHP version is considered outdated.
     6105 *
     6106 * The result of this function is not necessarily tied to whether the PHP version
     6107 * is actually no longer supported.
     6108 *
     6109 * @since 5.0.0
     6110 *
     6111 * @return bool True if the PHP version is outdated, false otherwise.
     6112 */
     6113function wp_is_php_version_outdated() {
     6114        $outdated = ! wp_satisfies_php_version( '5.3.0' );
     6115
     6116        /**
     6117         * Filters whether the currently active PHP version is outdated.
     6118         *
     6119         * @since 5.0.0
     6120         *
     6121         * @param bool $outdated Whether the PHP version is outdated.
     6122         */
     6123        return apply_filters( 'wp_is_php_version_outdated', $outdated );
     6124}
  • tests/phpunit/tests/admin/includesUpdatePHP.php

     
     1<?php
     2
     3/**
     4 * @group admin
     5 * @group upgrade
     6 */
     7class Tests_Admin_IncludesUpdatePHP extends WP_UnitTestCase {
     8
     9        protected static $user;
     10
     11        public static function wpSetUpBeforeClass( $factory ) {
     12                self::$user = $factory->user->create_and_get( array( 'role' => 'administrator' ) );
     13
     14                grant_super_admin( self::$user->ID );
     15        }
     16
     17        public static function wpTearDownAfterClass() {
     18                wp_delete_user( self::$user->ID );
     19        }
     20
     21        public function test_display_notice_when_old_php() {
     22                wp_set_current_user( self::$user->ID );
     23                add_filter( 'wp_is_php_version_outdated', '__return_true' );
     24
     25                $this->assertTrue( wp_should_display_upgrade_php_notice() );
     26        }
     27
     28        public function test_hide_notice_when_old_php() {
     29                wp_set_current_user( self::$user->ID );
     30                add_filter( 'wp_is_php_version_outdated', '__return_false' );
     31
     32                $this->assertFalse( wp_should_display_upgrade_php_notice() );
     33        }
     34
     35        public function test_hide_notice_when_dismissed_pointer() {
     36                wp_set_current_user( self::$user->ID );
     37                add_filter( 'wp_is_php_version_outdated', '__return_true' );
     38                update_user_meta( self::$user->ID, 'dismissed_wp_pointers', 'upgrade_php_notice' );
     39
     40                $this->assertFalse( wp_should_display_upgrade_php_notice() );
     41        }
     42
     43        public function test_hide_notice_when_lacking_capabilities() {
     44                wp_set_current_user( self::$user->ID );
     45                add_filter( 'wp_is_php_version_outdated', '__return_true' );
     46                add_filter( 'map_meta_cap', array( $this, 'filter_prevent_upgrade_php_cap' ), 10, 2 );
     47
     48                $this->assertFalse( wp_should_display_upgrade_php_notice() );
     49        }
     50
     51        public function filter_prevent_upgrade_php_cap( $caps, $cap ) {
     52                if ( 'upgrade_php' === $cap ) {
     53                        return array( 'do_not_allow' );
     54                }
     55
     56                return $caps;
     57        }
     58}
  • tests/phpunit/tests/user/capabilities.php

     
    236236                        'install_languages'      => array( 'administrator' ),
    237237                        'update_languages'       => array( 'administrator' ),
    238238                        'deactivate_plugins'     => array( 'administrator' ),
     239                        'upgrade_php'            => array( 'administrator' ),
    239240
    240241                        'edit_categories'        => array( 'administrator', 'editor' ),
    241242                        'delete_categories'      => array( 'administrator', 'editor' ),
     
    267268                        'install_languages'      => array(),
    268269                        'update_languages'       => array(),
    269270                        'deactivate_plugins'     => array(),
     271                        'upgrade_php'            => array(),
    270272
    271273                        'customize'              => array( 'administrator' ),
    272274                        'delete_site'            => array( 'administrator' ),