PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.3
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.3
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / core / libraries / wp-async-request.php

wp-async-request.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.2.3, at includes/core/libraries/wp-async-request.php

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