Skip to content

Commit a01d760

Browse files
authored
Merge branch 'trunk' into update/od-initialization-timing
2 parents 53a91a2 + ab0b99a commit a01d760

File tree

13 files changed

+726
-549
lines changed

13 files changed

+726
-549
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,21 @@ static function ( $host ) {
292292
*
293293
* @return WP_REST_Response|WP_HTTP_Response|WP_Error|mixed Result to send to the client.
294294
* @noinspection PhpDocMissingThrowsInspection
295+
* @noinspection PhpDeprecationInspection
295296
*/
296297
function image_prioritizer_filter_rest_request_before_callbacks( $response, array $handler, WP_REST_Request $request ) {
298+
unset( $handler ); // Unused.
299+
300+
// Check for class existence and use constant or class method calls accordingly.
301+
$route_endpoint = class_exists( 'OD_REST_URL_Metrics_Store_Endpoint' )
302+
? OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE
303+
: OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE; // @phpstan-ignore constant.deprecated, constant.deprecated (To be replaced with class method calls in subsequent release.)
304+
297305
if (
298306
$request->get_method() !== 'POST'
299307
||
300308
// The strtolower() and outer trim are due to \WP_REST_Server::match_request_to_handler() using case-insensitive pattern match and using '$' instead of '\z'.
301-
OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) )
309+
( rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) ) !== $route_endpoint )
302310
) {
303311
return $response;
304312
}

plugins/image-prioritizer/tests/test-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo
727727
};
728728

729729
$create_request = static function ( array $url_metric_data ): WP_REST_Request {
730-
$request = new WP_REST_Request( 'POST', '/' . OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE );
730+
$request = new WP_REST_Request( 'POST', '/' . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE );
731731
$request->set_header( 'content-type', 'application/json' );
732732
$request->set_body( wp_json_encode( $url_metric_data ) );
733733
return $request;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Deprecated functions and constants.
4+
*
5+
* @package optimization-detective
6+
*
7+
* @since n.e.x.t
8+
*/
9+
10+
// @codeCoverageIgnoreStart
11+
if ( ! defined( 'ABSPATH' ) ) {
12+
exit; // Exit if accessed directly.
13+
}
14+
15+
/**
16+
* Namespace for optimization-detective.
17+
*
18+
* @since 0.1.0
19+
* @access private
20+
* @deprecated n.e.x.t This constant should not be used, instead use OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE method.
21+
* @var string
22+
*/
23+
const OD_REST_API_NAMESPACE = 'optimization-detective/v1';
24+
25+
/**
26+
* Route for storing a URL Metric.
27+
*
28+
* Note the `:store` art of the endpoint follows Google's guidance in AIP-136 for the use of the POST method in a way
29+
* that does not strictly follow the standard usage. Namely, submitting a POST request to this endpoint will either
30+
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
31+
*
32+
* @since 0.1.0
33+
* @access private
34+
* @link https://google.aip.dev/136
35+
* @deprecated n.e.x.t This constant should not be used, instead use OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE method.
36+
* @var string
37+
*/
38+
const OD_URL_METRICS_ROUTE = '/url-metrics:store';
39+
// @codeCoverageIgnoreEnd

plugins/optimization-detective/detection.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function od_get_detection_script( string $slug, OD_URL_Metric_Group_Collection $
121121
'maxViewportAspectRatio' => od_get_maximum_viewport_aspect_ratio(),
122122
'isDebug' => WP_DEBUG,
123123
'extensionModuleUrls' => $extension_module_urls,
124-
'restApiEndpoint' => rest_url( OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE ),
124+
'restApiEndpoint' => rest_url( OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE ),
125125
'currentETag' => $current_etag,
126126
'currentUrl' => $current_url,
127127
'urlMetricSlug' => $slug,
@@ -159,3 +159,67 @@ static function ( OD_URL_Metric_Group $group ): array {
159159
array( 'type' => 'module' )
160160
);
161161
}
162+
163+
/**
164+
* Registers the REST API endpoint for storing URL Metrics.
165+
*
166+
* @since n.e.x.t
167+
* @access private
168+
*/
169+
function od_register_rest_url_metric_store_endpoint(): void {
170+
$endpoint_controller = new OD_REST_URL_Metrics_Store_Endpoint();
171+
172+
register_rest_route(
173+
$endpoint_controller::ROUTE_NAMESPACE,
174+
$endpoint_controller::ROUTE_BASE,
175+
$endpoint_controller->get_registration_args()
176+
);
177+
178+
add_filter( 'rest_pre_dispatch', array( $endpoint_controller, 'decompress_rest_request_body' ), 10, 3 );
179+
}
180+
181+
/**
182+
* Triggers post update actions for page caches to invalidate their caches related to the supplied cache purge post ID.
183+
*
184+
* This is intended to flush any page cache for the URL after the new URL Metric was submitted so that the optimizations
185+
* which depend on that URL Metric can start to take effect.
186+
*
187+
* @since n.e.x.t
188+
*
189+
* @param positive-int $cache_purge_post_id Cache purge post ID.
190+
*/
191+
function od_trigger_post_update_actions( int $cache_purge_post_id ): void {
192+
193+
$post = get_post( $cache_purge_post_id );
194+
if ( ! ( $post instanceof WP_Post ) ) {
195+
return;
196+
}
197+
198+
// Fire actions that page caching plugins listen to flush caches.
199+
200+
/*
201+
* The clean_post_cache action is used to flush page caches by:
202+
* - Pantheon Advanced Cache <https://github.com/pantheon-systems/pantheon-advanced-page-cache/blob/e3b5552b0cb9268d9b696cb200af56cc044920d9/pantheon-advanced-page-cache.php#L185>
203+
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1615>
204+
* - Batcache <https://github.com/Automattic/batcache/blob/ed0e6b2d9bcbab3924c49a6c3247646fb87a0957/batcache.php#L18>
205+
*/
206+
/** This action is documented in wp-includes/post.php. */
207+
do_action( 'clean_post_cache', $post->ID, $post );
208+
209+
/*
210+
* The transition_post_status action is used to flush page caches by:
211+
* - Jetpack Boost <https://github.com/Automattic/jetpack-boost-production/blob/4090a3f9414c2171cd52d8a397f00b0d1151475f/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php#L76>
212+
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1616>
213+
* - LightSpeed Cache <https://github.com/litespeedtech/lscache_wp/blob/7c707469b3c88b4f45d9955593b92f9aeaed54c3/src/purge.cls.php#L68>
214+
*/
215+
/** This action is documented in wp-includes/post.php. */
216+
do_action( 'transition_post_status', $post->post_status, $post->post_status, $post );
217+
218+
/*
219+
* The clean_post_cache action is used to flush page caches by:
220+
* - W3 Total Cache <https://github.com/BoldGrid/w3-total-cache/blob/ab08f104294c6a8dcb00f1c66aaacd0615c42850/Util_AttachToActions.php#L32>
221+
* - WP Rocket <https://github.com/wp-media/wp-rocket/blob/e5bca6673a3669827f3998edebc0c785210fe561/inc/common/purge.php#L283>
222+
*/
223+
/** This action is documented in wp-includes/post.php. */
224+
do_action( 'save_post', $post->ID, $post, /* $update */ true );
225+
}

plugins/optimization-detective/hooks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
add_filter( 'site_status_tests', 'od_add_rest_api_availability_test' );
2525
add_action( 'admin_init', 'od_maybe_run_rest_api_health_check' );
2626
add_action( 'after_plugin_row_meta', 'od_render_rest_api_health_check_admin_notice_in_plugin_row', 30 );
27-
add_filter( 'rest_pre_dispatch', 'od_decompress_rest_request_body', 10, 3 );
27+
add_action( 'rest_api_init', 'od_register_rest_url_metric_store_endpoint' );
28+
add_action( 'od_trigger_page_cache_invalidation', 'od_trigger_post_update_actions' );
2829
// @codeCoverageIgnoreEnd

plugins/optimization-detective/load.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ static function ( string $version ): void {
9999

100100
require_once __DIR__ . '/helper.php';
101101

102+
// Deprecations.
103+
require_once __DIR__ . '/deprecated.php';
104+
102105
// Core infrastructure classes.
103106
require_once __DIR__ . '/class-od-data-validation-exception.php';
104107
require_once __DIR__ . '/class-od-html-tag-processor.php';
@@ -114,7 +117,7 @@ class_alias( OD_URL_Metric_Group_Collection::class, 'OD_URL_Metrics_Group_Collec
114117
require_once __DIR__ . '/storage/class-od-url-metrics-post-type.php';
115118
require_once __DIR__ . '/storage/class-od-storage-lock.php';
116119
require_once __DIR__ . '/storage/data.php';
117-
require_once __DIR__ . '/storage/rest-api.php';
120+
require_once __DIR__ . '/storage/class-od-rest-url-metrics-store-endpoint.php';
118121
require_once __DIR__ . '/storage/class-od-url-metric-store-request-context.php';
119122

120123
// Detection logic.

plugins/optimization-detective/site-health.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function od_compose_site_health_result( $response ): array {
9191
sprintf(
9292
/* translators: %s is the REST API endpoint */
9393
__( 'To collect URL Metrics from visitors the REST API must be available to unauthenticated users. Specifically, visitors must be able to perform a <code>POST</code> request to the <code>%s</code> endpoint.', 'optimization-detective' ),
94-
'/' . OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE
94+
'/' . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE
9595
),
9696
array( 'code' => array() )
9797
) . '</p>';
@@ -192,7 +192,7 @@ function od_get_rest_api_health_check_response( bool $use_cached ) {
192192
if ( false !== $response ) {
193193
return $response;
194194
}
195-
$rest_url = get_rest_url( null, OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE );
195+
$rest_url = get_rest_url( null, OD_REST_URL_Metrics_Store_Endpoint::ROUTE_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::ROUTE_BASE );
196196
$response = wp_remote_post(
197197
$rest_url,
198198
array(

0 commit comments

Comments
 (0)