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