PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.2.3
JetFormBuilder — Dynamic Blocks Form Builder v3.2.3
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 / modules / gateways / base-gateway-action.php
jetformbuilder / modules / gateways Last commit date
actions-abstract 2 years ago assets 2 years ago db-models 2 years ago export 2 years ago legacy 2 years ago meta-boxes 2 years ago pages 2 years ago paypal 2 years ago query-views 2 years ago rest-api 2 years ago scenarios-abstract 2 years ago tab-handlers 2 years ago table-views 2 years ago base-gateway-action.php 2 years ago base-gateway.php 2 years ago base-scenario-gateway.php 2 years ago gateways-editor-data.php 2 years ago legacy-base-gateway.php 2 years ago migrate-legacy-data.php 2 years ago module.php 2 years ago scenario-item.php 2 years ago
base-gateway-action.php
387 lines
1 <?php
2
3
4 namespace JFB_Modules\Gateways;
5
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Exceptions\Gateway_Exception;
8 use JFB_Modules\Gateways\Actions_Abstract\Action_Application_Raw_Body_It;
9
10 // If this file is called directly, abort.
11 if ( ! defined( 'WPINC' ) ) {
12 die;
13 }
14
15 abstract class Base_Gateway_Action {
16
17 const CODE_OK = 200;
18 const CODE_CREATED = 201;
19 const CODE_NO_CONTENT = 204;
20
21 const SUCCESS_CODES = array(
22 self::CODE_OK,
23 self::CODE_CREATED,
24 self::CODE_NO_CONTENT,
25 );
26
27 protected $auth;
28 protected $method = 'POST';
29 protected $body = array();
30 protected $response;
31 protected $response_body;
32 protected $response_code;
33 protected $response_message;
34 protected $response_headers;
35 protected $path_parts = array();
36 protected $custom_url = '';
37
38 abstract public function action_endpoint();
39
40 abstract public function base_url(): string;
41
42 public function action_headers() {
43 return array();
44 }
45
46 public function action_body() {
47 return array();
48 }
49
50 public function action_query_args(): array {
51 return array();
52 }
53
54 public function set_url( $url ): self {
55 $this->custom_url = esc_url_raw( $url );
56
57 return $this;
58 }
59
60 public function get_raw_url(): string {
61 return $this->api_url( $this->action_endpoint(), $this->action_query_args() );
62 }
63
64 public function get_url(): string {
65 if ( $this->custom_url ) {
66 return $this->custom_url;
67 }
68
69 $url = $this->get_raw_url();
70
71 if ( empty( $this->path_parts ) ) {
72 return esc_url_raw( $url );
73 }
74
75 return esc_url_raw(
76 preg_replace_callback_array(
77 $this->get_parts_patterns(),
78 $url
79 )
80 );
81 }
82
83 public function get_parts_patterns(): array {
84 $patterns = array();
85
86 foreach ( $this->path_parts as $key => $value ) {
87 $patterns[ "#\{($key)\}#" ] = function () use ( $value ) {
88 return $value;
89 };
90 }
91
92 return $patterns;
93 }
94
95 public function get_method(): string {
96 return $this->method;
97 }
98
99 public function get_auth() {
100 return $this->auth;
101 }
102
103 public function set_bearer_auth( $token ): self {
104 $this->set_auth( "Bearer $token" );
105
106 return $this;
107 }
108
109 public function set_basic_auth( $token ): self {
110 $this->set_auth( "Basic $token" );
111
112 return $this;
113 }
114
115 public function set_auth( $auth_str ): self {
116 $this->auth = $auth_str;
117
118 return $this;
119 }
120
121 public function set_path( array $parts ): self {
122 $this->path_parts = array_merge( $this->path_parts, $parts );
123
124 return $this;
125 }
126
127 public function set_body( $content ): self {
128 if ( ! $content ) {
129 return $this;
130 }
131
132 if ( is_string( $content ) ) {
133 $this->body = $content;
134 }
135
136 if ( is_array( $content ) ) {
137 $this->body = array_merge( $this->body, $content );
138 }
139
140 return $this;
141 }
142
143 public function get_headers(): array {
144 $args = array(
145 'Accept-Language' => get_locale(),
146 );
147
148 if ( $this->get_auth() ) {
149 $args['Authorization'] = $this->get_auth();
150 }
151
152 return array_merge( $args, $this->action_headers() );
153 }
154
155 protected function is_body_ready(): bool {
156 if ( $this instanceof Action_Application_Raw_Body_It ) {
157 return is_array( $this->body ) && ! empty( $this->body );
158 }
159
160 return is_string( $this->body );
161 }
162
163 public function get_body() {
164 if ( $this->is_body_ready() ) {
165 return $this->body;
166 }
167
168 // phpcs:disable WordPress.PHP.IniSet.Risky
169 if ( version_compare( phpversion(), '7.1', '>=' ) ) {
170 ini_set( 'precision', 17 );
171 ini_set( 'serialize_precision', - 1 );
172 }
173 // phpcs:enable WordPress.PHP.IniSet.Risky
174
175 $this->body = $this->to_json( $this->body );
176
177 return $this->body;
178 }
179
180 protected function to_json( $body ) {
181 if ( $this instanceof Action_Application_Raw_Body_It ) {
182 return $body;
183 }
184
185 return wp_unslash( Tools::encode_json( $body ) );
186 }
187
188 public function get_request_args(): array {
189 $args = array(
190 'timeout' => 45,
191 'headers' => $this->get_headers(),
192 );
193
194 if ( 'GET' === $this->get_method() ) {
195 return $args;
196 }
197
198 $args['method'] = $this->get_method();
199
200 if ( ! $this->is_body_ready() ) {
201 $this->set_body( $this->action_body() );
202 }
203
204 if ( ! empty( $this->body ) ) {
205 $args['body'] = $this->get_body();
206 }
207
208 return $args;
209 }
210
211 public function set_method( $method ) {
212 $this->method = $method;
213
214 return $this;
215 }
216
217 /**
218 * Return API url
219 *
220 * @param $endpoint
221 * @param $query_args
222 *
223 * @return string
224 */
225 public function api_url( $endpoint, $query_args ): string {
226 $url = $this->base_url() . $endpoint;
227
228 if ( empty( $query_args ) ) {
229 return $url;
230 }
231
232 return add_query_arg( $query_args, $url );
233 }
234
235 public function get_response() {
236 $params = array( $this->get_url(), $this->get_request_args() );
237
238 return 'POST' === $this->get_method()
239 ? wp_remote_post( ...$params )
240 : wp_remote_get( ...$params );
241 }
242
243 public function before_make_request() {
244 }
245
246 /**
247 * Make a request
248 */
249 public function request(): self {
250 $this->before_make_request();
251
252 $response = $this->get_response();
253
254 $this->set_response_body( wp_remote_retrieve_body( $response ) );
255 $this->set_response_code( (int) wp_remote_retrieve_response_code( $response ) );
256 $this->set_response_message( wp_remote_retrieve_response_message( $response ) );
257 $this->set_response_headers( wp_remote_retrieve_headers( $response ) );
258
259 return $this;
260 }
261
262 public function get_response_headers() {
263 return $this->response_headers;
264 }
265
266 private function set_response_headers( $headers ): self {
267 $this->response_headers = $headers;
268
269 return $this;
270 }
271
272 private function set_response_message( $message ): self {
273 $this->response_message = $message;
274
275 return $this;
276 }
277
278 public function get_response_message() {
279 return $this->response_message;
280 }
281
282 public function get_response_body() {
283 return $this->response_body;
284 }
285
286 private function set_response_body( $body ): self {
287 $this->response_body = $body;
288
289 return $this;
290 }
291
292 public function get_response_code() {
293 return $this->response_code;
294 }
295
296
297 private function set_response_code( $code ): self {
298 $this->response_code = $code;
299
300 return $this;
301 }
302
303 public function response_message( $base_message ): string {
304 return $base_message . "\r\n" . sprintf(
305 '%d: %s',
306 $this->get_response_code(),
307 $this->get_response_message()
308 );
309 }
310
311 /**
312 * @throws Gateway_Exception
313 */
314 public function check_response_code(): self {
315 if ( in_array( $this->get_response_code(), self::SUCCESS_CODES, true ) ) {
316 return $this;
317 }
318
319 $this->response_body_as_array();
320
321 throw new Gateway_Exception(
322 esc_html( $this->response_message( 'Invalid HTTP code.' ) ),
323 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
324 $this->get_response_body(),
325 $this->get_body()
326 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
327 );
328 }
329
330 /**
331 * @throws Gateway_Exception
332 */
333 public function response_body_as_array(): self {
334
335 if ( is_array( $this->get_response_body() ) ) {
336 return $this;
337 }
338
339 if ( empty( $this->get_response_body() ) ) {
340 throw new Gateway_Exception(
341 esc_html( $this->response_message( 'Empty response.' ) )
342 );
343 }
344 if ( is_wp_error( $this->get_response_body() ) ) {
345 /** @var \WP_Error $response */
346 $response = $this->get_response_body();
347
348 throw new Gateway_Exception(
349 esc_html( $this->response_message( 'Internal error.' ) ),
350 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
351 $response->get_error_message(
352 $response->get_error_code()
353 )
354 );
355 }
356
357 $parsed_response = Tools::decode_json( $this->get_response_body() );
358
359 if ( is_null( $parsed_response ) ) {
360 throw new Gateway_Exception(
361 esc_html( $this->response_message( 'Invalid JSON.' ) ),
362 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
363 $this->get_response_body(),
364 $this->get_request_args(),
365 $this->get_url()
366 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
367 );
368 }
369
370 $this->set_response_body( $parsed_response );
371
372 return $this;
373 }
374
375 /**
376 * @throws Gateway_Exception
377 */
378 public function send_request() {
379 $this->request();
380
381 $this->response_body_as_array();
382
383 return $this->get_response_body();
384 }
385
386 }
387