wp-hooks / generator

Generates a JSON representation of the WordPress actions and filters in your code
https://packagist.org/packages/wp-hooks/generator
78 stars 6 forks source link

Hooks of WP cron schedule events #9

Open kkmuffme opened 2 years ago

kkmuffme commented 2 years ago

https://developer.wordpress.org/reference/functions/wp_schedule_single_event/ https://developer.wordpress.org/reference/functions/wp_schedule_event/

Both can declare hooks that will be called. These should be included in actions.json ?

johnbillion commented 2 years ago

Thanks for the report. How should these be documented? It's a dynamic hook added by other plugins.

kkmuffme commented 2 years ago

Just normally with phpdoc for the $args I'd say

/**
 * @param string $foo
 * @param int
 */
wp_schedule_single_event( strtotime( '+ 2 days' ), 'my_custom_cron_hook', array( $foo, 10 ) );

However, I would be 100% happy if these hooks would just be included in the actions.json without any args/docs, as I use actions.json primarily to ensure that used hooks exist (and only secondary to validate the hook types)

johnbillion commented 2 years ago

Ah I see what you mean. While that would be useful it would require a whole different parser to detect and extract these. PRs welcome!

kkmuffme commented 2 years ago

It's easy enough with nikic PHP parser directly in enterNode function for e.g. wp_schedule_single_event: (for wp_schedule_event index is 2 instead of 1)

if ( in_array( (string) $node->name, $event_functions, true ) ) {
    $hook_type = 'action';

    if ( ! $origNode->args[1]->value instanceof String_ ) {
            return null;
        }

    $hook_name = $origNode->args[1]->value->value;
}

And the way to get the phpdoc is the same as now for actions/filters.

kkmuffme commented 2 years ago

Is there maybe some way I can custom add this (like a custom visitor or something I can declare when calling the hooks generator)? bc I realized there are some custom cases, that will call a hook callback too, I'd like to add (besides the obvious WP cron stuff and the WC action scheduler)

johnbillion commented 2 years ago

I'm really not sure - the parsing is handled by wp-parser-lib which is just the parsing code from the WP Parser plugin extracted into a reusable library. I've not looked into it in depth.