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