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

Fix triggering post update actions after storing a URL Metric and refactor REST API endpoint logic into class #1865

Merged
merged 33 commits into from
Mar 14, 2025

Conversation

hbhalodia
Copy link
Contributor

@hbhalodia hbhalodia commented Feb 11, 2025

Summary

Fixes #1859

Relevant technical choices

  1. Converted storage/rest-api.php to class based approach and named storage/class-od-rest-api.php.
  2. Converted the test file from test-rest-api.php to test-class-rest-api.php.
  3. Updated the constants and functions which were directly used within files and used the static way to call the functions and class variables.

TODO

  • Incorporate add_action( 'od_trigger_page_cache_invalidation', 'od_trigger_page_cache_invalidation' ) after refactor.

…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.
Copy link

codecov bot commented Feb 11, 2025

Codecov Report

Attention: Patch coverage is 97.63033% with 5 lines in your changes missing coverage. Please review.

Project coverage is 71.25%. Comparing base (da320f3) to head (70002a5).
Report is 34 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/optimization-detective/load.php 0.00% 2 Missing ⚠️
...orage/class-od-rest-url-metrics-store-endpoint.php 98.93% 2 Missing ⚠️
plugins/image-prioritizer/helper.php 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1865      +/-   ##
==========================================
+ Coverage   71.18%   71.25%   +0.06%     
==========================================
  Files          85       85              
  Lines        7000     7006       +6     
==========================================
+ Hits         4983     4992       +9     
+ Misses       2017     2014       -3     
Flag Coverage Δ
multisite 71.25% <97.63%> (+0.06%) ⬆️
single 40.65% <3.79%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hbhalodia hbhalodia marked this pull request as ready for review February 11, 2025 09:35
Copy link

github-actions bot commented Feb 11, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: hbhalodia <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

… 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.
@hbhalodia
Copy link
Contributor Author

Hi @westonruter, I have updated the PR with the required change, I need some inputs on,

  1. I have used @since n.e.x.t, since I am not sure what exact version should be added.
  2. Also, when I ran phpstan, it throws an error in image priortizer to use OD_Rest_API class methods to call those constant, as we have added a deprecated tag there, so how to remove that, for now I have committed using --no-verify.

Can you please suggest what should be there in both the cases?

Thank You,

@westonruter
Copy link
Member

1. I have used @since n.e.x.t, since I am not sure what exact version should be added.

That is correct.

2. Also, when I ran phpstan, it throws an error in image priortizer to use OD_Rest_API class methods to call those constant, as we have added a deprecated tag there, so how to remove that, for now I have committed using --no-verify.

Here's one way: bd3f309.

@hbhalodia
Copy link
Contributor Author

Hi @westonruter, adding this for visibility, I would be going for long vacation starting tomorrow, so if we can land this PR before that, it would be very helpful, else I would not able to work on feedbacks after tomorrow 🙇. Apologies!

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Feb 13, 2025
@westonruter
Copy link
Member

@hbhalodia got it! Unfortunately we may not be able to finish this before you go on vacation since my availability is limited now for the next week and a half per #1850 (comment).

@hbhalodia
Copy link
Contributor Author

@hbhalodia got it! Unfortunately we may not be able to finish this before you go on vacation since my availability is limited now for the next week and a half per #1850 (comment).

Hey @westonruter, No issues. I will cover once back. Also, let's meet at WC Asia 🙌, I would be also there.

@westonruter
Copy link
Member

westonruter commented Feb 28, 2025

@hbhalodia Did we end up crossing paths at WC Asia? I'm sorry to say I can't recall chatting with you, although there were so many people my memory may have been overloaded.

I've resolved the merge conflicts introduced by #1867 by porting the change to rest-api.php in that PR over to this class in 4f9b1c9.

@hbhalodia
Copy link
Contributor Author

@hbhalodia Did we end up crossing paths at WC Asia? I'm sorry to say I can't recall chatting with you, although there were so many people my memory may have been overloaded.

I've resolved the merge conflicts introduced by #1867 by porting the change to rest-api.php in that PR over to this class in 4f9b1c9.

Hi @westonruter, unfortunately not.

Thanks for resolving the conflicts.

Also is there anything pending in this PR other than, #1865 (comment)?

Let me know as I am back from vacation and would work on the same.

Thank You,

@westonruter
Copy link
Member

@hbhalodia ah, sorry we couldn't connect in Manila.

For this PR, the bigger aspect to get clarity on is #1859 (comment). Based on @felixarntz's feedback there we may not use a static class but rather a singleton class. You can expect a resolution to that discussion tomorrow.

@hbhalodia
Copy link
Contributor Author

#1859 (comment)

Ah, alright, I missed that. No worries, we can update the PR based on the discussion and resolution.

Thank You,

@hbhalodia hbhalodia requested a review from westonruter March 13, 2025 06:13
@westonruter
Copy link
Member

Sorry, after merging #1905 there are now some merge conflicts that need to be resolved. I think the new function which was introduced in that PR should made a method of the class introduced here

@westonruter
Copy link
Member

I'm working on resolving the merge conflicts.

@westonruter
Copy link
Member

That was more difficult than I was expecting.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

@hbhalodia Thank you for the many iterations on this!

@westonruter westonruter added [Type] Bug An existing feature is broken and removed [Type] Enhancement A suggestion for improvement of an existing feature labels Mar 14, 2025
@westonruter westonruter changed the title Update: REST API logic in Optimization Detective to be encapsulated into a static class Fix triggering post update actions after storing a URL Metric and refactor REST API endpoint logic into class Mar 14, 2025
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thanks @hbhalodia and @westonruter!

This looks good, just one point of feedback, but not a blocker for merge, though it would be great to follow up on this separately.

* @param WP_REST_Request $request Request used to generate the response.
* @return mixed Passed through $result if successful, or otherwise a WP_Error.
*/
public function decompress_rest_request_body( $result, WP_REST_Server $server, WP_REST_Request $request ) {
Copy link
Member

Choose a reason for hiding this comment

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

Having this in here doesn't seem ideal, since the method is not scoped to this endpoint (even though it technically only acts if the endpoint is hit).

So I think ideally this would be either a standalone function or in a general REST utility class with static methods.

Since it's marked private, not a blocker for merging, but wanted to flag. It could be refactored later.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I'll address in a follow-up I'm working on right now.

Copy link
Member

Choose a reason for hiding this comment

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

See #1924

@westonruter westonruter merged commit ab0b99a into WordPress:trunk Mar 14, 2025
26 checks passed
@westonruter
Copy link
Member

I can see this is working now because in my Stream records on my site I see that the posts are updated:

image

This is happening before updates to URL Metrics:

image

@hbhalodia
Copy link
Contributor Author

Thanks, @westonruter For updating the PR and apologies that I was not available last friday, hence was not able to pick the further feedbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REST API logic in Optimization Detective can be encapsulated into a ~static~ class
3 participants