| 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 |
$handler_form_id = (int) $handler->get_form_id(); |
| 137 |
$selected = $this->get_allowed_gateway_for_submission( |
| 138 |
jet_fb_context()->get_request( 'multi_gateway' ), |
| 139 |
$handler_form_id |
| 140 |
); |
| 141 |
|
| 142 |
// ADDED: normalize to parent for meta read (_jf_gateways stored on parent form post) |
| 143 |
$real_form_id = $this->get_real_form_id( $handler_form_id ); |
| 144 |
|
| 145 |
$gateways_module = \JFB_Modules\Gateways\Module::instance(); |
| 146 |
|
| 147 |
$gateways = $gateways_module->get_form_gateways_by_id( $real_form_id ); |
| 148 |
$is_manual = 'manual' === ( $gateways['mode'] ?? 'single' ); |
| 149 |
|
| 150 |
if ( '' === $selected && ! $is_manual ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$gateways['gateway'] = $selected ?: 'none'; |
| 155 |
|
| 156 |
// IMPORTANT: set module form_id to handler form id (revision), |
| 157 |
// so Default_With_Gateway_Executor won't overwrite our runtime selection. |
| 158 |
$gateways_module->set_form_id( $handler_form_id ); |
| 159 |
$gateways_module->save_gateways_form_data( $gateways ); |
| 160 |
} |
| 161 |
|
| 162 |
public function get_allowed_gateway_for_submission( $selected, int $form_id ): string { |
| 163 |
if ( ! is_string( $selected ) || '' === $selected ) { |
| 164 |
return ''; |
| 165 |
} |
| 166 |
|
| 167 |
$selected = sanitize_key( $selected ); |
| 168 |
|
| 169 |
if ( '' === $selected ) { |
| 170 |
return ''; |
| 171 |
} |
| 172 |
|
| 173 |
$real_form_id = $this->get_real_form_id( $form_id ); |
| 174 |
$gateways = \JFB_Modules\Gateways\Module::instance()->get_form_gateways_by_id( $real_form_id ); |
| 175 |
|
| 176 |
if ( ! is_array( $gateways ) ) { |
| 177 |
return ''; |
| 178 |
} |
| 179 |
|
| 180 |
$allowed = $this->get_allowed_gateways_for_frontend( $gateways ); |
| 181 |
|
| 182 |
return in_array( $selected, $allowed, true ) ? $selected : ''; |
| 183 |
} |
| 184 |
|
| 185 |
private function get_real_form_id( int $form_id ): int { |
| 186 |
$real_form_id = $form_id; |
| 187 |
|
| 188 |
$parent = wp_is_post_revision( $real_form_id ); |
| 189 |
if ( $parent ) { |
| 190 |
$real_form_id = (int) $parent; |
| 191 |
} |
| 192 |
|
| 193 |
$parent = wp_is_post_autosave( $real_form_id ); |
| 194 |
if ( $parent ) { |
| 195 |
$real_form_id = (int) $parent; |
| 196 |
} |
| 197 |
|
| 198 |
$parent = wp_get_post_parent_id( $real_form_id ); |
| 199 |
if ( $parent ) { |
| 200 |
$real_form_id = (int) $parent; |
| 201 |
} |
| 202 |
|
| 203 |
return $real_form_id; |
| 204 |
} |
| 205 |
|
| 206 |
private function get_allowed_gateways_for_frontend( array $gateways ): array { |
| 207 |
$mode = $gateways['mode'] ?? 'single'; |
| 208 |
$allowed = array(); |
| 209 |
|
| 210 |
if ( 'manual' === $mode ) { |
| 211 |
foreach ( $gateways as $key => $value ) { |
| 212 |
if ( ! is_string( $key ) || in_array( $key, array( 'mode', 'gateway' ), true ) ) { |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
if ( is_array( $value ) && ! empty( $value['show_on_front'] ) ) { |
| 217 |
$allowed[] = sanitize_key( $key ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return array_filter( $allowed ); |
| 222 |
} |
| 223 |
|
| 224 |
$gateway = $gateways['gateway'] ?? 'none'; |
| 225 |
|
| 226 |
if ( is_string( $gateway ) && '' !== $gateway && 'none' !== $gateway ) { |
| 227 |
$allowed[] = sanitize_key( $gateway ); |
| 228 |
} |
| 229 |
|
| 230 |
return array_filter( $allowed ); |
| 231 |
} |
| 232 |
} |
| 233 |
|