| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Notices |
| 4 |
* - add notices via static method or filter. |
| 5 |
* |
| 6 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 7 |
* |
| 8 |
* @see http://wcpos.com |
| 9 |
* @package WCPOS\WooCommercePOS |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WCPOS\WooCommercePOS\Admin; |
| 13 |
|
| 14 |
/** |
| 15 |
* Notices class. |
| 16 |
*/ |
| 17 |
class Notices { |
| 18 |
/** |
| 19 |
* Stored notices. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private static $notices = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add a message for display. |
| 34 |
* |
| 35 |
* @param string $message The notice message. |
| 36 |
* @param string $type The notice type. |
| 37 |
* @param bool $dismissable Whether the notice is dismissable. |
| 38 |
*/ |
| 39 |
public static function add( $message = '', $type = 'error', $dismissable = true ): void { |
| 40 |
self::$notices[] = array( |
| 41 |
'type' => $type, |
| 42 |
'message' => $message, |
| 43 |
'dismissable' => $dismissable, |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Display the admin notices. |
| 49 |
*/ |
| 50 |
public function admin_notices(): void { |
| 51 |
/** |
| 52 |
* Filters the POS admin notices. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* |
| 56 |
* @param array $notices |
| 57 |
* |
| 58 |
* @return array $notices |
| 59 |
* |
| 60 |
* @hook woocommerce_pos_admin_notices |
| 61 |
*/ |
| 62 |
$notices = apply_filters( 'woocommerce_pos_admin_notices', self::$notices ); |
| 63 |
if ( empty( $notices ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $notices as $notice ) { |
| 68 |
$classes = 'notice notice-' . $notice['type']; |
| 69 |
if ( $notice['dismissable'] ) { |
| 70 |
$classes .= ' is-dismissable'; |
| 71 |
} |
| 72 |
if ( $notice['message'] ) { |
| 73 |
echo '<div class="' . esc_attr( $classes ) . '"><p>' . |
| 74 |
wp_kses( $notice['message'], wp_kses_allowed_html( 'post' ) ) . |
| 75 |
'</p></div>'; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|