builder.php
3 years ago
manager.php
3 years ago
msg-router.php
3 years ago
status-info.php
3 years ago
manager.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Messages; |
| 5 | |
| 6 | use Jet_Form_Builder\Plugin; |
| 7 | |
| 8 | class Manager { |
| 9 | |
| 10 | public $_types = array(); |
| 11 | |
| 12 | const DYNAMIC_SUCCESS_PREF = 'dsuccess|'; |
| 13 | const DYNAMIC_FAILED_PREF = 'derror|'; |
| 14 | |
| 15 | |
| 16 | public static function dynamic_success( $message ) { |
| 17 | return self::DYNAMIC_SUCCESS_PREF . $message; |
| 18 | } |
| 19 | |
| 20 | public static function dynamic_error( $message ) { |
| 21 | return self::DYNAMIC_FAILED_PREF . $message; |
| 22 | } |
| 23 | |
| 24 | public static function dynamic_types() { |
| 25 | return array( |
| 26 | 'dsuccess' => array( |
| 27 | 'type' => 'success', |
| 28 | ), |
| 29 | 'derror' => array( |
| 30 | 'type' => 'failed', |
| 31 | ), |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return Manager |
| 37 | */ |
| 38 | public function set_up() { |
| 39 | if ( ! jet_form_builder()->msg_router->get_form_id() || $this->get_messages() ) { |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | $this->_types = array_merge( |
| 44 | $this->get_form_types_messages(), |
| 45 | $this->get_action_types_messages() |
| 46 | ); |
| 47 | |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | public function get_form_types_messages() { |
| 52 | return Plugin::instance()->post_type->get_messages( |
| 53 | jet_form_builder()->msg_router->get_form_id() |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function get_action_types_messages() { |
| 58 | $messages = array(); |
| 59 | |
| 60 | foreach ( jet_form_builder()->msg_router->get_actions() as $action ) { |
| 61 | if ( ! isset( $action->messages ) ) { |
| 62 | continue; |
| 63 | } |
| 64 | $default_action_messages = wp_list_pluck( $action->messages, 'value' ); |
| 65 | $action_messages = isset( $action->settings['messages'] ) ? $action->settings['messages'] : array(); |
| 66 | |
| 67 | $messages = array_merge( $messages, $default_action_messages, $action_messages ); |
| 68 | } |
| 69 | |
| 70 | return $messages; |
| 71 | } |
| 72 | |
| 73 | public function isset_message_type( $type ) { |
| 74 | return isset( $this->_types[ $type ] ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | public function get_message( $type ): string { |
| 79 | $info = new Status_Info( $type ); |
| 80 | |
| 81 | /** |
| 82 | * Return dynamic message |
| 83 | */ |
| 84 | return $this->get_message_by_info( $info ); |
| 85 | } |
| 86 | |
| 87 | public function get_message_by_info( Status_Info $info ): string { |
| 88 | if ( $info->is_dynamic() ) { |
| 89 | return $info->get_message(); |
| 90 | } |
| 91 | |
| 92 | $status = $info->get_message(); |
| 93 | |
| 94 | if ( $this->isset_message_type( $status ) ) { |
| 95 | return $this->_types[ $status ]['value'] ?? $this->_types[ $status ]; |
| 96 | } |
| 97 | |
| 98 | return $info->get_raw_message(); |
| 99 | } |
| 100 | |
| 101 | public function get_messages() { |
| 102 | return $this->_types; |
| 103 | } |
| 104 | |
| 105 | public static function parse_message( $status ) { |
| 106 | return explode( '|', $status ); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 |