add_action('some_action', 'SomeClass::someStaticMethod') throws InvalidName
pjeby opened this issue · 2 comments
pjeby commented
This code in a static method under test:
add_action('save_post', __CLASS__ . "::on_save_post", 10, 2);
Causes the following to be thrown:
''dirtsimple\imposer\PostModel::on_save_post'' is not a valid function name.
at /repos/imposer/vendor/brain/monkey/src/Name/Exception/InvalidName.php:80
#0 Brain\Monkey\Name\Exception\InvalidName::createFor (/repos/imposer/vendor/brain/monkey/src/Name/Exception/InvalidName.php:32)
#1 Brain\Monkey\Name\Exception\InvalidName::forFunction (/repos/imposer/vendor/brain/monkey/src/Name/FunctionName.php:93)
#2 Brain\Monkey\Name\FunctionName->parseName (/repos/imposer/vendor/brain/monkey/src/Name/FunctionName.php:39)
#3 Brain\Monkey\Name\FunctionName->__construct (/repos/imposer/vendor/brain/monkey/src/Name/CallbackStringForm.php:132)
#4 Brain\Monkey\Name\CallbackStringForm->parseString (/repos/imposer/vendor/brain/monkey/src/Name/CallbackStringForm.php:73)
#5 Brain\Monkey\Name\CallbackStringForm->parseCallback (/repos/imposer/vendor/brain/monkey/src/Name/CallbackStringForm.php:39)
#6 Brain\Monkey\Name\CallbackStringForm->__construct (/repos/imposer/vendor/brain/monkey/src/Hook/HookStorage.php:227)
#7 Brain\Monkey\Hook\HookStorage->parseArgsToAdd (/repos/imposer/vendor/brain/monkey/src/Hook/HookStorage.php:163)
#8 Brain\Monkey\Hook\HookStorage->pushToStorage (/repos/imposer/vendor/brain/monkey/src/Hook/HookStorage.php:58)
#9 Brain\Monkey\Hook\HookStorage->pushToAdded (/repos/imposer/vendor/brain/monkey/inc/wp-hook-functions.php:21)
#10 add_action (/repos/imposer/src/PostModel.php:27)
#11 dirtsimple\imposer\PostModel::configure (/repos/imposer/specs/PostModel.spec.php:111)
The issue appears to be that CallbackStringForm->parseString
doesn't think that static method string callbacks such as 'dirtsimple\imposer\PostModel::on_save_post'
are valid Wordpress filters or actions. Changing the code under test to use the array form of static method specification works, but the issue should be fixed in BrainMonkey.
gmazzap commented
Hi @pjeby thanks for reporting.
I did not considered the case that a callback could be passed in that way, which makes this a bug.
Before this is fixed, you could avoid the issue by using:
add_action('save_post', [__CLASS__ , "on_save_post"], 10, 2);
which is equivalent.
gmazzap commented
This is fixed in v2.2.1 https://github.com/Brain-WP/BrainMonkey/releases/tag/2.2.1