| @@ -1,0 +1,353 @@ | ||
| 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 2.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | + if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | + } | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * WP Admin notices manager both for site level and network level. | |
| 15 | + * | |
| 16 | + * Class FS_Admin_Notices | |
| 17 | + */ | |
| 18 | + class FS_Admin_Notices { | |
| 19 | + /** | |
| 20 | + * @since 1.2.2 | |
| 21 | + * | |
| 22 | + * @var string | |
| 23 | + */ | |
| 24 | + protected $_module_unique_affix; | |
| 25 | + /** | |
| 26 | + * @var string | |
| 27 | + */ | |
| 28 | + protected $_id; | |
| 29 | + /** | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + protected $_title; | |
| 33 | + /** | |
| 34 | + * @var FS_Admin_Notice_Manager | |
| 35 | + */ | |
| 36 | + protected $_notices; | |
| 37 | + /** | |
| 38 | + * @var FS_Admin_Notice_Manager | |
| 39 | + */ | |
| 40 | + protected $_network_notices; | |
| 41 | + /** | |
| 42 | + * @var int The ID of the blog that is associated with the current site level options. | |
| 43 | + */ | |
| 44 | + private $_blog_id = 0; | |
| 45 | + /** | |
| 46 | + * @var bool | |
| 47 | + */ | |
| 48 | + private $_is_multisite; | |
| 49 | + /** | |
| 50 | + * @var FS_Admin_Notices[] | |
| 51 | + */ | |
| 52 | + private static $_instances = array(); | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * @param string $id | |
| 56 | + * @param string $title | |
| 57 | + * @param string $module_unique_affix | |
| 58 | + * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and | |
| 59 | + * blog admin pages. | |
| 60 | + * | |
| 61 | + * @return FS_Admin_Notices | |
| 62 | + */ | |
| 63 | + static function instance( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) { | |
| 64 | + if ( ! isset( self::$_instances[ $id ] ) ) { | |
| 65 | + self::$_instances[ $id ] = new FS_Admin_Notices( $id, $title, $module_unique_affix, $is_network_and_blog_admins ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + return self::$_instances[ $id ]; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * @param string $id | |
| 73 | + * @param string $title | |
| 74 | + * @param string $module_unique_affix | |
| 75 | + * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and | |
| 76 | + * blog admin pages. | |
| 77 | + */ | |
| 78 | + protected function __construct( $id, $title = '', $module_unique_affix = '', $is_network_and_blog_admins = false ) { | |
| 79 | + $this->_id = $id; | |
| 80 | + $this->_title = $title; | |
| 81 | + $this->_module_unique_affix = $module_unique_affix; | |
| 82 | + $this->_is_multisite = is_multisite(); | |
| 83 | + | |
| 84 | + if ( $this->_is_multisite ) { | |
| 85 | + $this->_blog_id = get_current_blog_id(); | |
| 86 | + | |
| 87 | + $this->_network_notices = FS_Admin_Notice_Manager::instance( | |
| 88 | + $id, | |
| 89 | + $title, | |
| 90 | + $module_unique_affix, | |
| 91 | + $is_network_and_blog_admins, | |
| 92 | + true | |
| 93 | + ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + $this->_notices = FS_Admin_Notice_Manager::instance( | |
| 97 | + $id, | |
| 98 | + $title, | |
| 99 | + $module_unique_affix, | |
| 100 | + false, | |
| 101 | + $this->_blog_id | |
| 102 | + ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. | |
| 107 | + * | |
| 108 | + * @author Vova Feldman (@svovaf) | |
| 109 | + * @since 1.0.4 | |
| 110 | + * | |
| 111 | + * @param string $message | |
| 112 | + * @param string $title | |
| 113 | + * @param string $type | |
| 114 | + * @param bool $is_sticky | |
| 115 | + * @param string $id Message ID | |
| 116 | + * @param bool $store_if_sticky | |
| 117 | + * @param int|null $network_level_or_blog_id | |
| 118 | + * | |
| 119 | + * @uses add_action() | |
| 120 | + */ | |
| 121 | + function add( | |
| 122 | + $message, | |
| 123 | + $title = '', | |
| 124 | + $type = 'success', | |
| 125 | + $is_sticky = false, | |
| 126 | + $id = '', | |
| 127 | + $store_if_sticky = true, | |
| 128 | + $network_level_or_blog_id = null, | |
| 129 | + $is_dimissible = null | |
| 130 | + ) { | |
| 131 | + $notices = $this->get_site_or_network_notices( $id, $network_level_or_blog_id ); | |
| 132 | + | |
| 133 | + $notices->add( | |
| 134 | + $message, | |
| 135 | + $title, | |
| 136 | + $type, | |
| 137 | + $is_sticky, | |
| 138 | + $id, | |
| 139 | + $store_if_sticky, | |
| 140 | + null, | |
| 141 | + null, | |
| 142 | + false, | |
| 143 | + $is_dimissible | |
| 144 | + ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * @author Vova Feldman (@svovaf) | |
| 149 | + * @since 1.0.7 | |
| 150 | + * | |
| 151 | + * @param string|string[] $ids | |
| 152 | + * @param int|null $network_level_or_blog_id | |
| 153 | + * @param bool $store | |
| 154 | + */ | |
| 155 | + function remove_sticky( $ids, $network_level_or_blog_id = null, $store = true ) { | |
| 156 | + if ( ! is_array( $ids ) ) { | |
| 157 | + $ids = array( $ids ); | |
| 158 | + } | |
| 159 | + | |
| 160 | + if ( $this->should_use_network_notices( $ids[0], $network_level_or_blog_id ) ) { | |
| 161 | + $notices = $this->_network_notices; | |
| 162 | + } else { | |
| 163 | + $notices = $this->get_site_notices( $network_level_or_blog_id ); | |
| 164 | + } | |
| 165 | + | |
| 166 | + return $notices->remove_sticky( $ids, $store ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Check if sticky message exists by id. | |
| 171 | + * | |
| 172 | + * @author Vova Feldman (@svovaf) | |
| 173 | + * @since 1.0.9 | |
| 174 | + * | |
| 175 | + * @param string $id | |
| 176 | + * @param int|null $network_level_or_blog_id | |
| 177 | + * | |
| 178 | + * @return bool | |
| 179 | + */ | |
| 180 | + function has_sticky( $id, $network_level_or_blog_id = null ) { | |
| 181 | + $notices = $this->get_site_or_network_notices( $id, $network_level_or_blog_id ); | |
| 182 | + | |
| 183 | + return $notices->has_sticky( $id ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Adds sticky admin notification. | |
| 188 | + * | |
| 189 | + * @author Vova Feldman (@svovaf) | |
| 190 | + * @since 1.0.7 | |
| 191 | + * | |
| 192 | + * @param string $message | |
| 193 | + * @param string $id Message ID | |
| 194 | + * @param string $title | |
| 195 | + * @param string $type | |
| 196 | + * @param int|null $network_level_or_blog_id | |
| 197 | + * @param number|null $wp_user_id | |
| 198 | + * @param string|null $plugin_title | |
| 199 | + * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and | |
| 200 | + * blog admin pages. | |
| 201 | + * @param bool $is_dismissible | |
| 202 | + */ | |
| 203 | + function add_sticky( | |
| 204 | + $message, | |
| 205 | + $id, | |
| 206 | + $title = '', | |
| 207 | + $type = 'success', | |
| 208 | + $network_level_or_blog_id = null, | |
| 209 | + $wp_user_id = null, | |
| 210 | + $plugin_title = null, | |
| 211 | + $is_network_and_blog_admins = false, | |
| 212 | + $is_dismissible = true, | |
| 213 | + $data = array() | |
| 214 | + ) { | |
| 215 | + $notices = $this->get_site_or_network_notices( $id, $network_level_or_blog_id ); | |
| 216 | + | |
| 217 | + $notices->add_sticky( $message, $id, $title, $type, $wp_user_id, $plugin_title, $is_network_and_blog_admins, $is_dismissible, $data ); | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Retrieves the data of a sticky notice. | |
| 222 | + * | |
| 223 | + * @author Leo Fajardo (@leorw) | |
| 224 | + * @since 2.4.3 | |
| 225 | + * | |
| 226 | + * @param string $id | |
| 227 | + * @param int|null $network_level_or_blog_id | |
| 228 | + * | |
| 229 | + * @return array|null | |
| 230 | + */ | |
| 231 | + function get_sticky( $id, $network_level_or_blog_id ) { | |
| 232 | + $notices = $this->get_site_or_network_notices( $id, $network_level_or_blog_id ); | |
| 233 | + | |
| 234 | + return $notices->get_sticky( $id ); | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Clear all sticky messages. | |
| 239 | + * | |
| 240 | + * @author Vova Feldman (@svovaf) | |
| 241 | + * @since 2.0.0 | |
| 242 | + * | |
| 243 | + * @param int|null $network_level_or_blog_id | |
| 244 | + * @param bool $is_temporary | |
| 245 | + */ | |
| 246 | + function clear_all_sticky( $network_level_or_blog_id = null, $is_temporary = false ) { | |
| 247 | + if ( ! $this->_is_multisite || | |
| 248 | + false === $network_level_or_blog_id || | |
| 249 | + 0 == $network_level_or_blog_id || | |
| 250 | + is_null( $network_level_or_blog_id ) | |
| 251 | + ) { | |
| 252 | + $notices = $this->get_site_notices( $network_level_or_blog_id ); | |
| 253 | + $notices->clear_all_sticky( $is_temporary ); | |
| 254 | + } | |
| 255 | + | |
| 256 | + if ( $this->_is_multisite && | |
| 257 | + ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) ) | |
| 258 | + ) { | |
| 259 | + $this->_network_notices->clear_all_sticky( $is_temporary ); | |
| 260 | + } | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked. | |
| 265 | + * | |
| 266 | + * @author Vova Feldman (@svovaf) | |
| 267 | + * @since 1.0.4 | |
| 268 | + * | |
| 269 | + * @param string $message | |
| 270 | + * @param string $title | |
| 271 | + * @param string $type | |
| 272 | + * @param bool $is_sticky | |
| 273 | + * @param string $id Message ID | |
| 274 | + */ | |
| 275 | + function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) { | |
| 276 | + $this->add( $message, $title, $type, $is_sticky, true, $id ); | |
| 277 | + } | |
| 278 | + | |
| 279 | + #-------------------------------------------------------------------------------- | |
| 280 | + #region Helper Methods | |
| 281 | + #-------------------------------------------------------------------------------- | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * @author Vova Feldman (@svovaf) | |
| 285 | + * @since 2.0.0 | |
| 286 | + * | |
| 287 | + * @param int $blog_id | |
| 288 | + * | |
| 289 | + * @return FS_Admin_Notice_Manager | |
| 290 | + */ | |
| 291 | + private function get_site_notices( $blog_id = 0 ) { | |
| 292 | + if ( 0 == $blog_id || $blog_id == $this->_blog_id ) { | |
| 293 | + return $this->_notices; | |
| 294 | + } | |
| 295 | + | |
| 296 | + return FS_Admin_Notice_Manager::instance( | |
| 297 | + $this->_id, | |
| 298 | + $this->_title, | |
| 299 | + $this->_module_unique_affix, | |
| 300 | + false, | |
| 301 | + $blog_id | |
| 302 | + ); | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * Check if the network notices should be used. | |
| 307 | + * | |
| 308 | + * @author Vova Feldman (@svovaf) | |
| 309 | + * @since 2.0.0 | |
| 310 | + * | |
| 311 | + * @param string $id | |
| 312 | + * @param null|bool|int $network_level_or_blog_id When an integer, use the given blog storage. When `true` use the multisite notices (if there's a network). When `false`, use the current context blog notices. When `null`, the decision which notices manager to use (MS vs. Current S) will be handled internally and determined based on the $id and the context admin (blog admin vs. network level admin). | |
| 313 | + * | |
| 314 | + * @return bool | |
| 315 | + */ | |
| 316 | + private function should_use_network_notices( $id = '', $network_level_or_blog_id = null ) { | |
| 317 | + if ( ! $this->_is_multisite ) { | |
| 318 | + // Not a multisite environment. | |
| 319 | + return false; | |
| 320 | + } | |
| 321 | + | |
| 322 | + if ( is_numeric( $network_level_or_blog_id ) ) { | |
| 323 | + // Explicitly asked to use a specified blog storage. | |
| 324 | + return false; | |
| 325 | + } | |
| 326 | + | |
| 327 | + if ( is_bool( $network_level_or_blog_id ) ) { | |
| 328 | + // Explicitly specified whether should use the network or blog level storage. | |
| 329 | + return $network_level_or_blog_id; | |
| 330 | + } | |
| 331 | + | |
| 332 | + return fs_is_network_admin(); | |
| 333 | + } | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Retrieves an instance of FS_Admin_Notice_Manager. | |
| 337 | + * | |
| 338 | + * @author Leo Fajardo (@leorw) | |
| 339 | + * @since 2.5.0 | |
| 340 | + * | |
| 341 | + * @param string $id | |
| 342 | + * @param int|null $network_level_or_blog_id | |
| 343 | + * | |
| 344 | + * @return FS_Admin_Notice_Manager | |
| 345 | + */ | |
| 346 | + private function get_site_or_network_notices( $id, $network_level_or_blog_id ) { | |
| 347 | + return $this->should_use_network_notices( $id, $network_level_or_blog_id ) ? | |
| 348 | + $this->_network_notices : | |
| 349 | + $this->get_site_notices( $network_level_or_blog_id ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + #endregion | |
| 353 | + } | |