| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Actions\Events; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Actions\Events_List; |
| 7 |
use Jet_Form_Builder\Actions\Events_Manager; |
| 8 |
use Jet_Form_Builder\Actions\Types\Base; |
| 9 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'WPINC' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
abstract class Base_Executor implements \ArrayAccess, \Iterator, \Countable { |
| 17 |
|
| 18 |
/** @var Base_Event */ |
| 19 |
private $event; |
| 20 |
private $action_ids = array(); |
| 21 |
private $position = 0; |
| 22 |
|
| 23 |
abstract public function is_supported(): bool; |
| 24 |
|
| 25 |
public function before_execute() { |
| 26 |
$this->validate_actions(); |
| 27 |
} |
| 28 |
|
| 29 |
public function after_execute() { |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @throws Action_Exception |
| 34 |
*/ |
| 35 |
final public function execute() { |
| 36 |
/* |
| 37 |
* Here you can set custom hooks or validate actions. |
| 38 |
* |
| 39 |
* By default all actions is executed, |
| 40 |
* even if they should run on another event |
| 41 |
*/ |
| 42 |
$this->before_execute(); |
| 43 |
|
| 44 |
// execute all actions |
| 45 |
$this->execute_actions(); |
| 46 |
|
| 47 |
$this->after_execute(); |
| 48 |
} |
| 49 |
|
| 50 |
protected function validate_actions() { |
| 51 |
$this->action_ids = array(); |
| 52 |
$actions = jet_fb_action_handler()->get_all(); |
| 53 |
|
| 54 |
foreach ( $actions as $action ) { |
| 55 |
if ( ! $this->is_valid_action( $action ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
$action->on_validate( $this ); |
| 59 |
|
| 60 |
$this->action_ids[] = $action->_id; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @throws Action_Exception |
| 66 |
*/ |
| 67 |
protected function execute_actions() { |
| 68 |
jet_fb_action_handler()->soft_run_actions( $this ); |
| 69 |
} |
| 70 |
|
| 71 |
protected function is_valid_action( Base $action ): bool { |
| 72 |
return $this->get_event()->is_valid_action( $action ); |
| 73 |
} |
| 74 |
|
| 75 |
final public function set_event( Base_Event $event ): Base_Executor { |
| 76 |
$this->event = $event; |
| 77 |
|
| 78 |
return $this; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return Base_Event |
| 83 |
*/ |
| 84 |
public function get_event(): Base_Event { |
| 85 |
return $this->event; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return Base |
| 90 |
*/ |
| 91 |
#[\ReturnTypeWillChange] |
| 92 |
public function current() { |
| 93 |
return jet_fb_action_handler()->get_action_by_id( |
| 94 |
$this->action_ids[ $this->position ] ?? 0 |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
#[\ReturnTypeWillChange] |
| 99 |
public function next() { |
| 100 |
++$this->position; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return bool|float|int|string|null |
| 105 |
*/ |
| 106 |
#[\ReturnTypeWillChange] |
| 107 |
public function key() { |
| 108 |
return $this->position; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public function valid(): bool { |
| 115 |
return isset( $this->action_ids[ $this->position ] ); |
| 116 |
} |
| 117 |
|
| 118 |
#[\ReturnTypeWillChange] |
| 119 |
public function rewind() { |
| 120 |
$this->position = 0; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param mixed $offset |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function offsetExists( $offset ): bool { |
| 129 |
return isset( jet_fb_action_handler()->form_actions[ $offset ] ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param mixed $offset |
| 134 |
* |
| 135 |
* @return mixed |
| 136 |
*/ |
| 137 |
#[\ReturnTypeWillChange] |
| 138 |
public function offsetGet( $offset ) { |
| 139 |
return jet_fb_action_handler()->get_action_by_id( $offset ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @param mixed $offset |
| 144 |
* @param mixed $value |
| 145 |
*/ |
| 146 |
#[\ReturnTypeWillChange] |
| 147 |
public function offsetSet( $offset, $value ) { |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @param mixed $offset |
| 152 |
*/ |
| 153 |
#[\ReturnTypeWillChange] |
| 154 |
public function offsetUnset( $offset ) { |
| 155 |
jet_fb_action_handler()->get_action_by_id( $offset )->unregister(); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @return int |
| 160 |
*/ |
| 161 |
public function count(): int { |
| 162 |
return count( $this->action_ids ); |
| 163 |
} |
| 164 |
} |
| 165 |
|