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
manager.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Messages; |
| 5 | |
| 6 | use Jet_Form_Builder\Actions\Types\Base; |
| 7 | use Jet_Form_Builder\Plugin; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Manager { |
| 15 | |
| 16 | // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 17 | public $_types = array(); |
| 18 | |
| 19 | const DYNAMIC_SUCCESS_PREF = 'dsuccess|'; |
| 20 | const DYNAMIC_FAILED_PREF = 'derror|'; |
| 21 | |
| 22 | /** |
| 23 | * @return Manager |
| 24 | */ |
| 25 | public function set_up() { |
| 26 | if ( ! jet_form_builder()->msg_router->get_form_id() || $this->get_messages() ) { |
| 27 | return $this; |
| 28 | } |
| 29 | |
| 30 | $this->_types = array_merge( |
| 31 | $this->get_form_types_messages(), |
| 32 | $this->get_action_types_messages() |
| 33 | ); |
| 34 | |
| 35 | return $this; |
| 36 | } |
| 37 | |
| 38 | public function get_form_types_messages() { |
| 39 | return Plugin::instance()->post_type->get_messages( |
| 40 | jet_form_builder()->msg_router->get_form_id() |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function get_action_types_messages() { |
| 45 | $messages = array(); |
| 46 | |
| 47 | /** @var Base $action */ |
| 48 | foreach ( jet_form_builder()->msg_router->get_actions() as $action ) { |
| 49 | $messages = array_merge( $messages, Action_Messages_Manager::instance()->get_messages_values( $action ) ); |
| 50 | } |
| 51 | |
| 52 | return $messages; |
| 53 | } |
| 54 | |
| 55 | public function isset_message_type( $type ) { |
| 56 | return isset( $this->_types[ $type ] ); |
| 57 | } |
| 58 | |
| 59 | public function get_message( $type ): string { |
| 60 | $info = new Status_Info( $type ); |
| 61 | |
| 62 | /** |
| 63 | * Return dynamic message |
| 64 | */ |
| 65 | return $this->get_message_by_info( $info ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | public function get_message_by_info( Status_Info $info ): string { |
| 70 | if ( $info->is_dynamic() ) { |
| 71 | return $info->get_message(); |
| 72 | } |
| 73 | |
| 74 | $status = $info->get_message(); |
| 75 | |
| 76 | if ( $this->isset_message_type( $status ) ) { |
| 77 | return $this->_types[ $status ]['value'] ?? $this->_types[ $status ]; |
| 78 | } |
| 79 | |
| 80 | return $info->get_raw_message(); |
| 81 | } |
| 82 | |
| 83 | public static function dynamic_success( $message ) { |
| 84 | return self::DYNAMIC_SUCCESS_PREF . $message; |
| 85 | } |
| 86 | |
| 87 | public static function dynamic_error( $message ) { |
| 88 | return self::DYNAMIC_FAILED_PREF . $message; |
| 89 | } |
| 90 | |
| 91 | public static function dynamic_types() { |
| 92 | return array( |
| 93 | 'dsuccess' => array( |
| 94 | 'type' => 'success', |
| 95 | ), |
| 96 | 'derror' => array( |
| 97 | 'type' => 'failed', |
| 98 | ), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | public function get_messages() { |
| 103 | return $this->_types; |
| 104 | } |
| 105 | |
| 106 | public static function parse_message( $status ) { |
| 107 | return explode( '|', $status ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @since 3.1.0 |
| 112 | * |
| 113 | * @param string $message |
| 114 | */ |
| 115 | public function set_success( string $message ) { |
| 116 | $this->set_message( 'success', $message ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @since 3.1.0 |
| 121 | * |
| 122 | * @param string $type |
| 123 | * @param string $message |
| 124 | */ |
| 125 | public function set_message( string $type, string $message ) { |
| 126 | $this->_types[ $type ] = $message; |
| 127 | } |
| 128 | |
| 129 | } |
| 130 |