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

wp_sync...action hook not being executed as async #18

Open
pratikgup opened this issue Jun 6, 2017 · 7 comments
Open

wp_sync...action hook not being executed as async #18

pratikgup opened this issue Jun 6, 2017 · 7 comments

Comments

@pratikgup
Copy link

pratikgup commented Jun 6, 2017

Hi there, this plugin seems useful, but doesn't work for me in WP v4.7.5.

I'm trying to get a action hook from another plugin running asynchronously. This is the action hook I'm trying to run asynchronously https://www.gravityhelp.com/documentation/article/gform_after_submission/

I tried to incorporate that hook using similar steps described in the plugin's Github readme.

To just test the gform_after_submission_19 (where form id=19) action this is what I have added in my theme's function.php after activating the TechCrunch's plugin

if (class_exists('WP_Async_Task')) {

    class GF_Async_Task extends WP_Async_Task {
        
        protected $action = 'gform_after_submission_19';

        public function __construct() {
            parent::__construct(parent::BOTH);
        }
        protected function prepare_data($data) {
            return array(
                'entry' => $data[0],
                'form' => $data[1]
            );
        }
        protected function run_action() {
            if (isset($_POST['entry'])) {
                do_action("wp_async_$this->action", $_POST['entry'], $_POST['form']);
            }
        }
    }
}
function send_custom_notification($entry, $form) {
    error_log("test", 3, $_SERVER['DOCUMENT_ROOT'] . "/GFTEST.log");
}

add_action('wp_async_gform_after_submission_19', 'send_custom_notification', 10, 2);

function my_init_gf_task() {
    new GF_Async_Task();
}
add_action('plugins_loaded', 'my_init_gf_task');

But when I post the gravity form the expected action send_custom_notification action is just not doing the log
What could be wrong?

I also tried to set the sslverify option to false in the the launch_on_shutdown method of WP_Async_Task class by the editing original plugin file wp-async-task.php. I may needed to do this as i'm working on localhost for now.

But still no luck, any help/tips will be much appreciated, thanks!

@sudar
Copy link

sudar commented Jun 8, 2017

You need to change

function send_custom_notification($entry, $form) {
    error_log("test", 3, $_SERVER['DOCUMENT_ROOT'] . "/GFTEST.log");
}

add_action('gform_after_submission_19', 'send_custom_notification', 10, 2);

to

function send_custom_notification($entry, $form) {
    error_log("test", 3, $_SERVER['DOCUMENT_ROOT'] . "/GFTEST.log");
}

add_action('wp_async_gform_after_submission_19', 'send_custom_notification', 10, 2);

@pratikgup
Copy link
Author

pratikgup commented Jun 9, 2017

Sorry that was a silly typo on my post which I have amended now. However I definitely have the above code in my theme's functions.php. The action still won't work with the right tag for action.
I wonder if my prepare_data and run_action functions are correct? And/Or the class instantiation is correct? Not sure why my above code won't work when I post the Gravity Form. I also tried removing _19 (ID of the form in the above code), but still no luck after posting any Gravity Form - cannot see the log.

I also tried the wp_async_save_post action variance of my above code, where I used below in my theme's function.php instead, but no expected log found when a post is saved (created or edited from admin), if I remove the wp_async_ prefix in the add_action it would work fine just like the Gravity Form after submission action, but it wouldn't run asynchronously and from this plugin.

class GF_Async_Task extends WP_Async_Task {
    protected $action = 'save_post';

    protected function prepare_data($data){
        return array(
            'post_id' => $data[0]
        );
    }

    protected function run_action() {
        if( isset( $_POST[ 'post_id' ] ) && 0 < absint( $_POST[ 'post_id' ] ) ){
            do_action( "wp_async_$this->action", $_POST[ 'post_id' ], get_post( $_POST[ 'post_id' ] ) );
        }
    }
}

function send_custom_notification($id) {
    error_log("test", 3, $_SERVER['DOCUMENT_ROOT'] . "/GFTEST.log");
}

add_action('wp_async_save_post', 'send_custom_notification');

function my_init_gf_task() {
    new GF_Async_Task();
}
add_action('plugins_loaded', 'my_init_gf_task');

@sudar
Copy link

sudar commented Jun 9, 2017

Looking at the documentation of the GForm hook it looks like the first parameter is the entry.

So you have to change the way you are retrieving post_id inside prepare_data.

 protected function prepare_data($data){
        return array(
            'post_id' => $data['post_id']
        );
    }

@pratikgup
Copy link
Author

pratikgup commented Jun 9, 2017

Thanks but that is only for the save_post action example, not the GForm hook. The prepare_data below is already setup for the GForm hook as shown in the full code in my 1st comment.
Is this correct way of retrieving entry and form? I was having doubts about it initially.

My 1st comment above has the full code for the GForm hook async attempt, which is still not working. Could I breakpoint and debug what is going on by stepping through in WP?

protected function prepare_data($data) {
            return array(
                'entry' => $data[0],
                'form' => $data[1]
            );
        }

@sudar
Copy link

sudar commented Jun 9, 2017

Okay, you are correct. I mixed both the examples.

The prepare_data that you have right now looks like the correct one. If it is still not working try to set a break point (or log the value) and see what is the value of $data.

@pratikgup
Copy link
Author

Alright I have just added this in the plugin's class instead of functions.php. I shouldn't add this functionality in a theme anyway as it is not theme dependent.

After posting the form, I was able to print the value of $data it does contain 2 arrays, entry and form. So something wrong with the return in my prepare_data because it doesn't hit the run_action function?

@pratikgup
Copy link
Author

Ok the run_action and wp_async hook with the gravity form submission works with a shorter form.

But not with a form that contains many fields (about 4 steps). Could that be because of a limit in the WP Async Task class specification?

I have set the local server's php.ini with config post_max_size = 100M. The $_POSTcontent is certainly not that big, it is probably less than 1MB file size of $_POST array content. Why would it not work with a larger form?

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

2 participants