PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.1
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / importer / batch-process / helpers / class-wp-async-request.php

class-wp-async-request.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.1, at classes/importer/batch-process/helpers/class-wp-async-request.php

168 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Async Request
4 *
5 * @see https://github.com/A5hleyRich/wp-background-processing/
6 * @package WP-Background-Processing
7 */
8
9 // Exit if accessed directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13 if ( ! class_exists( 'WP_Async_Request' ) ) {
14
15 /**
16 * Abstract WP_Async_Request class.
17 *
18 * @abstract
19 */
20 abstract class WP_Async_Request {
21
22 /**
23 * Prefix
24 *
25 * (default value: 'wp')
26 *
27 * @var string
28 * @access protected
29 */
30 protected $prefix = 'wp';
31
32 /**
33 * Action
34 *
35 * (default value: 'async_request')
36 *
37 * @var string
38 * @access protected
39 */
40 protected $action = 'async_request';
41
42 /**
43 * Identifier
44 *
45 * @var mixed
46 * @access protected
47 */
48 protected $identifier;
49
50 /**
51 * Data
52 *
53 * (default value: array())
54 *
55 * @var array
56 * @access protected
57 */
58 protected $data = array();
59
60 /**
61 * Initiate new async request
62 */
63 public function __construct() {
64 $this->identifier = $this->prefix . '_' . $this->action;
65
66 add_action( 'wp_ajax_' . $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 return array(
105 'action' => $this->identifier,
106 'nonce' => wp_create_nonce( $this->identifier ),
107 );
108 }
109
110 /**
111 * Get query URL
112 *
113 * @return string
114 */
115 protected function get_query_url() {
116 if ( property_exists( $this, 'query_url' ) ) {
117 return $this->query_url;
118 }
119
120 return admin_url( 'admin-ajax.php' );
121 }
122
123 /**
124 * Get post args
125 *
126 * @return array
127 */
128 protected function get_post_args() {
129 if ( property_exists( $this, 'post_args' ) ) {
130 return $this->post_args;
131 }
132
133 return array(
134 'timeout' => 0.01,
135 'blocking' => false,
136 'body' => $this->data,
137 'cookies' => $_COOKIE,
138 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
139 );
140 }
141
142 /**
143 * Maybe handle
144 *
145 * Check for correct nonce and pass to handler.
146 */
147 public function maybe_handle() {
148 // Don't lock up other requests while processing.
149 session_write_close();
150
151 check_ajax_referer( $this->identifier, 'nonce' );
152
153 $this->handle();
154
155 wp_die();
156 }
157
158 /**
159 * Handle
160 *
161 * Override this method to perform any actions required
162 * during the async request.
163 */
164 abstract protected function handle();
165
166 }
167 }
168