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

pushing a class instance to the queue from outside of the plugin init #123

Open
gkv-ckultzow opened this issue Sep 22, 2024 · 0 comments
Open

Comments

@gkv-ckultzow
Copy link

gkv-ckultzow commented Sep 22, 2024

Solving this took a good twenty hours of my life away, and maybe it's "common sense" but I thought it might be good to keep here as a note for some future troubleshooting reference.

The example plugin works on page load by checking GET for process and nonce, then pushing strings to the queue.

For my logic, I needed two changes: a class instance be pushed to the queue instead of a string, and the trigger to be a php function called by AJAX, which I declared in functions.php like this:

$instance = new Example_Background_Processing()
$instance->handle_all();

after adjusting handle_all to public, and having handle_all instantiate instances of a class to push_to_queue, instead of the names.

When WP_Background_Process->task($item) is called this way, rather than from the plugin declaration, PHP often - but importantly, not always! - does not recognize the class of the instantiated class object being pushed to the queue as having been declared, and re-casts that object as __PHP_Incomplete_Class containing the instantiated object.

This adds an extra layer, breaking references to variables in functions. But if you're testing this several times in a row, sometimes the background process DOES recognize the class, and lets it go through as intended. Which is awesome for troubleshooting.

So, in order to call this handle_all function via AJAX in functions.php, I needed to include/define the class that I was creating and passing to task($item) at the top of functions.php, even though it wasn't being used until handle_all(), which allowed PHP to always see the class as correctly defined during task($item), and not recast it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant