SHARE
TWEET

Untitled

a guest Aug 23rd, 2017 138 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_action( 'wp_async_save_post', 'josh_send_to_api' );
  2. function josh_send_to_api( $id ) {
  3.     wp_mail( 'abc@test.com', date("H:i:s", time() ), $id );
  4.  
  5.     sleep(25);
  6.     wp_mail( 'abc@test.com', date("H:i:s", time() ), $id );
  7. }
  8.    
  9. class Josh_Task extends WP_Async_Task {
  10.     /**
  11.     * Action to use to trigger this task
  12.     *
  13.     * @var string
  14.     */
  15.     protected $action = 'save_post';
  16.  
  17.     /**
  18.     * Prepare POST data to send to session that processes the task
  19.     *
  20.     * @param array $data Params from hook
  21.     *
  22.     * @return array
  23.     */
  24.     protected function prepare_data($data){
  25.         return array(
  26.             'post_id' => 5
  27.         );
  28.     }
  29.  
  30.     /**
  31.     * Run the asynchronous task
  32.     *
  33.     * Calls send_to_api()
  34.     */
  35.     protected function run_action() {
  36.         if( isset( $_POST[ 'post_id' ] ) && 0 < absint( $_POST[ 'post_id' ] ) ) {
  37.         do_action( "wp_async_$this->action", 5 );
  38.         }
  39.     }
  40. }
  41.  
  42. new Josh_Task();
RAW Paste Data
Top