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