lbrypress/classes/LBRY_Admin_Notice.php

73 lines
1.8 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');
2020-02-18 02:13:55 +01:00
if (!in_array($notice, $notices)) {
$notices[] = $notice;
}
2018-09-13 21:10:37 +02:00
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';
}
2020-02-18 02:13:55 +01:00
ob_start();
?>
<div class="<?= $class ?>">
<p>
<span style="font-weight:bold">LBRYPress: </span>
<?= $notice['message'] ?>
</p>
</div>
<?php
echo ob_get_clean();
2018-09-13 21:10:37 +02:00
}
}