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