0 ) {
$key .= ":{$network_level_or_blog_id}";
} else {
$network_level_or_blog_id = get_current_blog_id();
$key .= ":{$network_level_or_blog_id}";
}
}
if ( ! isset( self::$_instances[ $key ] ) ) {
self::$_instances[ $key ] = new FS_Admin_Notice_Manager(
$id,
$title,
$module_unique_affix,
$is_network_and_blog_admins,
$network_level_or_blog_id
);
}
return self::$_instances[ $key ];
}
/**
* @param string $id
* @param string $title
* @param string $module_unique_affix
* @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
* blog admin pages.
* @param bool|int $network_level_or_blog_id
*/
protected function __construct(
$id,
$title = '',
$module_unique_affix = '',
$is_network_and_blog_admins = false,
$network_level_or_blog_id = false
) {
$this->_id = $id;
$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->_id . '_data', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
$this->_title = ! empty( $title ) ? $title : '';
$this->_module_unique_affix = $module_unique_affix;
$this->_sticky_storage = FS_Key_Value_Storage::instance( 'admin_notices', $this->_id, $network_level_or_blog_id );
if ( is_multisite() ) {
$this->_is_network_notices = ( true === $network_level_or_blog_id );
if ( is_numeric( $network_level_or_blog_id ) ) {
$this->_blog_id = $network_level_or_blog_id;
}
} else {
$this->_is_network_notices = false;
}
$is_network_admin = fs_is_network_admin();
$is_blog_admin = fs_is_blog_admin();
if ( ( $this->_is_network_notices && $is_network_admin ) ||
( ! $this->_is_network_notices && $is_blog_admin ) ||
( $is_network_and_blog_admins && ( $is_network_admin || $is_blog_admin ) )
) {
if ( 0 < count( $this->_sticky_storage ) ) {
$ajax_action_suffix = str_replace( ':', '-', $this->_id );
// If there are sticky notices for the current slug, add a callback
// to the AJAX action that handles message dismiss.
add_action( "wp_ajax_fs_dismiss_notice_action_{$ajax_action_suffix}", array(
&$this,
'dismiss_notice_ajax_callback'
) );
foreach ( $this->_sticky_storage as $msg ) {
// Add admin notice.
$this->add(
$msg['message'],
$msg['title'],
$msg['type'],
true,
$msg['id'],
false,
isset( $msg['wp_user_id'] ) ? $msg['wp_user_id'] : null,
! empty( $msg['plugin'] ) ? $msg['plugin'] : null,
$is_network_and_blog_admins,
isset( $msg['dismissible'] ) ?
$msg['dismissible'] :
null
);
}
}
}
}
/**
* Remove sticky message by ID.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.7
*
*/
function dismiss_notice_ajax_callback() {
check_admin_referer( 'fs_dismiss_notice_action' );
if ( ! is_numeric( $_POST['message_id'] ) ) {
$this->_sticky_storage->remove( $_POST['message_id'] );
}
wp_die();
}
/**
* Rendered sticky message dismiss JavaScript.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.7
*/
static function _add_sticky_dismiss_javascript() {
$params = array();
fs_require_once_template( 'sticky-admin-notice-js.php', $params );
}
private static $_added_sticky_javascript = false;
/**
* Hook to the admin_footer to add sticky message dismiss JavaScript handler.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.7
*/
private static function has_sticky_messages() {
if ( ! self::$_added_sticky_javascript ) {
add_action( 'admin_footer', array( 'FS_Admin_Notice_Manager', '_add_sticky_dismiss_javascript' ) );
}
}
/**
* Handle admin_notices by printing the admin messages stacked in the queue.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.4
*
*/
function _admin_notices_hook() {
if ( function_exists( 'current_user_can' ) &&
! current_user_can( 'manage_options' )
) {
// Only show messages to admins.
return;
}
foreach ( $this->_notices as $id => $msg ) {
if ( isset( $msg['wp_user_id'] ) && is_numeric( $msg['wp_user_id'] ) ) {
if ( get_current_user_id() != $msg['wp_user_id'] ) {
continue;
}
}
/**
* Added a filter to control the visibility of admin notices.
*
* Usage example:
*
* /**
* * @param bool $show
* * @param array $msg {
* * @var string $message The actual message.
* * @var string $title An optional message title.
* * @var string $type The type of the message ('success', 'update', 'warning', 'promotion').
* * @var string $id The unique identifier of the message.
* * @var string $manager_id The unique identifier of the notices manager. For plugins it would be the plugin's slug, for themes - `