Ensure we load our autoloader from our plugin directory #231
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.
Description of the Change
In #217 a new composer dependency was added and to ensure it gets loaded, a composer autoloader was required. This was loaded with a
require_once
statement that just referenced thevendor
directory.The problem here is that we weren't specifically referencing the directory we should load from. This can result in the autoloader being loaded from somewhere else. For instance, if you have an autoloader at the root of your site (say you use composer to manage all your plugins), this autoloader will be loaded instead in certain situations, causing fatal errors since the dependency we expect is now not loaded.
This PR fixes this by adding the
__DIR__
constant before the file path, ensuring the autoloader is loaded from the root directory of this plugin. We also output an admin notice if that autoloader isn't found and if the base Factory class doesn't exist. This should only be those that are running this plugin directly from GitHub and need to runcomposer install
Alternate Designs
Could use
plugin_dir_path( __FILE__ )
instead of__DIR__
but they both return the same value and my preference is the latter.Benefits
The correct autoloader is always used
Possible Drawbacks
None
Verification Process
Before checking this PR out, add a composer autoloader to the root of your WordPress install (something like
wordpress/vendor/autoload.php
).Setup Restricted Site Access, add one dummy IP address that is allowed and then try and access the site from an incognito window (seems like the issue happens only if you're not logged into WordPress).
You should get a fatal error.
Then check out the code from this PR and run the same tests again.
Things should work now
Checklist:
Changelog Entry
Credits
Props @dkotter, @pablojmarti, @shahzaib10up
Applicable Issues
Closes #229