actions
2 years ago
action-messages-manager.php
2 years ago
builder.php
2 years ago
manager.php
2 years ago
msg-router.php
2 years ago
status-info.php
2 years ago
msg-router.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Messages; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | class Msg_Router { |
| 12 | |
| 13 | /** @var Builder */ |
| 14 | private $builder; |
| 15 | |
| 16 | /** @var Manager */ |
| 17 | private $manager; |
| 18 | |
| 19 | private $form_id; |
| 20 | private $actions; |
| 21 | |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->builder = new Builder(); |
| 25 | $this->manager = new Manager(); |
| 26 | } |
| 27 | |
| 28 | public function get_form_id() { |
| 29 | return (int) $this->form_id; |
| 30 | } |
| 31 | |
| 32 | public function set_form_id( $form_id ) { |
| 33 | if ( ! $form_id || $form_id === $this->get_form_id() ) { |
| 34 | return false; |
| 35 | } |
| 36 | $this->form_id = (int) $form_id; |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | public function set_actions( $actions ) { |
| 42 | if ( ! $actions || ! is_array( $actions ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | $this->actions = $actions; |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | public function get_actions() { |
| 51 | return (array) $this->actions; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | public function set_up( $data = false ) { |
| 56 | $this->maybe_set_form_id_from_handler( $data['form_id'] ?? false ); |
| 57 | $this->maybe_set_actions_from_handler( $data['actions'] ?? false ); |
| 58 | |
| 59 | return $this; |
| 60 | } |
| 61 | |
| 62 | public function maybe_set_form_id_from_handler( $form_id ) { |
| 63 | if ( $this->set_form_id( $form_id ) ) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | // phpcs:ignore Universal.Operators.DisallowShortTernary.Found |
| 68 | return $this->set_form_id( jet_form_builder()->form_handler->form_id ?: false ); |
| 69 | } |
| 70 | |
| 71 | public function maybe_set_actions_from_handler( $actions ) { |
| 72 | if ( $this->set_actions( $actions ) ) { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | return $this->query_actions_if_empty(); |
| 77 | } |
| 78 | |
| 79 | public function query_actions() { |
| 80 | if ( ! $this->form_id ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | $this->set_actions( |
| 85 | jet_fb_action_handler() |
| 86 | ->set_form_id( $this->form_id ) |
| 87 | ->get_all() |
| 88 | ); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | public function query_actions_if_empty() { |
| 94 | if ( $this->actions && is_array( $this->actions ) ) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return $this->query_actions(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param $data |
| 103 | * |
| 104 | * @return Builder |
| 105 | */ |
| 106 | public function get_builder( $data = array() ) { |
| 107 | $this->set_up( $data ); |
| 108 | |
| 109 | return $this->builder; |
| 110 | } |
| 111 | |
| 112 | public function get_manager( $data = array() ) { |
| 113 | $this->set_up( $data ); |
| 114 | |
| 115 | return $this->manager->set_up(); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 |