| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Utilities |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Utilities { |
| 10 |
|
| 11 |
/** |
| 12 |
* Class WPDA_Message_Box |
| 13 |
* |
| 14 |
* Displays a message box the WordPress way. |
| 15 |
* |
| 16 |
* @author Peter Schulz |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class WPDA_Message_Box { |
| 20 |
|
| 21 |
/** |
| 22 |
* Default message type |
| 23 |
*/ |
| 24 |
const DEFAULT_MESSAGE_TYPE = 'notice'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Default setting dismissible |
| 28 |
*/ |
| 29 |
const DEFAULT_MESSAGE_IS_DISMISSIBLE = true; |
| 30 |
|
| 31 |
/** |
| 32 |
* Sequence number |
| 33 |
* |
| 34 |
* Used to give message boxes a unique number within the current response |
| 35 |
* |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
protected static $message_box_seq = 0; |
| 39 |
|
| 40 |
/** |
| 41 |
* Message box number |
| 42 |
* |
| 43 |
* @var int |
| 44 |
*/ |
| 45 |
protected $message_box_no; |
| 46 |
|
| 47 |
/** |
| 48 |
* Message box text |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
protected $message_text; |
| 53 |
|
| 54 |
/** |
| 55 |
* Message type (error, notice, action) |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
protected $message_type; |
| 60 |
|
| 61 |
/** |
| 62 |
* Indicates whether message is dismissible |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
protected $message_is_dismissible; |
| 67 |
|
| 68 |
/** |
| 69 |
* Message type shown to user |
| 70 |
* |
| 71 |
* @var string |
| 72 |
*/ |
| 73 |
protected $message_hint; |
| 74 |
|
| 75 |
/** |
| 76 |
* WPDA_Message_Box constructor |
| 77 |
* |
| 78 |
* @param array $args [ |
| 79 |
* |
| 80 |
* 'message_text' => (string) Message text |
| 81 |
* |
| 82 |
* 'message_type' => (string) Message type |
| 83 |
* |
| 84 |
* 'message_is_dismissible' => (boolean) Indicates whether message box is dismissible |
| 85 |
* |
| 86 |
* ]. |
| 87 |
* @since 1.0.0 |
| 88 |
*/ |
| 89 |
public function __construct( $args = array() ) { |
| 90 |
$args = wp_parse_args( |
| 91 |
$args, |
| 92 |
array( |
| 93 |
'message_text' => '', |
| 94 |
'message_type' => self::DEFAULT_MESSAGE_TYPE, |
| 95 |
'message_is_dismissible' => self::DEFAULT_MESSAGE_IS_DISMISSIBLE, |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
if ( '' === $args['message_text'] ) { |
| 100 |
wp_die( __( 'ERROR: Wrong arguments [missing message text argument]', 'wp-data-access' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
$this->message_text = $args['message_text']; |
| 104 |
$this->message_type = $args['message_type']; |
| 105 |
$this->message_is_dismissible = true === $args['message_is_dismissible'] ? 'is-dismissible' : ''; |
| 106 |
switch ( $this->message_type ) { |
| 107 |
case 'error': |
| 108 |
$this->message_hint = 'ERROR'; |
| 109 |
break; |
| 110 |
case 'action': |
| 111 |
$this->message_hint = 'ACTION'; |
| 112 |
$this->message_type = 'notice'; |
| 113 |
break; |
| 114 |
default: |
| 115 |
$this->message_hint = 'INFO'; |
| 116 |
} |
| 117 |
|
| 118 |
$this->message_box_no = ++ self::$message_box_seq; // Give message box a unique number. |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Show message box |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
public function box() { |
| 127 |
$msgbox_id = 'message_box_' . esc_attr( $this->message_box_no ); |
| 128 |
global $allowedposttags; |
| 129 |
?> |
| 130 |
<div id='<?php echo esc_attr( $msgbox_id ); ?>' |
| 131 |
class='updated published <?php echo esc_attr( $this->message_type ); ?> <?php echo esc_attr( $this->message_is_dismissible ); ?>'> |
| 132 |
<p> |
| 133 |
<?php echo esc_attr( $this->message_hint ); ?>: <?php echo wp_kses( $this->message_text, $allowedposttags ); ?> |
| 134 |
</p> |
| 135 |
</div> |
| 136 |
<?php |
| 137 |
} |
| 138 |
|
| 139 |
public function custom_box( $content ) { |
| 140 |
$msgbox_id = 'message_box_' . esc_attr( $this->message_box_no ); |
| 141 |
?> |
| 142 |
<div id='<?php echo esc_attr( $msgbox_id ); ?>' |
| 143 |
class='updated published <?php echo esc_attr( $this->message_type ); ?> <?php echo esc_attr( $this->message_is_dismissible ); ?>'> |
| 144 |
<p> |
| 145 |
<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 146 |
</p> |
| 147 |
</div> |
| 148 |
<?php |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|