form_id = $form_id; $this->set_form_actions(); } public function add_request( $request ) { $this->request_data = $request; return $this; } /** * Set form actions, * which were saved in form meta * * @return $this */ public function set_form_actions() { $available_actions = Plugin::instance()->actions->get_actions(); $form_actions = Plugin::instance()->post_type->get_actions( $this->form_id ); foreach ( $form_actions as $form_action ) { $id = $form_action['id']; $type = $form_action['type']; $conditions = isset( $form_action['conditions'] ) ? $form_action['conditions'] : array(); if ( isset( $available_actions[ $type ] ) ) { $action = clone $available_actions[ $type ]; /** * Save action settings to the class field, * it allows to not send action settings * in action hook */ $action->_id = $id; $action->settings = $form_action['settings']; $action->conditions = $conditions; $this->form_actions[ $id ] = $action; } } return $this; } /** * Unregister notification by id * * @param $id [description] * * @return void [description] */ public function unregister_action( $key ) { if ( is_numeric( $key ) && isset( $this->form_actions[ $key ] ) ) { unset( $this->form_actions[ $key ] ); return; } foreach ( $this->form_actions as $index => $action ) { if ( $key === $action->get_id() ) { unset( $this->form_actions[ $index ] ); } } } /** * Returns all registered notifications * * @return array [description] */ public function get_all() { return $this->form_actions; } /** * Send form notifications * * @return array [type] [description] * @throws Action_Exception */ public function do_actions() { $this->before_run_actions(); $this->run_actions(); $this->after_run_actions(); return $this->response_data; } public function before_run_actions() { $callbacks = array(); if ( Plugin::instance()->allow_gateways ) { $callbacks[] = array( Gateway_Manager::instance(), Gateway_Manager::BEFORE_ACTIONS_CALLABLE ); } Tools::run_callbacks( apply_filters( 'jet-form-builder/actions/before-send/callbacks', $callbacks ), $this ); } public function after_run_actions() { $callbacks = array(); if ( Plugin::instance()->allow_gateways ) { $callbacks[] = array( Gateway_Manager::instance(), Gateway_Manager::AFTER_ACTIONS_CALLABLE ); } Tools::run_callbacks( apply_filters( 'jet-form-builder/actions/after-send/callbacks', $callbacks ), $this ); } public function run_actions() { if ( empty( $this->form_actions ) ) { throw new Action_Exception( 'failed' ); } $this->size_all = sizeof( $this->form_actions ); foreach ( $this->form_actions as $index => $action ) { $this->current_position = $index; /** * Check conditions for action */ try { $action->condition( $this ); } catch ( Condition_Exception $exception ) { continue; } /** * Process single action */ $action->do_action( $this->request_data, $this ); } } public function get_inserted_post_id( $action_id = 0 ) { if ( ! $action_id ) { return absint( $this->response_data['inserted_post_id'] ); } $action_id = absint( $action_id ); if ( empty( $this->response_data['inserted_posts'] ) ) { return absint( $this->response_data['inserted_post_id'] ); } foreach ( $this->response_data['inserted_posts'] as $posts ) { if ( $action_id === $posts['action_id'] ) { return $posts['post_id']; } } } public function add_response( $values ) { Plugin::instance()->form_handler->add_response_data( $values ); } }