PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.5
JetFormBuilder — Dynamic Blocks Form Builder v2.1.5
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
364 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 const SUCCESS_CODES = array(
17 self::CODE_OK,
18 self::CODE_CREATED,
19 self::CODE_NO_CONTENT,
20 );
21
22 protected $auth;
23 protected $method = 'POST';
24 protected $body = array();
25 protected $response;
26 protected $response_body;
27 protected $response_code;
28 protected $response_message;
29 protected $response_headers;
30 protected $path_parts = array();
31 protected $custom_url = '';
32
33 abstract public function action_endpoint();
34
35 abstract public function base_url(): string;
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 ): self {
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 ): self {
99 $this->set_auth( "Bearer $token" );
100
101 return $this;
102 }
103
104 public function set_basic_auth( $token ): self {
105 $this->set_auth( "Basic $token" );
106
107 return $this;
108 }
109
110 public function set_auth( $auth_str ): self {
111 $this->auth = $auth_str;
112
113 return $this;
114 }
115
116 public function set_path( array $parts ): self {
117 $this->path_parts = array_merge( $this->path_parts, $parts );
118
119 return $this;
120 }
121
122 public function set_body( $content ): self {
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 if ( ! $this->is_body_ready() ) {
186 $this->set_body( $this->action_body() );
187 }
188
189 if ( ! empty( $this->body ) ) {
190 $args['body'] = $this->get_body();
191 }
192
193 return $args;
194 }
195
196 public function set_method( $method ) {
197 $this->method = $method;
198
199 return $this;
200 }
201
202 /**
203 * Return API url
204 *
205 * @param $endpoint
206 * @param $query_args
207 *
208 * @return string
209 */
210 public function api_url( $endpoint, $query_args ): string {
211 $url = $this->base_url() . $endpoint;
212
213 if ( empty( $query_args ) ) {
214 return $url;
215 }
216
217 return add_query_arg( $query_args, $url );
218 }
219
220 public function get_response() {
221 $params = array( $this->get_url(), $this->get_request_args() );
222
223 return 'POST' === $this->get_method()
224 ? wp_remote_post( ...$params )
225 : wp_remote_get( ...$params );
226 }
227
228 public function before_make_request() {
229 }
230
231 /**
232 * Make a request
233 */
234 public function request(): self {
235 $this->before_make_request();
236
237 $response = $this->get_response();
238
239 $this->set_response_body( wp_remote_retrieve_body( $response ) );
240 $this->set_response_code( (int) wp_remote_retrieve_response_code( $response ) );
241 $this->set_response_message( wp_remote_retrieve_response_message( $response ) );
242 $this->set_response_headers( wp_remote_retrieve_headers( $response ) );
243
244 return $this;
245 }
246
247 public function get_response_headers() {
248 return $this->response_headers;
249 }
250
251 private function set_response_headers( $headers ): self {
252 $this->response_headers = $headers;
253
254 return $this;
255 }
256
257 private function set_response_message( $message ): self {
258 $this->response_message = $message;
259
260 return $this;
261 }
262
263 public function get_response_message() {
264 return $this->response_message;
265 }
266
267 public function get_response_body() {
268 return $this->response_body;
269 }
270
271 private function set_response_body( $body ): self {
272 $this->response_body = $body;
273
274 return $this;
275 }
276
277 public function get_response_code() {
278 return $this->response_code;
279 }
280
281
282 private function set_response_code( $code ): self {
283 $this->response_code = $code;
284
285 return $this;
286 }
287
288 public function response_message( $base_message ): string {
289 return $base_message . "\r\n" . sprintf(
290 '%d: %s',
291 $this->get_response_code(),
292 $this->get_response_message()
293 );
294 }
295
296 /**
297 * @throws Gateway_Exception
298 */
299 public function check_response_code(): self {
300 if ( in_array( $this->get_response_code(), self::SUCCESS_CODES, true ) ) {
301 return $this;
302 }
303
304 $this->response_body_as_array();
305
306 throw new Gateway_Exception(
307 $this->response_message( 'Invalid HTTP code.' ),
308 $this->get_response_body(),
309 $this->get_body()
310 );
311 }
312
313 /**
314 * @throws Gateway_Exception
315 */
316 public function response_body_as_array(): self {
317
318 if ( is_array( $this->get_response_body() ) ) {
319 return $this;
320 }
321
322 if ( empty( $this->get_response_body() ) ) {
323 throw new Gateway_Exception( $this->response_message( 'Empty response.' ) );
324 }
325 if ( is_wp_error( $this->get_response_body() ) ) {
326 /** @var \WP_Error $response */
327 $response = $this->get_response_body();
328
329 throw new Gateway_Exception(
330 $this->response_message( 'Internal error.' ),
331 $response->get_error_message(
332 $response->get_error_code()
333 )
334 );
335 }
336
337 $parsed_response = Tools::decode_json( $this->get_response_body() );
338
339 if ( is_null( $parsed_response ) ) {
340 throw new Gateway_Exception(
341 $this->response_message( 'Invalid JSON.' ),
342 $this->get_response_body(),
343 $this->get_request_args(),
344 $this->get_url()
345 );
346 }
347
348 $this->set_response_body( $parsed_response );
349
350 return $this;
351 }
352
353 /**
354 * @throws Gateway_Exception
355 */
356 public function send_request() {
357 $this->request();
358
359 $this->response_body_as_array();
360
361 return $this->get_response_body();
362 }
363
364 }