| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder\Actions; |
| 4 |
|
| 5 |
// If this file is called directly, abort. |
| 6 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 7 |
use Jet_Form_Builder\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Define Actions handler class class |
| 15 |
*/ |
| 16 |
class Action_Handler { |
| 17 |
|
| 18 |
public $form_id = null; |
| 19 |
public $request_data = null; |
| 20 |
public $manager = null; |
| 21 |
|
| 22 |
|
| 23 |
public $form_actions = array(); |
| 24 |
public $is_ajax = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Data for actions |
| 28 |
*/ |
| 29 |
public $size_all; |
| 30 |
public $current_position; |
| 31 |
public $response_data = array(); |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor for the class |
| 36 |
* |
| 37 |
* @param $form_id |
| 38 |
* @param array $request_data |
| 39 |
*/ |
| 40 |
function __construct( $form_id, $request_data = array() ) { |
| 41 |
|
| 42 |
$this->form_id = $form_id; |
| 43 |
$this->request_data = $request_data; |
| 44 |
|
| 45 |
$this->set_form_actions(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set form actions, |
| 50 |
* which were saved in form meta |
| 51 |
* |
| 52 |
* @return $this |
| 53 |
*/ |
| 54 |
public function set_form_actions() { |
| 55 |
$available_actions = Plugin::instance()->actions->get_actions(); |
| 56 |
|
| 57 |
$form_actions = Plugin::instance()->post_type->get_actions( $this->form_id ); |
| 58 |
|
| 59 |
foreach ( $form_actions as $form_action ) { |
| 60 |
$id = $form_action['type']; |
| 61 |
|
| 62 |
if ( isset( $available_actions[ $id ] ) ) { |
| 63 |
/** |
| 64 |
* Save action settings to the class field, |
| 65 |
* it allows to not send action settings |
| 66 |
* in action hook |
| 67 |
*/ |
| 68 |
$available_actions[ $id ]->settings = $form_action['settings']; |
| 69 |
|
| 70 |
$this->form_actions[ $id ] = $available_actions[ $id ]; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Unregister notification by id |
| 80 |
* |
| 81 |
* @param $id [description] |
| 82 |
* |
| 83 |
* @return void [description] |
| 84 |
*/ |
| 85 |
public function unregister_action( $id ) { |
| 86 |
|
| 87 |
if ( isset( $this->form_actions[ $id ] ) ) { |
| 88 |
unset( $this->form_actions[ $id ] ); |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns all registered notifications |
| 95 |
* |
| 96 |
* @return array [description] |
| 97 |
*/ |
| 98 |
public function get_all() { |
| 99 |
return $this->form_actions; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Send form notifications |
| 104 |
* |
| 105 |
* @return array [type] [description] |
| 106 |
*/ |
| 107 |
public function do_actions() { |
| 108 |
|
| 109 |
if ( empty( $this->form_actions ) ) { |
| 110 |
throw new Action_Exception( 'failed' ); |
| 111 |
} |
| 112 |
|
| 113 |
$this->size_all = sizeof( $this->form_actions ); |
| 114 |
|
| 115 |
do_action( 'jet-form-builder/actions/before-send', $this ); |
| 116 |
|
| 117 |
foreach ( $this->form_actions as $index => $action ) { |
| 118 |
|
| 119 |
$this->current_position = $index; |
| 120 |
/** |
| 121 |
* Process single action |
| 122 |
*/ |
| 123 |
$action->do_action( $this->request_data, $this ); |
| 124 |
} |
| 125 |
|
| 126 |
do_action( 'jet-form-builder/actions/after-send', $this ); |
| 127 |
|
| 128 |
return $this->response_data; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
|