PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / trunk
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI vtrunk
3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / libraries / wp-async-request.php
everest-forms / includes / libraries Last commit date
wp-async-request.php 8 years ago wp-background-process.php 7 years ago wptt-webfont-loader.php 2 years ago
wp-async-request.php
161 lines
1 <?php
2 /**
3 * WP Async Request
4 *
5 * @package WP-Background-Processing
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Abstract WP_Async_Request class.
12 */
13 abstract class WP_Async_Request {
14
15 /**
16 * Prefix
17 *
18 * (default value: 'wp')
19 *
20 * @var string
21 * @access protected
22 */
23 protected $prefix = 'wp';
24
25 /**
26 * Action
27 *
28 * (default value: 'async_request')
29 *
30 * @var string
31 * @access protected
32 */
33 protected $action = 'async_request';
34
35 /**
36 * Identifier
37 *
38 * @var mixed
39 * @access protected
40 */
41 protected $identifier;
42
43 /**
44 * Data
45 *
46 * (default value: array())
47 *
48 * @var array
49 * @access protected
50 */
51 protected $data = array();
52
53 /**
54 * Initiate new async request
55 */
56 public function __construct() {
57 $this->identifier = $this->prefix . '_' . $this->action;
58
59 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
60 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
61 }
62
63 /**
64 * Set data used during the request
65 *
66 * @param array $data Data.
67 *
68 * @return $this
69 */
70 public function data( $data ) {
71 $this->data = $data;
72
73 return $this;
74 }
75
76 /**
77 * Dispatch the async request
78 *
79 * @return array|WP_Error
80 */
81 public function dispatch() {
82 $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
83 $args = $this->get_post_args();
84
85 return wp_remote_post( esc_url_raw( $url ), $args );
86 }
87
88 /**
89 * Get query args
90 *
91 * @return array
92 */
93 protected function get_query_args() {
94 if ( property_exists( $this, 'query_args' ) ) {
95 return $this->query_args;
96 }
97
98 return array(
99 'action' => $this->identifier,
100 'nonce' => wp_create_nonce( $this->identifier ),
101 );
102 }
103
104 /**
105 * Get query URL
106 *
107 * @return string
108 */
109 protected function get_query_url() {
110 if ( property_exists( $this, 'query_url' ) ) {
111 return $this->query_url;
112 }
113
114 return admin_url( 'admin-ajax.php' );
115 }
116
117 /**
118 * Get post args
119 *
120 * @return array
121 */
122 protected function get_post_args() {
123 if ( property_exists( $this, 'post_args' ) ) {
124 return $this->post_args;
125 }
126
127 return array(
128 'timeout' => 0.01,
129 'blocking' => false,
130 'body' => $this->data,
131 'cookies' => $_COOKIE,
132 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
133 );
134 }
135
136 /**
137 * Maybe handle
138 *
139 * Check for correct nonce and pass to handler.
140 */
141 public function maybe_handle() {
142 // Don't lock up other requests while processing
143 session_write_close();
144
145 check_ajax_referer( $this->identifier, 'nonce' );
146
147 $this->handle();
148
149 wp_die();
150 }
151
152 /**
153 * Handle
154 *
155 * Override this method to perform any actions required
156 * during the async request.
157 */
158 abstract protected function handle();
159
160 }
161