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 — feature/video-support ( 96814b )
by Brad
05:36
created

FS_Admin_Notice_Manager::instance()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 16
nop 5
dl 0
loc 37
rs 6.7272
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...
Coding Style introduced by
dismiss_notice_ajax_callback uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
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
                fs_require_template( 'admin-notice.php', $msg );
230
231
                if ( $msg['sticky'] ) {
232
                    self::has_sticky_messages();
233
                }
234
            }
235
        }
236
237
        /**
238
         * Enqueue common stylesheet to style admin notice.
239
         *
240
         * @author Vova Feldman (@svovaf)
241
         * @since  1.0.7
242
         */
243
        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...
244
            fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
245
        }
246
247
        /**
248
         * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked.
249
         *
250
         * @author Vova Feldman (@svovaf)
251
         * @since  1.0.4
252
         *
253
         * @param string      $message
254
         * @param string      $title
255
         * @param string      $type
256
         * @param bool        $is_sticky
257
         * @param string      $id Message ID
258
         * @param bool        $store_if_sticky
259
         * @param number|null $wp_user_id
260
         * @param string|null $plugin_title
261
         * @param bool        $is_network_and_blog_admins Whether or not the message should be shown both on network
262
         *                                                and blog admin pages.
263
         *
264
         * @uses   add_action()
265
         */
266
        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...
267
            $message,
268
            $title = '',
269
            $type = 'success',
270
            $is_sticky = false,
271
            $id = '',
272
            $store_if_sticky = true,
273
            $wp_user_id = null,
274
            $plugin_title = null,
275
            $is_network_and_blog_admins = false
276
        ) {
277
            $notices_type = $this->get_notices_type();
278
279
            if ( empty( $this->_notices ) ) {
280
                if ( ! $is_network_and_blog_admins ) {
281
                    add_action( $notices_type, array( &$this, "_admin_notices_hook" ) );
282
                } else {
283
                    add_action( 'network_admin_notices', array( &$this, "_admin_notices_hook" ) );
284
                    add_action( 'admin_notices', array( &$this, "_admin_notices_hook" ) );
285
                }
286
287
                add_action( 'admin_enqueue_scripts', array( &$this, '_enqueue_styles' ) );
288
            }
289
290
            if ( '' === $id ) {
291
                $id = md5( $title . ' ' . $message . ' ' . $type );
292
            }
293
294
            $message_object = array(
295
                'message'    => $message,
296
                'title'      => $title,
297
                'type'       => $type,
298
                'sticky'     => $is_sticky,
299
                'id'         => $id,
300
                'manager_id' => $this->_id,
301
                'plugin'     => ( ! is_null( $plugin_title ) ? $plugin_title : $this->_title ),
302
                'wp_user_id' => $wp_user_id,
303
            );
304
305
            if ( $is_sticky && $store_if_sticky ) {
306
                $this->_sticky_storage->{$id} = $message_object;
307
            }
308
309
            $this->_notices[ $id ] = $message_object;
310
        }
311
312
        /**
313
         * @author Vova Feldman (@svovaf)
314
         * @since  1.0.7
315
         *
316
         * @param string|string[] $ids
317
         */
318
        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...
319
            if ( ! is_array( $ids ) ) {
320
                $ids = array( $ids );
321
            }
322
323
            foreach ( $ids as $id ) {
324
                // Remove from sticky storage.
325
                $this->_sticky_storage->remove( $id );
326
327
                if ( isset( $this->_notices[ $id ] ) ) {
328
                    unset( $this->_notices[ $id ] );
329
                }
330
            }
331
        }
332
333
        /**
334
         * Check if sticky message exists by id.
335
         *
336
         * @author Vova Feldman (@svovaf)
337
         * @since  1.0.9
338
         *
339
         * @param $id
340
         *
341
         * @return bool
342
         */
343
        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...
344
            return isset( $this->_sticky_storage[ $id ] );
345
        }
346
347
        /**
348
         * Adds sticky admin notification.
349
         *
350
         * @author Vova Feldman (@svovaf)
351
         * @since  1.0.7
352
         *
353
         * @param string      $message
354
         * @param string      $id Message ID
355
         * @param string      $title
356
         * @param string      $type
357
         * @param number|null $wp_user_id
358
         * @param string|null $plugin_title
359
         * @param bool        $is_network_and_blog_admins Whether or not the message should be shown both on network
360
         *                                                and blog admin pages.
361
         */
362
        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...
363
            if ( ! empty( $this->_module_unique_affix ) ) {
364
                $message = fs_apply_filter( $this->_module_unique_affix, "sticky_message_{$id}", $message );
365
                $title   = fs_apply_filter( $this->_module_unique_affix, "sticky_title_{$id}", $title );
366
            }
367
368
            $this->add( $message, $title, $type, true, $id, true, $wp_user_id, $plugin_title, $is_network_and_blog_admins );
369
        }
370
371
        /**
372
         * Clear all sticky messages.
373
         *
374
         * @author Vova Feldman (@svovaf)
375
         * @since  1.0.8
376
         */
377
        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...
378
            $this->_sticky_storage->clear_all();
379
        }
380
381
        #--------------------------------------------------------------------------------
382
        #region Helper Method
383
        #--------------------------------------------------------------------------------
384
385
        /**
386
         * @author Vova Feldman (@svovaf)
387
         * @since  2.0.0
388
         *
389
         * @return string
390
         */
391
        private function get_notices_type() {
392
            return $this->_is_network_notices ?
393
                'network_admin_notices' :
394
                'admin_notices';
395
        }
396
397
        #endregion
398
    }