Moved admin notice to its own class

This commit is contained in:
Paul Kirby 2018-09-13 14:10:37 -05:00
parent ce80e287ff
commit 94f6c6eca2
3 changed files with 68 additions and 49 deletions

View file

@ -17,7 +17,6 @@ class LBRY_Admin
add_action('admin_menu', array($this, 'create_options_page'));
add_action('admin_init', array($this, 'page_init'));
add_action('admin_post_lbry_add_channel', array($this, 'add_channel'));
add_action('admin_notices', array($this, 'admin_notices'));
}
/**
@ -185,58 +184,12 @@ class LBRY_Admin
// Check that nonce
if (! isset($_POST['_lbrynonce']) || ! wp_verify_nonce($_POST['_lbrynonce'], 'lbry_add_channel')) {
$this->set_notice('error');
LBRY()->notice->set_notice('error');
} else {
$this->set_notice('success', 'Successfully added a new channel!', true);
LBRY()->notice->set_notice('success', 'Successfully added a new channel!', true);
}
wp_safe_redirect($redirect_url);
exit();
}
/**
* 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
*/
private 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']));
}
}

View file

@ -0,0 +1,60 @@
<?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
*/
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']));
}
}

View file

@ -33,6 +33,11 @@ class LBRYPress
*/
public $speech = null;
/**
* The Admin Notice object
*/
public $notice = null;
/**
* Main LBRYPress Instance.
*
@ -118,6 +123,7 @@ class LBRYPress
{
$this->daemon = new LBRY_Daemon();
$this->speech = new LBRY_Speech();
$this->notice = new LBRY_Admin_Notice();
}
/**