Skip to content

Constructor

John P. Bloch edited this page Jul 14, 2014 · 3 revisions

The class constructor takes one argument that can change how your task behaves. The WP_Async_Task class uses wp-admin/admin-post.php to handle asynchronous requests. Like wp-admin/admin-ajax.php, admin-post.php generates the action to run from $_POST['action'] with 'admin_post_' or 'admin_post_nopriv_' prepended, depending on whether a user is logged in. Based on the constructor argument, async tasks will support logged out users, logged in users, or both. Here's how to specify:

<?php

// Assuming there's an async library called JPB_Task
$logged_out_task = new JPB_Task( WP_Async_Task::LOGGED_OUT );
$logged_in_task  = new JPB_Task( WP_Async_Task::LOGGED_IN  );
$both_task       = new JPB_Task( WP_Async_Task::BOTH       );

The default value is WP_Async_Task::BOTH.

Of course, you can force one by overriding the constructor:

<?php

class JPB_Task extends WP_Async_Task {

    public function __construct( $auth_level = 0 ) {
        parent::__construct( parent::LOGGED_IN );
    }

    /*...*/

}