PluginProbe
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder / 1.5
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder v1.5
3.6.0 3.5.0 trunk 1.0.10 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.6.1 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 All 110 releases
buttonizer-multifunctional-button / freemius / includes / class-fs-admin-notices.php

class-fs-admin-notices.php in Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder 1.5, at freemius/includes/class-fs-admin-notices.php

302 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 *
59 * @return FS_Admin_Notices
60 */
61 static function instance( $id, $title = '', $module_unique_affix = '' ) {
62 if ( ! isset( self::$_instances[ $id ] ) ) {
63 self::$_instances[ $id ] = new FS_Admin_Notices( $id, $title, $module_unique_affix );
64 }
65
66 return self::$_instances[ $id ];
67 }
68
69 protected function __construct( $id, $title = '', $module_unique_affix = '' ) {
70 $this->_id = $id;
71 $this->_title = $title;
72 $this->_module_unique_affix = $module_unique_affix;
73 $this->_is_multisite = is_multisite();
74
75 if ( $this->_is_multisite ) {
76 $this->_blog_id = get_current_blog_id();
77
78 $this->_network_notices = FS_Admin_Notice_Manager::instance(
79 $id,
80 $title,
81 $module_unique_affix,
82 true
83 );
84 }
85
86 $this->_notices = FS_Admin_Notice_Manager::instance(
87 $id,
88 $title,
89 $module_unique_affix,
90 $this->_blog_id
91 );
92 }
93
94 /**
95 * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked.
96 *
97 * @author Vova Feldman (@svovaf)
98 * @since 1.0.4
99 *
100 * @param string $message
101 * @param string $title
102 * @param string $type
103 * @param bool $is_sticky
104 * @param string $id Message ID
105 * @param bool $store_if_sticky
106 * @param int|null $network_level_or_blog_id
107 *
108 * @uses add_action()
109 */
110 function add(
111 $message,
112 $title = '',
113 $type = 'success',
114 $is_sticky = false,
115 $id = '',
116 $store_if_sticky = true,
117 $network_level_or_blog_id = null
118 ) {
119 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
120 $notices = $this->_network_notices;
121 } else {
122 $notices = $this->get_site_notices( $network_level_or_blog_id );
123 }
124
125 $notices->add(
126 $message,
127 $title,
128 $type,
129 $is_sticky,
130 $id,
131 $store_if_sticky
132 );
133 }
134
135 /**
136 * @author Vova Feldman (@svovaf)
137 * @since 1.0.7
138 *
139 * @param string|string[] $ids
140 * @param int|null $network_level_or_blog_id
141 */
142 function remove_sticky( $ids, $network_level_or_blog_id = null ) {
143 if ( ! is_array( $ids ) ) {
144 $ids = array( $ids );
145 }
146
147 if ( $this->should_use_network_notices( $ids[0], $network_level_or_blog_id ) ) {
148 $notices = $this->_network_notices;
149 } else {
150 $notices = $this->get_site_notices( $network_level_or_blog_id );
151 }
152
153 return $notices->remove_sticky( $ids );
154 }
155
156 /**
157 * Check if sticky message exists by id.
158 *
159 * @author Vova Feldman (@svovaf)
160 * @since 1.0.9
161 *
162 * @param string $id
163 * @param int|null $network_level_or_blog_id
164 *
165 * @return bool
166 */
167 function has_sticky( $id, $network_level_or_blog_id = null ) {
168 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
169 $notices = $this->_network_notices;
170 } else {
171 $notices = $this->get_site_notices( $network_level_or_blog_id );
172 }
173
174 return $notices->has_sticky( $id );
175 }
176
177 /**
178 * Adds sticky admin notification.
179 *
180 * @author Vova Feldman (@svovaf)
181 * @since 1.0.7
182 *
183 * @param string $message
184 * @param string $id Message ID
185 * @param string $title
186 * @param string $type
187 * @param int|null $network_level_or_blog_id
188 */
189 function add_sticky(
190 $message,
191 $id,
192 $title = '',
193 $type = 'success',
194 $network_level_or_blog_id = null
195 ) {
196 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
197 $notices = $this->_network_notices;
198 } else {
199 $notices = $this->get_site_notices( $network_level_or_blog_id );
200 }
201
202 $notices->add_sticky( $message, $id, $title, $type );
203 }
204
205 /**
206 * Clear all sticky messages.
207 *
208 * @author Vova Feldman (@svovaf)
209 * @since 2.0.0
210 *
211 * @param int|null $network_level_or_blog_id
212 */
213 function clear_all_sticky( $network_level_or_blog_id = null ) {
214 if ( ! $this->_is_multisite ||
215 false === $network_level_or_blog_id ||
216 0 == $network_level_or_blog_id ||
217 is_null( $network_level_or_blog_id )
218 ) {
219 $notices = $this->get_site_notices( $network_level_or_blog_id );
220 $notices->clear_all_sticky();
221 }
222
223 if ( $this->_is_multisite &&
224 ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
225 ) {
226 $this->_network_notices->clear_all_sticky();
227 }
228 }
229
230 /**
231 * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked.
232 *
233 * @author Vova Feldman (@svovaf)
234 * @since 1.0.4
235 *
236 * @param string $message
237 * @param string $title
238 * @param string $type
239 * @param bool $is_sticky
240 * @param string $id Message ID
241 */
242 function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) {
243 $this->add( $message, $title, $type, $is_sticky, true, $id );
244 }
245
246 #--------------------------------------------------------------------------------
247 #region Helper Methods
248 #--------------------------------------------------------------------------------
249
250 /**
251 * @author Vova Feldman (@svovaf)
252 * @since 2.0.0
253 *
254 * @param int $blog_id
255 *
256 * @return FS_Admin_Notice_Manager
257 */
258 private function get_site_notices( $blog_id = 0 ) {
259 if ( 0 == $blog_id || $blog_id == $this->_blog_id ) {
260 return $this->_notices;
261 }
262
263 return FS_Admin_Notice_Manager::instance(
264 $this->_id,
265 $this->_title,
266 $this->_module_unique_affix,
267 $blog_id
268 );
269 }
270
271 /**
272 * Check if the network notices should be used.
273 *
274 * @author Vova Feldman (@svovaf)
275 * @since 2.0.0
276 *
277 * @param string $id
278 * @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).
279 *
280 * @return bool
281 */
282 private function should_use_network_notices( $id = '', $network_level_or_blog_id = null ) {
283 if ( ! $this->_is_multisite ) {
284 // Not a multisite environment.
285 return false;
286 }
287
288 if ( is_numeric( $network_level_or_blog_id ) ) {
289 // Explicitly asked to use a specified blog storage.
290 return false;
291 }
292
293 if ( is_bool( $network_level_or_blog_id ) ) {
294 // Explicitly specified whether should use the network or blog level storage.
295 return $network_level_or_blog_id;
296 }
297
298 return fs_is_network_admin();
299 }
300
301 #endregion
302 }