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

How to use this, exactly? #20

Open
Luc45 opened this issue Apr 19, 2018 · 1 comment
Open

How to use this, exactly? #20

Luc45 opened this issue Apr 19, 2018 · 1 comment

Comments

@Luc45
Copy link

Luc45 commented Apr 19, 2018

Hey, great code, thanks!

I'm having some trouble using it, though. Here's a quick test I'm doing:

add_action( 'wp_async_shutdown', [$this, 'testing_async'], 10, 1 );

I expect this to run public function testing_async() asynchronously when WordPress be shutting down, right?

public function testing_async() {
    error_log('test', 1, 'someemail@gmail.com');
}

But this is never called. OK, this didn't work. Let's try the "extend class" approach.

public function testing_async() {
    $test = new WP_Async_Test();
}

WP_Async_Test.php:

<?php

class WP_Async_Test extends WP_Async_Task {

    //protected $action = 'shutdown';
    protected $action = 'testing_async';

    /**
     * Prepare data for the asynchronous request
     *
     * @throws Exception If for any reason the request should not happen
     *
     * @param array $data An array of data sent to the hook
     *
     * @return array
     */
    protected function prepare_data( $data ) {

    }

    /**
     * Run the async task action
     */
    protected function run_action() {
        error_log('hi', 1, 'someemail@gmail.com');
    }

}

This didn't work either... What am I doing wrong?

@Luc45
Copy link
Author

Luc45 commented Apr 19, 2018

OK, Part 2. I'm trying to do a slow task after a user registers. Everything works, except that it is not Async.

Register.php
do_action('my_app_user_register', $user_id);

MyAppPlugin.php:

public function __construct() {
    add_action( 'my_app_user_register', [$this, 'user_register'], 10, 1 );
    add_action( 'plugins_loaded', [$this, 'listen_async'] );
}

/**
*   Runs when a user registers on WordPress
*/
public function user_register($user_id) {
    error_log('user_register', 1, 'someemail@gmail.com'); // runs
    add_action( 'wp_async_myapp_user_registered', [$this, 'call_async']);
}

/**
*   Listen to Async Calls
*/
public function listen_async() {
    new WP_Async_Setup_Database;
}

/**
*   Bogus function just so WP_Async_Setup_Database can listen for the "myapp_user_registered" action
*/
public function call_async() {
    return;
}

WP_Async_Setup_Database.php

<?php

class WP_Async_Setup_Database extends WP_Async_Task {

    protected $action = 'myapp_user_registered';

    /**
     * Prepare data for the asynchronous request
     *
     * @throws Exception If for any reason the request should not happen
     *
     * @param array $data An array of data sent to the hook
     *
     * @return array
     */
    protected function prepare_data( $data ) {
        error_log('prepare_data '.json_encode($data), 1, 'someemail@gmail.com'); // runs
        return array(
            'user_id' => $data[0]
        );
    }

    /**
     * Run the async task action
     */
    protected function run_action() {
        error_log('run_action '.json_encode($_POST), 1, 'someemail@gmail.com'); // runs
        if (isset($_POST['user_id'])) {
            // Slow action here. It runs, but not async. I will not include MyAppCPanel here as it's just a bunch of code running MySQL on a external server
           $myApp = new MyAppCPanel;
           $myApp->setup_database($user_id);
        }
    }

}

Everything runs, but not Async.

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