| Server IP : 159.253.20.91 / Your IP : 216.73.216.111 Web Server : nginx/1.20.2 System : Linux se38c9e53.fastvps-server.com 5.10.0-13-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64 User : pr11_ru_usr ( 1041) PHP Version : 8.1.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/pr11_ru_usr/data/www/pr11.ru/wp-content/plugins/cyr2lat/src/php/ |
Upload File : |
<?php
/**
* Admin Notices.
*
* @package cyr-to-lat
*/
namespace CyrToLat;
/**
* Class AdminNotices
*
* @class AdminNotices
*/
class AdminNotices {
/**
* Admin notices array.
*
* @var array
*/
private $notices = [];
/**
* AdminNotices constructor.
*/
public function __construct() {
add_action( 'admin_notices', [ $this, 'show_notices' ] );
}
/**
* Add admin notice.
*
* @param string $message Message to show.
* @param string $class Message class: notice notice-success notice-error notice-warning notice-info
* is-dismissible.
* @param array $options Notice options.
*/
public function add_notice( string $message, string $class = 'notice', array $options = [] ) {
$this->notices[] = [
'message' => $message,
'class' => $class,
'options' => $options,
];
}
/**
* Show all notices.
*
* @return void
*/
public function show_notices() {
foreach ( $this->notices as $notice ) {
if ( ! $this->is_screen_allowed( $notice ) ) {
continue;
}
?>
<div class="<?php echo esc_attr( $notice['class'] ); ?>">
<p>
<strong>
<?php echo wp_kses_post( $notice['message'] ); ?>
</strong>
</p>
</div>
<?php
}
}
/**
* Is current admin screen allowed to show the notice.
*
* @param array $notice Notice.
*
* @return bool
*/
protected function is_screen_allowed( array $notice ): bool {
$screen_ids = isset( $notice['options']['screen_ids'] ) ? (array) $notice['options']['screen_ids'] : null;
if ( empty( $screen_ids ) ) {
return true;
}
$current_screen = get_current_screen();
foreach ( $screen_ids as $screen_id ) {
if ( $current_screen && $screen_id === $current_screen->id ) {
return true;
}
}
return false;
}
}