2016-05-11 11:43:54 +00:00
|
|
|
<?php
|
2016-06-09 10:48:50 +00:00
|
|
|
/**
|
2018-02-01 13:34:03 +00:00
|
|
|
* WP Async Request
|
2016-06-09 10:48:50 +00:00
|
|
|
*
|
|
|
|
* @package WP-Background-Processing
|
2018-02-01 13:34:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract WP_Async_Request class.
|
2016-06-09 10:48:50 +00:00
|
|
|
*/
|
2017-02-16 11:46:01 +00:00
|
|
|
abstract class WP_Async_Request {
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Prefix
|
|
|
|
*
|
|
|
|
* (default value: 'wp')
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $prefix = 'wp';
|
2016-06-09 10:48:50 +00:00
|
|
|
|
|
|
|
/**
|
2017-02-16 11:46:01 +00:00
|
|
|
* Action
|
2016-06-09 10:48:50 +00:00
|
|
|
*
|
2017-02-16 11:46:01 +00:00
|
|
|
* (default value: 'async_request')
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access protected
|
2016-06-09 10:48:50 +00:00
|
|
|
*/
|
2017-02-16 11:46:01 +00:00
|
|
|
protected $action = 'async_request';
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Identifier
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $identifier;
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Data
|
|
|
|
*
|
|
|
|
* (default value: array())
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Initiate new async request
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->identifier = $this->prefix . '_' . $this->action;
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
|
|
|
|
add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
|
|
|
|
}
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Set data used during the request
|
|
|
|
*
|
|
|
|
* @param array $data Data.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function data( $data ) {
|
|
|
|
$this->data = $data;
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the async request
|
|
|
|
*
|
|
|
|
* @return array|WP_Error
|
|
|
|
*/
|
|
|
|
public function dispatch() {
|
|
|
|
$url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
|
|
|
|
$args = $this->get_post_args();
|
|
|
|
|
|
|
|
return wp_remote_post( esc_url_raw( $url ), $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get query args
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_query_args() {
|
|
|
|
if ( property_exists( $this, 'query_args' ) ) {
|
|
|
|
return $this->query_args;
|
2016-05-11 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
return array(
|
|
|
|
'action' => $this->identifier,
|
|
|
|
'nonce' => wp_create_nonce( $this->identifier ),
|
|
|
|
);
|
|
|
|
}
|
2016-09-15 18:05:16 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Get query URL
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_query_url() {
|
|
|
|
if ( property_exists( $this, 'query_url' ) ) {
|
|
|
|
return $this->query_url;
|
|
|
|
}
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
return admin_url( 'admin-ajax.php' );
|
|
|
|
}
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
/**
|
|
|
|
* Get post args
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_post_args() {
|
|
|
|
if ( property_exists( $this, 'post_args' ) ) {
|
|
|
|
return $this->post_args;
|
2016-05-11 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
return array(
|
|
|
|
'timeout' => 0.01,
|
|
|
|
'blocking' => false,
|
|
|
|
'body' => $this->data,
|
|
|
|
'cookies' => $_COOKIE,
|
|
|
|
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maybe handle
|
|
|
|
*
|
|
|
|
* Check for correct nonce and pass to handler.
|
|
|
|
*/
|
|
|
|
public function maybe_handle() {
|
|
|
|
// Don't lock up other requests while processing
|
|
|
|
session_write_close();
|
|
|
|
|
|
|
|
check_ajax_referer( $this->identifier, 'nonce' );
|
2016-05-11 11:43:54 +00:00
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
$this->handle();
|
|
|
|
|
|
|
|
wp_die();
|
2016-05-11 11:43:54 +00:00
|
|
|
}
|
2017-02-16 11:46:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle
|
|
|
|
*
|
|
|
|
* Override this method to perform any actions required
|
|
|
|
* during the async request.
|
|
|
|
*/
|
|
|
|
abstract protected function handle();
|
2018-02-01 13:34:03 +00:00
|
|
|
|
2016-06-09 10:48:50 +00:00
|
|
|
}
|