conditions
3 years ago
events
3 years ago
methods
3 years ago
types
3 years ago
action-handler.php
3 years ago
action-localize.php
3 years ago
actions-tools.php
3 years ago
events-list.php
3 years ago
events-manager.php
3 years ago
manager.php
3 years ago
send-email-hooks.php
3 years ago
events-manager.php
146 lines
| 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\Actions\Types\Base; |
| 17 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 18 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 19 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 20 | use Jet_Form_Builder\Classes\Repository\Repository_Dynamic_Items_It; |
| 21 | use Jet_Form_Builder\Classes\Repository\Repository_Pattern_Trait; |
| 22 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 23 | use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 24 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 25 | use MailPoet\Automation\Engine\Validation\WorkflowRules\ValidStepArgsRule; |
| 26 | |
| 27 | /** |
| 28 | * @method static Events_Manager instance() |
| 29 | * |
| 30 | * Class Events_Manager |
| 31 | * @package Jet_Form_Builder\Actions |
| 32 | */ |
| 33 | class Events_Manager implements Arrayable, Repository_Dynamic_Items_It { |
| 34 | |
| 35 | use Instance_Trait; |
| 36 | use Repository_Pattern_Trait; |
| 37 | |
| 38 | private $types = array(); |
| 39 | /** |
| 40 | * @var string|null |
| 41 | */ |
| 42 | private $current; |
| 43 | |
| 44 | public function __construct() { |
| 45 | $this->rep_install(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return array |
| 50 | */ |
| 51 | public function rep_instances(): array { |
| 52 | return apply_filters( |
| 53 | 'jet-form-builder/event-types', |
| 54 | array( |
| 55 | new Never_Event(), |
| 56 | new Default_Process_Event(), |
| 57 | new Gateway_Success_Event(), |
| 58 | new Gateway_Failed_Event(), |
| 59 | new Bad_Request_Event(), |
| 60 | new Default_Required_Event(), |
| 61 | new On_Dynamic_State_Event(), |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param string $current |
| 68 | * @param null $form_id |
| 69 | * |
| 70 | * @throws Action_Exception |
| 71 | */ |
| 72 | public function execute( string $current, $form_id = null ) { |
| 73 | try { |
| 74 | $event = $this->get_event( $current ); |
| 75 | } catch ( Repository_Exception $exception ) { |
| 76 | return; |
| 77 | } |
| 78 | $this->current = $event->get_id(); |
| 79 | |
| 80 | // save all form actions |
| 81 | jet_fb_action_handler()->set_form_id( $form_id ); |
| 82 | |
| 83 | try { |
| 84 | do_action( 'jet-form-builder/before-trigger-event', $event ); |
| 85 | $event->execute(); |
| 86 | do_action( 'jet-form-builder/after-trigger-event', $event ); |
| 87 | } finally { |
| 88 | $this->current = null; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param string $slug |
| 94 | * |
| 95 | * @return Base_Event |
| 96 | * @throws Repository_Exception |
| 97 | */ |
| 98 | public function get_event( string $slug ): Base_Event { |
| 99 | return $this->rep_get_item( $slug ); |
| 100 | } |
| 101 | |
| 102 | public function get_gateways_events(): array { |
| 103 | /** @var Base_Event[] $response */ |
| 104 | $response = $this->rep_get_items(); |
| 105 | |
| 106 | foreach ( $response as $key => $event ) { |
| 107 | if ( ! ( $event instanceof Base_Gateway_Event ) ) { |
| 108 | unset( $response[ $key ] ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $response; |
| 113 | } |
| 114 | |
| 115 | public function get_actions_events(): array { |
| 116 | /** @var Base_Event[] $response */ |
| 117 | $response = $this->rep_get_items(); |
| 118 | |
| 119 | foreach ( $response as $key => $event ) { |
| 120 | if ( ! ( $event instanceof Base_Action_Event ) ) { |
| 121 | unset( $response[ $key ] ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $response; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * @return array |
| 131 | */ |
| 132 | public function to_array(): array { |
| 133 | return array( |
| 134 | 'types' => Array_Tools::to_array( $this->rep_get_items() ), |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | public function get_never_event(): Never_Event { |
| 139 | return self::instance()->rep_get_item( Never_Event::class ); |
| 140 | } |
| 141 | |
| 142 | public function is_current( string $event_id ): bool { |
| 143 | return $this->current === $event_id; |
| 144 | } |
| 145 | } |
| 146 |