Make WordPress Core


Ignore:
Timestamp:
08/31/2022 10:44:04 PM (2 years ago)
Author:
flixos90
Message:

Site Health: Introduce page cache check.

This changeset adds a new page_cache check which determines whether the site uses a full page cache, and in addition assesses the server response time. If no page cache is present and the server response time is slow, the check will suggest use of a page cache.

A few filters are included for customization of the check:

  • site_status_good_response_time_threshold filters the number of milliseconds below which the server response time is considered good. The default value is based on the server-response-time Lighthouse audit and can be altered using this filter.
  • site_status_page_cache_supported_cache_headers filters the map of supported cache headers and their callback to determine whether it was a cache hit. The default list includes commonly used cache headers, and it is filterable to support e.g. additional cache headers used by specific vendors.

Note that due to the nature of this check it is only run in production environments.

Props furi3r, westonruter, spacedmonkey, swissspidy, Clorith.
Fixes #56041.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-site-health-controller.php

    r49603 r54043  
    100100        $this->assertSame( 'dotorg_communication', $response->get_data()['test'] );
    101101    }
     102
     103    /**
     104     * Tests Page Cache Rest endpoint registration.
     105     *
     106     * @ticket 56041
     107     */
     108    public function test_page_cache_endpoint() {
     109        $server = rest_get_server();
     110        $routes = $server->get_routes();
     111
     112        $endpoint = '/wp-site-health/v1/tests/page-cache';
     113        $this->assertArrayHasKey( $endpoint, $routes );
     114
     115        $route = $routes[ $endpoint ];
     116        $this->assertCount( 1, $route );
     117
     118        $route = current( $route );
     119        $this->assertEquals(
     120            array( WP_REST_Server::READABLE => true ),
     121            $route['methods']
     122        );
     123
     124        $this->assertEquals(
     125            'test_page_cache',
     126            $route['callback'][1]
     127        );
     128
     129        $this->assertIsCallable( $route['permission_callback'] );
     130
     131        if ( current_user_can( 'view_site_health_checks' ) ) {
     132            $this->assertTrue( call_user_func( $route['permission_callback'] ) );
     133        } else {
     134            $this->assertFalse( call_user_func( $route['permission_callback'] ) );
     135        }
     136
     137        wp_set_current_user( self::factory()->user->create( array( 'role' => 'author' ) ) );
     138        $this->assertFalse( call_user_func( $route['permission_callback'] ) );
     139
     140        $user = wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     141        if ( is_multisite() ) {
     142            // Site health cap is only available for super admins in Multi sites.
     143            grant_super_admin( $user->ID );
     144        }
     145        $this->assertTrue( call_user_func( $route['permission_callback'] ) );
     146    }
    102147}
Note: See TracChangeset for help on using the changeset viewer.