Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: REST API logic in Optimization Detective to be encapsulated into a static class #1865

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), '/' ) ) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to be careful here. If someone updates Image Prioritizer first without also updating Optimization Detective, then they'll start getting fatal errors. The same would happen if they update Optimization Detective first without updating Image Prioritizer.

Copy link
Member

@felixarntz felixarntz Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't make this change then, at least not yet. Unfortunately we can't catch when someone uses a constant to show a deprecation warning, but I wouldn't be too worried about other extensions outside of our own ones at this point.

So maybe we should just keep the global constants around a bit longer and add a TODO to remove them eventually, while already introducing the new ones and updating our extensions to use the new ones. It would probably be fine to remove the global constants in 1-2 months from now.

We could even put a big Do not use them! above in the code so that someone looking at the code doesn't get the wrong idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add @deprecated tags to the constants so they show up crossed-out when someone tries to use them.

) {
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 @@
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
Loading