Skip to content

Commit

Permalink
Update the class to create new methods to get the namespace and route…
Browse files Browse the repository at this point in the history
… for optimization detective

This commit adds the deprecated.php file which has the global constant that are being inside image priortizer and updated the OD plugin to use this constants via a dedicated methods.
  • Loading branch information
hbhalodia committed Feb 12, 2025
1 parent 29199b8 commit 47e5ee8
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/image-prioritizer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/image-prioritizer/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions plugins/optimization-detective/deprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Deprecated functions and constants.
*
* @package optimization-detective
*
* @since n.e.x.t
*/

// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Namespace for optimization-detective.
*
* @since 0.1.0
* @access private
* @deprecated n.e.x.t This constant should not be used, instead use OD_Rest_API::get_namespace() method.
* @var string
*/
const OD_REST_API_NAMESPACE = 'optimization-detective/v1';

/**
* Route for storing a URL Metric.
*
* Note the `:store` art of the endpoint follows Google's guidance in AIP-136 for the use of the POST method in a way
* that does not strictly follow the standard usage. Namely, submitting a POST request to this endpoint will either
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
*
* @since 0.1.0
* @access private
* @link https://google.aip.dev/136
* @deprecated n.e.x.t This constant should not be used, instead use OD_Rest_API::get_route() method.
* @var string
*/
const OD_URL_METRICS_ROUTE = '/url-metrics:store';
// @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion plugins/optimization-detective/detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function od_get_detection_script( string $slug, OD_URL_Metric_Group_Collection $
'maxViewportAspectRatio' => 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,
Expand Down
3 changes: 3 additions & 0 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ static function ( string $version ): void {

require_once __DIR__ . '/helper.php';

// Deprecations.
require_once __DIR__ . '/deprecated.php';

Check warning on line 103 in plugins/optimization-detective/load.php

View check run for this annotation

Codecov / codecov/patch

plugins/optimization-detective/load.php#L103

Added line #L103 was not covered by tests

// Core infrastructure classes.
require_once __DIR__ . '/class-od-data-validation-exception.php';
require_once __DIR__ . '/class-od-html-tag-processor.php';
Expand Down
4 changes: 2 additions & 2 deletions plugins/optimization-detective/site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>POST</code> request to the <code>%s</code> 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() )
) . '</p>';
Expand Down Expand Up @@ -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(
Expand Down
26 changes: 24 additions & 2 deletions plugins/optimization-detective/storage/class-od-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion plugins/optimization-detective/tests/test-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 47e5ee8

Please sign in to comment.