lbrypress/classes/LBRY_Admin_Notice.php

62 lines
1.6 KiB
PHP
Raw Normal View History

2018-09-13 21:10:37 +02:00
<?php
/**
* Class for logging and displaying admin notices
*
* @package LBRYPress
*/
class LBRY_Admin_Notice
{
public function __construct()
{
add_action('admin_notices', array($this, 'admin_notices'));
}
/**
* Displays all messages set with the lbry_notices transient
*/
public function admin_notices()
{
if (get_transient('lbry_notices')) {
$notices = get_transient('lbry_notices');
foreach ($notices as $key => $notice) {
$this->create_admin_notice($notice);
}
delete_transient('lbry_notices');
}
}
/**
* Sets transients for admin errors
*/
// TODO: Make sure we only set one transient at a time per error
2018-09-13 21:10:37 +02:00
public function set_notice($status = 'error', $message = 'Something went wrong', $is_dismissible = false)
{
$notice = array(
'status' => $status,
'message' => $message,
'is_dismissible' => $is_dismissible
);
if (! get_transient('lbry_notices')) {
set_transient('lbry_notices', array($notice));
} else {
$notices = get_transient('lbry_notices');
$notices[] = $notice;
set_transient('lbry_notices', $notices);
}
}
/**
* Prints an admin notice
*/
private function create_admin_notice($notice)
{
$class = 'notice notice-' . $notice['status'];
if ($notice['is_dismissible']) {
$class .= ' is-dismissible';
}
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($notice['message']));
}
}