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