Total Complexity | 54 |
Total Lines | 533 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Lite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Lite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | final class MonsterInsights_Lite { |
||
55 | |||
56 | /** |
||
57 | * Holds the class object. |
||
58 | * |
||
59 | * @since 6.0.0 |
||
60 | * @access public |
||
61 | * @var object Instance of instantiated MonsterInsights class. |
||
62 | */ |
||
63 | public static $instance; |
||
64 | |||
65 | /** |
||
66 | * Plugin version, used for cache-busting of style and script file references. |
||
67 | * |
||
68 | * @since 6.0.0 |
||
69 | * @access public |
||
70 | * @var string $version Plugin version. |
||
71 | */ |
||
72 | public $version = '7.15.0'; |
||
73 | |||
74 | /** |
||
75 | * Plugin file. |
||
76 | * |
||
77 | * @since 6.0.0 |
||
78 | * @access public |
||
79 | * @var string $file PHP File constant for main file. |
||
80 | */ |
||
81 | public $file; |
||
82 | |||
83 | /** |
||
84 | * The name of the plugin. |
||
85 | * |
||
86 | * @since 6.0.0 |
||
87 | * @access public |
||
88 | * @var string $plugin_name Plugin name. |
||
89 | */ |
||
90 | public $plugin_name = 'MonsterInsights Lite'; |
||
91 | |||
92 | /** |
||
93 | * Unique plugin slug identifier. |
||
94 | * |
||
95 | * @since 6.0.0 |
||
96 | * @access public |
||
97 | * @var string $plugin_slug Plugin slug. |
||
98 | */ |
||
99 | public $plugin_slug = 'monsterinsights-lite'; |
||
100 | |||
101 | /** |
||
102 | * Holds instance of MonsterInsights License class. |
||
103 | * |
||
104 | * @since 6.0.0 |
||
105 | * @access public |
||
106 | * @var MonsterInsights_License $license Instance of License class. |
||
107 | */ |
||
108 | protected $license; |
||
109 | |||
110 | /** |
||
111 | * Holds instance of MonsterInsights Admin Notice class. |
||
112 | * |
||
113 | * @since 6.0.0 |
||
114 | * @access public |
||
115 | * @var MonsterInsights_Admin_Notice $notices Instance of Admin Notice class. |
||
|
|||
116 | */ |
||
117 | public $notices; |
||
118 | |||
119 | /** |
||
120 | * Holds instance of MonsterInsights Reporting class. |
||
121 | * |
||
122 | * @since 6.0.0 |
||
123 | * @access public |
||
124 | * @var MonsterInsights_Reporting $reporting Instance of Reporting class. |
||
125 | */ |
||
126 | public $reporting; |
||
127 | |||
128 | /** |
||
129 | * Holds instance of MonsterInsights Notifications class. |
||
130 | * |
||
131 | * @since 7.11 |
||
132 | * @access public |
||
133 | * @var MonsterInsights_Notifications $notifications Instance of Notifications class. |
||
134 | */ |
||
135 | public $notifications; |
||
136 | |||
137 | /** |
||
138 | * Holds instance of MonsterInsights Notification Events |
||
139 | * |
||
140 | * @since 7.12.3 |
||
141 | * @access public |
||
142 | * @var MonsterInsights_Notification_Event $notification_event Instance of MonsterInsights_Notification_Event class. |
||
143 | */ |
||
144 | public $notification_event; |
||
145 | |||
146 | /** |
||
147 | * Holds instance of MonsterInsights Auth class. |
||
148 | * |
||
149 | * @since 7.0.0 |
||
150 | * @access public |
||
151 | * @var MonsterInsights_Auth $auth Instance of Auth class. |
||
152 | */ |
||
153 | protected $auth; |
||
154 | |||
155 | /** |
||
156 | * Holds instance of MonsterInsights API Auth class. |
||
157 | * |
||
158 | * @since 6.0.0 |
||
159 | * @access public |
||
160 | * @var MonsterInsights_Auth $api_auth Instance of APIAuth class. |
||
161 | */ |
||
162 | public $api_auth; |
||
163 | |||
164 | /** |
||
165 | * Holds instance of MonsterInsights API Rest Routes class. |
||
166 | * |
||
167 | * @since 7.4.0 |
||
168 | * @access public |
||
169 | * @var MonsterInsights_Rest_Routes $routes Instance of rest routes. |
||
170 | */ |
||
171 | public $routes; |
||
172 | |||
173 | /** |
||
174 | * The tracking mode used in the frontend. |
||
175 | * |
||
176 | * @since 7.15.0 |
||
177 | * @accces public |
||
178 | * @var string |
||
179 | */ |
||
180 | public $tracking_mode; |
||
181 | |||
182 | /** |
||
183 | * Primary class constructor. |
||
184 | * |
||
185 | * @since 6.0.0 |
||
186 | * @access public |
||
187 | */ |
||
188 | public function __construct() { |
||
189 | // We don't use this |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Returns the singleton instance of the class. |
||
194 | * |
||
195 | * @access public |
||
196 | * @since 6.0.0 |
||
197 | * |
||
198 | * @return object The MonsterInsights_Lite object. |
||
199 | */ |
||
200 | public static function get_instance() { |
||
201 | |||
202 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof MonsterInsights_Lite ) ) { |
||
203 | self::$instance = new MonsterInsights_Lite(); |
||
204 | self::$instance->file = __FILE__; |
||
205 | |||
206 | global $wp_version; |
||
207 | |||
208 | // Detect non-supported WordPress version and return early |
||
209 | if ( version_compare( $wp_version, '3.8', '<' ) && ( ! defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) || ! MONSTERINSIGHTS_FORCE_ACTIVATION ) ) { |
||
210 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_wp_notice' ) ); |
||
211 | return; |
||
212 | } |
||
213 | |||
214 | // Detect Pro version and return early |
||
215 | if ( defined( 'MONSTERINSIGHTS_PRO_VERSION' ) ) { |
||
216 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_pro_notice' ) ); |
||
217 | return; |
||
218 | } |
||
219 | |||
220 | // Define constants |
||
221 | self::$instance->define_globals(); |
||
222 | |||
223 | // Load in settings |
||
224 | self::$instance->load_settings(); |
||
225 | |||
226 | // Load in Licensing |
||
227 | self::$instance->load_licensing(); |
||
228 | |||
229 | // Load in Auth |
||
230 | self::$instance->load_auth(); |
||
231 | |||
232 | // Load files |
||
233 | self::$instance->require_files(); |
||
234 | |||
235 | // This does the version to version background upgrade routines and initial install |
||
236 | $mi_version = get_option( 'monsterinsights_current_version', '5.5.3' ); |
||
237 | if ( version_compare( $mi_version, '7.15.0', '<' ) ) { |
||
238 | monsterinsights_lite_call_install_and_upgrade(); |
||
239 | } |
||
240 | |||
241 | if ( is_admin() ) { |
||
242 | new AM_Deactivation_Survey( 'MonsterInsights', basename( dirname( __FILE__ ) ) ); |
||
243 | } |
||
244 | |||
245 | // Load the plugin textdomain. |
||
246 | add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ), 15 ); |
||
247 | |||
248 | // Load admin only components. |
||
249 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
250 | self::$instance->notices = new MonsterInsights_Notice_Admin(); |
||
251 | self::$instance->reporting = new MonsterInsights_Reporting(); |
||
252 | self::$instance->api_auth = new MonsterInsights_API_Auth(); |
||
253 | self::$instance->routes = new MonsterInsights_Rest_Routes(); |
||
254 | self::$instance->notifications = new MonsterInsights_Notifications(); |
||
255 | self::$instance->notification_event = new MonsterInsights_Notification_Event(); |
||
256 | } |
||
257 | |||
258 | if ( monsterinsights_is_pro_version() ) { |
||
259 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/load.php'; |
||
260 | } else { |
||
261 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/load.php'; |
||
262 | } |
||
263 | |||
264 | // Run hook to load MonsterInsights addons. |
||
265 | do_action( 'monsterinsights_load_plugins' ); // the updater class for each addon needs to be instantiated via `monsterinsights_updater` |
||
266 | } |
||
267 | |||
268 | return self::$instance; |
||
269 | |||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Throw error on object clone |
||
274 | * |
||
275 | * The whole idea of the singleton design pattern is that there is a single |
||
276 | * object therefore, we don't want the object to be cloned. |
||
277 | * |
||
278 | * @since 6.0.0 |
||
279 | * @access public |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | public function __clone() { |
||
284 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Disable unserializing of the class |
||
289 | * |
||
290 | * Attempting to wakeup an MonsterInsights instance will throw a doing it wrong notice. |
||
291 | * |
||
292 | * @since 6.0.0 |
||
293 | * @access public |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | public function __wakeup() { |
||
298 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Magic get function. |
||
303 | * |
||
304 | * We use this to lazy load certain functionality. Right now used to lazyload |
||
305 | * the API & Auth frontend, so it's only loaded if user is using a plugin |
||
306 | * that requires it. |
||
307 | * |
||
308 | * @since 7.0.0 |
||
309 | * @access public |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | public function __get( $key ) { |
||
314 | if ( $key === 'auth' ) { |
||
315 | if ( empty( self::$instance->auth ) ) { |
||
316 | // LazyLoad Auth for Frontend |
||
317 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/auth.php'; |
||
318 | self::$instance->auth = new MonsterInsights_Auth(); |
||
319 | } |
||
320 | return self::$instance->$key; |
||
321 | } else { |
||
322 | return self::$instance->$key; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Define MonsterInsights constants. |
||
328 | * |
||
329 | * This function defines all of the MonsterInsights PHP constants. |
||
330 | * |
||
331 | * @since 6.0.0 |
||
332 | * @access public |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | public function define_globals() { |
||
337 | |||
338 | if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) { |
||
339 | define( 'MONSTERINSIGHTS_VERSION', $this->version ); |
||
340 | } |
||
341 | |||
342 | if ( ! defined( 'MONSTERINSIGHTS_LITE_VERSION' ) ) { |
||
343 | define( 'MONSTERINSIGHTS_LITE_VERSION', MONSTERINSIGHTS_VERSION ); |
||
344 | } |
||
345 | |||
346 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_NAME' ) ) { |
||
347 | define( 'MONSTERINSIGHTS_PLUGIN_NAME', $this->plugin_name ); |
||
348 | } |
||
349 | |||
350 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_SLUG' ) ) { |
||
351 | define( 'MONSTERINSIGHTS_PLUGIN_SLUG', $this->plugin_slug ); |
||
352 | } |
||
353 | |||
354 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_FILE' ) ) { |
||
355 | define( 'MONSTERINSIGHTS_PLUGIN_FILE', $this->file ); |
||
356 | } |
||
357 | |||
358 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_DIR' ) ) { |
||
359 | define( 'MONSTERINSIGHTS_PLUGIN_DIR', plugin_dir_path( $this->file ) ); |
||
360 | } |
||
361 | |||
362 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_URL' ) ) { |
||
363 | define( 'MONSTERINSIGHTS_PLUGIN_URL', plugin_dir_url( $this->file ) ); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Loads the plugin textdomain for translation. |
||
369 | * |
||
370 | * @access public |
||
371 | * @since 6.0.0 |
||
372 | * |
||
373 | * @return void |
||
374 | */ |
||
375 | public function load_plugin_textdomain() { |
||
376 | |||
377 | $mi_locale = get_locale(); |
||
378 | if ( function_exists( 'get_user_locale' ) ) { |
||
379 | $mi_locale = get_user_locale(); |
||
380 | } |
||
381 | |||
382 | // Traditional WordPress plugin locale filter. |
||
383 | $mi_locale = apply_filters( 'plugin_locale', $mi_locale, 'google-analytics-for-wordpress' ); |
||
384 | $mi_mofile = sprintf( '%1$s-%2$s.mo', 'google-analytics-for-wordpress', $mi_locale ); |
||
385 | |||
386 | // Look for wp-content/languages/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
387 | $mi_mofile1 = WP_LANG_DIR . '/google-analytics-for-wordpress/' . $mi_mofile; |
||
388 | |||
389 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
390 | $mi_mofile2 = WP_LANG_DIR . '/plugins/google-analytics-for-wordpress/' . $mi_mofile; |
||
391 | |||
392 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress-{lang}_{country}.mo |
||
393 | $mi_mofile3 = WP_LANG_DIR . '/plugins/' . $mi_mofile; |
||
394 | |||
395 | // Look in wp-content/plugins/google-analytics-for-wordpress/languages/google-analytics-for-wordpress-{lang}_{country}.mo |
||
396 | $mi_mofile4 = dirname( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ) . '/languages/'; |
||
397 | $mi_mofile4 = apply_filters( 'monsterinsights_lite_languages_directory', $mi_mofile4 ); |
||
398 | |||
399 | if ( file_exists( $mi_mofile1 ) ) { |
||
400 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile1 ); |
||
401 | } elseif ( file_exists( $mi_mofile2 ) ) { |
||
402 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile2 ); |
||
403 | } elseif ( file_exists( $mi_mofile3 ) ) { |
||
404 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile3 ); |
||
405 | } else { |
||
406 | load_plugin_textdomain( 'google-analytics-for-wordpress', false, $mi_mofile4 ); |
||
407 | } |
||
408 | |||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Output a nag notice if the user has an out of date WP version installed |
||
413 | * |
||
414 | * @access public |
||
415 | * @since 6.0.0 |
||
416 | * |
||
417 | * @return void |
||
418 | */ |
||
419 | public function monsterinsights_wp_notice() { |
||
420 | $url = admin_url( 'plugins.php' ); |
||
421 | // Check for MS dashboard |
||
422 | if( is_network_admin() ) { |
||
423 | $url = network_admin_url( 'plugins.php' ); |
||
424 | } |
||
425 | ?> |
||
426 | <div class="error"> |
||
427 | <p><?php echo sprintf( esc_html__( 'Sorry, but your version of WordPress does not meet MonsterInsights\'s required version of %1$s3.8%2$s to run properly. The plugin not been activated. %3$sClick here to return to the Dashboard%4$s.', 'google-analytics-for-wordpress' ), '<strong>', '</strong>', '<a href="' . $url . '">', '</a>' ); ?></p> |
||
428 | </div> |
||
429 | <?php |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Output a nag notice if the user has both Lite and Pro activated |
||
434 | * |
||
435 | * @access public |
||
436 | * @since 6.0.0 |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | public function monsterinsights_pro_notice() { |
||
441 | $url = admin_url( 'plugins.php' ); |
||
442 | // Check for MS dashboard |
||
443 | if( is_network_admin() ) { |
||
444 | $url = network_admin_url( 'plugins.php' ); |
||
445 | } |
||
446 | ?> |
||
447 | <div class="error"> |
||
448 | <p><?php echo sprintf( esc_html__( 'Please %1$suninstall%2$s the MonsterInsights Lite Plugin. Your Pro version of MonsterInsights may not work as expected until the Lite version is uninstalled.', 'google-analytics-for-wordpress' ), '<a href="' . $url . '">', '</a>' ); ?></p> |
||
449 | </div> |
||
450 | <?php |
||
451 | |||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Loads MonsterInsights settings |
||
456 | * |
||
457 | * Adds the items to the base object, and adds the helper functions. |
||
458 | * |
||
459 | * @since 6.0.0 |
||
460 | * @access public |
||
461 | * |
||
462 | * @return void |
||
463 | */ |
||
464 | public function load_settings() { |
||
465 | global $monsterinsights_settings; |
||
466 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/options.php'; |
||
467 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/helpers.php'; |
||
468 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/deprecated.php'; |
||
469 | $monsterinsights_settings = monsterinsights_get_options(); |
||
470 | } |
||
471 | |||
472 | |||
473 | /** |
||
474 | * Loads MonsterInsights License |
||
475 | * |
||
476 | * Loads license class used by MonsterInsights |
||
477 | * |
||
478 | * @since 7.0.0 |
||
479 | * @access public |
||
480 | * |
||
481 | * @return void |
||
482 | */ |
||
483 | public function load_licensing(){ |
||
484 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
485 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/license-compat.php'; |
||
486 | self::$instance->license = new MonsterInsights_License_Compat(); |
||
487 | } |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Loads MonsterInsights Auth |
||
492 | * |
||
493 | * Loads auth used by MonsterInsights |
||
494 | * |
||
495 | * @since 7.0.0 |
||
496 | * @access public |
||
497 | * |
||
498 | * @return void |
||
499 | */ |
||
500 | public function load_auth() { |
||
504 | } |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Loads all files into scope. |
||
509 | * |
||
510 | * @access public |
||
511 | * @since 6.0.0 |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | public function require_files() { |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Get the tracking mode for the frontend scripts. |
||
576 | * |
||
577 | * @return string |
||
578 | */ |
||
579 | public function get_tracking_mode() { |
||
587 | } |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Fired when the plugin is activated. |
||
592 | * |
||
593 | * @access public |
||
594 | * @since 6.0.0 |
||
595 | * |
||
815 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths