Total Complexity | 54 |
Total Lines | 527 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 = '8.0.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 | // Detect Pro version and return early |
||
207 | if ( defined( 'MONSTERINSIGHTS_PRO_VERSION' ) ) { |
||
208 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_pro_notice' ) ); |
||
209 | return self::$instance; |
||
210 | } |
||
211 | |||
212 | if ( ! self::$instance->check_compatibility() ) { |
||
213 | return self::$instance; |
||
214 | } |
||
215 | |||
216 | // Define constants |
||
217 | self::$instance->define_globals(); |
||
218 | |||
219 | // Load in settings |
||
220 | self::$instance->load_settings(); |
||
221 | |||
222 | // Load in Licensing |
||
223 | self::$instance->load_licensing(); |
||
224 | |||
225 | // Load in Auth |
||
226 | self::$instance->load_auth(); |
||
227 | |||
228 | // Load files |
||
229 | self::$instance->require_files(); |
||
230 | |||
231 | // This does the version to version background upgrade routines and initial install |
||
232 | $mi_version = get_option( 'monsterinsights_current_version', '5.5.3' ); |
||
233 | if ( version_compare( $mi_version, '7.15.0', '<' ) ) { |
||
234 | monsterinsights_lite_call_install_and_upgrade(); |
||
235 | } |
||
236 | |||
237 | if ( is_admin() ) { |
||
238 | new AM_Deactivation_Survey( 'MonsterInsights', basename( dirname( __FILE__ ) ) ); |
||
239 | } |
||
240 | |||
241 | // Load the plugin textdomain. |
||
242 | add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ), 15 ); |
||
243 | |||
244 | // Load admin only components. |
||
245 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
246 | self::$instance->notices = new MonsterInsights_Notice_Admin(); |
||
247 | self::$instance->reporting = new MonsterInsights_Reporting(); |
||
248 | self::$instance->api_auth = new MonsterInsights_API_Auth(); |
||
249 | self::$instance->routes = new MonsterInsights_Rest_Routes(); |
||
250 | self::$instance->notifications = new MonsterInsights_Notifications(); |
||
251 | self::$instance->notification_event = new MonsterInsights_Notification_Event(); |
||
252 | } |
||
253 | |||
254 | if ( monsterinsights_is_pro_version() ) { |
||
255 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/load.php'; |
||
256 | } else { |
||
257 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/load.php'; |
||
258 | } |
||
259 | |||
260 | // Run hook to load MonsterInsights addons. |
||
261 | do_action( 'monsterinsights_load_plugins' ); // the updater class for each addon needs to be instantiated via `monsterinsights_updater` |
||
262 | } |
||
263 | |||
264 | return self::$instance; |
||
265 | |||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Throw error on object clone |
||
270 | * |
||
271 | * The whole idea of the singleton design pattern is that there is a single |
||
272 | * object therefore, we don't want the object to be cloned. |
||
273 | * |
||
274 | * @since 6.0.0 |
||
275 | * @access public |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | public function __clone() { |
||
280 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Disable unserializing of the class |
||
285 | * |
||
286 | * Attempting to wakeup an MonsterInsights instance will throw a doing it wrong notice. |
||
287 | * |
||
288 | * @since 6.0.0 |
||
289 | * @access public |
||
290 | * |
||
291 | * @return void |
||
292 | */ |
||
293 | public function __wakeup() { |
||
294 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Magic get function. |
||
299 | * |
||
300 | * We use this to lazy load certain functionality. Right now used to lazyload |
||
301 | * the API & Auth frontend, so it's only loaded if user is using a plugin |
||
302 | * that requires it. |
||
303 | * |
||
304 | * @since 7.0.0 |
||
305 | * @access public |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | public function __get( $key ) { |
||
310 | if ( $key === 'auth' ) { |
||
311 | if ( empty( self::$instance->auth ) ) { |
||
312 | // LazyLoad Auth for Frontend |
||
313 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/auth.php'; |
||
314 | self::$instance->auth = new MonsterInsights_Auth(); |
||
315 | } |
||
316 | return self::$instance->$key; |
||
317 | } else { |
||
318 | return self::$instance->$key; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Check compatibility with PHP and WP, and display notices if necessary |
||
324 | * |
||
325 | * @since 8.0.0 |
||
326 | * @return bool |
||
327 | */ |
||
328 | private function check_compatibility() { |
||
329 | if ( defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) && MONSTERINSIGHTS_FORCE_ACTIVATION ) { |
||
330 | return true; |
||
331 | } |
||
332 | |||
333 | require_once plugin_dir_path( __FILE__ ) . 'includes/compatibility-check.php'; |
||
334 | $compatibility = MonsterInsights_Compatibility_Check::get_instance(); |
||
335 | $compatibility->maybe_display_notice(); |
||
336 | |||
337 | return $compatibility->is_php_compatible() && $compatibility->is_wp_compatible(); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Define MonsterInsights constants. |
||
342 | * |
||
343 | * This function defines all of the MonsterInsights PHP constants. |
||
344 | * |
||
345 | * @since 6.0.0 |
||
346 | * @access public |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | public function define_globals() { |
||
351 | |||
352 | if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) { |
||
353 | define( 'MONSTERINSIGHTS_VERSION', $this->version ); |
||
354 | } |
||
355 | |||
356 | if ( ! defined( 'MONSTERINSIGHTS_LITE_VERSION' ) ) { |
||
357 | define( 'MONSTERINSIGHTS_LITE_VERSION', MONSTERINSIGHTS_VERSION ); |
||
358 | } |
||
359 | |||
360 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_NAME' ) ) { |
||
361 | define( 'MONSTERINSIGHTS_PLUGIN_NAME', $this->plugin_name ); |
||
362 | } |
||
363 | |||
364 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_SLUG' ) ) { |
||
365 | define( 'MONSTERINSIGHTS_PLUGIN_SLUG', $this->plugin_slug ); |
||
366 | } |
||
367 | |||
368 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_FILE' ) ) { |
||
369 | define( 'MONSTERINSIGHTS_PLUGIN_FILE', $this->file ); |
||
370 | } |
||
371 | |||
372 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_DIR' ) ) { |
||
373 | define( 'MONSTERINSIGHTS_PLUGIN_DIR', plugin_dir_path( $this->file ) ); |
||
374 | } |
||
375 | |||
376 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_URL' ) ) { |
||
377 | define( 'MONSTERINSIGHTS_PLUGIN_URL', plugin_dir_url( $this->file ) ); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Loads the plugin textdomain for translation. |
||
383 | * |
||
384 | * @access public |
||
385 | * @since 6.0.0 |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | public function load_plugin_textdomain() { |
||
390 | |||
391 | $mi_locale = get_locale(); |
||
392 | if ( function_exists( 'get_user_locale' ) ) { |
||
393 | $mi_locale = get_user_locale(); |
||
394 | } |
||
395 | |||
396 | // Traditional WordPress plugin locale filter. |
||
397 | $mi_locale = apply_filters( 'plugin_locale', $mi_locale, 'google-analytics-for-wordpress' ); |
||
398 | $mi_mofile = sprintf( '%1$s-%2$s.mo', 'google-analytics-for-wordpress', $mi_locale ); |
||
399 | |||
400 | // Look for wp-content/languages/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
401 | $mi_mofile1 = WP_LANG_DIR . '/google-analytics-for-wordpress/' . $mi_mofile; |
||
402 | |||
403 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
404 | $mi_mofile2 = WP_LANG_DIR . '/plugins/google-analytics-for-wordpress/' . $mi_mofile; |
||
405 | |||
406 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress-{lang}_{country}.mo |
||
407 | $mi_mofile3 = WP_LANG_DIR . '/plugins/' . $mi_mofile; |
||
408 | |||
409 | // Look in wp-content/plugins/google-analytics-for-wordpress/languages/google-analytics-for-wordpress-{lang}_{country}.mo |
||
410 | $mi_mofile4 = dirname( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ) . '/languages/'; |
||
411 | $mi_mofile4 = apply_filters( 'monsterinsights_lite_languages_directory', $mi_mofile4 ); |
||
412 | |||
413 | if ( file_exists( $mi_mofile1 ) ) { |
||
414 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile1 ); |
||
415 | } elseif ( file_exists( $mi_mofile2 ) ) { |
||
416 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile2 ); |
||
417 | } elseif ( file_exists( $mi_mofile3 ) ) { |
||
418 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile3 ); |
||
419 | } else { |
||
420 | load_plugin_textdomain( 'google-analytics-for-wordpress', false, $mi_mofile4 ); |
||
421 | } |
||
422 | |||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Output a nag notice if the user has both Lite and Pro activated |
||
427 | * |
||
428 | * @access public |
||
429 | * @since 6.0.0 |
||
430 | * |
||
431 | * @return void |
||
432 | */ |
||
433 | public function monsterinsights_pro_notice() { |
||
434 | $url = admin_url( 'plugins.php' ); |
||
435 | // Check for MS dashboard |
||
436 | if( is_network_admin() ) { |
||
437 | $url = network_admin_url( 'plugins.php' ); |
||
438 | } |
||
439 | ?> |
||
440 | <div class="error"> |
||
441 | <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> |
||
442 | </div> |
||
443 | <?php |
||
444 | |||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Loads MonsterInsights settings |
||
449 | * |
||
450 | * Adds the items to the base object, and adds the helper functions. |
||
451 | * |
||
452 | * @since 6.0.0 |
||
453 | * @access public |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function load_settings() { |
||
458 | global $monsterinsights_settings; |
||
459 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/options.php'; |
||
460 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/helpers.php'; |
||
461 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/deprecated.php'; |
||
462 | $monsterinsights_settings = monsterinsights_get_options(); |
||
463 | } |
||
464 | |||
465 | |||
466 | /** |
||
467 | * Loads MonsterInsights License |
||
468 | * |
||
469 | * Loads license class used by MonsterInsights |
||
470 | * |
||
471 | * @since 7.0.0 |
||
472 | * @access public |
||
473 | * |
||
474 | * @return void |
||
475 | */ |
||
476 | public function load_licensing(){ |
||
477 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
478 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/license-compat.php'; |
||
479 | self::$instance->license = new MonsterInsights_License_Compat(); |
||
480 | } |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Loads MonsterInsights Auth |
||
485 | * |
||
486 | * Loads auth used by MonsterInsights |
||
487 | * |
||
488 | * @since 7.0.0 |
||
489 | * @access public |
||
490 | * |
||
491 | * @return void |
||
492 | */ |
||
493 | public function load_auth() { |
||
497 | } |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Loads all files into scope. |
||
502 | * |
||
503 | * @access public |
||
504 | * @since 6.0.0 |
||
505 | * |
||
506 | * @return void |
||
507 | */ |
||
508 | public function require_files() { |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Get the tracking mode for the frontend scripts. |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | public function get_tracking_mode() { |
||
581 | } |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Fired when the plugin is activated. |
||
586 | * |
||
587 | * @access public |
||
588 | * @since 6.0.0 |
||
589 | * |
||
824 |
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