| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Actions; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Actions\Events\Bad_Request\Bad_Request_Event; |
| 7 |
use Jet_Form_Builder\Actions\Events\Default_Required\Default_Required_Event; |
| 8 |
use Jet_Form_Builder\Actions\Events\Base_Action_Event; |
| 9 |
use Jet_Form_Builder\Actions\Events\Base_Event; |
| 10 |
use Jet_Form_Builder\Actions\Events\Base_Gateway_Event; |
| 11 |
use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event; |
| 12 |
use Jet_Form_Builder\Actions\Events\Gateway_Failed\Gateway_Failed_Event; |
| 13 |
use Jet_Form_Builder\Actions\Events\Gateway_Success\Gateway_Success_Event; |
| 14 |
use Jet_Form_Builder\Actions\Events\Never\Never_Event; |
| 15 |
use Jet_Form_Builder\Actions\Events\On_Dynamic_State\On_Dynamic_State_Event; |
| 16 |
use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 17 |
use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 18 |
use Jet_Form_Builder\Classes\Instance_Trait; |
| 19 |
use JFB_Components\Repository\Repository_Dynamic_Items_It; |
| 20 |
use JFB_Components\Repository\Repository_Pattern_Trait; |
| 21 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 22 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 23 |
|
| 24 |
// If this file is called directly, abort. |
| 25 |
if ( ! defined( 'WPINC' ) ) { |
| 26 |
die; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @method static Events_Manager instance() |
| 31 |
* |
| 32 |
* Class Events_Manager |
| 33 |
* @package Jet_Form_Builder\Actions |
| 34 |
*/ |
| 35 |
class Events_Manager implements Arrayable, Repository_Dynamic_Items_It { |
| 36 |
|
| 37 |
use Instance_Trait; |
| 38 |
use Repository_Pattern_Trait; |
| 39 |
|
| 40 |
private $types = array(); |
| 41 |
/** |
| 42 |
* @var string|null |
| 43 |
*/ |
| 44 |
private $current; |
| 45 |
|
| 46 |
public function __construct() { |
| 47 |
$this->rep_install(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
public function rep_instances(): array { |
| 54 |
return apply_filters( |
| 55 |
'jet-form-builder/event-types', |
| 56 |
array( |
| 57 |
new Never_Event(), |
| 58 |
new Default_Process_Event(), |
| 59 |
new Gateway_Success_Event(), |
| 60 |
new Gateway_Failed_Event(), |
| 61 |
new Bad_Request_Event(), |
| 62 |
new Default_Required_Event(), |
| 63 |
new On_Dynamic_State_Event(), |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param string $current |
| 70 |
* @param null $form_id |
| 71 |
* |
| 72 |
* @throws Action_Exception |
| 73 |
*/ |
| 74 |
public function execute( string $current, $form_id = null ) { |
| 75 |
try { |
| 76 |
$event = $this->get_event( $current ); |
| 77 |
} catch ( Repository_Exception $exception ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
$this->current = $event->get_id(); |
| 81 |
|
| 82 |
// save all form actions |
| 83 |
jet_fb_action_handler()->set_form_id( $form_id ); |
| 84 |
|
| 85 |
try { |
| 86 |
do_action( 'jet-form-builder/before-trigger-event', $event ); |
| 87 |
$event->execute(); |
| 88 |
do_action( 'jet-form-builder/after-trigger-event', $event ); |
| 89 |
} finally { |
| 90 |
$this->current = null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param string $slug |
| 96 |
* |
| 97 |
* @return Base_Event |
| 98 |
* @throws Repository_Exception |
| 99 |
*/ |
| 100 |
public function get_event( string $slug ): Base_Event { |
| 101 |
return $this->rep_get_item( $slug ); |
| 102 |
} |
| 103 |
|
| 104 |
public function get_gateways_events(): array { |
| 105 |
/** @var Base_Event[] $response */ |
| 106 |
$response = $this->rep_get_items(); |
| 107 |
|
| 108 |
foreach ( $response as $key => $event ) { |
| 109 |
if ( ! ( $event instanceof Base_Gateway_Event ) ) { |
| 110 |
unset( $response[ $key ] ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $response; |
| 115 |
} |
| 116 |
|
| 117 |
public function get_actions_events(): array { |
| 118 |
/** @var Base_Event[] $response */ |
| 119 |
$response = $this->rep_get_items(); |
| 120 |
|
| 121 |
foreach ( $response as $key => $event ) { |
| 122 |
if ( ! ( $event instanceof Base_Action_Event ) ) { |
| 123 |
unset( $response[ $key ] ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $response; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function to_array(): array { |
| 135 |
return array( |
| 136 |
'types' => Array_Tools::to_array( $this->rep_get_items() ), |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return Never_Event |
| 142 |
* @throws Repository_Exception |
| 143 |
*/ |
| 144 |
public function get_never_event(): Never_Event { |
| 145 |
return $this->rep_get_item( Never_Event::class ); |
| 146 |
} |
| 147 |
|
| 148 |
public function is_current( string $event_id ): bool { |
| 149 |
return $this->current === $event_id; |
| 150 |
} |
| 151 |
} |
| 152 |
|