PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.3
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.3
3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 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 All 131 releases
jetformbuilder / modules / gateways / module.php

module.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.5.3, at modules/gateways/module.php

465 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JFB_Modules\Gateways;
4
5 use Jet_Form_Builder\Actions\Events\Default_Process\Default_With_Gateway_Executor;
6 use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page;
7 use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container;
8 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
9 use JFB_Components\Admin\Print_Page\Header;
10 use JFB_Components\Module\Base_Module_After_Install_It;
11 use JFB_Components\Module\Base_Module_Dir_It;
12 use JFB_Components\Module\Base_Module_Dir_Trait;
13 use JFB_Components\Module\Base_Module_Handle_It;
14 use JFB_Components\Module\Base_Module_Handle_Trait;
15 use JFB_Components\Module\Base_Module_It;
16 use JFB_Components\Module\Base_Module_Static_Instance_It;
17 use JFB_Components\Module\Base_Module_Static_Instance_Trait;
18 use JFB_Components\Module\Base_Module_Url_It;
19 use JFB_Components\Module\Base_Module_Url_Trait;
20 use JFB_Components\Repository\Repository_Pattern_Trait;
21 use Jet_Form_Builder\Exceptions\Repository_Exception;
22 use JFB_Modules\Gateways\Meta_Boxes\Payment_Info_For_Record;
23 use JFB_Modules\Gateways\Paypal;
24 use JFB_Modules\Gateways\Rest_Api\Rest_Api_Controller;
25 use Jet_Form_Builder\Plugin;
26 use JFB_Modules\Gateways\Pages;
27
28 // If this file is called directly, abort.
29 if ( ! defined( 'WPINC' ) ) {
30 die;
31 }
32
33 /**
34 * @method static Module instance()
35 *
36 * @package JFB_Modules\Gateways
37 */
38 class Module implements
39 Base_Module_Dir_It,
40 Base_Module_It,
41 Base_Module_Handle_It,
42 Base_Module_Url_It,
43 Base_Module_After_Install_It,
44 Base_Module_Static_Instance_It {
45
46 use Gateways_Editor_Data;
47 use Repository_Pattern_Trait;
48
49 use Base_Module_Handle_Trait;
50 use Base_Module_Dir_Trait;
51 use Base_Module_Url_Trait;
52 use Base_Module_Static_Instance_Trait;
53
54 const PAYMENT_TYPE_PARAM = 'jet_form_gateway';
55 const OPTION_NAME = 'payments-gateways';
56
57 private $gateways_form_data = array();
58 private $form_id;
59
60 public $message = null;
61 public $data = null;
62 public $is_sandbox;
63 private $current_gateway_type;
64
65 private $rest;
66 private $secure_price_notice;
67
68 public static function get_instance_id(): string {
69 return 'gateways';
70 }
71
72 public function condition(): bool {
73 $allow = apply_filters_deprecated(
74 'jet-form-builder/allow-gateways',
75 array( false ),
76 '3.1.0',
77 __( 'Settings -> Payments Gateways -> Enable Gateways', 'jet-form-builder' )
78 );
79
80 // install tab handlers for Settings page
81 Tab_Handler_Manager::instance()->install( new Tab_Handlers\Payments_Gateways_Handler() );
82
83 $gateways = Tab_Handler_Manager::get_options( self::OPTION_NAME );
84
85 $use_gateways = isset( $gateways['use_gateways'] ) ? boolval( $gateways['use_gateways'] ) : $allow;
86 return apply_filters( 'jet-form-builder/use-gateways', $use_gateways );
87 }
88
89 public function init_hooks() {
90 add_action( 'rest_api_init', array( $this->get_rest(), 'register_routes' ) );
91 add_action( 'init', array( $this, 'register_gateways' ) );
92 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
93 add_action(
94 'jet-form-builder/before-print-page/header',
95 array( $this, 'before_print_page' ),
96 10,
97 2
98 );
99
100 add_filter( 'jet-form-builder/admin/pages', array( $this, 'add_stable_pages' ), 0 );
101 add_filter( 'jet-form-builder/admin/single-pages', array( $this, 'add_single_pages' ), 0 );
102 add_filter( 'jet-form-builder/admin/action-pages', array( $this, 'add_action_admin_pages' ) );
103 add_filter(
104 'jet-form-builder/page-containers/jfb-records-single',
105 array( $this, 'add_box_to_single_record' ),
106 0
107 );
108 add_filter(
109 'jet-form-builder/default-process-event/executors',
110 array( $this, 'add_executor_to_default_process' )
111 );
112 add_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_editor_config' ) );
113
114 $this->secure_price_notice->init_hooks();
115 }
116
117 public function remove_hooks() {
118 remove_action( 'rest_api_init', array( $this->get_rest(), 'register_routes' ) );
119 remove_action( 'init', array( $this, 'register_gateways' ) );
120 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
121 remove_action(
122 'jet-form-builder/before-print-page/header',
123 array( $this, 'before_print_page' )
124 );
125
126 remove_filter( 'jet-form-builder/admin/pages', array( $this, 'add_stable_pages' ), 0 );
127 remove_filter( 'jet-form-builder/admin/single-pages', array( $this, 'add_single_pages' ), 0 );
128 remove_filter( 'jet-form-builder/admin/action-pages', array( $this, 'add_action_admin_pages' ) );
129 remove_filter(
130 'jet-form-builder/page-containers/jfb-records-single',
131 array( $this, 'add_box_to_single_record' ),
132 0
133 );
134 remove_filter(
135 'jet-form-builder/default-process-event/executors',
136 array( $this, 'add_executor_to_default_process' )
137 );
138 remove_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_editor_config' ) );
139
140 $this->secure_price_notice->remove_hooks();
141 }
142
143 public function on_install() {
144 // backward compatibility
145 require_once $this->get_dir( 'legacy/payment-details-box.php' );
146 require_once $this->get_dir( 'legacy/base-gateway-action.php' );
147
148 $options = Tab_Handler_Manager::get_options( self::OPTION_NAME );
149 $this->is_sandbox = apply_filters_deprecated(
150 'jet-form-builder/gateways/paypal/sandbox-mode',
151 array( false ),
152 '3.1.0',
153 __( 'Settings -> Payments Gateways -> Enable Test Mode', 'jet-form-builder' )
154 );
155
156 if ( isset( $options['enable_test_mode'] ) ) {
157 $this->is_sandbox = $options['enable_test_mode'];
158 }
159
160 // rest api
161 $this->rest = new Rest_Api_Controller();
162 $this->secure_price_notice = new Secure_Price_Notice();
163
164 // catch webhook by get param
165 $this->catch_payment_result();
166 }
167
168 public function on_uninstall() {
169 // install tab handlers for Settings page
170 Tab_Handler_Manager::instance()->uninstall( new Tab_Handlers\Payments_Gateways_Handler() );
171
172 unset( $this->is_sandbox, $this->rest );
173 $this->secure_price_notice = null;
174 }
175
176 public function rep_instances(): array {
177 return array(
178 new Paypal\Controller(),
179 );
180 }
181
182 public function enqueue_editor_assets() {
183 $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' );
184
185 wp_enqueue_script(
186 $this->get_handle( 'editor' ),
187 $this->get_url( 'assets/build/editor.js' ),
188 $script_asset['dependencies'],
189 $script_asset['version'],
190 true
191 );
192 }
193
194 public function add_executor_to_default_process( array $executors ): array {
195 return array_merge(
196 array(
197 new Default_With_Gateway_Executor(),
198 ),
199 $executors
200 );
201 }
202
203 public function add_stable_pages( $pages ): array {
204 $pages[] = new Pages\Payments_Page();
205
206 return $pages;
207 }
208
209 public function add_single_pages( $pages ): array {
210 $pages[] = new Pages\Single_Payment_Page();
211
212 return $pages;
213 }
214
215 public function add_action_admin_pages( array $pages ): array {
216 array_push(
217 $pages,
218 new Pages\Export_Page(),
219 new Pages\Print_Page()
220 );
221
222 return $pages;
223 }
224
225 /**
226 * @param Header $header
227 * @param Base_Single_Page $page
228 */
229 public function before_print_page( Header $header, Base_Single_Page $page ) {
230 if ( ! ( $page instanceof Pages\Single_Payment_Print_Page ) ) {
231 return;
232 }
233 $header->set_title( __( 'JetFormBuilder Payment', 'jet-form-builder' ) );
234 }
235
236 /**
237 * @param Base_Meta_Container[] $containers
238 *
239 * @return array
240 */
241 public function add_box_to_single_record( array $containers ): array {
242 $containers[1]->add_meta_box( new Payment_Info_For_Record() );
243
244 return $containers;
245 }
246
247 public function on_localize_editor_config( array $config ): array {
248 $config['gateways'] = $this->editor_data();
249
250 return $config;
251 }
252
253 /**
254 * Returns all registered gateways
255 *
256 * @return void [description]
257 */
258 public function register_gateways() {
259 // install gateways
260 $this->rep_install();
261
262 do_action( 'jet-form-builder/gateways/register', $this );
263 }
264
265 /**
266 * @param Base_Gateway $gateway
267 *
268 * @throws Repository_Exception
269 */
270 public function register_gateway( Base_Gateway $gateway ) {
271 $this->rep_install_item( $gateway );
272 }
273
274 public function save_gateways_form_data( $data ) {
275 $id = $data['gateway'];
276
277 $this->gateways_form_data = $this->with_global_settings( $data, $id );
278 }
279
280 /**
281 * Catch processed payment results
282 *
283 * @return void [description]
284 */
285 public function catch_payment_result() {
286 // phpcs:disable WordPress.Security.NonceVerification.Recommended
287 if ( ! isset( $_GET[ self::PAYMENT_TYPE_PARAM ] ) ) {
288 return;
289 }
290
291 $this->current_gateway_type = wp_unslash( sanitize_key( $_GET[ self::PAYMENT_TYPE_PARAM ] ) );
292 // phpcs:enable WordPress.Security.NonceVerification.Recommended
293
294 add_action( 'parse_request', array( $this, 'on_has_gateway_request' ) );
295 }
296
297 public function on_has_gateway_request() {
298 try {
299 $this->get_gateway_controller( $this->current_gateway() )->try_run_on_catch();
300 } catch ( Repository_Exception $exception ) {
301 return;
302 }
303 }
304
305 /**
306 * @param false $type
307 *
308 * @return Base_Gateway|Base_Scenario_Gateway
309 * @throws Repository_Exception
310 */
311 public function get_gateway_controller( $type = false ) {
312 return $this->rep_get_item( $type );
313 }
314
315 /**
316 * @return Base_Gateway|Base_Scenario_Gateway
317 * @throws Repository_Exception
318 */
319 public function get_current_gateway_controller() {
320 return $this->get_gateway_controller( $this->get_gateway_id() );
321 }
322
323 /**
324 * @return false|Base_Gateway|Base_Scenario_Gateway
325 */
326 public function get_current_gateway_controller_or_die() {
327 try {
328 return $this->get_current_gateway_controller();
329 } catch ( Repository_Exception $exception ) {
330 return false;
331 }
332 }
333
334 /**
335 * @param $gateway_id
336 *
337 * @return Base_Gateway
338 */
339 public function controller( $gateway_id ) {
340 try {
341 return $this->get_gateway_controller( $gateway_id );
342 } catch ( Repository_Exception $exception ) {
343 _doing_it_wrong( __METHOD__, esc_html( "Undefined gateway: {$gateway_id}" ), '2.0.0' );
344 }
345 }
346
347 /**
348 * Returns gateways config for current form
349 *
350 * @param [type] $post_id [description]
351 *
352 * @return [type] [description]
353 */
354 public function get_form_gateways_by_id( $form_id = null ) {
355
356 if ( ! $form_id ) {
357 $form_id = get_the_ID();
358 }
359 $default = array( 'gateway' => 'none' );
360
361 $meta = Plugin::instance()->post_type->get_gateways( $form_id );
362
363 return is_array( $meta ) ? $meta : $default;
364 }
365
366 public function gateways( $prop = '', $if_empty = false ) {
367 return $prop ? ( $this->gateways_form_data[ $prop ] ?? $if_empty ) : $this->gateways_form_data;
368 }
369
370 public function has_gateway( $form_id ) {
371 $this->set_gateways_options_by_form_id( $form_id );
372
373 return $this->get_gateway_id( false );
374 }
375
376 public function get_global_settings( $gateway_id ) {
377 return Tab_Handler_Manager::instance()->tab( $gateway_id )->on_load();
378 }
379
380 public function with_global_settings( $gateways_data, $gateway_id ) {
381 if ( ! isset( $gateways_data[ $gateway_id ] ) ) {
382 return $gateways_data;
383 }
384 $gateway = &$gateways_data[ $gateway_id ];
385
386 if ( ! empty( $gateway['use_global'] ) ) {
387 $gateway = array_merge( $gateway, $this->get_global_settings( $gateway_id ) );
388 }
389
390 return $gateways_data;
391 }
392
393 public function get_gateway_id( $if_empty = 'none' ) {
394 return $this->gateways_form_data['gateway'] ?? $this->current_gateway_type ?? $if_empty;
395 }
396
397 public function set_form_id( $form_id ) {
398 $this->form_id = (int) $form_id;
399
400 return $this;
401 }
402
403 public function get_form_id() {
404 return $this->form_id;
405 }
406
407 public function set_gateways_options_by_form_id( $form_id ) {
408 if ( (int) $form_id === $this->get_form_id() ) {
409 return;
410 }
411 $this->set_form_id( $form_id );
412 $gateways = $this->get_form_gateways_by_id( $this->get_form_id() );
413
414 if ( 'none' === ( $gateways['gateway'] ?? 'none' ) ) {
415 return;
416 }
417 $this->save_gateways_form_data( $gateways );
418 }
419
420 public function current_gateway() {
421 return $this->current_gateway_type;
422 }
423
424 public function currency_symbol( $currency ) {
425 $currency = strtoupper( $currency );
426
427 switch ( $currency ) {
428 case 'USD':
429 case 'AUD':
430 case 'NZD':
431 case 'CAD':
432 case 'HKD':
433 case 'MXN':
434 case 'SGD':
435 $symbol = '&#36;';
436 break;
437 case 'EUR':
438 $symbol = '&euro;';
439 break;
440 case 'GBP':
441 $symbol = '&pound;';
442 break;
443 case 'BRL':
444 $symbol = 'R&#36;';
445 break;
446 case 'JPY':
447 $symbol = '&yen;';
448 break;
449 case 'AOA':
450 $symbol = 'Kz';
451 break;
452 default:
453 $symbol = $currency;
454 break;
455 }
456
457 return apply_filters( 'jet-form-builder/gateways/currency-symbol', $symbol, $currency );
458 }
459
460 public function get_rest(): Rest_Api_Controller {
461 return $this->rest;
462 }
463
464 }
465