Wrap polyfills in namespaces #134
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is an alternative to #133 that doesn't require calling
polyfill_wordpress_apis().php-toolkitrelies on WordPress APIs, such asapply_filters(). Sometimes they're missing, e.g. in PHPUnit runtime, and they're implicitly polyfilled when includingvendor/autoload.php:This polyfilling technique assumes the library is either:
However, it does not account an important third scenario that WooCommerce ran into:
wp-load.phpis includedSolution
This PR adds a
WordPress\\namespace to all the polyfills. Thephp-toolkitcode then refers to the namespaced code.Rationale
At the time of
require "vendor/autoload.php", we don't know what the developer is going to do next. Maybe they'll includewp-load.php, in which case we must not load the polyfills. Or maybe then won't includewp-load.php, in which case we must load the polyfills. Exposing an explicitpolyfill_wordpress_apis()function lets them decide.The one downside is:
php-toolkitwill use the polyfills instead of WordPress core code paths even when running as a WordPress plugin. We could counteract that with:Testing plan
Confirm the CI tests pass – many
php-toolkitPHPUnit tests rely on WordPress polyfills and won't work ifpolyfill_wordpress_apis()is not called or is incomplete.