wp-async-request.php 2.7 KB

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