PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.1
JetFormBuilder — Dynamic Blocks Form Builder v2.1.1
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / gateways / base-gateway-action.php
jetformbuilder / includes / gateways Last commit date
db-models 3 years ago meta-boxes 3 years ago pages 3 years ago paypal 3 years ago query-views 3 years ago rest-api 3 years ago scenarios-abstract 3 years ago table-views 3 years ago base-gateway-action.php 3 years ago base-gateway.php 3 years ago base-scenario-gateway.php 3 years ago gateway-manager.php 3 years ago gateways-editor-data.php 3 years ago legacy-base-gateway.php 3 years ago migrate-legacy-data.php 3 years ago scenario-item.php 3 years ago
base-gateway-action.php
361 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Gateways;
5
6
7 use Jet_Form_Builder\Classes\Tools;
8 use Jet_Form_Builder\Exceptions\Gateway_Exception;
9
10 abstract class Base_Gateway_Action {
11
12 const CODE_OK = 200;
13 const CODE_CREATED = 201;
14 const CODE_NO_CONTENT = 204;
15
16 protected $auth;
17 protected $method = 'POST';
18 protected $body = array();
19 protected $response;
20 protected $response_body;
21 protected $response_code;
22 protected $response_message;
23 protected $response_headers;
24 protected $path_parts = array();
25 protected $custom_url = '';
26
27 abstract public function action_slug();
28
29 abstract public function action_endpoint();
30
31 abstract public function base_url(): string;
32
33 public function accept_code(): int {
34 return self::CODE_OK;
35 }
36
37 public function action_headers() {
38 return array();
39 }
40
41 public function action_body() {
42 return array();
43 }
44
45 public function action_query_args(): array {
46 return array();
47 }
48
49 public function set_url( $url ): Base_Gateway_Action {
50 $this->custom_url = esc_url_raw( $url );
51
52 return $this;
53 }
54
55 public function get_raw_url(): string {
56 return $this->api_url( $this->action_endpoint(), $this->action_query_args() );
57 }
58
59 public function get_url(): string {
60 if ( $this->custom_url ) {
61 return $this->custom_url;
62 }
63
64 $url = $this->get_raw_url();
65
66 if ( empty( $this->path_parts ) ) {
67 return esc_url_raw( $url );
68 }
69
70 return esc_url_raw(
71 preg_replace_callback_array(
72 $this->get_parts_patterns(),
73 $url
74 )
75 );
76 }
77
78 public function get_parts_patterns(): array {
79 $patterns = array();
80
81 foreach ( $this->path_parts as $key => $value ) {
82 $patterns[ "#\{($key)\}#" ] = function ( $matches ) use ( $value ) {
83 return $value;
84 };
85 }
86
87 return $patterns;
88 }
89
90 public function get_method(): string {
91 return $this->method;
92 }
93
94 public function get_auth() {
95 return $this->auth;
96 }
97
98 public function set_bearer_auth( $token ): Base_Gateway_Action {
99 $this->set_auth( "Bearer $token" );
100
101 return $this;
102 }
103
104 public function set_basic_auth( $token ): Base_Gateway_Action {
105 $this->set_auth( "Basic $token" );
106
107 return $this;
108 }
109
110 public function set_auth( $auth_str ): Base_Gateway_Action {
111 $this->auth = $auth_str;
112
113 return $this;
114 }
115
116 public function set_path( array $parts ): Base_Gateway_Action {
117 $this->path_parts = array_merge( $this->path_parts, $parts );
118
119 return $this;
120 }
121
122 public function set_body( $content ): Base_Gateway_Action {
123 if ( ! $content ) {
124 return $this;
125 }
126
127 if ( is_string( $content ) ) {
128 $this->body = $content;
129 }
130
131 if ( is_array( $content ) ) {
132 $this->body = array_merge( $this->body, $content );
133 }
134
135 return $this;
136 }
137
138 public function get_headers(): array {
139 $args = array(
140 'Accept-Language' => get_locale(),
141 );
142
143 if ( $this->get_auth() ) {
144 $args['Authorization'] = $this->get_auth();
145 }
146
147 return array_merge( $args, $this->action_headers() );
148 }
149
150 protected function is_body_ready(): bool {
151 return is_string( $this->body );
152 }
153
154 public function get_body() {
155 if ( $this->is_body_ready() ) {
156 return $this->body;
157 }
158
159 if ( version_compare( phpversion(), '7.1', '>=' ) ) {
160 ini_set( 'precision', 17 );
161 ini_set( 'serialize_precision', - 1 );
162 }
163
164 $this->body = $this->to_json( $this->body );
165
166 return $this->body;
167 }
168
169 protected function to_json( $body ) {
170 return wp_unslash( Tools::encode_json( $body ) );
171 }
172
173 public function get_request_args(): array {
174 $args = array(
175 'timeout' => 45,
176 'headers' => $this->get_headers(),
177 );
178
179 if ( 'GET' === $this->get_method() ) {
180 return $args;
181 }
182
183 $args['method'] = $this->get_method();
184
185 $this->set_body( $this->action_body() );
186
187 if ( ! empty( $this->body ) ) {
188 $args['body'] = $this->get_body();
189 }
190
191 return $args;
192 }
193
194 public function set_method( $method ) {
195 $this->method = $method;
196
197 return $this;
198 }
199
200 /**
201 * Return API url
202 *
203 * @param $endpoint
204 * @param $query_args
205 *
206 * @return string
207 */
208 public function api_url( $endpoint, $query_args ): string {
209 $url = $this->base_url() . $endpoint;
210
211 if ( empty( $query_args ) ) {
212 return $url;
213 }
214
215 return add_query_arg( $query_args, $url );
216 }
217
218 public function get_response() {
219 $params = array( $this->get_url(), $this->get_request_args() );
220
221 return 'POST' === $this->get_method()
222 ? wp_remote_post( ...$params )
223 : wp_remote_get( ...$params );
224 }
225
226 public function before_make_request() {
227 }
228
229 /**
230 * Make a request
231 */
232 public function request(): Base_Gateway_Action {
233 $this->before_make_request();
234
235 $response = $this->get_response();
236
237 $this->set_response_body( wp_remote_retrieve_body( $response ) );
238 $this->set_response_code( (int) wp_remote_retrieve_response_code( $response ) );
239 $this->set_response_message( wp_remote_retrieve_response_message( $response ) );
240 $this->set_response_headers( wp_remote_retrieve_headers( $response ) );
241
242 return $this;
243 }
244
245 public function get_response_headers() {
246 return $this->response_headers;
247 }
248
249 private function set_response_headers( $headers ): Base_Gateway_Action {
250 $this->response_headers = $headers;
251
252 return $this;
253 }
254
255 private function set_response_message( $message ): Base_Gateway_Action {
256 $this->response_message = $message;
257
258 return $this;
259 }
260
261 public function get_response_message() {
262 return $this->response_message;
263 }
264
265 private function get_response_body() {
266 return $this->response_body;
267 }
268
269 private function set_response_body( $body ): Base_Gateway_Action {
270 $this->response_body = $body;
271
272 return $this;
273 }
274
275 public function get_response_code() {
276 return $this->response_code;
277 }
278
279
280 private function set_response_code( $code ): Base_Gateway_Action {
281 $this->response_code = $code;
282
283 return $this;
284 }
285
286 public function response_message( $base_message ): string {
287 return $base_message . "\r\n" . sprintf(
288 '%d: %s',
289 $this->get_response_code(),
290 $this->get_response_message()
291 );
292 }
293
294 /**
295 * @throws Gateway_Exception
296 */
297 public function check_response_code(): Base_Gateway_Action {
298 if ( $this->accept_code() === $this->get_response_code() ) {
299 return $this;
300 }
301 $this->response_body_as_array();
302
303 throw new Gateway_Exception(
304 $this->response_message( 'Invalid HTTP code.' ),
305 $this->get_response_body(),
306 $this->get_body()
307 );
308 }
309
310 /**
311 * @throws Gateway_Exception
312 */
313 public function response_body_as_array(): Base_Gateway_Action {
314
315 if ( is_array( $this->get_response_body() ) ) {
316 return $this;
317 }
318
319 if ( empty( $this->get_response_body() ) ) {
320 throw new Gateway_Exception( $this->response_message( 'Empty response.' ) );
321 }
322 if ( is_wp_error( $this->get_response_body() ) ) {
323 /** @var \WP_Error $response */
324 $response = $this->get_response_body();
325
326 throw new Gateway_Exception(
327 $this->response_message( 'Internal error.' ),
328 $response->get_error_message(
329 $response->get_error_code()
330 )
331 );
332 }
333
334 $parsed_response = Tools::decode_json( $this->get_response_body() );
335
336 if ( is_null( $parsed_response ) ) {
337 throw new Gateway_Exception(
338 $this->response_message( 'Invalid JSON.' ),
339 $this->get_response_body(),
340 $this->get_request_args(),
341 $this->get_url()
342 );
343 }
344
345 $this->set_response_body( $parsed_response );
346
347 return $this;
348 }
349
350 /**
351 * @throws Gateway_Exception
352 */
353 public function send_request() {
354 $this->request();
355
356 $this->response_body_as_array();
357
358 return $this->get_response_body();
359 }
360
361 }