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