WP_Async_Request

Abstract WP_Async_Request class.

Defined (1)

The class is defined in the following location(s).

/includes/libraries/wp-async-request.php  
  1. abstract class WP_Async_Request { 
  2.  
  3. /** 
  4. * Prefix 
  5. * (default value: 'wp') 
  6. * @var string 
  7. * @access protected 
  8. */ 
  9. protected $prefix = 'wp'; 
  10.  
  11. /** 
  12. * Action 
  13. * (default value: 'async_request') 
  14. * @var string 
  15. * @access protected 
  16. */ 
  17. protected $action = 'async_request'; 
  18.  
  19. /** 
  20. * Identifier 
  21. * @var mixed 
  22. * @access protected 
  23. */ 
  24. protected $identifier; 
  25.  
  26. /** 
  27. * Data 
  28. * (default value: array()) 
  29. * @var array 
  30. * @access protected 
  31. */ 
  32. protected $data = array(); 
  33.  
  34. /** 
  35. * Initiate new async request 
  36. */ 
  37. public function __construct() { 
  38. $this->identifier = $this->prefix . '_' . $this->action; 
  39.  
  40. add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); 
  41. add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); 
  42.  
  43. /** 
  44. * Set data used during the request 
  45. * @param array $data Data. 
  46. * @return $this 
  47. */ 
  48. public function data( $data ) { 
  49. $this->data = $data; 
  50.  
  51. return $this; 
  52.  
  53. /** 
  54. * Dispatch the async request 
  55. * @return array|WP_Error 
  56. */ 
  57. public function dispatch() { 
  58. $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); 
  59. $args = $this->get_post_args(); 
  60.  
  61. return wp_remote_post( esc_url_raw( $url ), $args ); 
  62.  
  63. /** 
  64. * Get query args 
  65. * @return array 
  66. */ 
  67. protected function get_query_args() { 
  68. if ( property_exists( $this, 'query_args' ) ) { 
  69. return $this->query_args; 
  70.  
  71. return array( 
  72. 'action' => $this->identifier,  
  73. 'nonce' => wp_create_nonce( $this->identifier ),  
  74. ); 
  75.  
  76. /** 
  77. * Get query URL 
  78. * @return string 
  79. */ 
  80. protected function get_query_url() { 
  81. if ( property_exists( $this, 'query_url' ) ) { 
  82. return $this->query_url; 
  83.  
  84. return admin_url( 'admin-ajax.php' ); 
  85.  
  86. /** 
  87. * Get post args 
  88. * @return array 
  89. */ 
  90. protected function get_post_args() { 
  91. if ( property_exists( $this, 'post_args' ) ) { 
  92. return $this->post_args; 
  93.  
  94. return array( 
  95. 'timeout' => 0.01,  
  96. 'blocking' => false,  
  97. 'body' => $this->data,  
  98. 'cookies' => $_COOKIE,  
  99. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),  
  100. ); 
  101.  
  102. /** 
  103. * Maybe handle 
  104. * Check for correct nonce and pass to handler. 
  105. */ 
  106. public function maybe_handle() { 
  107. // Don't lock up other requests while processing 
  108. session_write_close(); 
  109.  
  110. check_ajax_referer( $this->identifier, 'nonce' ); 
  111.  
  112. $this->handle(); 
  113.  
  114. wp_die(); 
  115.  
  116. /** 
  117. * Handle 
  118. * Override this method to perform any actions required 
  119. * during the async request. 
  120. */ 
  121. abstract protected function handle();