Make WordPress Core


Ignore:
Timestamp:
09/01/2022 03:26:29 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Site Health: Some documentation and test improvements for WP_Site_Health:

  • Add a @coversDefaultClass annotation to address @covers ::get_test_page_cache" is invalid notices.
  • Rename data providers to start with the data_ prefix and match the test method names, for consistency.
  • Move data providers next to the test methods they are used in.
  • Move ::get_test_page_cache() closer to ::get_test_persistent_object_cache(), for a bit more predictable placement.
  • Fix a typo in ::get_test_persistent_object_cache() description.

Follow-up to [53955], [54043], [54044], [54045].

See #56041.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r54044 r54047  
    352352     * Test if plugins are outdated, or unnecessary.
    353353     *
    354      * The tests checks if your plugins are up to date, and encourages you to remove any
     354     * The test checks if your plugins are up to date, and encourages you to remove any
    355355     * that are not in use.
    356356     *
     
    16681668
    16691669    /**
    1670      * Tests if a full page cache is available.
    1671      *
    1672      * @since 6.1.0
    1673      *
    1674      * @return array The test result.
    1675      */
    1676     public function get_test_page_cache() {
    1677         $description  = '<p>' . __( 'Page cache enhances the speed and performance of your site by saving and serving static pages instead of calling for a page every time a user visits.' ) . '</p>';
    1678         $description .= '<p>' . __( 'Page cache is detected by looking for an active page cache plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:' ) . '</p>';
    1679         $description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
    1680 
    1681         $result = array(
    1682             'badge'       => array(
    1683                 'label' => __( 'Performance' ),
    1684                 'color' => 'blue',
    1685             ),
    1686             'description' => wp_kses_post( $description ),
    1687             'test'        => 'page_cache',
    1688             'status'      => 'good',
    1689             'label'       => '',
    1690             'actions'     => sprintf(
    1691                 '<p><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
    1692                 __( 'https://wordpress.org/support/article/optimization/#Caching' ),
    1693                 __( 'Learn more about page cache' ),
    1694                 /* translators: Accessibility text. */
    1695                 __( '(opens in a new tab)' )
    1696             ),
    1697         );
    1698 
    1699         $page_cache_detail = $this->get_page_cache_detail();
    1700 
    1701         if ( is_wp_error( $page_cache_detail ) ) {
    1702             $result['label']  = __( 'Unable to detect the presence of page cache' );
    1703             $result['status'] = 'recommended';
    1704             $error_info       = sprintf(
    1705             /* translators: 1: Error message, 2: Error code. */
    1706                 __( 'Unable to detect page cache due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)' ),
    1707                 $page_cache_detail->get_error_message(),
    1708                 $page_cache_detail->get_error_code()
    1709             );
    1710             $result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
    1711             return $result;
    1712         }
    1713 
    1714         $result['status'] = $page_cache_detail['status'];
    1715 
    1716         switch ( $page_cache_detail['status'] ) {
    1717             case 'recommended':
    1718                 $result['label'] = __( 'Page cache is not detected but the server response time is OK' );
    1719                 break;
    1720             case 'good':
    1721                 $result['label'] = __( 'Page cache is detected and the server response time is good' );
    1722                 break;
    1723             default:
    1724                 if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
    1725                     $result['label'] = __( 'Page cache is not detected and the server response time is slow' );
    1726                 } else {
    1727                     $result['label'] = __( 'Page cache is detected but the server response time is still slow' );
    1728                 }
    1729         }
    1730 
    1731         $page_cache_test_summary = array();
    1732 
    1733         if ( empty( $page_cache_detail['response_time'] ) ) {
    1734             $page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
    1735         } else {
    1736 
    1737             $threshold = $this->get_good_response_time_threshold();
    1738             if ( $page_cache_detail['response_time'] < $threshold ) {
    1739                 $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
    1740                     /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
    1741                     __( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
    1742                     number_format_i18n( $page_cache_detail['response_time'] ),
    1743                     number_format_i18n( $threshold )
    1744                 );
    1745             } else {
    1746                 $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
    1747                     /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
    1748                     __( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
    1749                     number_format_i18n( $page_cache_detail['response_time'] ),
    1750                     number_format_i18n( $threshold )
    1751                 );
    1752             }
    1753 
    1754             if ( empty( $page_cache_detail['headers'] ) ) {
    1755                 $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
    1756             } else {
    1757                 $headers_summary  = '<span class="dashicons dashicons-yes-alt"></span>';
    1758                 $headers_summary .= ' ' . sprintf(
    1759                     /* translators: %d: Number of caching headers. */
    1760                     _n(
    1761                         'There was %d client caching response header detected:',
    1762                         'There were %d client caching response headers detected:',
    1763                         count( $page_cache_detail['headers'] )
    1764                     ),
    1765                     count( $page_cache_detail['headers'] )
    1766                 );
    1767                 $headers_summary          .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
    1768                 $page_cache_test_summary[] = $headers_summary;
    1769             }
    1770         }
    1771 
    1772         if ( $page_cache_detail['advanced_cache_present'] ) {
    1773             $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
    1774         } elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
    1775             // Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
    1776             $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
    1777         }
    1778 
    1779         $result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
    1780         return $result;
    1781     }
    1782 
    1783     /**
    17841670     * Check if the HTTP API can handle SSL/TLS requests.
    17851671     *
     
    23792265
    23802266    /**
    2381      * Tests if sites uses persistent object cache.
    2382      *
    2383      * Checks if site uses persistent object cache or recommends to use it if not.
     2267     * Tests if a full page cache is available.
     2268     *
     2269     * @since 6.1.0
     2270     *
     2271     * @return array The test result.
     2272     */
     2273    public function get_test_page_cache() {
     2274        $description  = '<p>' . __( 'Page cache enhances the speed and performance of your site by saving and serving static pages instead of calling for a page every time a user visits.' ) . '</p>';
     2275        $description .= '<p>' . __( 'Page cache is detected by looking for an active page cache plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:' ) . '</p>';
     2276        $description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
     2277
     2278        $result = array(
     2279            'badge'       => array(
     2280                'label' => __( 'Performance' ),
     2281                'color' => 'blue',
     2282            ),
     2283            'description' => wp_kses_post( $description ),
     2284            'test'        => 'page_cache',
     2285            'status'      => 'good',
     2286            'label'       => '',
     2287            'actions'     => sprintf(
     2288                '<p><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
     2289                __( 'https://wordpress.org/support/article/optimization/#Caching' ),
     2290                __( 'Learn more about page cache' ),
     2291                /* translators: Accessibility text. */
     2292                __( '(opens in a new tab)' )
     2293            ),
     2294        );
     2295
     2296        $page_cache_detail = $this->get_page_cache_detail();
     2297
     2298        if ( is_wp_error( $page_cache_detail ) ) {
     2299            $result['label']  = __( 'Unable to detect the presence of page cache' );
     2300            $result['status'] = 'recommended';
     2301            $error_info       = sprintf(
     2302            /* translators: 1: Error message, 2: Error code. */
     2303                __( 'Unable to detect page cache due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)' ),
     2304                $page_cache_detail->get_error_message(),
     2305                $page_cache_detail->get_error_code()
     2306            );
     2307            $result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
     2308            return $result;
     2309        }
     2310
     2311        $result['status'] = $page_cache_detail['status'];
     2312
     2313        switch ( $page_cache_detail['status'] ) {
     2314            case 'recommended':
     2315                $result['label'] = __( 'Page cache is not detected but the server response time is OK' );
     2316                break;
     2317            case 'good':
     2318                $result['label'] = __( 'Page cache is detected and the server response time is good' );
     2319                break;
     2320            default:
     2321                if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
     2322                    $result['label'] = __( 'Page cache is not detected and the server response time is slow' );
     2323                } else {
     2324                    $result['label'] = __( 'Page cache is detected but the server response time is still slow' );
     2325                }
     2326        }
     2327
     2328        $page_cache_test_summary = array();
     2329
     2330        if ( empty( $page_cache_detail['response_time'] ) ) {
     2331            $page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
     2332        } else {
     2333
     2334            $threshold = $this->get_good_response_time_threshold();
     2335            if ( $page_cache_detail['response_time'] < $threshold ) {
     2336                $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
     2337                    /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
     2338                    __( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
     2339                    number_format_i18n( $page_cache_detail['response_time'] ),
     2340                    number_format_i18n( $threshold )
     2341                );
     2342            } else {
     2343                $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
     2344                    /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
     2345                    __( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
     2346                    number_format_i18n( $page_cache_detail['response_time'] ),
     2347                    number_format_i18n( $threshold )
     2348                );
     2349            }
     2350
     2351            if ( empty( $page_cache_detail['headers'] ) ) {
     2352                $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
     2353            } else {
     2354                $headers_summary  = '<span class="dashicons dashicons-yes-alt"></span>';
     2355                $headers_summary .= ' ' . sprintf(
     2356                    /* translators: %d: Number of caching headers. */
     2357                    _n(
     2358                        'There was %d client caching response header detected:',
     2359                        'There were %d client caching response headers detected:',
     2360                        count( $page_cache_detail['headers'] )
     2361                    ),
     2362                    count( $page_cache_detail['headers'] )
     2363                );
     2364                $headers_summary          .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
     2365                $page_cache_test_summary[] = $headers_summary;
     2366            }
     2367        }
     2368
     2369        if ( $page_cache_detail['advanced_cache_present'] ) {
     2370            $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
     2371        } elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
     2372            // Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
     2373            $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
     2374        }
     2375
     2376        $result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
     2377        return $result;
     2378    }
     2379
     2380    /**
     2381     * Tests if the site uses persistent object cache and recommends to use it if not.
    23842382     *
    23852383     * @since 6.1.0
Note: See TracChangeset for help on using the changeset viewer.