Skip to content
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

add_action('some_action', 'SomeClass::someStaticMethod') throws InvalidName #47

Closed
pjeby opened this issue Mar 15, 2019 · 2 comments
Closed
Assignees
Labels

Comments

@pjeby
Copy link

pjeby commented Mar 15, 2019

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 gmazzap self-assigned this Mar 15, 2019
@gmazzap gmazzap added the bug label Mar 15, 2019
@gmazzap
Copy link
Collaborator

gmazzap commented Mar 15, 2019

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 added a commit that referenced this issue Mar 15, 2019
Makse sure that static methods can be passed as string, as this is an acceptable way in PHP to pass a callable.

See #47
@gmazzap
Copy link
Collaborator

gmazzap commented Mar 15, 2019

This is fixed in v2.2.1 https://github.com/Brain-WP/BrainMonkey/releases/tag/2.2.1

@gmazzap gmazzap closed this as completed Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants