16
16
* OD_REST_URL_Metrics_Store_Endpoint class
17
17
*
18
18
* @since n.e.x.t
19
- *
20
19
* @access private
21
20
*/
22
- class OD_REST_URL_Metrics_Store_Endpoint {
21
+ final class OD_REST_URL_Metrics_Store_Endpoint {
23
22
24
23
/**
25
- * Get the namespace for optimization-detective
24
+ * Gets the namespace for the REST API endpoint.
26
25
*
27
26
* @since n.e.x.t
27
+ *
28
28
* @return non-empty-string Namespace.
29
29
*/
30
30
public static function get_namespace (): string {
@@ -39,7 +39,6 @@ public static function get_namespace(): string {
39
39
* create a new `od_url_metrics` post, or it will update an existing post if one already exists for the provided slug.
40
40
*
41
41
* @since n.e.x.t
42
- *
43
42
* @link https://google.aip.dev/136
44
43
*
45
44
* @return non-empty-string Route.
@@ -49,7 +48,7 @@ public static function get_route(): string {
49
48
}
50
49
51
50
/**
52
- * Get the args for the URL Metric storage endpoint.
51
+ * Gets the arguments for registering the endpoint.
53
52
*
54
53
* @since n.e.x.t
55
54
*
@@ -100,18 +99,13 @@ public function get_registration_args(): array {
100
99
),
101
100
);
102
101
103
- /**
104
- * Create route args.
105
- */
106
- $ route_args = array (
102
+ return array (
107
103
'methods ' => 'POST ' ,
108
104
'args ' => array_merge (
109
105
$ args ,
110
106
rest_get_endpoint_args_for_schema ( OD_Strict_URL_Metric::get_json_schema () )
111
107
),
112
- 'callback ' => static function ( WP_REST_Request $ request ) {
113
- return self ::handle_rest_request ( $ request );
114
- },
108
+ 'callback ' => array ( $ this , 'handle_rest_request ' ),
115
109
'permission_callback ' => static function () {
116
110
// Needs to be available to unauthenticated visitors.
117
111
if ( OD_Storage_Lock::is_locked () ) {
@@ -124,8 +118,6 @@ public function get_registration_args(): array {
124
118
return true ;
125
119
},
126
120
);
127
-
128
- return $ route_args ;
129
121
}
130
122
131
123
/**
@@ -136,31 +128,28 @@ public function get_registration_args(): array {
136
128
* the `is_allowed_http_origin()` function in core for some reason returns a string rather than a boolean.
137
129
*
138
130
* @since n.e.x.t
139
- *
140
131
* @see is_allowed_http_origin()
141
132
*
142
133
* @param string $origin Origin to check.
143
- *
144
134
* @return bool Whether the origin is allowed.
145
135
*/
146
- private static function is_allowed_http_origin ( string $ origin ): bool {
136
+ protected static function is_allowed_http_origin ( string $ origin ): bool {
147
137
// Strip out the port number since core does not account for it yet as noted in get_allowed_http_origins().
148
138
$ origin = preg_replace ( '/:\d+$/ ' , '' , $ origin );
149
139
return '' !== is_allowed_http_origin ( $ origin );
150
140
}
151
141
152
142
/**
153
- * Handles REST API request to store metrics .
143
+ * Handles the REST API request to store a URL Metric .
154
144
*
155
145
* @since n.e.x.t
156
146
*
157
147
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
158
148
*
159
149
* @param WP_REST_Request $request Request.
160
- *
161
150
* @return WP_REST_Response|WP_Error Response.
162
151
*/
163
- private static function handle_rest_request ( WP_REST_Request $ request ) {
152
+ public function handle_rest_request ( WP_REST_Request $ request ) {
164
153
// Block cross-origin storage requests since by definition URL Metrics data can only be sourced from the frontend of the site.
165
154
$ origin = $ request ->get_header ( 'origin ' );
166
155
if ( null === $ origin || ! self ::is_allowed_http_origin ( $ origin ) ) {
0 commit comments