10up / WP-Minions

ARCHIVED: Job Queue for WordPress

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: remove need for client code to set an action.

nerrad opened this issue · comments

Currently client code sends a string representing a registered "action" that will be used to execute the actual callback on the code. I propose a slight improvement to WPGears so that client code just sends in the actual callback and wpgears will set and call its own action.

Example for how would work is in this gist https://gist.github.com/nerrad/b1c651bbe60466c064805216a9045756 (which was created for #12).

If there's interest, I can do a pull request?

Thanks for the suggestion!

I'm not sure that this would be quite a fit for inclusion in the core plugin - here's why:

Right now, the callback (action) system is modeled after how WordPress core handles single tasks with wp-cron. Passing a callback would work in some cases (where it's just a simple function that you are calling) but in others, it would likely cause complications. For example, if you were to pass a callback as a class method array( $this, 'my_callback' ), we'd have to handle serializing and unserializing $this, which would likely lead to unexpected issues, depending on the environment the plugin is running in.

yah makes sense.

although, just as a comment, there could be checks to disallow non static class method callbacks, (and throw an exception or warning if a developer attempts that).

So for example you COULD allow something like, array( 'ClassName', 'staticMethod' ) or simple function functionname but test for whether the first element in the array is_object or not.

In general though, I agree with the intent of that kind of inherent protection via the current method of using the WP action hook system.

Another idea, is you could actually leave both methods of registering in place.

When wp_async_task_add(); is called via client code, you can first check if the provided callback exists (function_exists or method_exists) and also validate whether its allowable. If its a string and does NOT exist, then just handle as currently. Otherwise it'll get picked up by the default action already registered.

This provides two methods of registering a job. Using the native wp action system OR registering an actual callback.