wp-async-request.php 2.8 KB

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