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.4.4, 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[] |
| 30 | */ |
| 31 | private $_admin_messages = array(); |
| 32 | /** |
| 33 | * @var FS_Key_Value_Storage |
| 34 | */ |
| 35 | private $_sticky_storage; |
| 36 | /** |
| 37 | * @var FS_Plugin_Manager[] |
| 38 | */ |
| 39 | private static $_instances = array(); |
| 40 | /** |
| 41 | * @var FS_Logger |
| 42 | */ |
| 43 | protected $_logger; |
| 44 | |
| 45 | /** |
| 46 | * @param string $id |
| 47 | * @param string $title |
| 48 | * @param string $module_unique_affix |
| 49 | * |
| 50 | * @return FS_Admin_Notice_Manager |
| 51 | */ |
| 52 | static function instance( $id, $title = '', $module_unique_affix = '' ) { |
| 53 | if ( ! isset( self::$_instances[ $id ] ) ) { |
| 54 | self::$_instances[ $id ] = new FS_Admin_Notice_Manager( $id, $title, $module_unique_affix ); |
| 55 | } |
| 56 | |
| 57 | return self::$_instances[ $id ]; |
| 58 | } |
| 59 | |
| 60 | protected function __construct( $id, $title = '', $module_unique_affix = '' ) { |
| 61 | $this->_id = $id; |
| 62 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->_id . '_data', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 63 | $this->_title = ! empty( $title ) ? $title : ''; |
| 64 | $this->_module_unique_affix = $module_unique_affix; |
| 65 | $this->_sticky_storage = FS_Key_Value_Storage::instance( 'admin_notices', $this->_id ); |
| 66 | |
| 67 | if ( is_admin() ) { |
| 68 | if ( 0 < count( $this->_sticky_storage ) ) { |
| 69 | $ajax_action_suffix = str_replace( ':', '-', $this->_id ); |
| 70 | |
| 71 | // If there are sticky notices for the current slug, add a callback |
| 72 | // to the AJAX action that handles message dismiss. |
| 73 | add_action( "wp_ajax_fs_dismiss_notice_action_{$ajax_action_suffix}", array( |
| 74 | &$this, |
| 75 | 'dismiss_notice_ajax_callback' |
| 76 | ) ); |
| 77 | |
| 78 | foreach ( $this->_sticky_storage as $msg ) { |
| 79 | // Add admin notice. |
| 80 | $this->add( |
| 81 | $msg['message'], |
| 82 | $msg['title'], |
| 83 | $msg['type'], |
| 84 | true, |
| 85 | $msg['all'], |
| 86 | $msg['id'], |
| 87 | false |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Remove sticky message by ID. |
| 96 | * |
| 97 | * @author Vova Feldman (@svovaf) |
| 98 | * @since 1.0.7 |
| 99 | * |
| 100 | */ |
| 101 | function dismiss_notice_ajax_callback() { |
| 102 | $this->_sticky_storage->remove( $_POST['message_id'] ); |
| 103 | wp_die(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Rendered sticky message dismiss JavaScript. |
| 108 | * |
| 109 | * @author Vova Feldman (@svovaf) |
| 110 | * @since 1.0.7 |
| 111 | */ |
| 112 | static function _add_sticky_dismiss_javascript() { |
| 113 | $params = array(); |
| 114 | fs_require_once_template( 'sticky-admin-notice-js.php', $params ); |
| 115 | } |
| 116 | |
| 117 | private static $_added_sticky_javascript = false; |
| 118 | |
| 119 | /** |
| 120 | * Hook to the admin_footer to add sticky message dismiss JavaScript handler. |
| 121 | * |
| 122 | * @author Vova Feldman (@svovaf) |
| 123 | * @since 1.0.7 |
| 124 | */ |
| 125 | private static function has_sticky_messages() { |
| 126 | if ( ! self::$_added_sticky_javascript ) { |
| 127 | add_action( 'admin_footer', array( 'FS_Admin_Notice_Manager', '_add_sticky_dismiss_javascript' ) ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Handle admin_notices by printing the admin messages stacked in the queue. |
| 133 | * |
| 134 | * @author Vova Feldman (@svovaf) |
| 135 | * @since 1.0.4 |
| 136 | * |
| 137 | */ |
| 138 | function _admin_notices_hook() { |
| 139 | $notice_type = 'admin_notices'; |
| 140 | |
| 141 | if ( function_exists( 'current_user_can' ) && |
| 142 | ! current_user_can( 'manage_options' ) |
| 143 | ) { |
| 144 | // Only show messages to admins. |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if ( ! isset( $this->_admin_messages[ $notice_type ] ) || ! is_array( $this->_admin_messages[ $notice_type ] ) ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | foreach ( $this->_admin_messages[ $notice_type ] as $id => $msg ) { |
| 153 | fs_require_template( 'admin-notice.php', $msg ); |
| 154 | |
| 155 | if ( $msg['sticky'] ) { |
| 156 | self::has_sticky_messages(); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Handle all_admin_notices by printing the admin messages stacked in the queue. |
| 163 | * |
| 164 | * @author Vova Feldman (@svovaf) |
| 165 | * @since 1.0.4 |
| 166 | * |
| 167 | */ |
| 168 | function _all_admin_notices_hook() { |
| 169 | $notice_type = 'all_admin_notices'; |
| 170 | |
| 171 | if ( ! isset( $this->_admin_messages[ $notice_type ] ) || ! is_array( $this->_admin_messages[ $notice_type ] ) ) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | foreach ( $this->_admin_messages[ $notice_type ] as $id => $msg ) { |
| 176 | fs_require_template( 'all-admin-notice.php', $msg ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Enqueue common stylesheet to style admin notice. |
| 182 | * |
| 183 | * @author Vova Feldman (@svovaf) |
| 184 | * @since 1.0.7 |
| 185 | */ |
| 186 | function _enqueue_styles() { |
| 187 | fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. |
| 192 | * |
| 193 | * @author Vova Feldman (@svovaf) |
| 194 | * @since 1.0.4 |
| 195 | * |
| 196 | * @param string $message |
| 197 | * @param string $title |
| 198 | * @param string $type |
| 199 | * @param bool $is_sticky |
| 200 | * @param bool $all_admin |
| 201 | * @param string $id Message ID |
| 202 | * @param bool $store_if_sticky |
| 203 | * |
| 204 | * @uses add_action() |
| 205 | */ |
| 206 | function add( $message, $title = '', $type = 'success', $is_sticky = false, $all_admin = false, $id = '', $store_if_sticky = true ) { |
| 207 | $key = ( $all_admin ? 'all_admin_notices' : 'admin_notices' ); |
| 208 | |
| 209 | if ( ! isset( $this->_admin_messages[ $key ] ) ) { |
| 210 | $this->_admin_messages[ $key ] = array(); |
| 211 | |
| 212 | add_action( $key, array( &$this, "_{$key}_hook" ) ); |
| 213 | add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_styles' ) ); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | if ( '' === $id ) { |
| 218 | $id = md5( $title . ' ' . $message . ' ' . $type ); |
| 219 | } |
| 220 | |
| 221 | $message_object = array( |
| 222 | 'message' => $message, |
| 223 | 'title' => $title, |
| 224 | 'type' => $type, |
| 225 | 'sticky' => $is_sticky, |
| 226 | 'id' => $id, |
| 227 | 'all' => $all_admin, |
| 228 | 'manager_id' => $this->_id, |
| 229 | 'plugin' => $this->_title, |
| 230 | ); |
| 231 | |
| 232 | if ( $is_sticky && $store_if_sticky ) { |
| 233 | $this->_sticky_storage->{$id} = $message_object; |
| 234 | } |
| 235 | |
| 236 | $this->_admin_messages[ $key ][ $id ] = $message_object; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @author Vova Feldman (@svovaf) |
| 241 | * @since 1.0.7 |
| 242 | * |
| 243 | * @param string|string[] $ids |
| 244 | */ |
| 245 | function remove_sticky( $ids ) { |
| 246 | if ( ! is_array( $ids ) ) { |
| 247 | $ids = array( $ids ); |
| 248 | } |
| 249 | |
| 250 | foreach ( $ids as $id ) { |
| 251 | // Remove from sticky storage. |
| 252 | $this->_sticky_storage->remove( $id ); |
| 253 | |
| 254 | // Remove from current admin messages. |
| 255 | if ( isset( $this->_admin_messages['all_admin_notices'] ) && isset( $this->_admin_messages['all_admin_notices'][ $id ] ) ) { |
| 256 | unset( $this->_admin_messages['all_admin_notices'][ $id ] ); |
| 257 | } |
| 258 | if ( isset( $this->_admin_messages['admin_notices'] ) && isset( $this->_admin_messages['admin_notices'][ $id ] ) ) { |
| 259 | unset( $this->_admin_messages['admin_notices'][ $id ] ); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Check if sticky message exists by id. |
| 266 | * |
| 267 | * @author Vova Feldman (@svovaf) |
| 268 | * @since 1.0.9 |
| 269 | * |
| 270 | * @param $id |
| 271 | * |
| 272 | * @return bool |
| 273 | */ |
| 274 | function has_sticky( $id ) { |
| 275 | return isset( $this->_sticky_storage[ $id ] ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Adds sticky admin notification. |
| 280 | * |
| 281 | * @author Vova Feldman (@svovaf) |
| 282 | * @since 1.0.7 |
| 283 | * |
| 284 | * @param string $message |
| 285 | * @param string $id Message ID |
| 286 | * @param string $title |
| 287 | * @param string $type |
| 288 | * @param bool $all_admin |
| 289 | */ |
| 290 | function add_sticky( $message, $id, $title = '', $type = 'success', $all_admin = false ) { |
| 291 | if ( ! empty( $this->_module_unique_affix ) ) { |
| 292 | $message = fs_apply_filter( $this->_module_unique_affix, "sticky_message_{$id}", $message ); |
| 293 | $title = fs_apply_filter( $this->_module_unique_affix, "sticky_title_{$id}", $title ); |
| 294 | } |
| 295 | |
| 296 | $this->add( $message, $title, $type, true, $all_admin, $id ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Clear all sticky messages. |
| 301 | * |
| 302 | * @author Vova Feldman (@svovaf) |
| 303 | * @since 1.0.8 |
| 304 | */ |
| 305 | function clear_all_sticky() { |
| 306 | $this->_sticky_storage->clear_all(); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked. |
| 311 | * |
| 312 | * @author Vova Feldman (@svovaf) |
| 313 | * @since 1.0.4 |
| 314 | * |
| 315 | * @param string $message |
| 316 | * @param string $title |
| 317 | * @param string $type |
| 318 | * @param bool $is_sticky |
| 319 | * @param string $id Message ID |
| 320 | */ |
| 321 | function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) { |
| 322 | $this->add( $message, $title, $type, $is_sticky, true, $id ); |
| 323 | } |
| 324 | } |