Skip to content

Commit

Permalink
Remove class constants and address deprecated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Feb 12, 2025
1 parent 5edaddf commit bd3f309
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
6 changes: 5 additions & 1 deletion plugins/image-prioritizer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ 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_NAMESPACE . OD_URL_METRICS_ROUTE // @phpstan-ignore constant.deprecated, constant.deprecated (To be replaced with class method calls in subsequent release.)
!==
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::get_namespace() . OD_Rest_API::get_route() );
$request->set_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $url_metric_data ) );
return $request;
Expand Down
38 changes: 10 additions & 28 deletions plugins/optimization-detective/storage/class-od-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,6 @@
*/
class OD_Rest_API {

/**
* Namespace for optimization-detective.
*
* @since 0.1.0
* @access private
* @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
* @var string
*/
const OD_URL_METRICS_ROUTE = '/url-metrics:store';

/**
* Adds hooks.
*
Expand All @@ -56,20 +33,25 @@ public static function add_hooks(): void {
* Get the namespace for optimization-detective
*
* @since n.e.x.t
* @access private
* @return non-empty-string Namespace.
*/
public static function get_namespace(): string {
return self::OD_REST_API_NAMESPACE;
return OD_REST_API_NAMESPACE; // @phpstan-ignore constant.deprecated (To be replaced with string literal when constant is removed.)
}

/**
* Get the route for storing URL metric.
* Gets the 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 n.e.x.t
* @access private
* @link https://google.aip.dev/136
* @return non-empty-string Route.
*/
public static function get_route(): string {
return self::OD_URL_METRICS_ROUTE;
return OD_URL_METRICS_ROUTE; // @phpstan-ignore constant.deprecated (To be replaced with string literal when constant is removed.)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class Test_OD_REST_API extends WP_UnitTestCase {
use Optimization_Detective_Test_Helpers;

/**
* @var string
* Gets the route.
*
* @return string Route.
*/
const ROUTE = '/' . OD_Rest_API::OD_REST_API_NAMESPACE . OD_Rest_API::OD_URL_METRICS_ROUTE;
private function get_route(): string {
return '/' . OD_Rest_API::get_namespace() . OD_Rest_API::get_route();
}

/**
* Test add_hooks().
Expand Down Expand Up @@ -518,7 +522,7 @@ public function test_rest_request_bad_params( array $params, int $expected_statu
* @covers OD_Rest_API::od_is_allowed_http_origin
*/
public function test_rest_request_without_origin(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_body_params( $this->get_valid_params() ); // Valid and yet set as POST params and not as JSON body, so this is why it fails.
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 403, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
Expand All @@ -534,7 +538,7 @@ public function test_rest_request_without_origin(): void {
* @covers OD_Rest_API::od_is_allowed_http_origin
*/
public function test_rest_request_cross_origin(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_header( 'Origin', 'https://cross-origin.example.com' );
$request->set_body_params( $this->get_valid_params() ); // Valid and yet set as POST params and not as JSON body, so this is why it fails.
$response = rest_get_server()->dispatch( $request );
Expand Down Expand Up @@ -569,7 +573,7 @@ static function ( string $url ): string {
* @covers OD_Rest_API::od_handle_rest_request
*/
public function test_rest_request_not_json_data(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_header( 'Origin', home_url() );
$request->set_body_params( $this->get_valid_params() ); // Valid and yet set as POST params and not as JSON body, so this is why it fails.
$response = rest_get_server()->dispatch( $request );
Expand All @@ -585,7 +589,7 @@ public function test_rest_request_not_json_data(): void {
* @covers OD_Rest_API::od_handle_rest_request
*/
public function test_rest_request_not_json_content_type(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_body( wp_json_encode( $this->get_valid_params() ) );
$request->set_header( 'Content-Type', 'text/plain' );
$response = rest_get_server()->dispatch( $request );
Expand All @@ -601,7 +605,7 @@ public function test_rest_request_not_json_content_type(): void {
* @covers OD_Rest_API::od_handle_rest_request
*/
public function test_rest_request_empty_array_json_body(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_body( '[]' );
$request->set_header( 'Content-Type', 'application/json' );
$response = rest_get_server()->dispatch( $request );
Expand All @@ -617,7 +621,7 @@ public function test_rest_request_empty_array_json_body(): void {
* @covers OD_Rest_API::od_handle_rest_request
*/
public function test_rest_request_non_array_json_body(): void {
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_body( '"Hello World!"' );
$request->set_header( 'Content-Type', 'application/json' );
$response = rest_get_server()->dispatch( $request );
Expand Down Expand Up @@ -973,7 +977,7 @@ private function create_request( array $params ): WP_REST_Request {
*
* @var WP_REST_Request<array<string, mixed>> $request
*/
$request = new WP_REST_Request( 'POST', self::ROUTE );
$request = new WP_REST_Request( 'POST', $this->get_route() );
$request->set_header( 'Content-Type', 'application/json' );
$request->set_query_params( wp_array_slice_assoc( $params, array( 'hmac', 'current_etag', 'slug', 'cache_purge_post_id' ) ) );
$request->set_header( 'Origin', home_url() );
Expand Down

0 comments on commit bd3f309

Please sign in to comment.