PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.3.1
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.3.1
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / freemius / includes / class-fs-admin-notices.php

class-fs-admin-notices.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 1.3.1, at freemius/includes/class-fs-admin-notices.php

321 lines 10.6 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 * @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 ) {
130 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
131 $notices = $this->_network_notices;
132 } else {
133 $notices = $this->get_site_notices( $network_level_or_blog_id );
134 }
135
136 $notices->add(
137 $message,
138 $title,
139 $type,
140 $is_sticky,
141 $id,
142 $store_if_sticky
143 );
144 }
145
146 /**
147 * @author Vova Feldman (@svovaf)
148 * @since 1.0.7
149 *
150 * @param string|string[] $ids
151 * @param int|null $network_level_or_blog_id
152 */
153 function remove_sticky( $ids, $network_level_or_blog_id = null ) {
154 if ( ! is_array( $ids ) ) {
155 $ids = array( $ids );
156 }
157
158 if ( $this->should_use_network_notices( $ids[0], $network_level_or_blog_id ) ) {
159 $notices = $this->_network_notices;
160 } else {
161 $notices = $this->get_site_notices( $network_level_or_blog_id );
162 }
163
164 return $notices->remove_sticky( $ids );
165 }
166
167 /**
168 * Check if sticky message exists by id.
169 *
170 * @author Vova Feldman (@svovaf)
171 * @since 1.0.9
172 *
173 * @param string $id
174 * @param int|null $network_level_or_blog_id
175 *
176 * @return bool
177 */
178 function has_sticky( $id, $network_level_or_blog_id = null ) {
179 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
180 $notices = $this->_network_notices;
181 } else {
182 $notices = $this->get_site_notices( $network_level_or_blog_id );
183 }
184
185 return $notices->has_sticky( $id );
186 }
187
188 /**
189 * Adds sticky admin notification.
190 *
191 * @author Vova Feldman (@svovaf)
192 * @since 1.0.7
193 *
194 * @param string $message
195 * @param string $id Message ID
196 * @param string $title
197 * @param string $type
198 * @param int|null $network_level_or_blog_id
199 * @param number|null $wp_user_id
200 * @param string|null $plugin_title
201 * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and
202 * blog admin pages.
203 */
204 function add_sticky(
205 $message,
206 $id,
207 $title = '',
208 $type = 'success',
209 $network_level_or_blog_id = null,
210 $wp_user_id = null,
211 $plugin_title = null,
212 $is_network_and_blog_admins = false
213 ) {
214 if ( $this->should_use_network_notices( $id, $network_level_or_blog_id ) ) {
215 $notices = $this->_network_notices;
216 } else {
217 $notices = $this->get_site_notices( $network_level_or_blog_id );
218 }
219
220 $notices->add_sticky( $message, $id, $title, $type, $wp_user_id, $plugin_title, $is_network_and_blog_admins );
221 }
222
223 /**
224 * Clear all sticky messages.
225 *
226 * @author Vova Feldman (@svovaf)
227 * @since 2.0.0
228 *
229 * @param int|null $network_level_or_blog_id
230 */
231 function clear_all_sticky( $network_level_or_blog_id = null ) {
232 if ( ! $this->_is_multisite ||
233 false === $network_level_or_blog_id ||
234 0 == $network_level_or_blog_id ||
235 is_null( $network_level_or_blog_id )
236 ) {
237 $notices = $this->get_site_notices( $network_level_or_blog_id );
238 $notices->clear_all_sticky();
239 }
240
241 if ( $this->_is_multisite &&
242 ( true === $network_level_or_blog_id || is_null( $network_level_or_blog_id ) )
243 ) {
244 $this->_network_notices->clear_all_sticky();
245 }
246 }
247
248 /**
249 * Add admin message to all admin messages queue, and hook to all_admin_notices if not yet hooked.
250 *
251 * @author Vova Feldman (@svovaf)
252 * @since 1.0.4
253 *
254 * @param string $message
255 * @param string $title
256 * @param string $type
257 * @param bool $is_sticky
258 * @param string $id Message ID
259 */
260 function add_all( $message, $title = '', $type = 'success', $is_sticky = false, $id = '' ) {
261 $this->add( $message, $title, $type, $is_sticky, true, $id );
262 }
263
264 #--------------------------------------------------------------------------------
265 #region Helper Methods
266 #--------------------------------------------------------------------------------
267
268 /**
269 * @author Vova Feldman (@svovaf)
270 * @since 2.0.0
271 *
272 * @param int $blog_id
273 *
274 * @return FS_Admin_Notice_Manager
275 */
276 private function get_site_notices( $blog_id = 0 ) {
277 if ( 0 == $blog_id || $blog_id == $this->_blog_id ) {
278 return $this->_notices;
279 }
280
281 return FS_Admin_Notice_Manager::instance(
282 $this->_id,
283 $this->_title,
284 $this->_module_unique_affix,
285 false,
286 $blog_id
287 );
288 }
289
290 /**
291 * Check if the network notices should be used.
292 *
293 * @author Vova Feldman (@svovaf)
294 * @since 2.0.0
295 *
296 * @param string $id
297 * @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).
298 *
299 * @return bool
300 */
301 private function should_use_network_notices( $id = '', $network_level_or_blog_id = null ) {
302 if ( ! $this->_is_multisite ) {
303 // Not a multisite environment.
304 return false;
305 }
306
307 if ( is_numeric( $network_level_or_blog_id ) ) {
308 // Explicitly asked to use a specified blog storage.
309 return false;
310 }
311
312 if ( is_bool( $network_level_or_blog_id ) ) {
313 // Explicitly specified whether should use the network or blog level storage.
314 return $network_level_or_blog_id;
315 }
316
317 return fs_is_network_admin();
318 }
319
320 #endregion
321 }