-
Notifications
You must be signed in to change notification settings - Fork 109
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
base: trunk
Are you sure you want to change the base?
Conversation
…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.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #1865 +/- ##
==========================================
+ Coverage 66.64% 66.71% +0.07%
==========================================
Files 88 88
Lines 7015 7025 +10
==========================================
+ Hits 4675 4687 +12
+ Misses 2340 2338 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
plugins/image-prioritizer/helper.php
Outdated
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(), '/' ) ) ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
/** | ||
* 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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should learn from the trouble we had the global constants with regard to there not being a way to indicate these being deprecated and change them to instead be methods like get_namespace()
and get_route()
that return the strings. This will give us more flexibility in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @westonruter, So I should revert the global constant what it was before OD_REST_API_NAMESPACE
and OD_URL_METRICS_ROUTE
and also add these 2 constants in class as well and then create a static function in class like get_namespace()
and get_route()
so that we can use that function inside Optimization detective
and for Image Priortizer
for now we should just use the global constant, so it does not break that.
Also, Can we add a check to Image Priortizer
to first check if global constant exists, if yes use that if not, check for the OD_Rest_API
class and call these 2 static methods to get this value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these constants could be moved to a new deprecated.php
in Optimization Detective and then they can continue to be used in Image Prioritizer for the time being. In a subsequent release we'll update Image Prioritizer to use what whatever new method we determine for accessing the namespace and route, such as methods on the new class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also specify the specific minimum version of Optimization Detective that Image Prioritizer requires here:
$required_od_version = '0.9.0'; |
But we can't go the other way around. So we have to keep those private deprecated constants in Optimization Detective around for awhile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, So creating a new method to access this from class is out of scope in this PR, correct?
So I also need to remove this constant from class as well for the time being and use global constant everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within Optimization Detective itself we'll use the new method on the class. In fact, the methods can just return the constants for now. Eventually we'll get rid of the constants and have the new methods return strings directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds Good, Would update the PR with required changes.
… for optimization detective This commit adds the deprecated.php file which has the global constant that are being inside image priortizer and updated the OD plugin to use this constants via a dedicated methods.
Hi @westonruter, I have updated the PR with the required change, I need some inputs on,
Can you please suggest what should be there in both the cases? Thank You, |
That is correct.
Here's one way: bd3f309. |
* @since 0.1.0 | ||
* @access private | ||
*/ | ||
class OD_Rest_API { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of OD_Rest_API
how about OD_URL_Metric_Store_Endpoint
. Or maybe to follow core's naming convention more: OD_REST_URL_Metric_Storage_Controller
. Let's see what @felixarntz thinks before you rename it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, would wait for the confirmation from @felixarntz.
… prefix from methods
Summary
Fixes #1859
Relevant technical choices
storage/rest-api.php
to class based approach and namedstorage/class-od-rest-api.php
.test-rest-api.php
totest-class-rest-api.php
.