GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b3ee5f...a83cd4 )
by Brad
06:08 queued 03:01
created

FS_Admin_Notice_Manager::clear_all_sticky()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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[string]array
30
         */
31
        private $_notices = array();
32
        /**
33
         * @var FS_Key_Value_Storage
34
         */
35
        private $_sticky_storage;
36
        /**
37
         * @var FS_Logger
38
         */
39
        protected $_logger;
40
        /**
41
         * @since 2.0.0
42
         * @var int The ID of the blog that is associated with the current site level admin notices.
43
         */
44
        private $_blog_id = 0;
45
        /**
46
         * @since 2.0.0
47
         * @var bool
48
         */
49
        private $_is_network_notices;
50
51
        /**
52
         * @var FS_Admin_Notice_Manager[]
53
         */
54
        private static $_instances = array();
55
56
        /**
57
         * @param string $id
58
         * @param string $title
59
         * @param string $module_unique_affix
60
         * @param bool   $is_network_and_blog_admins           Whether or not the message should be shown both on
61
         *                                                     network and blog admin pages.
62
         * @param bool   $network_level_or_blog_id Since 2.0.0
63
         *
64
         * @return \FS_Admin_Notice_Manager
65
         */
66
        static function instance(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
            $id,
68
            $title = '',
69
            $module_unique_affix = '',
70
            $is_network_and_blog_admins = false,
71
            $network_level_or_blog_id = false
72
        ) {
73
            if ( $is_network_and_blog_admins ) {
74
                $network_level_or_blog_id = true;
75
            }
76
77
            $key = strtolower( $id );
78
79
            if ( is_multisite() ) {
80
                if ( true === $network_level_or_blog_id ) {
81
                    $key .= ':ms';
82
                } else if ( is_numeric( $network_level_or_blog_id ) && $network_level_or_blog_id > 0 ) {
83
                    $key .= ":{$network_level_or_blog_id}";
84
                } else {
85
                    $network_level_or_blog_id = get_current_blog_id();
86
87
                    $key .= ":{$network_level_or_blog_id}";
88
                }
89
            }
90
91
            if ( ! isset( self::$_instances[ $key ] ) ) {
92
                self::$_instances[ $key ] = new FS_Admin_Notice_Manager(
93
                    $id,
94
                    $title,
95
                    $module_unique_affix,
96
                    $is_network_and_blog_admins,
97
                    $network_level_or_blog_id
98
                );
99
            }
100
101
            return self::$_instances[ $key ];
102
        }
103
104
        /**
105
         * @param string $id
106
         * @param string $title
107
         * @param string $module_unique_affix
108
         * @param bool   $is_network_and_blog_admins Whether or not the message should be shown both on network and
109
         *                                             blog admin pages.
110
         * @param bool|int $network_level_or_blog_id
111
         */
112
        protected function __construct(
113
            $id,
114
            $title = '',
115
            $module_unique_affix = '',
116
            $is_network_and_blog_admins = false,
117
            $network_level_or_blog_id = false
118
        ) {
119
            $this->_id                  = $id;
120
            $this->_logger              = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->_id . '_data', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
121
            $this->_title               = ! empty( $title ) ? $title : '';
122
            $this->_module_unique_affix = $module_unique_affix;
123
            $this->_sticky_storage      = FS_Key_Value_Storage::instance( 'admin_notices', $this->_id, $network_level_or_blog_id );
0 ignored issues
show
Bug introduced by
It seems like $network_level_or_blog_id defined by parameter $network_level_or_blog_id on line 117 can also be of type integer; however, FS_Key_Value_Storage::instance() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
124
125
            if ( is_multisite() ) {
126
                $this->_is_network_notices = ( true === $network_level_or_blog_id );
127
128
                if ( is_numeric( $network_level_or_blog_id ) ) {
129
                    $this->_blog_id = $network_level_or_blog_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $network_level_or_blog_id can also be of type double or string. However, the property $_blog_id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
130
                }
131
            } else {
132
                $this->_is_network_notices = false;
133
            }
134
135
            $is_network_admin = fs_is_network_admin();
136
            $is_blog_admin    = fs_is_blog_admin();
137
138
            if ( ( $this->_is_network_notices && $is_network_admin ) ||
139
                 ( ! $this->_is_network_notices && $is_blog_admin ) ||
140
                ( $is_network_and_blog_admins && ( $is_network_admin || $is_blog_admin ) )
141
            ) {
142
                if ( 0 < count( $this->_sticky_storage ) ) {
143
                    $ajax_action_suffix = str_replace( ':', '-', $this->_id );
144
145
                    // If there are sticky notices for the current slug, add a callback
146
                    // to the AJAX action that handles message dismiss.
147
                    add_action( "wp_ajax_fs_dismiss_notice_action_{$ajax_action_suffix}", array(
148
                        &$this,
149
                        'dismiss_notice_ajax_callback'
150
                    ) );
151
152
                    foreach ( $this->_sticky_storage as $msg ) {
153
                        // Add admin notice.
154
                        $this->add(
155
                            $msg['message'],
156
                            $msg['title'],
157
                            $msg['type'],
158
                            true,
159
                            $msg['id'],
160
                            false,
161
                            isset( $msg['wp_user_id'] ) ? $msg['wp_user_id'] : null,
162
                            ! empty( $msg['plugin'] ) ? $msg['plugin'] : null,
163
                            $is_network_and_blog_admins
164
                        );
165
                    }
166
                }
167
            }
168
        }
169
170
        /**
171
         * Remove sticky message by ID.
172
         *
173
         * @author Vova Feldman (@svovaf)
174
         * @since  1.0.7
175
         *
176
         */
177
        function dismiss_notice_ajax_callback() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
178
            $this->_sticky_storage->remove( $_POST['message_id'] );
179
            wp_die();
180
        }
181
182
        /**
183
         * Rendered sticky message dismiss JavaScript.
184
         *
185
         * @author Vova Feldman (@svovaf)
186
         * @since  1.0.7
187
         */
188
        static function _add_sticky_dismiss_javascript() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
189
            $params = array();
190
            fs_require_once_template( 'sticky-admin-notice-js.php', $params );
191
        }
192
193
        private static $_added_sticky_javascript = false;
194
195
        /**
196
         * Hook to the admin_footer to add sticky message dismiss JavaScript handler.
197
         *
198
         * @author Vova Feldman (@svovaf)
199
         * @since  1.0.7
200
         */
201
        private static function has_sticky_messages() {
202
            if ( ! self::$_added_sticky_javascript ) {
203
                add_action( 'admin_footer', array( 'FS_Admin_Notice_Manager', '_add_sticky_dismiss_javascript' ) );
204
            }
205
        }
206
207
        /**
208
         * Handle admin_notices by printing the admin messages stacked in the queue.
209
         *
210
         * @author Vova Feldman (@svovaf)
211
         * @since  1.0.4
212
         *
213
         */
214
        function _admin_notices_hook() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
215
            if ( function_exists( 'current_user_can' ) &&
216
                 ! current_user_can( 'manage_options' )
217
            ) {
218
                // Only show messages to admins.
219
                return;
220
            }
221
222
            foreach ( $this->_notices as $id => $msg ) {
223
                if ( isset( $msg['wp_user_id'] ) && is_numeric( $msg['wp_user_id'] ) ) {
224
                    if ( get_current_user_id() != $msg['wp_user_id'] ) {
225
                        continue;
226
                    }
227
                }
228
229
                /**
230
                 * Added a filter to control the visibility of admin notices.
231
                 *
232
                 * Usage example:
233
                 *
234
                 *     /**
235
                 *      * @param bool  $show
236
                 *      * @param array $msg {
237
                 *      *     @var string $message The actual message.
238
                 *      *     @var string $title An optional message title.
239
                 *      *     @var string $type The type of the message ('success', 'update', 'warning', 'promotion').
240
                 *      *     @var string $id The unique identifier of the message.
241
                 *      *     @var string $manager_id The unique identifier of the notices manager. For plugins it would be the plugin's slug, for themes - `<slug>-theme`.
242
                 *      *     @var string $plugin The product's title.
243
                 *      *     @var string $wp_user_id An optional WP user ID that this admin notice is for.
244
                 *      * }
245
                 *      *
246
                 *      * @return bool
247
                 *      *\/
248
                 *      function my_custom_show_admin_notice( $show, $msg ) {
249
                 *          if ('trial_promotion' != $msg['id']) {
250
                 *              return false;
251
                 *          }
252
                 *
253
                 *          return $show;
254
                 *      }
255
                 *
256
                 *      my_fs()->add_filter( 'show_admin_notice', 'my_custom_show_admin_notice', 10, 2 );
257
                 *
258
                 * @author Vova Feldman
259
                 * @since 2.2.0
260
                 */
261
                $show_notice = call_user_func_array( 'fs_apply_filter', array(
262
                    $this->_module_unique_affix,
263
                    'show_admin_notice',
264
                    true,
265
                    $msg
266
                ) );
267
268
                if ( true !== $show_notice ) {
269
                    continue;
270
                }
271
272
                fs_require_template( 'admin-notice.php', $msg );
273
274
                if ( $msg['sticky'] ) {
275
                    self::has_sticky_messages();
276
                }
277
            }
278
        }
279
280
        /**
281
         * Enqueue common stylesheet to style admin notice.
282
         *
283
         * @author Vova Feldman (@svovaf)
284
         * @since  1.0.7
285
         */
286
        function _enqueue_styles() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
287
            fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
288
        }
289
290
        /**
291
         * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked.
292
         *
293
         * @author Vova Feldman (@svovaf)
294
         * @since  1.0.4
295
         *
296
         * @param string      $message
297
         * @param string      $title
298
         * @param string      $type
299
         * @param bool        $is_sticky
300
         * @param string      $id Message ID
301
         * @param bool        $store_if_sticky
302
         * @param number|null $wp_user_id
303
         * @param string|null $plugin_title
304
         * @param bool        $is_network_and_blog_admins Whether or not the message should be shown both on network
305
         *                                                and blog admin pages.
306
         *
307
         * @uses   add_action()
308
         */
309
        function add(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
310
            $message,
311
            $title = '',
312
            $type = 'success',
313
            $is_sticky = false,
314
            $id = '',
315
            $store_if_sticky = true,
316
            $wp_user_id = null,
317
            $plugin_title = null,
318
            $is_network_and_blog_admins = false
319
        ) {
320
            $notices_type = $this->get_notices_type();
321
322
            if ( empty( $this->_notices ) ) {
323
                if ( ! $is_network_and_blog_admins ) {
324
                    add_action( $notices_type, array( &$this, "_admin_notices_hook" ) );
325
                } else {
326
                    add_action( 'network_admin_notices', array( &$this, "_admin_notices_hook" ) );
327
                    add_action( 'admin_notices', array( &$this, "_admin_notices_hook" ) );
328
                }
329
330
                add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_styles' ) );
331
            }
332
333
            if ( '' === $id ) {
334
                $id = md5( $title . ' ' . $message . ' ' . $type );
335
            }
336
337
            $message_object = array(
338
                'message'    => $message,
339
                'title'      => $title,
340
                'type'       => $type,
341
                'sticky'     => $is_sticky,
342
                'id'         => $id,
343
                'manager_id' => $this->_id,
344
                'plugin'     => ( ! is_null( $plugin_title ) ? $plugin_title : $this->_title ),
345
                'wp_user_id' => $wp_user_id,
346
            );
347
348
            if ( $is_sticky && $store_if_sticky ) {
349
                $this->_sticky_storage->{$id} = $message_object;
350
            }
351
352
            $this->_notices[ $id ] = $message_object;
353
        }
354
355
        /**
356
         * @author Vova Feldman (@svovaf)
357
         * @since  1.0.7
358
         *
359
         * @param string|string[] $ids
360
         */
361
        function remove_sticky( $ids ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
362
            if ( ! is_array( $ids ) ) {
363
                $ids = array( $ids );
364
            }
365
366
            foreach ( $ids as $id ) {
367
                // Remove from sticky storage.
368
                $this->_sticky_storage->remove( $id );
369
370
                if ( isset( $this->_notices[ $id ] ) ) {
371
                    unset( $this->_notices[ $id ] );
372
                }
373
            }
374
        }
375
376
        /**
377
         * Check if sticky message exists by id.
378
         *
379
         * @author Vova Feldman (@svovaf)
380
         * @since  1.0.9
381
         *
382
         * @param $id
383
         *
384
         * @return bool
385
         */
386
        function has_sticky( $id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
387
            return isset( $this->_sticky_storage[ $id ] );
388
        }
389
390
        /**
391
         * Adds sticky admin notification.
392
         *
393
         * @author Vova Feldman (@svovaf)
394
         * @since  1.0.7
395
         *
396
         * @param string      $message
397
         * @param string      $id Message ID
398
         * @param string      $title
399
         * @param string      $type
400
         * @param number|null $wp_user_id
401
         * @param string|null $plugin_title
402
         * @param bool        $is_network_and_blog_admins Whether or not the message should be shown both on network
403
         *                                                and blog admin pages.
404
         */
405
        function add_sticky( $message, $id, $title = '', $type = 'success', $wp_user_id = null, $plugin_title = null, $is_network_and_blog_admins = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
406
            if ( ! empty( $this->_module_unique_affix ) ) {
407
                $message = fs_apply_filter( $this->_module_unique_affix, "sticky_message_{$id}", $message );
408
                $title   = fs_apply_filter( $this->_module_unique_affix, "sticky_title_{$id}", $title );
409
            }
410
411
            $this->add( $message, $title, $type, true, $id, true, $wp_user_id, $plugin_title, $is_network_and_blog_admins );
412
        }
413
414
        /**
415
         * Clear all sticky messages.
416
         *
417
         * @author Vova Feldman (@svovaf)
418
         * @since  1.0.8
419
         */
420
        function clear_all_sticky() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
421
            $this->_sticky_storage->clear_all();
422
        }
423
424
        #--------------------------------------------------------------------------------
425
        #region Helper Method
426
        #--------------------------------------------------------------------------------
427
428
        /**
429
         * @author Vova Feldman (@svovaf)
430
         * @since  2.0.0
431
         *
432
         * @return string
433
         */
434
        private function get_notices_type() {
435
            return $this->_is_network_notices ?
436
                'network_admin_notices' :
437
                'admin_notices';
438
        }
439
440
        #endregion
441
    }