buttonizer-multifunctional-button
/
freemius
/
includes
/
managers
/
class-fs-admin-notice-manager.php
class-fs-admin-notice-manager.php in Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder 1.5, at freemius/includes/managers/class-fs-admin-notice-manager.php
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 | * @since 1.0.7 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class FS_Admin_Notice_Manager { |
| 14 | /** |
| 15 | * @since 1.2.2 |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $_module_unique_affix; |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $_id; |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $_title; |
| 28 | /** |
| 29 | * @var array[string]array |
| 30 | */ |
| 31 | private $_notices = array(); |
| 32 | /** |
| 33 | * @var FS_Key_Value_Storage |
| 34 | */ |
| 35 | private $_sticky_storage; |
| 36 | /** |
| 37 | * @var FS_Logger |
| 38 | */ |
| 39 | protected $_logger; |
| 40 | /** |
| 41 | * @since 2.0.0 |
| 42 | * @var int The ID of the blog that is associated with the current site level admin notices. |
| 43 | */ |
| 44 | private $_blog_id = 0; |
| 45 | /** |
| 46 | * @since 2.0.0 |
| 47 | * @var bool |
| 48 | */ |
| 49 | private $_is_network_notices; |
| 50 | |
| 51 | /** |
| 52 | * @var FS_Admin_Notice_Manager[] |
| 53 | */ |
| 54 | private static $_instances = array(); |
| 55 | |
| 56 | /** |
| 57 | * @param string $id |
| 58 | * @param string $title |
| 59 | * @param string $module_unique_affix |
| 60 | * @param bool $network_level_or_blog_id Since 2.0.0 |
| 61 | * |
| 62 | * @return \FS_Admin_Notice_Manager |
| 63 | */ |
| 64 | static function instance( |
| 65 | $id, |
| 66 | $title = '', |
| 67 | $module_unique_affix = '', |
| 68 | $network_level_or_blog_id = false |
| 69 | ) { |
| 70 | $key = strtolower( $id ); |
| 71 | |
| 72 | if ( is_multisite() ) { |
| 73 | if ( true === $network_level_or_blog_id ) { |
| 74 | $key .= ':ms'; |
| 75 | } else if ( is_numeric( $network_level_or_blog_id ) && $network_level_or_blog_id > 0 ) { |
| 76 | $key .= ":{$network_level_or_blog_id}"; |
| 77 | } else { |
| 78 | $network_level_or_blog_id = get_current_blog_id(); |
| 79 | |
| 80 | $key .= ":{$network_level_or_blog_id}"; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if ( ! isset( self::$_instances[ $key ] ) ) { |
| 85 | self::$_instances[ $key ] = new FS_Admin_Notice_Manager( |
| 86 | $id, |
| 87 | $title, |
| 88 | $module_unique_affix, |
| 89 | $network_level_or_blog_id |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | return self::$_instances[ $key ]; |
| 94 | } |
| 95 | |
| 96 | protected function __construct( |
| 97 | $id, |
| 98 | $title = '', |
| 99 | $module_unique_affix = '', |
| 100 | $network_level_or_blog_id = false |
| 101 | ) { |
| 102 | $this->_id = $id; |
| 103 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->_id . '_data', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 104 | $this->_title = ! empty( $title ) ? $title : ''; |
| 105 | $this->_module_unique_affix = $module_unique_affix; |
| 106 | $this->_sticky_storage = FS_Key_Value_Storage::instance( 'admin_notices', $this->_id, $network_level_or_blog_id ); |
| 107 | |
| 108 | if ( is_multisite() ) { |
| 109 | $this->_is_network_notices = ( true === $network_level_or_blog_id ); |
| 110 | |
| 111 | if ( is_numeric( $network_level_or_blog_id ) ) { |
| 112 | $this->_blog_id = $network_level_or_blog_id; |
| 113 | } |
| 114 | } else { |
| 115 | $this->_is_network_notices = false; |
| 116 | } |
| 117 | |
| 118 | if ( ( $this->_is_network_notices && fs_is_network_admin() ) || |
| 119 | ( ! $this->_is_network_notices && fs_is_blog_admin() ) |
| 120 | ) { |
| 121 | if ( 0 < count( $this->_sticky_storage ) ) { |
| 122 | $ajax_action_suffix = str_replace( ':', '-', $this->_id ); |
| 123 | |
| 124 | // If there are sticky notices for the current slug, add a callback |
| 125 | // to the AJAX action that handles message dismiss. |
| 126 | add_action( "wp_ajax_fs_dismiss_notice_action_{$ajax_action_suffix}", array( |
| 127 | &$this, |
| 128 | 'dismiss_notice_ajax_callback' |
| 129 | ) ); |
| 130 | |
| 131 | foreach ( $this->_sticky_storage as $msg ) { |
| 132 | // Add admin notice. |
| 133 | $this->add( |
| 134 | $msg['message'], |
| 135 | $msg['title'], |
| 136 | $msg['type'], |
| 137 | true, |
| 138 | $msg['id'], |
| 139 | false |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Remove sticky message by ID. |
| 148 | * |
| 149 | * @author Vova Feldman (@svovaf) |
| 150 | * @since 1.0.7 |
| 151 | * |
| 152 | */ |
| 153 | function dismiss_notice_ajax_callback() { |
| 154 | $this->_sticky_storage->remove( $_POST['message_id'] ); |
| 155 | wp_die(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Rendered sticky message dismiss JavaScript. |
| 160 | * |
| 161 | * @author Vova Feldman (@svovaf) |
| 162 | * @since 1.0.7 |
| 163 | */ |
| 164 | static function _add_sticky_dismiss_javascript() { |
| 165 | $params = array(); |
| 166 | fs_require_once_template( 'sticky-admin-notice-js.php', $params ); |
| 167 | } |
| 168 | |
| 169 | private static $_added_sticky_javascript = false; |
| 170 | |
| 171 | /** |
| 172 | * Hook to the admin_footer to add sticky message dismiss JavaScript handler. |
| 173 | * |
| 174 | * @author Vova Feldman (@svovaf) |
| 175 | * @since 1.0.7 |
| 176 | */ |
| 177 | private static function has_sticky_messages() { |
| 178 | if ( ! self::$_added_sticky_javascript ) { |
| 179 | add_action( 'admin_footer', array( 'FS_Admin_Notice_Manager', '_add_sticky_dismiss_javascript' ) ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Handle admin_notices by printing the admin messages stacked in the queue. |
| 185 | * |
| 186 | * @author Vova Feldman (@svovaf) |
| 187 | * @since 1.0.4 |
| 188 | * |
| 189 | */ |
| 190 | function _admin_notices_hook() { |
| 191 | if ( function_exists( 'current_user_can' ) && |
| 192 | ! current_user_can( 'manage_options' ) |
| 193 | ) { |
| 194 | // Only show messages to admins. |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | foreach ( $this->_notices as $id => $msg ) { |
| 199 | fs_require_template( 'admin-notice.php', $msg ); |
| 200 | |
| 201 | if ( $msg['sticky'] ) { |
| 202 | self::has_sticky_messages(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Enqueue common stylesheet to style admin notice. |
| 209 | * |
| 210 | * @author Vova Feldman (@svovaf) |
| 211 | * @since 1.0.7 |
| 212 | */ |
| 213 | function _enqueue_styles() { |
| 214 | fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. |
| 219 | * |
| 220 | * @author Vova Feldman (@svovaf) |
| 221 | * @since 1.0.4 |
| 222 | * |
| 223 | * @param string $message |
| 224 | * @param string $title |
| 225 | * @param string $type |
| 226 | * @param bool $is_sticky |
| 227 | * @param string $id Message ID |
| 228 | * @param bool $store_if_sticky |
| 229 | * |
| 230 | * @uses add_action() |
| 231 | */ |
| 232 | function add( $message, $title = '', $type = 'success', $is_sticky = false, $id = '', $store_if_sticky = true ) { |
| 233 | $notices_type = $this->get_notices_type(); |
| 234 | |
| 235 | if ( empty( $this->_notices ) ) { |
| 236 | add_action( $notices_type, array( &$this, "_admin_notices_hook" ) ); |
| 237 | add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_styles' ) ); |
| 238 | } |
| 239 | |
| 240 | if ( '' === $id ) { |
| 241 | $id = md5( $title . ' ' . $message . ' ' . $type ); |
| 242 | } |
| 243 | |
| 244 | $message_object = array( |
| 245 | 'message' => $message, |
| 246 | 'title' => $title, |
| 247 | 'type' => $type, |
| 248 | 'sticky' => $is_sticky, |
| 249 | 'id' => $id, |
| 250 | 'manager_id' => $this->_id, |
| 251 | 'plugin' => $this->_title, |
| 252 | ); |
| 253 | |
| 254 | if ( $is_sticky && $store_if_sticky ) { |
| 255 | $this->_sticky_storage->{$id} = $message_object; |
| 256 | } |
| 257 | |
| 258 | $this->_notices[ $id ] = $message_object; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @author Vova Feldman (@svovaf) |
| 263 | * @since 1.0.7 |
| 264 | * |
| 265 | * @param string|string[] $ids |
| 266 | */ |
| 267 | function remove_sticky( $ids ) { |
| 268 | if ( ! is_array( $ids ) ) { |
| 269 | $ids = array( $ids ); |
| 270 | } |
| 271 | |
| 272 | foreach ( $ids as $id ) { |
| 273 | // Remove from sticky storage. |
| 274 | $this->_sticky_storage->remove( $id ); |
| 275 | |
| 276 | if ( isset( $this->_notices[ $id ] ) ) { |
| 277 | unset( $this->_notices[ $id ] ); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Check if sticky message exists by id. |
| 284 | * |
| 285 | * @author Vova Feldman (@svovaf) |
| 286 | * @since 1.0.9 |
| 287 | * |
| 288 | * @param $id |
| 289 | * |
| 290 | * @return bool |
| 291 | */ |
| 292 | function has_sticky( $id ) { |
| 293 | return isset( $this->_sticky_storage[ $id ] ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Adds sticky admin notification. |
| 298 | * |
| 299 | * @author Vova Feldman (@svovaf) |
| 300 | * @since 1.0.7 |
| 301 | * |
| 302 | * @param string $message |
| 303 | * @param string $id Message ID |
| 304 | * @param string $title |
| 305 | * @param string $type |
| 306 | */ |
| 307 | function add_sticky( $message, $id, $title = '', $type = 'success' ) { |
| 308 | if ( ! empty( $this->_module_unique_affix ) ) { |
| 309 | $message = fs_apply_filter( $this->_module_unique_affix, "sticky_message_{$id}", $message ); |
| 310 | $title = fs_apply_filter( $this->_module_unique_affix, "sticky_title_{$id}", $title ); |
| 311 | } |
| 312 | |
| 313 | $this->add( $message, $title, $type, true, $id ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Clear all sticky messages. |
| 318 | * |
| 319 | * @author Vova Feldman (@svovaf) |
| 320 | * @since 1.0.8 |
| 321 | */ |
| 322 | function clear_all_sticky() { |
| 323 | $this->_sticky_storage->clear_all(); |
| 324 | } |
| 325 | |
| 326 | #-------------------------------------------------------------------------------- |
| 327 | #region Helper Method |
| 328 | #-------------------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * @author Vova Feldman (@svovaf) |
| 332 | * @since 2.0.0 |
| 333 | * |
| 334 | * @return string |
| 335 | */ |
| 336 | private function get_notices_type() { |
| 337 | return $this->_is_network_notices ? |
| 338 | 'network_admin_notices' : |
| 339 | 'admin_notices'; |
| 340 | } |
| 341 | |
| 342 | #endregion |
| 343 | } |