Skip to content

Commit 0d71556

Browse files
committed
Fix the error feedbacks
1. Related to comments and common naming convention.
1 parent 127e015 commit 0d71556

File tree

4 files changed

+18
-44
lines changed

4 files changed

+18
-44
lines changed

plugins/optimization-detective/detection.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,18 @@ static function ( OD_URL_Metric_Group $group ): array {
159159
}
160160

161161
/**
162-
* Create the rest request and register the endpoint routes for the URL Metrics Store.
162+
* Registers the REST API endpoint for storing URL Metrics.
163163
*
164164
* @since n.e.x.t
165+
* @access private
165166
*/
166-
function od_create_rest_url_metric_store_rest_request(): void {
167+
function od_register_rest_url_metric_store_endpoint(): void {
167168

168169
$endpoint_controller = new OD_REST_URL_Metrics_Store_Endpoint();
169-
$route_args = $endpoint_controller->get_registration_args();
170170

171-
/**
172-
* Register the endpoint.
173-
*/
174171
register_rest_route(
175172
$endpoint_controller::get_namespace(),
176173
$endpoint_controller::get_route(),
177-
$route_args
174+
$endpoint_controller->get_registration_args()
178175
);
179176
}

plugins/optimization-detective/hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
add_filter( 'site_status_tests', 'od_add_rest_api_availability_test' );
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 );
27-
add_action( 'rest_api_init', 'od_create_rest_url_metric_store_rest_request' );
27+
add_action( 'rest_api_init', 'od_register_rest_url_metric_store_endpoint' );
2828
// @codeCoverageIgnoreEnd

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
* OD_REST_URL_Metrics_Store_Endpoint class
1717
*
1818
* @since n.e.x.t
19-
*
2019
* @access private
2120
*/
22-
class OD_REST_URL_Metrics_Store_Endpoint {
21+
final class OD_REST_URL_Metrics_Store_Endpoint {
2322

2423
/**
25-
* Get the namespace for optimization-detective
24+
* Gets the namespace for the REST API endpoint.
2625
*
2726
* @since n.e.x.t
27+
*
2828
* @return non-empty-string Namespace.
2929
*/
3030
public static function get_namespace(): string {
@@ -39,7 +39,6 @@ public static function get_namespace(): string {
3939
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
4040
*
4141
* @since n.e.x.t
42-
*
4342
* @link https://google.aip.dev/136
4443
*
4544
* @return non-empty-string Route.
@@ -49,7 +48,7 @@ public static function get_route(): string {
4948
}
5049

5150
/**
52-
* Get the args for the URL Metric storage endpoint.
51+
* Gets the arguments for registering the endpoint.
5352
*
5453
* @since n.e.x.t
5554
*
@@ -100,18 +99,13 @@ public function get_registration_args(): array {
10099
),
101100
);
102101

103-
/**
104-
* Create route args.
105-
*/
106-
$route_args = array(
102+
return array(
107103
'methods' => 'POST',
108104
'args' => array_merge(
109105
$args,
110106
rest_get_endpoint_args_for_schema( OD_Strict_URL_Metric::get_json_schema() )
111107
),
112-
'callback' => static function ( WP_REST_Request $request ) {
113-
return self::handle_rest_request( $request );
114-
},
108+
'callback' => array( $this, 'handle_rest_request' ),
115109
'permission_callback' => static function () {
116110
// Needs to be available to unauthenticated visitors.
117111
if ( OD_Storage_Lock::is_locked() ) {
@@ -124,8 +118,6 @@ public function get_registration_args(): array {
124118
return true;
125119
},
126120
);
127-
128-
return $route_args;
129121
}
130122

131123
/**
@@ -136,31 +128,28 @@ public function get_registration_args(): array {
136128
* the `is_allowed_http_origin()` function in core for some reason returns a string rather than a boolean.
137129
*
138130
* @since n.e.x.t
139-
*
140131
* @see is_allowed_http_origin()
141132
*
142133
* @param string $origin Origin to check.
143-
*
144134
* @return bool Whether the origin is allowed.
145135
*/
146-
private static function is_allowed_http_origin( string $origin ): bool {
136+
protected static function is_allowed_http_origin( string $origin ): bool {
147137
// Strip out the port number since core does not account for it yet as noted in get_allowed_http_origins().
148138
$origin = preg_replace( '/:\d+$/', '', $origin );
149139
return '' !== is_allowed_http_origin( $origin );
150140
}
151141

152142
/**
153-
* Handles REST API request to store metrics.
143+
* Handles the REST API request to store a URL Metric.
154144
*
155145
* @since n.e.x.t
156146
*
157147
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
158148
*
159149
* @param WP_REST_Request $request Request.
160-
*
161150
* @return WP_REST_Response|WP_Error Response.
162151
*/
163-
private static function handle_rest_request( WP_REST_Request $request ) {
152+
public function handle_rest_request( WP_REST_Request $request ) {
164153
// Block cross-origin storage requests since by definition URL Metrics data can only be sourced from the frontend of the site.
165154
$origin = $request->get_header( 'origin' );
166155
if ( null === $origin || ! self::is_allowed_http_origin( $origin ) ) {

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,13 @@ public function test_od_get_detection_script_returns_script( Closure $set_up, ar
227227
}
228228

229229
/**
230-
* Test od_create_rest_url_metric_store_rest_request().
230+
* Test od_register_rest_url_metric_store_endpoint().
231231
*
232-
* @covers ::od_create_rest_url_metric_store_rest_request
232+
* @covers ::od_register_rest_url_metric_store_endpoint
233233
*/
234-
public function test_od_create_rest_url_metric_store_rest_request(): void {
234+
public function od_register_rest_url_metric_store_endpoint(): void {
235235

236-
global $wp_rest_server;
237-
238-
if ( ! isset( $wp_rest_server ) ) {
239-
$wp_rest_server = new WP_REST_Server();
240-
241-
// Run the action to register the routes.
242-
do_action( 'rest_api_init' );
243-
}
244-
245-
// Get the routes.
246-
$routes = $wp_rest_server->get_routes();
247-
248-
// Check that the route is present.
236+
$routes = rest_get_server()->get_routes();
249237
$this->assertArrayHasKey( '/' . OD_REST_URL_Metrics_Store_Endpoint::get_namespace() . OD_REST_URL_Metrics_Store_Endpoint::get_route(), $routes );
250238
}
251239
}

0 commit comments

Comments
 (0)