diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index c7f95c9901..998a27a2f4 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -298,7 +298,7 @@ function image_prioritizer_filter_rest_request_before_callbacks( $response, arra $request->get_method() !== 'POST' || // 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'. - OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) ) + OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) ) ) { return $response; } diff --git a/plugins/image-prioritizer/tests/test-helper.php b/plugins/image-prioritizer/tests/test-helper.php index ff164afe63..79a4092c41 100644 --- a/plugins/image-prioritizer/tests/test-helper.php +++ b/plugins/image-prioritizer/tests/test-helper.php @@ -727,7 +727,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo }; $create_request = static function ( array $url_metric_data ): WP_REST_Request { - $request = new WP_REST_Request( 'POST', '/' . OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE ); + $request = new WP_REST_Request( 'POST', '/' . OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE ); $request->set_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $url_metric_data ) ); return $request; diff --git a/plugins/optimization-detective/deprecated.php b/plugins/optimization-detective/deprecated.php new file mode 100644 index 0000000000..a23b724a2d --- /dev/null +++ b/plugins/optimization-detective/deprecated.php @@ -0,0 +1,39 @@ + od_get_maximum_viewport_aspect_ratio(), 'isDebug' => WP_DEBUG, 'extensionModuleUrls' => $extension_module_urls, - 'restApiEndpoint' => rest_url( OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE ), + 'restApiEndpoint' => rest_url( OD_Rest_API::get_namespace() . OD_Rest_API::get_route() ), 'currentETag' => $current_etag, 'currentUrl' => $current_url, 'urlMetricSlug' => $slug, diff --git a/plugins/optimization-detective/load.php b/plugins/optimization-detective/load.php index ff8870cca6..0c637147ac 100644 --- a/plugins/optimization-detective/load.php +++ b/plugins/optimization-detective/load.php @@ -99,6 +99,9 @@ static function ( string $version ): void { require_once __DIR__ . '/helper.php'; + // Deprecations. + require_once __DIR__ . '/deprecated.php'; + // Core infrastructure classes. require_once __DIR__ . '/class-od-data-validation-exception.php'; require_once __DIR__ . '/class-od-html-tag-processor.php'; diff --git a/plugins/optimization-detective/site-health.php b/plugins/optimization-detective/site-health.php index a5ae5ed946..cdbbcc17ac 100644 --- a/plugins/optimization-detective/site-health.php +++ b/plugins/optimization-detective/site-health.php @@ -91,7 +91,7 @@ function od_compose_site_health_result( $response ): array { sprintf( /* translators: %s is the REST API endpoint */ __( 'To collect URL Metrics from visitors the REST API must be available to unauthenticated users. Specifically, visitors must be able to perform a POST request to the %s endpoint.', 'optimization-detective' ), - '/' . OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE + '/' . OD_Rest_API::get_namespace() . OD_Rest_API::get_route() ), array( 'code' => array() ) ) . '

'; @@ -192,7 +192,7 @@ function od_get_rest_api_health_check_response( bool $use_cached ) { if ( false !== $response ) { return $response; } - $rest_url = get_rest_url( null, OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE ); + $rest_url = get_rest_url( null, OD_Rest_API::get_namespace() . OD_Rest_API::get_route() ); $response = wp_remote_post( $rest_url, array( diff --git a/plugins/optimization-detective/storage/class-od-rest-api.php b/plugins/optimization-detective/storage/class-od-rest-api.php index e338f48672..d423c8baff 100644 --- a/plugins/optimization-detective/storage/class-od-rest-api.php +++ b/plugins/optimization-detective/storage/class-od-rest-api.php @@ -52,6 +52,28 @@ public static function add_hooks(): void { add_action( 'rest_api_init', array( __CLASS__, 'od_register_endpoint' ) ); } + /** + * Get the namespace for optimization-detective + * + * @since n.e.x.t + * @access private + * @return string + */ + public static function get_namespace(): string { + return self::OD_REST_API_NAMESPACE; + } + + /** + * Get the route for storing URL metric. + * + * @since n.e.x.t + * @access private + * @return string + */ + public static function get_route(): string { + return self::OD_URL_METRICS_ROUTE; + } + /** * Registers endpoint for storage of URL Metric. * @@ -102,8 +124,8 @@ public static function od_register_endpoint(): void { ); register_rest_route( - self::OD_REST_API_NAMESPACE, - self::OD_URL_METRICS_ROUTE, + self::get_namespace(), + self::get_route(), array( 'methods' => 'POST', 'args' => array_merge( diff --git a/plugins/optimization-detective/tests/storage/test-class-od-rest-api.php b/plugins/optimization-detective/tests/storage/test-class-od-rest-api.php index c36d0d76b2..04145e89c3 100644 --- a/plugins/optimization-detective/tests/storage/test-class-od-rest-api.php +++ b/plugins/optimization-detective/tests/storage/test-class-od-rest-api.php @@ -32,6 +32,32 @@ public function test_add_hooks(): void { ); } + /** + * Test get_namespace(). + * + * @covers OD_Rest_API::get_namespace + */ + public function test_get_namespace(): void { + + $expected = 'optimization-detective/v1'; + $actual = OD_Rest_API::get_namespace(); + + $this->assertSame( $expected, $actual ); + } + + /** + * Test get_route(). + * + * @covers OD_Rest_API::get_route + */ + public function test_get_route(): void { + + $expected = '/url-metrics:store'; + $actual = OD_Rest_API::get_route(); + + $this->assertSame( $expected, $actual ); + } + /** * Data provider. * diff --git a/plugins/optimization-detective/tests/test-site-health.php b/plugins/optimization-detective/tests/test-site-health.php index d80732e08e..f8e633096d 100644 --- a/plugins/optimization-detective/tests/test-site-health.php +++ b/plugins/optimization-detective/tests/test-site-health.php @@ -420,7 +420,7 @@ protected function filter_rest_api_response( $mocked_response ): object { add_filter( 'pre_http_request', static function ( $pre, array $args, string $url ) use ( $mocked_response, $observer ) { - if ( rest_url( OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE ) === $url ) { + if ( rest_url( OD_Rest_API::get_namespace() . OD_Rest_API::get_route() ) === $url ) { $observer->counter++; return $mocked_response; }