Skip to content

Commit a87d356

Browse files
committed
Remove the static method from the class and updated to use the constant
1 parent 7973f68 commit a87d356

File tree

10 files changed

+39
-53
lines changed

10 files changed

+39
-53
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function image_prioritizer_filter_rest_request_before_callbacks( $response, arra
297297

298298
// Check for class existence and use constant or class method calls accordingly.
299299
$route_endpoint = class_exists( 'OD_REST_URL_Metrics_Store_Endpoint' )
300-
? OD_REST_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route()
300+
? OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE
301301
: OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE; // @phpstan-ignore constant.deprecated, constant.deprecated (To be replaced with class method calls in subsequent release.)
302302

303303
if (

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_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route() );
730+
$request = new WP_REST_Request( 'POST', '/' . OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE );
731731
$request->set_header( 'content-type', 'application/json' );
732732
$request->set_body( wp_json_encode( $url_metric_data ) );
733733
return $request;

plugins/optimization-detective/deprecated.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @since 0.1.0
1919
* @access private
20-
* @deprecated n.e.x.t This constant should not be used, instead use OD_REST_URL_Metrics_Store_Endpoint::get_namespace() method.
20+
* @deprecated n.e.x.t This constant should not be used, instead use OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE method.
2121
* @var string
2222
*/
2323
const OD_REST_API_NAMESPACE = 'optimization-detective/v1';
@@ -32,7 +32,7 @@
3232
* @since 0.1.0
3333
* @access private
3434
* @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::get_route() method.
35+
* @deprecated n.e.x.t This constant should not be used, instead use OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE method.
3636
* @var string
3737
*/
3838
const OD_URL_METRICS_ROUTE = '/url-metrics:store';

plugins/optimization-detective/detection.php

Lines changed: 17 additions & 3 deletions
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_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route() ),
124+
'restApiEndpoint' => rest_url( OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE ),
125125
'currentETag' => $current_etag,
126126
'currentUrl' => $current_url,
127127
'urlMetricSlug' => $slug,
@@ -169,8 +169,22 @@ function od_register_rest_url_metric_store_endpoint(): void {
169169
$endpoint_controller = new OD_REST_URL_Metrics_Store_Endpoint();
170170

171171
register_rest_route(
172-
$endpoint_controller::get_namespace(),
173-
$endpoint_controller::get_route(),
172+
$endpoint_controller::REST_API_NAMESPACE,
173+
$endpoint_controller::REST_API_ROUTE,
174174
$endpoint_controller->get_registration_args()
175175
);
176176
}
177+
178+
/**
179+
* Triggers the page cache invalidation action for Optimization Detective.
180+
*
181+
* @since n.e.x.t
182+
* @access private
183+
*
184+
* @param positive-int $cache_purge_post_id Cache purge post ID.
185+
*/
186+
function od_trigger_page_cache_invalidation_callback( int $cache_purge_post_id ): void {
187+
188+
$endpoint_controller = new OD_REST_URL_Metrics_Store_Endpoint();
189+
$endpoint_controller->trigger_page_cache_invalidation( $cache_purge_post_id );
190+
}

plugins/optimization-detective/hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +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' );
2829
// @codeCoverageIgnoreEnd

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_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route()
94+
'/' . OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE
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_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route() );
195+
$rest_url = get_rest_url( null, OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE );
196196
$response = wp_remote_post(
197197
$rest_url,
198198
array(

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,25 @@
2121
final class OD_REST_URL_Metrics_Store_Endpoint {
2222

2323
/**
24-
* Gets the namespace for the REST API endpoint.
24+
* Namespace for the REST API endpoint.
2525
*
2626
* @since n.e.x.t
27-
*
28-
* @return non-empty-string Namespace.
27+
* @var string
2928
*/
30-
public static function get_namespace(): string {
31-
return OD_REST_API_NAMESPACE; // @phpstan-ignore constant.deprecated (To be replaced with string literal when constant is removed.)
32-
}
29+
const REST_API_NAMESPACE = 'optimization-detective/v1';
3330

3431
/**
35-
* Gets the route for storing a URL Metric.
32+
* Route for storing a URL Metric.
3633
*
3734
* Note the `:store` art of the endpoint follows Google's guidance in AIP-136 for the use of the POST method in a way
3835
* that does not strictly follow the standard usage. Namely, submitting a POST request to this endpoint will either
3936
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
4037
*
4138
* @since n.e.x.t
4239
* @link https://google.aip.dev/136
43-
*
44-
* @return non-empty-string Route.
40+
* @var string
4541
*/
46-
public static function get_route(): string {
47-
return OD_URL_METRICS_ROUTE; // @phpstan-ignore constant.deprecated (To be replaced with string literal when constant is removed.)
48-
}
42+
const REST_API_ROUTE = '/url-metrics:store';
4943

5044
/**
5145
* Gets the arguments for registering the endpoint.
@@ -316,7 +310,7 @@ public function handle_rest_request( WP_REST_Request $request ) {
316310
*
317311
* @param positive-int $cache_purge_post_id Cache purge post ID.
318312
*/
319-
public static function trigger_page_cache_invalidation( int $cache_purge_post_id ): void {
313+
public function trigger_page_cache_invalidation( int $cache_purge_post_id ): void {
320314
$post = get_post( $cache_purge_post_id );
321315
if ( ! ( $post instanceof WP_Post ) ) {
322316
return;

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,7 @@ class Test_OD_REST_URL_Metrics_Store_Endpoint extends WP_UnitTestCase {
1919
* @return string Route.
2020
*/
2121
private function get_route(): string {
22-
return '/' . OD_REST_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route();
23-
}
24-
25-
/**
26-
* Test get_namespace().
27-
*
28-
* @covers OD_REST_URL_Metrics_Store_Endpoint::get_namespace
29-
*/
30-
public function test_get_namespace(): void {
31-
32-
$expected = 'optimization-detective/v1';
33-
$actual = OD_REST_URL_Metrics_Store_Endpoint::get_namespace();
34-
35-
$this->assertSame( $expected, $actual );
36-
}
37-
38-
/**
39-
* Test get_route().
40-
*
41-
* @covers OD_REST_URL_Metrics_Store_Endpoint::get_route
42-
*/
43-
public function test_get_route(): void {
44-
45-
$expected = '/url-metrics:store';
46-
$actual = OD_REST_URL_Metrics_Store_Endpoint::get_route();
47-
48-
$this->assertSame( $expected, $actual );
22+
return '/' . OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE;
4923
}
5024

5125
/**
@@ -828,7 +802,8 @@ static function ( string $hook, ...$args ) use ( &$all_hook_callback_args ): voi
828802
PHP_INT_MAX
829803
);
830804

831-
OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation( $cache_purge_post_id );
805+
$url_metric_endpoint = new OD_REST_URL_Metrics_Store_Endpoint();
806+
$url_metric_endpoint->trigger_page_cache_invalidation( $cache_purge_post_id );
832807

833808
$this->assertArrayHasKey( 'clean_post_cache', $all_hook_callback_args );
834809
$found = false;
@@ -869,13 +844,15 @@ static function ( string $hook, ...$args ) use ( &$all_hook_callback_args ): voi
869844
* Test trigger_page_cache_invalidation() for an invalid post.
870845
*
871846
* @covers OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation
847+
* @covers ::od_trigger_page_cache_invalidation_callback
872848
*/
873849
public function test_trigger_page_cache_invalidation_invalid_post_id(): void {
874850
wp_delete_post( 1, true );
875851
$before_clean_post_cache_count = did_action( 'clean_post_cache' );
876852
$before_transition_post_status_count = did_action( 'transition_post_status' );
877853
$before_save_post_count = did_action( 'save_post' );
878-
OD_REST_URL_Metrics_Store_Endpoint::trigger_page_cache_invalidation( 1 );
854+
$url_metric_endpoint = new OD_REST_URL_Metrics_Store_Endpoint();
855+
$url_metric_endpoint->trigger_page_cache_invalidation( 1 );
879856
$this->assertSame( $before_clean_post_cache_count, did_action( 'clean_post_cache' ) );
880857
$this->assertSame( $before_transition_post_status_count, did_action( 'transition_post_status' ) );
881858
$this->assertSame( $before_save_post_count, did_action( 'save_post' ) );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,6 @@ public function test_od_get_detection_script_returns_script( Closure $set_up, ar
234234
public function od_register_rest_url_metric_store_endpoint(): void {
235235

236236
$routes = rest_get_server()->get_routes();
237-
$this->assertArrayHasKey( '/' . OD_REST_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route(), $routes );
237+
$this->assertArrayHasKey( '/' . OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE, $routes );
238238
}
239239
}

plugins/optimization-detective/tests/test-site-health.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ protected function filter_rest_api_response( $mocked_response ): object {
420420
add_filter(
421421
'pre_http_request',
422422
static function ( $pre, array $args, string $url ) use ( $mocked_response, $observer ) {
423-
if ( rest_url( OD_REST_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route() ) === $url ) {
423+
if ( rest_url( OD_REST_URL_Metrics_Store_Endpoint::REST_API_NAMESPACE . OD_REST_URL_Metrics_Store_Endpoint::REST_API_ROUTE ) === $url ) {
424424
$observer->counter++;
425425
return $mocked_response;
426426
}

0 commit comments

Comments
 (0)