Skip to content

Commit c668aa2

Browse files
committed
Update the trigger page cache validation function to remove from class and add it separate method
1 parent a87d356 commit c668aa2

File tree

5 files changed

+110
-127
lines changed

5 files changed

+110
-127
lines changed

plugins/optimization-detective/detection.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,47 @@ function od_register_rest_url_metric_store_endpoint(): void {
176176
}
177177

178178
/**
179-
* Triggers the page cache invalidation action for Optimization Detective.
179+
* Triggers actions for page caches to invalidate their caches related to the supplied cache purge post ID.
180+
*
181+
* This is intended to flush any page cache for the URL after the new URL Metric was submitted so that the optimizations
182+
* which depend on that URL Metric can start to take effect.
180183
*
181184
* @since n.e.x.t
182-
* @access private
183185
*
184186
* @param positive-int $cache_purge_post_id Cache purge post ID.
185187
*/
186-
function od_trigger_page_cache_invalidation_callback( int $cache_purge_post_id ): void {
188+
function od_handle_trigger_page_cache_invalidation( int $cache_purge_post_id ): void {
187189

188-
$endpoint_controller = new OD_REST_URL_Metrics_Store_Endpoint();
189-
$endpoint_controller->trigger_page_cache_invalidation( $cache_purge_post_id );
190+
$post = get_post( $cache_purge_post_id );
191+
if ( ! ( $post instanceof WP_Post ) ) {
192+
return;
193+
}
194+
195+
// Fire actions that page caching plugins listen to flush caches.
196+
197+
/*
198+
* The clean_post_cache action is used to flush page caches by:
199+
* - Pantheon Advanced Cache <https://github.com/pantheon-systems/pantheon-advanced-page-cache/blob/e3b5552b0cb9268d9b696cb200af56cc044920d9/pantheon-advanced-page-cache.php#L185>
200+
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1615>
201+
* - Batcache <https://github.com/Automattic/batcache/blob/ed0e6b2d9bcbab3924c49a6c3247646fb87a0957/batcache.php#L18>
202+
*/
203+
/** This action is documented in wp-includes/post.php. */
204+
do_action( 'clean_post_cache', $post->ID, $post );
205+
206+
/*
207+
* The transition_post_status action is used to flush page caches by:
208+
* - Jetpack Boost <https://github.com/Automattic/jetpack-boost-production/blob/4090a3f9414c2171cd52d8a397f00b0d1151475f/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php#L76>
209+
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1616>
210+
* - LightSpeed Cache <https://github.com/litespeedtech/lscache_wp/blob/7c707469b3c88b4f45d9955593b92f9aeaed54c3/src/purge.cls.php#L68>
211+
*/
212+
/** This action is documented in wp-includes/post.php. */
213+
do_action( 'transition_post_status', $post->post_status, $post->post_status, $post );
214+
215+
/*
216+
* The clean_post_cache action is used to flush page caches by:
217+
* - W3 Total Cache <https://github.com/BoldGrid/w3-total-cache/blob/ab08f104294c6a8dcb00f1c66aaacd0615c42850/Util_AttachToActions.php#L32>
218+
* - WP Rocket <https://github.com/wp-media/wp-rocket/blob/e5bca6673a3669827f3998edebc0c785210fe561/inc/common/purge.php#L283>
219+
*/
220+
/** This action is documented in wp-includes/post.php. */
221+
do_action( 'save_post', $post->ID, $post, /* $update */ true );
190222
}

plugins/optimization-detective/hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
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 );
2727
add_action( 'rest_api_init', 'od_register_rest_url_metric_store_endpoint' );
28-
add_action( 'od_trigger_page_cache_invalidation', 'od_trigger_page_cache_invalidation_callback' );
28+
add_action( 'od_trigger_page_cache_invalidation', 'od_handle_trigger_page_cache_invalidation' );
2929
// @codeCoverageIgnoreEnd

plugins/optimization-detective/storage/class-od-rest-url-metrics-store-endpoint.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -299,49 +299,4 @@ public function handle_rest_request( WP_REST_Request $request ) {
299299
)
300300
);
301301
}
302-
303-
/**
304-
* Triggers actions for page caches to invalidate their caches related to the supplied cache purge post ID.
305-
*
306-
* This is intended to flush any page cache for the URL after the new URL Metric was submitted so that the optimizations
307-
* which depend on that URL Metric can start to take effect.
308-
*
309-
* @since n.e.x.t
310-
*
311-
* @param positive-int $cache_purge_post_id Cache purge post ID.
312-
*/
313-
public function trigger_page_cache_invalidation( int $cache_purge_post_id ): void {
314-
$post = get_post( $cache_purge_post_id );
315-
if ( ! ( $post instanceof WP_Post ) ) {
316-
return;
317-
}
318-
319-
// Fire actions that page caching plugins listen to flush caches.
320-
321-
/*
322-
* The clean_post_cache action is used to flush page caches by:
323-
* - Pantheon Advanced Cache <https://github.com/pantheon-systems/pantheon-advanced-page-cache/blob/e3b5552b0cb9268d9b696cb200af56cc044920d9/pantheon-advanced-page-cache.php#L185>
324-
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1615>
325-
* - Batcache <https://github.com/Automattic/batcache/blob/ed0e6b2d9bcbab3924c49a6c3247646fb87a0957/batcache.php#L18>
326-
*/
327-
/** This action is documented in wp-includes/post.php. */
328-
do_action( 'clean_post_cache', $post->ID, $post );
329-
330-
/*
331-
* The transition_post_status action is used to flush page caches by:
332-
* - Jetpack Boost <https://github.com/Automattic/jetpack-boost-production/blob/4090a3f9414c2171cd52d8a397f00b0d1151475f/app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php#L76>
333-
* - WP Super Cache <https://github.com/Automattic/wp-super-cache/blob/73b428d2fce397fd874b3056ad3120c343bc1a0c/wp-cache-phase2.php#L1616>
334-
* - LightSpeed Cache <https://github.com/litespeedtech/lscache_wp/blob/7c707469b3c88b4f45d9955593b92f9aeaed54c3/src/purge.cls.php#L68>
335-
*/
336-
/** This action is documented in wp-includes/post.php. */
337-
do_action( 'transition_post_status', $post->post_status, $post->post_status, $post );
338-
339-
/*
340-
* The clean_post_cache action is used to flush page caches by:
341-
* - W3 Total Cache <https://github.com/BoldGrid/w3-total-cache/blob/ab08f104294c6a8dcb00f1c66aaacd0615c42850/Util_AttachToActions.php#L32>
342-
* - WP Rocket <https://github.com/wp-media/wp-rocket/blob/e5bca6673a3669827f3998edebc0c785210fe561/inc/common/purge.php#L283>
343-
*/
344-
/** This action is documented in wp-includes/post.php. */
345-
do_action( 'save_post', $post->ID, $post, /* $update */ true );
346-
}
347302
}

plugins/optimization-detective/tests/storage/test-class-od-rest-url-metrics-store-endpoint.php

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ static function ( array $properties ) use ( $property_name ): array {
7474
*
7575
* @covers OD_REST_URL_Metrics_Store_Endpoint::get_registration_args
7676
* @covers OD_REST_URL_Metrics_Store_Endpoint::handle_rest_request
77-
* @covers OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation
7877
* @covers OD_Strict_URL_Metric::set_additional_properties_to_false
7978
* @covers OD_URL_Metric_Store_Request_Context::__construct
8079
* @covers OD_URL_Metric_Store_Request_Context::__get
@@ -144,7 +143,7 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
144143

145144
$this->assertInstanceOf( OD_URL_Metric_Store_Request_Context::class, $stored_context );
146145

147-
// Now check that trigger_page_cache_invalidation() cleaned caches as expected.
146+
// Now check that od_handle_trigger_page_cache_invalidation() cleaned caches as expected.
148147
$this->assertSame( $url_metrics[0]->jsonSerialize(), $stored_context->url_metric->jsonSerialize() );
149148
if ( isset( $valid_params['cache_purge_post_id'] ) ) {
150149
$cache_purge_post_id = $stored_context->request->get_param( 'cache_purge_post_id' );
@@ -784,80 +783,6 @@ static function () use ( $breakpoint_width ): array {
784783
$this->assertSame( 403, $response->get_status(), 'Response: ' . wp_json_encode( $response->get_data() ) );
785784
}
786785

787-
/**
788-
* Test trigger_page_cache_invalidation().
789-
*
790-
* @covers OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation
791-
*/
792-
public function test_trigger_page_cache_invalidation(): void {
793-
$cache_purge_post_id = self::factory()->post->create();
794-
795-
$all_hook_callback_args = array();
796-
add_action(
797-
'all',
798-
static function ( string $hook, ...$args ) use ( &$all_hook_callback_args ): void {
799-
$all_hook_callback_args[ $hook ][] = $args;
800-
},
801-
10,
802-
PHP_INT_MAX
803-
);
804-
805-
$url_metric_endpoint = new OD_REST_URL_Metrics_Store_Endpoint();
806-
$url_metric_endpoint->trigger_page_cache_invalidation( $cache_purge_post_id );
807-
808-
$this->assertArrayHasKey( 'clean_post_cache', $all_hook_callback_args );
809-
$found = false;
810-
foreach ( $all_hook_callback_args['clean_post_cache'] as $args ) {
811-
if ( $args[0] === $cache_purge_post_id ) {
812-
$this->assertInstanceOf( WP_Post::class, $args[1] );
813-
$this->assertSame( $cache_purge_post_id, $args[1]->ID );
814-
$found = true;
815-
}
816-
}
817-
$this->assertTrue( $found, 'Expected clean_post_cache to have been fired for the post queried object.' );
818-
819-
$this->assertArrayHasKey( 'transition_post_status', $all_hook_callback_args );
820-
$found = false;
821-
foreach ( $all_hook_callback_args['transition_post_status'] as $args ) {
822-
$this->assertInstanceOf( WP_Post::class, $args[2] );
823-
if ( $args[2]->ID === $cache_purge_post_id ) {
824-
$this->assertSame( $args[2]->post_status, $args[0] );
825-
$this->assertSame( $args[2]->post_status, $args[1] );
826-
$found = true;
827-
}
828-
}
829-
$this->assertTrue( $found, 'Expected transition_post_status to have been fired for the post queried object.' );
830-
831-
$this->assertArrayHasKey( 'save_post', $all_hook_callback_args );
832-
$found = false;
833-
foreach ( $all_hook_callback_args['save_post'] as $args ) {
834-
if ( $args[0] === $cache_purge_post_id ) {
835-
$this->assertInstanceOf( WP_Post::class, $args[1] );
836-
$this->assertSame( $cache_purge_post_id, $args[1]->ID );
837-
$found = true;
838-
}
839-
}
840-
$this->assertTrue( $found, 'Expected save_post to have been fired for the post queried object.' );
841-
}
842-
843-
/**
844-
* Test trigger_page_cache_invalidation() for an invalid post.
845-
*
846-
* @covers OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation
847-
* @covers ::od_trigger_page_cache_invalidation_callback
848-
*/
849-
public function test_trigger_page_cache_invalidation_invalid_post_id(): void {
850-
wp_delete_post( 1, true );
851-
$before_clean_post_cache_count = did_action( 'clean_post_cache' );
852-
$before_transition_post_status_count = did_action( 'transition_post_status' );
853-
$before_save_post_count = did_action( 'save_post' );
854-
$url_metric_endpoint = new OD_REST_URL_Metrics_Store_Endpoint();
855-
$url_metric_endpoint->trigger_page_cache_invalidation( 1 );
856-
$this->assertSame( $before_clean_post_cache_count, did_action( 'clean_post_cache' ) );
857-
$this->assertSame( $before_transition_post_status_count, did_action( 'transition_post_status' ) );
858-
$this->assertSame( $before_save_post_count, did_action( 'save_post' ) );
859-
}
860-
861786
/**
862787
* Populate URL Metrics.
863788
*

plugins/optimization-detective/tests/test-detection.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,75 @@ public function od_register_rest_url_metric_store_endpoint(): void {
236236
$routes = rest_get_server()->get_routes();
237237
$this->assertArrayHasKey( '/' . OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE, $routes );
238238
}
239+
240+
/**
241+
* Test od_handle_trigger_page_cache_invalidation().
242+
*
243+
* @covers ::od_handle_trigger_page_cache_invalidation
244+
*/
245+
public function test_trigger_page_cache_invalidation(): void {
246+
$cache_purge_post_id = self::factory()->post->create();
247+
248+
$all_hook_callback_args = array();
249+
add_action(
250+
'all',
251+
static function ( string $hook, ...$args ) use ( &$all_hook_callback_args ): void {
252+
$all_hook_callback_args[ $hook ][] = $args;
253+
},
254+
10,
255+
PHP_INT_MAX
256+
);
257+
258+
od_handle_trigger_page_cache_invalidation( $cache_purge_post_id );
259+
260+
$this->assertArrayHasKey( 'clean_post_cache', $all_hook_callback_args );
261+
$found = false;
262+
foreach ( $all_hook_callback_args['clean_post_cache'] as $args ) {
263+
if ( $args[0] === $cache_purge_post_id ) {
264+
$this->assertInstanceOf( WP_Post::class, $args[1] );
265+
$this->assertSame( $cache_purge_post_id, $args[1]->ID );
266+
$found = true;
267+
}
268+
}
269+
$this->assertTrue( $found, 'Expected clean_post_cache to have been fired for the post queried object.' );
270+
271+
$this->assertArrayHasKey( 'transition_post_status', $all_hook_callback_args );
272+
$found = false;
273+
foreach ( $all_hook_callback_args['transition_post_status'] as $args ) {
274+
$this->assertInstanceOf( WP_Post::class, $args[2] );
275+
if ( $args[2]->ID === $cache_purge_post_id ) {
276+
$this->assertSame( $args[2]->post_status, $args[0] );
277+
$this->assertSame( $args[2]->post_status, $args[1] );
278+
$found = true;
279+
}
280+
}
281+
$this->assertTrue( $found, 'Expected transition_post_status to have been fired for the post queried object.' );
282+
283+
$this->assertArrayHasKey( 'save_post', $all_hook_callback_args );
284+
$found = false;
285+
foreach ( $all_hook_callback_args['save_post'] as $args ) {
286+
if ( $args[0] === $cache_purge_post_id ) {
287+
$this->assertInstanceOf( WP_Post::class, $args[1] );
288+
$this->assertSame( $cache_purge_post_id, $args[1]->ID );
289+
$found = true;
290+
}
291+
}
292+
$this->assertTrue( $found, 'Expected save_post to have been fired for the post queried object.' );
293+
}
294+
295+
/**
296+
* Test od_handle_trigger_page_cache_invalidation() for an invalid post.
297+
*
298+
* @covers ::od_handle_trigger_page_cache_invalidation
299+
*/
300+
public function test_trigger_page_cache_invalidation_invalid_post_id(): void {
301+
wp_delete_post( 1, true );
302+
$before_clean_post_cache_count = did_action( 'clean_post_cache' );
303+
$before_transition_post_status_count = did_action( 'transition_post_status' );
304+
$before_save_post_count = did_action( 'save_post' );
305+
od_handle_trigger_page_cache_invalidation( 1 );
306+
$this->assertSame( $before_clean_post_cache_count, did_action( 'clean_post_cache' ) );
307+
$this->assertSame( $before_transition_post_status_count, did_action( 'transition_post_status' ) );
308+
$this->assertSame( $before_save_post_count, did_action( 'save_post' ) );
309+
}
239310
}

0 commit comments

Comments
 (0)