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
migrate-legacy-data.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Gateways; |
| 5 | |
| 6 | use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event; |
| 7 | use Jet_Form_Builder\Actions\Events\Gateway_Failed\Gateway_Failed_Event; |
| 8 | use Jet_Form_Builder\Actions\Events\Gateway_Success\Gateway_Success_Event; |
| 9 | use Jet_Form_Builder\Actions\Events_List; |
| 10 | |
| 11 | class Migrate_Legacy_Data { |
| 12 | |
| 13 | protected $gateways; |
| 14 | |
| 15 | public function __construct( array $gateways ) { |
| 16 | $this->gateways = $gateways; |
| 17 | } |
| 18 | |
| 19 | public static function migrate( array $gateways ) { |
| 20 | if ( ! empty( $gateways['last_migrate'] ) ) { |
| 21 | return; |
| 22 | } |
| 23 | $self = new static( $gateways ); |
| 24 | |
| 25 | jet_fb_action_handler()->merge_events( |
| 26 | $self->get_actions_ids() |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | protected function get_actions_ids(): array { |
| 31 | $on_success = $this->get_active_actions( $this->gateways['notifications_success'] ?? array() ); |
| 32 | $on_failed = $this->get_active_actions( $this->gateways['notifications_failed'] ?? array() ); |
| 33 | $on_before = $this->get_active_actions( $this->gateways['notifications_before'] ?? array() ); |
| 34 | $use_redirect = $this->gateways['use_success_redirect'] ?? false; |
| 35 | |
| 36 | $has_redirect = false; |
| 37 | |
| 38 | if ( ! $on_success && ! $on_failed && ! $on_before && ! $use_redirect ) { |
| 39 | return array(); |
| 40 | } |
| 41 | |
| 42 | $response = array(); |
| 43 | |
| 44 | foreach ( jet_fb_action_handler()->get_all() as $action ) { |
| 45 | $events = new Events_List(); |
| 46 | |
| 47 | if ( in_array( $action->_id, $on_success, true ) ) { |
| 48 | $events->push( Gateway_Success_Event::class ); |
| 49 | } |
| 50 | if ( in_array( $action->_id, $on_failed, true ) ) { |
| 51 | $events->push( Gateway_Failed_Event::class ); |
| 52 | } |
| 53 | if ( in_array( $action->_id, $on_before, true ) ) { |
| 54 | $events->push( Default_Process_Event::class ); |
| 55 | } |
| 56 | if ( $use_redirect && ! $has_redirect && 'redirect_to_page' === $action->get_id() ) { |
| 57 | $events->push( Gateway_Success_Event::class ); |
| 58 | $has_redirect = true; |
| 59 | } |
| 60 | |
| 61 | if ( ! $events ) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | $response[ $action->_id ] = $events; |
| 66 | } |
| 67 | |
| 68 | return $response; |
| 69 | } |
| 70 | |
| 71 | protected function get_active_actions( $actions ): array { |
| 72 | $response = array(); |
| 73 | |
| 74 | foreach ( $actions as $id => $settings ) { |
| 75 | if ( ! empty( $settings['active'] ) ) { |
| 76 | $response[] = $id; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return $response; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 |