-
Notifications
You must be signed in to change notification settings - Fork 50
Fix REST API initialization for composer package usage #55
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 REST API initialization for composer package usage #55
Conversation
- Move REST API route registration from abilities-api.php to bootstrap.php - Ensure REST API routes are initialized when used as composer package - Prevent duplicate registration by placing hook inside class existence check - Consolidate initialization logic in single location 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
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. |
|
PHPUnit tests fail. It's very likely that |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #55 +/- ##
============================================
- Coverage 84.61% 84.28% -0.34%
Complexity 96 96
============================================
Files 8 8
Lines 507 509 +2
============================================
Hits 429 429
- Misses 78 80 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
77552b1 to
867af46
Compare
gziolo
left a comment
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.
This looks like a good compromise to me. Thank you for proposing the enhancement.
@justlevine, I would appreciate your sanity check before we land and cut a new release.
justlevine
left a comment
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.
Nothing here is breaking or going into core. Worst case in the ~7 weeks before merge someone reports a bug caused by a race condition on action loading, and we tell them to either to instead install it as a plugin or move their require_once vendor/autoload.php somewhere else 😅
A couple smaller and equally nonimportant comments on the diff, but otherwise LGTM 🚀
That said,
I think I still need a sanity check when it comes to this whole Composer use case (cc @Jameswlepage ). nonblocking feedback with that in mind:
- Is initialization something that is wanted/expected when autoloading a library? (I would usually assume no.)
- If it is (or the real use case is as a plugin), then do we even need an
includes/boostrap.phpor can we just useabilities-api.phpas the entrypoint?
(Only tangential to this PR: is autoloading something that is wanted/expected or is a downstream require_once vendor/wordpress/abilities-api/{whatever-entrypoint}.php a better pattern for this particular repo's goals?)
| require_once __DIR__ . '/rest-api/class-wp-rest-abilities-init.php'; | ||
|
|
||
| // Initialize REST API routes when WordPress is available. | ||
| if ( function_exists( 'add_action' ) ) { |
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.
Do we need the function check if we've already proven ABSPATH, or do we need ABSPATH if we're anyway checking for add_action() before running any WP code?
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.
Good point.
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.
ABSPATH prevents processing of the file outside WP env - this is generally useful and in our context stops processing of the file during PHPUnit start procedure, when WP is not yet ready. We want to control the moment this gets loaded.
add_action tests could be considered redundant if ABSPATH is present. Though in WP startup procedure ABSPATH is defined earlier than add_action becomes available. Potentially we can have ABSPATH but no add_action. This just prevents a very "wrong" usage patter from crashing the site.
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.
It's also fine to be extra careful. Let's land this as is, as it won't be included in WP core in the same fashion.
| static function (): void { | ||
| require_once dirname( __DIR__ ) . '/abilities-api.php'; | ||
| // Require ( to bypass require_once ). | ||
| require dirname( __DIR__ ) . '/includes/bootstrap.php'; |
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'd feel better about skipping abilities-api.php if understood the purpose of all this, but since it's being left out of core, it doesn't actually matter)
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.
This is a test bootstrap. The purpose of this is to circumvent the quirkiness of PHPUnit which automatically loads autoloader, at the very beginning, and thus is triggering the loading of the library. Since it is doing this before WordPress environment has been loaded nothing gets actually loaded ( ABSPATH test prevents it ). Though PHP remembers the require_once calls. If we would load again abilities-api.php nothing would happen, because require_once for bootstrap.php file has been already called. Thus I have made a decision to call it directly for tests once again ( using require to force the load ) - after the WP environment has been loaded. This allows proper loading of all library elements and registering of the filter.
For normal operations from inside WP or a plugin this alterations would not be necessary - this is just for the unit tests and avoids tampering with require_once directives inside the production code which I wanted to avoid.
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.
This one is tricky as autoloader requires the same file, but it doesn't run any logic because of ABSPATH check, so it looks that all is correct.
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.
(@budzanowski yes I understood the what and the how, it's the premise that we should be "triggering the loading of the library" - or more precisely calling that autoloading is now conflated with instantiating the REST endpoint - but still have a separate bootstrap file that confuses me. The architectural implication is that there would be other functionality in ./abilities-api.php now or in the future that we'd no longer be able to test... but again since it's not true IRL it doesn't matter)
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.
@justlevine I agree this could makes things tricky down the road, but I did not wanted to spend too much time anticipating what may be required in the future without actually knowing how this will be used. I bet we will be able to test ./abilities-api.php separately if needed so hopefully this is not limiting.
|
I enabled auto-merge. It's interesting to see Claude on the list of commit authors 😄 |
|
@justlevine Thank you very much for the review.
I agree in general about the initialization. But I think the pattern here is just to make all of the global functions available.
This is mostly a semantic separation. |
I'm good with this separation seperation of concerns, thanks both for the contribution and your time with the explanation! |
Summary
Changes
rest_api_inithook registration fromabilities-api.phptoincludes/bootstrap.php🤖 Generated with Claude Code