actions-abstract
2 years ago
assets
2 years ago
db-models
2 years ago
export
2 years ago
legacy
2 years ago
meta-boxes
2 years ago
pages
2 years ago
paypal
2 years ago
query-views
2 years ago
rest-api
2 years ago
scenarios-abstract
2 years ago
tab-handlers
2 years ago
table-views
2 years ago
base-gateway-action.php
2 years ago
base-gateway.php
2 years ago
base-scenario-gateway.php
2 years ago
gateways-editor-data.php
2 years ago
legacy-base-gateway.php
2 years ago
migrate-legacy-data.php
2 years ago
module.php
2 years ago
scenario-item.php
2 years ago
migrate-legacy-data.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\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 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class Migrate_Legacy_Data { |
| 17 | |
| 18 | protected $gateways; |
| 19 | |
| 20 | public function __construct( array $gateways ) { |
| 21 | $this->gateways = $gateways; |
| 22 | } |
| 23 | |
| 24 | public static function migrate( array $gateways ) { |
| 25 | if ( ! empty( $gateways['last_migrate'] ) ) { |
| 26 | return; |
| 27 | } |
| 28 | $self = new static( $gateways ); |
| 29 | |
| 30 | jet_fb_action_handler()->merge_events( |
| 31 | $self->get_actions_ids() |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | protected function get_actions_ids(): array { |
| 36 | $on_success = $this->get_active_actions( $this->gateways['notifications_success'] ?? array() ); |
| 37 | $on_failed = $this->get_active_actions( $this->gateways['notifications_failed'] ?? array() ); |
| 38 | $on_before = $this->get_active_actions( $this->gateways['notifications_before'] ?? array() ); |
| 39 | $use_redirect = $this->gateways['use_success_redirect'] ?? false; |
| 40 | |
| 41 | $has_redirect = false; |
| 42 | |
| 43 | if ( ! $on_success && ! $on_failed && ! $on_before && ! $use_redirect ) { |
| 44 | return array(); |
| 45 | } |
| 46 | |
| 47 | $response = array(); |
| 48 | |
| 49 | foreach ( jet_fb_action_handler()->get_all() as $action ) { |
| 50 | $events = new Events_List(); |
| 51 | |
| 52 | if ( in_array( $action->_id, $on_success, true ) ) { |
| 53 | $events->push( Gateway_Success_Event::class ); |
| 54 | } |
| 55 | if ( in_array( $action->_id, $on_failed, true ) ) { |
| 56 | $events->push( Gateway_Failed_Event::class ); |
| 57 | } |
| 58 | if ( in_array( $action->_id, $on_before, true ) ) { |
| 59 | $events->push( Default_Process_Event::class ); |
| 60 | } |
| 61 | if ( $use_redirect && ! $has_redirect && 'redirect_to_page' === $action->get_id() ) { |
| 62 | $events->push( Gateway_Success_Event::class ); |
| 63 | $has_redirect = true; |
| 64 | } |
| 65 | |
| 66 | if ( ! $events ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | $response[ $action->_id ] = $events; |
| 71 | } |
| 72 | |
| 73 | return $response; |
| 74 | } |
| 75 | |
| 76 | protected function get_active_actions( $actions ): array { |
| 77 | $response = array(); |
| 78 | |
| 79 | foreach ( $actions as $id => $settings ) { |
| 80 | if ( ! empty( $settings['active'] ) ) { |
| 81 | $response[] = $id; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return $response; |
| 86 | } |
| 87 | |
| 88 | } |
| 89 |