module.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Multi_Gateway; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Blocks\Module as BlocksModule; |
| 12 | use Jet_Form_Builder\Form_Handler; |
| 13 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 14 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 15 | use JFB_Components\Module\Base_Module_Handle_It; |
| 16 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 17 | use JFB_Components\Module\Base_Module_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_It; |
| 19 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 20 | |
| 21 | class Module implements |
| 22 | Base_Module_After_Install_It, |
| 23 | Base_Module_It, |
| 24 | Base_Module_Url_It, |
| 25 | Base_Module_Handle_It { |
| 26 | |
| 27 | use Base_Module_Handle_Trait; |
| 28 | use Base_Module_Dir_Trait; |
| 29 | use Base_Module_Url_Trait; |
| 30 | |
| 31 | public function rep_item_id() { |
| 32 | return 'multi-gateway'; |
| 33 | } |
| 34 | |
| 35 | public function condition(): bool { |
| 36 | return jet_form_builder()->has_module( 'jet-style' ); |
| 37 | } |
| 38 | |
| 39 | public function on_install() { |
| 40 | } |
| 41 | |
| 42 | public function on_uninstall() { |
| 43 | } |
| 44 | |
| 45 | public function init_hooks() { |
| 46 | add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 47 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 48 | add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 49 | add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 50 | add_action( 'enqueue_block_editor_assets', array( $this, 'preview_styles' ) ); |
| 51 | |
| 52 | add_action( |
| 53 | 'jet-form-builder/form-handler/before-send', |
| 54 | array( $this, 'set_gateway_from_submitted_multi_gateway' ), |
| 55 | 0 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | public function remove_hooks() { |
| 60 | remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) ); |
| 61 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 62 | remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 63 | remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) ); |
| 64 | remove_action( 'jet-form-builder/form-handler/before-send', array( $this, 'set_gateway_from_submitted_multi_gateway' ), 0 ); |
| 65 | } |
| 66 | |
| 67 | public function add_blocks_types( array $types ): array { |
| 68 | $types[] = new Blocks\Multi_Gateway\Block_Type(); |
| 69 | |
| 70 | return $types; |
| 71 | } |
| 72 | |
| 73 | public function preview_styles() { |
| 74 | |
| 75 | $script_asset = require $this->get_dir( 'assets/build/editor.asset.php' ); |
| 76 | |
| 77 | wp_enqueue_style( |
| 78 | $this->get_handle(), |
| 79 | $this->get_url( 'assets/build/editor.css' ), |
| 80 | array(), |
| 81 | $script_asset['version'] |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public function enqueue_admin_assets() { |
| 86 | |
| 87 | $script_asset = require $this->get_dir( 'assets/build/editor.asset.php' ); |
| 88 | |
| 89 | wp_enqueue_script( |
| 90 | $this->get_handle(), |
| 91 | $this->get_url( 'assets/build/editor.js' ), |
| 92 | $script_asset['dependencies'], |
| 93 | $script_asset['version'], |
| 94 | true |
| 95 | ); |
| 96 | |
| 97 | wp_enqueue_style( |
| 98 | $this->get_handle(), |
| 99 | $this->get_url( 'assets/build/editor.css' ), |
| 100 | array(), |
| 101 | $script_asset['version'] |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | public function register_frontend_scripts() { |
| 106 | $script_asset = require $this->get_dir( 'assets/build/multi-gateway.asset.php' ); |
| 107 | |
| 108 | $deps = $script_asset['dependencies'] ?? array(); |
| 109 | $ver = $script_asset['version'] ?? false; |
| 110 | |
| 111 | $deps[] = BlocksModule::MAIN_SCRIPT_HANDLE; |
| 112 | |
| 113 | wp_register_script( |
| 114 | $this->get_handle(), |
| 115 | $this->get_url( 'assets/build/multi-gateway.js' ), |
| 116 | $deps, |
| 117 | $ver, |
| 118 | true |
| 119 | ); |
| 120 | |
| 121 | wp_register_style( |
| 122 | $this->get_handle(), |
| 123 | $this->get_url( 'assets/build/multi-gateway.css' ), |
| 124 | array(), |
| 125 | $ver |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Set active gateway for current submission from Multi-Gateway field value. |
| 131 | * |
| 132 | * @param Form_Handler $handler |
| 133 | */ |
| 134 | public function set_gateway_from_submitted_multi_gateway( Form_Handler $handler ) { |
| 135 | |
| 136 | $selected = jet_fb_context()->get_request( 'multi_gateway' ); |
| 137 | |
| 138 | if ( ! is_string( $selected ) || '' === $selected ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | $selected = sanitize_key( $selected ); |
| 143 | |
| 144 | $handler_form_id = (int) $handler->get_form_id(); |
| 145 | |
| 146 | // ADDED: normalize to parent for meta read (_jf_gateways stored on parent form post) |
| 147 | $real_form_id = $handler_form_id; |
| 148 | |
| 149 | $parent = wp_is_post_revision( $real_form_id ); |
| 150 | if ( $parent ) { |
| 151 | $real_form_id = (int) $parent; |
| 152 | } |
| 153 | |
| 154 | $parent = wp_is_post_autosave( $real_form_id ); |
| 155 | if ( $parent ) { |
| 156 | $real_form_id = (int) $parent; |
| 157 | } |
| 158 | |
| 159 | $parent = wp_get_post_parent_id( $real_form_id ); |
| 160 | if ( $parent ) { |
| 161 | $real_form_id = (int) $parent; |
| 162 | } |
| 163 | |
| 164 | $gateways_module = \JFB_Modules\Gateways\Module::instance(); |
| 165 | |
| 166 | $gateways = $gateways_module->get_form_gateways_by_id( $real_form_id ); |
| 167 | |
| 168 | $gateways['gateway'] = $selected; |
| 169 | |
| 170 | // IMPORTANT: set module form_id to handler form id (revision), |
| 171 | // so Default_With_Gateway_Executor won't overwrite our runtime selection. |
| 172 | $gateways_module->set_form_id( $handler_form_id ); |
| 173 | $gateways_module->save_gateways_form_data( $gateways ); |
| 174 | } |
| 175 | } |
| 176 |