Closed kupoback closed 2 months ago
Issue is with me, not the package. It's actually prolly a WP issue with how they do their functions and I might need to explore other options to call to those.
EDIT: There's gotta be something I'm missing or some setting from this that's not implemented. Would love help.
Hey, sorry, I saw "Issue is with me, not the package." and never checked back.
What's happening is you're using an undefined function add_action
in rap-site-reset.php
, which you're autoloading in your files
autoloader:
"autoload": {
"files": [ "rap-site-reset.php" ],
"psr-4": {
"RapReset\\": "src/"
}
},
The files
autoloader is relatively dumb compared to classmap or PSR-4 which only load the file when the class is first needed, but files
loads the files as soon as vendor/autoload.php
is loaded.
When you use this inside a WordPress plugin, WordPress is already loaded, so obviously add_action
is ready to use.
I'll take a look at how PhpStan WordPress and at how WP_Mock stub the functions. In principle, it's just a matter of:
if( ! function_exists( 'add_action' ) ) {
function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { }
}
but I'll have to think about it. It should be safe to define those functions when Strauss is running, but maybe not when it's run as part of a Composer script (i.e. I don't think it has its own process).
That's pretty wild that you're prefixing WP CLI commands, that never crossed my mind.
The correct solution is to start each file that uses WordPress functions with:
if( ! defined( 'ABSPATH' ) ) {
return;
}
Otherwise, as packages that are autoloaded by Composer are autoloaded when running dev tools they will try to load WordPress functions which won't be present.
So switching from Mozart to this, has been fantastic as this is all working out of the box without the need for patches, or figuring out which versions of a composer package I need. However, when I made a new class with an admin notice add_action, just to test and ensure this is all properly working, I get the following error. Am I missing something, do I need to somehow wrap the wp core as well? I know the way I'm calling to the action is fine, but not sure why this error would trigger when calling to a default WP function.
Here's my composer.json
Here's my
rap-site-reset.php
file:And my Class