Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
463486e
First commit, adding amp tests, start refactoring
manuelRod Mar 18, 2022
f0c7beb
some refactoring + coding standards
manuelRod Mar 22, 2022
19609d4
refactoring in helper file + wp standards
manuelRod Mar 22, 2022
6f8ca76
Adding first unit tests
manuelRod Mar 23, 2022
c295ace
Merge branch 'release/1.0.0-beta.3' into add/sitehealth-test-full-pag…
manuelRod Mar 23, 2022
0a78791
Adding tests
manuelRod Mar 24, 2022
b9c704f
test refactor and bugfix
manuelRod Mar 24, 2022
f6098e7
ups
manuelRod Mar 24, 2022
fea0974
fixing test
manuelRod Mar 25, 2022
ce59323
Fixing array header scenario per AMP approach https://github.com/ampp…
manuelRod Mar 30, 2022
b548bfb
fixing multisite test
manuelRod Mar 30, 2022
d1fb05a
adding aruba cache
manuelRod Apr 12, 2022
30a35b6
removing escaping html, its exported to json and will cause problems
manuelRod Apr 12, 2022
bce19f0
little refactor + nitpicks
manuelRod Apr 19, 2022
6ef0b44
lint
manuelRod Apr 19, 2022
3f8f505
Merge branch 'trunk' into add/sitehealth-test-full-page-cache
manuelRod May 4, 2022
7a239fa
Adding CODEOWNERS
manuelRod May 4, 2022
dfad675
replacing @since to n.e.x.t
manuelRod May 4, 2022
2e6d1d9
moving things around
manuelRod May 4, 2022
fded890
updating loopback error test
manuelRod May 5, 2022
a8c3f7f
Adding @westonruter as code owner
manuelRod May 16, 2022
e137e28
Update modules/site-health/audit-full-page-cache/load.php
manuelRod May 30, 2022
6f5eb4a
Update modules/site-health/audit-full-page-cache/load.php
manuelRod May 30, 2022
c1dbc3b
Update modules/site-health/audit-full-page-cache/load.php
manuelRod May 30, 2022
e130163
Update modules/site-health/audit-full-page-cache/load.php
manuelRod May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some refactoring + coding standards
  • Loading branch information
manuelRod committed Mar 22, 2022
commit f0c7bebee120773dcbfe0c166347bc7e538e6633
223 changes: 116 additions & 107 deletions modules/site-health/audit-full-page-cache/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,16 @@
* @return array
Comment thread
manuelRod marked this conversation as resolved.
Outdated
*/
function perflab_afpc_add_full_page_cache_test( $tests ) {
$tests['async']['perflab_page_cache'] = array(
'label' => esc_html__( 'Page caching', 'performance-lab' ),
'test' => rest_url( 'performance-lab/v1/tests/page-cache' ),
$tests['async']['perflab_page_cache'] = array(
'label' => esc_html__( 'Page caching', 'performance-lab' ),
Comment thread
manuelRod marked this conversation as resolved.
Outdated
'test' => rest_url( 'performance-lab/v1/tests/page-cache' ),
'has_rest' => true,
'async_direct_test' => 'perflab_afpc_page_cache_test',
);
return $tests;
}
add_filter( 'site_status_tests', 'perflab_afpc_add_full_page_cache_test' );


function perflab_afpc_page_cache_no_cache_plugin_test( $response, $error_message, $error_code ) {
$response['badge']['color'] = 'red';
$response['label'] = esc_html__( 'Unable to detect the presence of page caching', 'performance-lab' );
$response['status'] = 'critical';
$error_info = sprintf(
/* translators: 1 is error message, 2 is error code */
esc_html__( 'Unable to detect page caching due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)', 'performance-lab' ),
$error_message,
$error_code
);
$response['description'] = wp_kses_post( "<p>$error_info</p>" ) . $response['description'];
return $response;
}


/**
* Callback for perflab_page_cache test.
*
Expand All @@ -56,17 +40,15 @@ function perflab_afpc_page_cache_no_cache_plugin_test( $response, $error_message
* @return array
Comment thread
manuelRod marked this conversation as resolved.
Outdated
*/
function perflab_afpc_page_cache_test() {
$page_cache_detail = get_page_cache_detail();

$description = '<p>' . esc_html__( 'Wordpress performs at its best when page caching is enabled. This is because the additional optimizations performed require additional server processing time, and page caching ensures that responses are served quickly.', 'performance-lab' ) . '</p>';
$description = '<p>' . esc_html__( 'WordPress performs at its best when page caching is enabled. This is because the additional optimizations performed require additional server processing time, and page caching ensures that responses are served quickly.', 'performance-lab' ) . '</p>';
Comment thread
felixarntz marked this conversation as resolved.
Outdated
$description .= '<p>' . esc_html__( 'Page caching is detected by looking for an active page caching plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:', 'performance-lab' )
. ' <code>' . implode( '</code>, <code>', array_keys( get_page_cache_headers() ) ) . '.</code>';

$result = array(
'badge' => [
'badge' => array(
'label' => esc_html__( 'Performance', 'performance-lab' ),
Comment thread
manuelRod marked this conversation as resolved.
Outdated
'color' => 'green',
],
),
'description' => wp_kses_post( $description ),
'test' => 'perflab_page_cache',
'status' => 'good',
Expand All @@ -80,97 +62,124 @@ function perflab_afpc_page_cache_test() {
),
);

if ( is_wp_error( $page_cache_detail ) ) {
return perflab_afpc_page_cache_no_cache_plugin_test( $result, $page_cache_detail->get_error_message(), $page_cache_detail->get_error_code() );
}
$page_cache_detail = get_page_cache_detail();

if ( 'recommended' === $page_cache_detail['status'] ) {
$badge_color = 'orange';
$status = $page_cache_detail['status'];
$label = __( 'Page caching is not detected but the server response time is OK', 'performance-lab' );
} elseif ( 'good' === $page_cache_detail['status'] ) {
$badge_color = 'green';
$status = $page_cache_detail['status'];
$label = __( 'Page caching is detected and the server response time is good', 'performance-lab' );
} else {
$badge_color = 'red';
$status = $page_cache_detail['status'];
if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
$label = __( 'Page caching is not detected and the server response time is slow', 'performance-lab' );
} else {
$label = __( 'Page caching is detected but the server response time is still slow', 'performance-lab' );
}
if ( is_wp_error( $page_cache_detail ) ) {
return perflab_afpc_page_cache_unable_detect_cache_test( $result, $page_cache_detail );
Comment thread
manuelRod marked this conversation as resolved.
Outdated
}
Comment thread
manuelRod marked this conversation as resolved.

if ( ! is_wp_error( $page_cache_detail ) ) {
$page_cache_test_summary = [];
return perflab_afpc_page_cache_detected_test( $result, $page_cache_detail );
Comment thread
manuelRod marked this conversation as resolved.
Outdated
}

if ( empty( $page_cache_detail['response_time'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.', 'performance-lab' );
} else {
/**
* Response when unable to detect the presence of page caching.
*
* @param array $default_response Default perflab_page_cache callback response.
* @param WP_Error $page_cache_detail_error get_page_cache_detail() response.
*
* @return array
* @since 1.0.0
*/
function perflab_afpc_page_cache_unable_detect_cache_test( $default_response, $page_cache_detail_error ) {
$default_response['badge']['color'] = 'red';
$default_response['label'] = esc_html__( 'Unable to detect the presence of page caching', 'performance-lab' );
$default_response['status'] = 'critical';
$error_info = sprintf(
/* translators: 1 is error message, 2 is error code */
esc_html__( 'Unable to detect page caching due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)', 'performance-lab' ),
$page_cache_detail_error->get_error_message(),
$page_cache_detail_error->get_error_code()
);
$default_response['description'] = wp_kses_post( "<p>$error_info</p>" ) . $default_response['description'];
return $default_response;
}

$threshold = get_good_response_time_threshold();
if ( $page_cache_detail['response_time'] < $threshold ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
__( 'Median server response time was %1$s milliseconds. This is less than the %2$s millisecond threshold.', 'performance-lab' ),
number_format_i18n( $page_cache_detail['response_time'] ),
number_format_i18n( $threshold )
);
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
__( 'Median server response time was %1$s milliseconds. It should be less than %2$s milliseconds.', 'performance-lab' ),
number_format_i18n( $page_cache_detail['response_time'] ),
number_format_i18n( $threshold )
);
/**
* Response when page cache detected.
*
* @param array $default_response Default perflab_page_cache callback response.
* @param array $page_cache_detail get_page_cache_detail() response.
*
* @return array
* @since 1.0.0
*/
function perflab_afpc_page_cache_detected_test( $default_response, $page_cache_detail ) {

$default_response['status'] = $page_cache_detail['status'];

switch ( $page_cache_detail['status'] ) {
case 'recommended':
$default_response['badge']['color'] = 'orange';
$default_response['label'] = esc_html__( 'Page caching is not detected but the server response time is OK', 'performance-lab' );
Comment thread
felixarntz marked this conversation as resolved.
Outdated
Comment thread
manuelRod marked this conversation as resolved.
Outdated
break;
case 'good':
$default_response['badge']['color'] = 'green';
$default_response['label'] = esc_html__( 'Page caching is detected and the server response time is good', 'performance-lab' );
Comment thread
mxbclang marked this conversation as resolved.
Outdated
Comment thread
manuelRod marked this conversation as resolved.
Outdated
break;
default:
$default_response['badge']['color'] = 'red';
$default_response['label'] = esc_html__( 'Page caching is detected but the server response time is still slow', 'performance-lab' );
Comment thread
manuelRod marked this conversation as resolved.
Outdated
Comment thread
manuelRod marked this conversation as resolved.
Outdated
if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
$default_response['label'] = esc_html__( 'Page caching is not detected and the server response time is slow', 'performance-lab' );
Comment thread
manuelRod marked this conversation as resolved.
Outdated
}
}

if ( empty( $page_cache_detail['headers'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.', 'performance-lab' );
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' .
sprintf(
/* translators: Placeholder is number of caching headers */
_n(
'There was %d client caching response header detected:',
'There were %d client caching response headers detected:',
count( $page_cache_detail['headers'] ),
'performance-lab'
),
count( $page_cache_detail['headers'] )
) .
' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
}
$page_cache_test_summary = array();

if ( empty( $page_cache_detail['response_time'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . esc_html__( 'Server response time could not be determined. Verify that loopback requests are working.', 'performance-lab' );
} else {

/**
* Filters the threshold below which a response time is considered good.
*
* @since 1.0.0
* @param int $threshold Threshold in milliseconds. Default 600.
*/
$threshold = (int) apply_filters( 'perflab_afpc_page_cache_good_response_time_threshold', 600 );

if ( $page_cache_detail['response_time'] < $threshold ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
esc_html__( 'Median server response time was %1$s milliseconds. This is less than the %2$s millisecond threshold.', 'performance-lab' ),
Comment thread
manuelRod marked this conversation as resolved.
Outdated
number_format_i18n( $page_cache_detail['response_time'] ),
number_format_i18n( $threshold )
);
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
esc_html__( 'Median server response time was %1$s milliseconds. It should be less than %2$s milliseconds.', 'performance-lab' ),
number_format_i18n( $page_cache_detail['response_time'] ),
number_format_i18n( $threshold )
);
}

if ( $page_cache_detail['advanced_cache_present'] ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page caching plugin was detected.', 'performance-lab' );
} elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
// Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page caching plugin was not detected.', 'performance-lab' );
if ( empty( $page_cache_detail['headers'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.', 'performance-lab' );
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' .
sprintf(
/* translators: Placeholder is number of caching headers */
_n(
'There was %d client caching response header detected:',
'There were %d client caching response headers detected:',
count( $page_cache_detail['headers'] ),
'performance-lab'
),
count( $page_cache_detail['headers'] )
) . ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
Comment thread
manuelRod marked this conversation as resolved.
Outdated
}
}

$description .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
if ( $page_cache_detail['advanced_cache_present'] ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . esc_html__( 'A page caching plugin was detected.', 'performance-lab' );
} elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
// Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . esc_html__( 'A page caching plugin was not detected.', 'performance-lab' );
}

return [
'badge' => [
'label' => esc_html__( 'Performance', 'performance-lab' ),
'color' => $badge_color,
],
'description' => wp_kses_post( $description ),
'test' => 'perflab_page_cache',
'status' => $status,
'label' => esc_html( $label ),
'actions' => sprintf(
'<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>',
esc_url( 'https://wordpress.org/support/article/optimization/#Caching' ),
esc_html__( 'Learn more about page caching', 'performance-lab' ),
/* translators: The accessibility text. */
esc_html__( '(opens in a new tab)', 'performance-lab' )
),
];
$default_response['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
return $default_response;
}

/**
Expand All @@ -182,18 +191,18 @@ function perflab_afpc_register_async_test_endpoints() {
register_rest_route(
'performance-lab/v1',
'/tests/page-cache',
[
[
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'perflab_afpc_page_cache_test',
/**'permission_callback' => static function() {
return current_user_can( 'view_site_health_checks' );
},**/
},*/
'permission_callback' => static function() {
return true;
},
],
]
),
)
);
}
add_action( 'rest_api_init', 'perflab_afpc_register_async_test_endpoints' );