* @copyright 2019 WPTRT * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0-or-later * @link https://github.com/WPTRT/admin-notices */ namespace WPTRT\AdminNotices; /** * The Admin_Notice class, responsible for creating admin notices. * * Each notice is a new instance of the object. * * @since 1.0.0 */ class Notice { /** * The notice-ID. * * @access private * @since 1.0 * @var string */ private $id; /** * The notice message. * * @access private * @since 1.0 * @var string */ private $message; /** * The notice title. * * @access private * @since 1.0 * @var string */ private $title; /** * An instance of the \WPTRT\AdminNotices\Dismiss object. * * @access public * @since 1.0 * @var \WPTRT\AdminNotices\Dismiss */ public $dismiss; /** * The notice arguments. * * @access private * @since 1.0 * @var array */ private $options = [ 'scope' => 'global', 'type' => 'info', 'alt_style' => false, 'capability' => 'edit_theme_options', 'option_prefix' => 'wptrt_notice_dismissed', 'screens' => [], ]; /** * Allowed HTML in the message. * * @access private * @since 1.0 * @var array */ private $allowed_html = [ 'p' => [], 'a' => [ 'href' => [], 'rel' => [], ], 'em' => [], 'strong' => [], 'br' => [], ]; /** * An array of allowed types. * * @access private * @since 1.0 * @var array */ private $allowed_types = [ 'info', 'success', 'error', 'warning', ]; /** * Constructor. * * @access public * @since 1.0 * @param string $id A unique ID for this notice. Can contain lowercase characters and underscores. * @param string $title The title for our notice. * @param string $message The message for our notice. * @param array $options An array of additional options to change the defaults for this notice. * [ * 'screens' => (array) An array of screens where the notice will be displayed. * Leave empty to always show. * Defaults to an empty array. * 'scope' => (string) Can be "global" or "user". * Determines if the dismissed status will be saved as an option or user-meta. * Defaults to "global". * 'type' => (string) Can be one of "info", "success", "warning", "error". * Defaults to "info". * 'alt_style' => (bool) Whether we want to use alt styles or not. * Defaults to false. * 'capability' => (string) The user capability required to see the notice. * Defaults to "edit_theme_options". * 'option_prefix' => (string) The prefix that will be used to build the option (or post-meta) name. * Can contain lowercase latin letters and underscores. * ]. */ public function __construct( $id, $title, $message, $options = [] ) { // Set the object properties. $this->id = $id; $this->title = $title; $this->message = $message; $this->options = wp_parse_args( $options, $this->options ); // Sanity check: Early exit if ID or message are empty. if ( ! $this->id || ! $this->message ) { return; } /** * Allow filtering the allowed HTML tags array. * * @since 1.0.2 * @param array $allowed_html The list of allowed HTML tags. * @return array */ $this->allowed_html = apply_filters( 'wptrt_admin_notices_allowed_html', $this->allowed_html ); // Instantiate the Dismiss object. $this->dismiss = new Dismiss( $this->id, $this->options['option_prefix'], $this->options['scope'] ); } /** * Prints the notice. * * @access public * @since 1.0 * @return void */ public function the_notice() { // Early exit if we don't want to show this notice. if ( ! $this->show() ) { return; } $html = $this->get_title(); $html .= $this->get_message(); // Print the notice. printf( '