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