types
4 years ago
action-handler.php
4 years ago
action-localize.php
4 years ago
manager.php
4 years ago
action-handler.php
225 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Actions; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Actions\Types\Base; |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Condition_Exception; |
| 10 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 11 | use Jet_Form_Builder\Gateways\Gateway_Manager; |
| 12 | use Jet_Form_Builder\Plugin; |
| 13 | |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Define Actions handler class class |
| 20 | */ |
| 21 | class Action_Handler { |
| 22 | |
| 23 | public $form_id = null; |
| 24 | public $request_data = null; |
| 25 | public $manager = null; |
| 26 | |
| 27 | |
| 28 | public $form_actions = array(); |
| 29 | public $is_ajax = false; |
| 30 | |
| 31 | /** |
| 32 | * Data for actions |
| 33 | */ |
| 34 | public $size_all; |
| 35 | public $current_position; |
| 36 | public $response_data = array(); |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Constructor for the class |
| 41 | * |
| 42 | * @param $form_id |
| 43 | */ |
| 44 | public function __construct( $form_id ) { |
| 45 | $this->form_id = $form_id; |
| 46 | $this->set_form_actions(); |
| 47 | } |
| 48 | |
| 49 | public function add_request( $request ) { |
| 50 | $this->request_data = $request; |
| 51 | |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set form actions, |
| 57 | * which were saved in form meta |
| 58 | * |
| 59 | * @return $this |
| 60 | */ |
| 61 | public function set_form_actions() { |
| 62 | $available_actions = Plugin::instance()->actions->get_actions(); |
| 63 | $form_actions = Plugin::instance()->post_type->get_actions( $this->form_id ); |
| 64 | |
| 65 | foreach ( $form_actions as $form_action ) { |
| 66 | $id = $form_action['id']; |
| 67 | $type = $form_action['type']; |
| 68 | $conditions = isset( $form_action['conditions'] ) ? $form_action['conditions'] : array(); |
| 69 | |
| 70 | if ( isset( $available_actions[ $type ] ) ) { |
| 71 | $action = clone $available_actions[ $type ]; |
| 72 | /** |
| 73 | * Save action settings to the class field, |
| 74 | * it allows to not send action settings |
| 75 | * in action hook |
| 76 | */ |
| 77 | $action->_id = $id; |
| 78 | $action->conditions = $conditions; |
| 79 | $action->settings = isset( $form_action['settings'][ $type ] ) |
| 80 | ? $form_action['settings'][ $type ] : $form_action['settings']; |
| 81 | |
| 82 | $this->form_actions[ $id ] = $action; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Unregister notification by id |
| 92 | * |
| 93 | * @param $id [description] |
| 94 | * |
| 95 | * @return void [description] |
| 96 | */ |
| 97 | public function unregister_action( $key ) { |
| 98 | if ( is_numeric( $key ) && isset( $this->form_actions[ $key ] ) ) { |
| 99 | unset( $this->form_actions[ $key ] ); |
| 100 | |
| 101 | return; |
| 102 | } |
| 103 | foreach ( $this->form_actions as $index => $action ) { |
| 104 | if ( $key === $action->get_id() ) { |
| 105 | unset( $this->form_actions[ $index ] ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns all registered notifications |
| 112 | * |
| 113 | * @return Base[] [description] |
| 114 | */ |
| 115 | public function get_all() { |
| 116 | return $this->form_actions; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Send form notifications |
| 121 | * |
| 122 | * @return array [type] [description] |
| 123 | * @throws Action_Exception |
| 124 | */ |
| 125 | public function do_actions() { |
| 126 | |
| 127 | $this->before_run_actions(); |
| 128 | $this->run_actions(); |
| 129 | $this->after_run_actions(); |
| 130 | |
| 131 | return $this->response_data; |
| 132 | } |
| 133 | |
| 134 | public function before_run_actions() { |
| 135 | $callbacks = array(); |
| 136 | |
| 137 | if ( Plugin::instance()->allow_gateways ) { |
| 138 | $callbacks[] = array( |
| 139 | Gateway_Manager::instance(), |
| 140 | Gateway_Manager::BEFORE_ACTIONS_CALLABLE |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | Tools::run_callbacks( |
| 145 | apply_filters( 'jet-form-builder/actions/before-send/callbacks', $callbacks ), |
| 146 | $this |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | public function after_run_actions() { |
| 151 | $callbacks = array(); |
| 152 | |
| 153 | if ( Plugin::instance()->allow_gateways ) { |
| 154 | $callbacks[] = array( |
| 155 | Gateway_Manager::instance(), |
| 156 | Gateway_Manager::AFTER_ACTIONS_CALLABLE |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | Tools::run_callbacks( |
| 161 | apply_filters( 'jet-form-builder/actions/after-send/callbacks', $callbacks ), |
| 162 | $this |
| 163 | ); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | public function run_actions() { |
| 168 | |
| 169 | if ( empty( $this->form_actions ) ) { |
| 170 | throw new Action_Exception( 'failed' ); |
| 171 | } |
| 172 | |
| 173 | $this->size_all = sizeof( $this->form_actions ); |
| 174 | |
| 175 | foreach ( $this->form_actions as $index => $action ) { |
| 176 | |
| 177 | $this->current_position = $index; |
| 178 | |
| 179 | /** |
| 180 | * Check conditions for action |
| 181 | * |
| 182 | * @var Base $action |
| 183 | */ |
| 184 | try { |
| 185 | $action->condition( $this ); |
| 186 | } catch ( Condition_Exception $exception ) { |
| 187 | continue; |
| 188 | } |
| 189 | /** |
| 190 | * Process single action |
| 191 | */ |
| 192 | $action->do_action( $this->request_data, $this ); |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | public function get_inserted_post_id( $action_id = 0 ) { |
| 198 | $default_post_id = absint( $this->response_data['inserted_post_id'] ); |
| 199 | |
| 200 | if ( ! $action_id ) { |
| 201 | return $default_post_id; |
| 202 | } |
| 203 | |
| 204 | $action_id = absint( $action_id ); |
| 205 | |
| 206 | if ( empty( $this->response_data['inserted_posts'] ) ) { |
| 207 | return $default_post_id; |
| 208 | } |
| 209 | |
| 210 | foreach ( $this->response_data['inserted_posts'] as $posts ) { |
| 211 | if ( $action_id === $posts['action_id'] ) { |
| 212 | return $posts['post_id']; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $default_post_id; |
| 217 | } |
| 218 | |
| 219 | public function add_response( $values ) { |
| 220 | Plugin::instance()->form_handler->add_response_data( $values ); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | } |
| 225 |