Skip to content

Commit

Permalink
Add new class to encapsulate logic of rest api to make consitent code…
Browse files Browse the repository at this point in the history
…base

This commit adds new class `class-od-rest-api.php` from `rest-api.php` file and adds the necessary required changes whereever normal functions were used, that are now refrencing the class static methods and variables. Also updated the test file test-rest-api.php to test-class-od-rest-api.php file and updated the required test cases as per the class.

Note: Have directly used function docs and comment that were present in the original file.
  • Loading branch information
hbhalodia committed Feb 11, 2025
1 parent d21b74d commit 29199b8
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 396 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_NAMESPACE . OD_URL_METRICS_ROUTE !== rtrim( strtolower( ltrim( $request->get_route(), '/' ) ) )
OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::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_NAMESPACE . OD_URL_METRICS_ROUTE );
$request = new WP_REST_Request( 'POST', '/' . OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE );
$request->set_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $url_metric_data ) );
return $request;
Expand Down
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_NAMESPACE . OD_URL_METRICS_ROUTE ),
'restApiEndpoint' => rest_url( OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE ),
'currentETag' => $current_etag,
'currentUrl' => $current_url,
'urlMetricSlug' => $slug,
Expand Down
1 change: 1 addition & 0 deletions plugins/optimization-detective/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
add_filter( 'template_include', 'od_buffer_output', PHP_INT_MAX );
OD_URL_Metrics_Post_Type::add_hooks();
OD_Storage_Lock::add_hooks();
OD_Rest_API::add_hooks();
add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' );
add_action( 'wp_head', 'od_render_generator_meta_tag' );
add_filter( 'site_status_tests', 'od_add_rest_api_availability_test' );
Expand Down
2 changes: 1 addition & 1 deletion plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class_alias( OD_URL_Metric_Group_Collection::class, 'OD_URL_Metrics_Group_Collec
require_once __DIR__ . '/storage/class-od-url-metrics-post-type.php';
require_once __DIR__ . '/storage/class-od-storage-lock.php';
require_once __DIR__ . '/storage/data.php';
require_once __DIR__ . '/storage/rest-api.php';
require_once __DIR__ . '/storage/class-od-rest-api.php';

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

View check run for this annotation

Codecov / codecov/patch

plugins/optimization-detective/load.php#L117

Added line #L117 was not covered by tests
require_once __DIR__ . '/storage/class-od-url-metric-store-request-context.php';

// Detection logic.
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_NAMESPACE . OD_URL_METRICS_ROUTE
'/' . OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_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_NAMESPACE . OD_URL_METRICS_ROUTE );
$rest_url = get_rest_url( null, OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE );
$response = wp_remote_post(
$rest_url,
array(
Expand Down
Loading

0 comments on commit 29199b8

Please sign in to comment.