lbrypress/classes/LBRY_Admin_Notice.php
2020-02-17 19:13:55 -06:00

72 lines
1.8 KiB
PHP

<?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
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');
if (!in_array($notice, $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';
}
ob_start();
?>
<div class="<?= $class ?>">
<p>
<span style="font-weight:bold">LBRYPress: </span>
<?= $notice['message'] ?>
</p>
</div>
<?php
echo ob_get_clean();
}
}