Total Complexity | 58 |
Total Lines | 525 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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.4.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 License Actions class. |
||
112 | * |
||
113 | * @since 6.0.0 |
||
114 | * @access public |
||
115 | * @var MonsterInsights_License_Actions $license_actions Instance of License Actions class. |
||
116 | */ |
||
117 | public $license_actions; |
||
118 | |||
119 | /** |
||
120 | * Holds instance of MonsterInsights Admin Notice class. |
||
121 | * |
||
122 | * @since 6.0.0 |
||
123 | * @access public |
||
124 | * @var MonsterInsights_Admin_Notice $notices Instance of Admin Notice class. |
||
|
|||
125 | */ |
||
126 | public $notices; |
||
127 | |||
128 | /** |
||
129 | * Holds instance of MonsterInsights Reporting class. |
||
130 | * |
||
131 | * @since 6.0.0 |
||
132 | * @access public |
||
133 | * @var MonsterInsights_Reporting $reporting Instance of Reporting class. |
||
134 | */ |
||
135 | public $reporting; |
||
136 | |||
137 | /** |
||
138 | * Holds instance of MonsterInsights Auth class. |
||
139 | * |
||
140 | * @since 7.0.0 |
||
141 | * @access public |
||
142 | * @var MonsterInsights_Auth $auth Instance of Auth class. |
||
143 | */ |
||
144 | protected $auth; |
||
145 | |||
146 | /** |
||
147 | * Holds instance of MonsterInsights API Auth class. |
||
148 | * |
||
149 | * @since 6.0.0 |
||
150 | * @access public |
||
151 | * @var MonsterInsights_Auth $api_auth Instance of APIAuth class. |
||
152 | */ |
||
153 | public $api_auth; |
||
154 | |||
155 | /** |
||
156 | * Holds instance of MonsterInsights API Rest Routes class. |
||
157 | * |
||
158 | * @since 7.4.0 |
||
159 | * @access public |
||
160 | * @var MonsterInsights_Rest_Routes $routes Instance of rest routes. |
||
161 | */ |
||
162 | public $routes; |
||
163 | |||
164 | /** |
||
165 | * Primary class constructor. |
||
166 | * |
||
167 | * @since 6.0.0 |
||
168 | * @access public |
||
169 | */ |
||
170 | public function __construct() { |
||
171 | // We don't use this |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the singleton instance of the class. |
||
176 | * |
||
177 | * @access public |
||
178 | * @since 6.0.0 |
||
179 | * |
||
180 | * @return object The MonsterInsights_Lite object. |
||
181 | */ |
||
182 | public static function get_instance() { |
||
183 | |||
184 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof MonsterInsights_Lite ) ) { |
||
185 | self::$instance = new MonsterInsights_Lite(); |
||
186 | self::$instance->file = __FILE__; |
||
187 | |||
188 | global $wp_version; |
||
189 | |||
190 | // Detect non-supported WordPress version and return early |
||
191 | if ( version_compare( $wp_version, '3.8', '<' ) && ( ! defined( 'MONSTERINSIGHTS_FORCE_ACTIVATION' ) || ! MONSTERINSIGHTS_FORCE_ACTIVATION ) ) { |
||
192 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_wp_notice' ) ); |
||
193 | return; |
||
194 | } |
||
195 | |||
196 | // Detect Pro version and return early |
||
197 | if ( defined( 'MONSTERINSIGHTS_PRO_VERSION' ) ) { |
||
198 | add_action( 'admin_notices', array( self::$instance, 'monsterinsights_pro_notice' ) ); |
||
199 | return; |
||
200 | } |
||
201 | |||
202 | // Define constants |
||
203 | self::$instance->define_globals(); |
||
204 | |||
205 | // Load in settings |
||
206 | self::$instance->load_settings(); |
||
207 | |||
208 | // Load in Licensing |
||
209 | self::$instance->load_licensing(); |
||
210 | |||
211 | // Load in Auth |
||
212 | self::$instance->load_auth(); |
||
213 | |||
214 | // Load files |
||
215 | self::$instance->require_files(); |
||
216 | |||
217 | // This does the version to version background upgrade routines and initial install |
||
218 | $mi_version = get_option( 'monsterinsights_current_version', '5.5.3' ); |
||
219 | if ( version_compare( $mi_version, '7.4.0', '<' ) ) { |
||
220 | monsterinsights_lite_call_install_and_upgrade(); |
||
221 | } |
||
222 | |||
223 | if ( is_admin() ) { |
||
224 | new AM_Notification( 'mi-lite', self::$instance->version ); |
||
225 | new AM_Deactivation_Survey( 'MonsterInsights', basename( dirname( __FILE__ ) ) ); |
||
226 | } |
||
227 | |||
228 | // Load the plugin textdomain. |
||
229 | add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ) ); |
||
230 | |||
231 | // Load admin only components. |
||
232 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
233 | self::$instance->notices = new MonsterInsights_Notice_Admin(); |
||
234 | self::$instance->license_actions = new MonsterInsights_License_Actions(); |
||
235 | self::$instance->reporting = new MonsterInsights_Reporting(); |
||
236 | self::$instance->api_auth = new MonsterInsights_API_Auth(); |
||
237 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
||
238 | self::$instance->require_updater(); |
||
239 | } else { |
||
240 | add_action( 'admin_init', array( self::$instance, 'require_updater' ) ); |
||
241 | } |
||
242 | |||
243 | self::$instance->routes = new MonsterInsights_Rest_Routes(); |
||
244 | } |
||
245 | |||
246 | if ( monsterinsights_is_pro_version() ) { |
||
247 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'pro/includes/load.php'; |
||
248 | } else { |
||
249 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'lite/includes/load.php'; |
||
250 | } |
||
251 | |||
252 | // Run hook to load MonsterInsights addons. |
||
253 | do_action( 'monsterinsights_load_plugins' ); // the updater class for each addon needs to be instantiated via `monsterinsights_updater` |
||
254 | } |
||
255 | |||
256 | return self::$instance; |
||
257 | |||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Throw error on object clone |
||
262 | * |
||
263 | * The whole idea of the singleton design pattern is that there is a single |
||
264 | * object therefore, we don't want the object to be cloned. |
||
265 | * |
||
266 | * @since 6.0.0 |
||
267 | * @access public |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | public function __clone() { |
||
272 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Disable unserializing of the class |
||
277 | * |
||
278 | * Attempting to wakeup an MonsterInsights instance will throw a doing it wrong notice. |
||
279 | * |
||
280 | * @since 6.0.0 |
||
281 | * @access public |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | public function __wakeup() { |
||
286 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'google-analytics-for-wordpress' ), '6.0.0' ); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Magic get function. |
||
291 | * |
||
292 | * We use this to lazy load certain functionality. Right now used to lazyload |
||
293 | * the API & Auth frontend, so it's only loaded if user is using a plugin |
||
294 | * that requires it. |
||
295 | * |
||
296 | * @since 7.0.0 |
||
297 | * @access public |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function __get( $key ) { |
||
302 | if ( $key === 'auth' ) { |
||
303 | if ( empty( self::$instance->auth ) ) { |
||
304 | // LazyLoad Auth for Frontend |
||
305 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/auth.php'; |
||
306 | self::$instance->auth = new MonsterInsights_Auth(); |
||
307 | } |
||
308 | return self::$instance->$key; |
||
309 | } else if ( $key === 'license' ) { |
||
310 | if ( empty( self::$instance->license ) ) { |
||
311 | // LazyLoad Licensing for Frontend |
||
312 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/license.php'; |
||
313 | self::$instance->license = new MonsterInsights_License(); |
||
314 | } |
||
315 | return self::$instance->$key; |
||
316 | } else { |
||
317 | return self::$instance->$key; |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Define MonsterInsights constants. |
||
323 | * |
||
324 | * This function defines all of the MonsterInsights PHP constants. |
||
325 | * |
||
326 | * @since 6.0.0 |
||
327 | * @access public |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function define_globals() { |
||
332 | |||
333 | if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) { |
||
334 | define( 'MONSTERINSIGHTS_VERSION', $this->version ); |
||
335 | } |
||
336 | |||
337 | if ( ! defined( 'MONSTERINSIGHTS_LITE_VERSION' ) ) { |
||
338 | define( 'MONSTERINSIGHTS_LITE_VERSION', MONSTERINSIGHTS_VERSION ); |
||
339 | } |
||
340 | |||
341 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_NAME' ) ) { |
||
342 | define( 'MONSTERINSIGHTS_PLUGIN_NAME', $this->plugin_name ); |
||
343 | } |
||
344 | |||
345 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_SLUG' ) ) { |
||
346 | define( 'MONSTERINSIGHTS_PLUGIN_SLUG', $this->plugin_slug ); |
||
347 | } |
||
348 | |||
349 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_FILE' ) ) { |
||
350 | define( 'MONSTERINSIGHTS_PLUGIN_FILE', $this->file ); |
||
351 | } |
||
352 | |||
353 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_DIR' ) ) { |
||
354 | define( 'MONSTERINSIGHTS_PLUGIN_DIR', plugin_dir_path( $this->file ) ); |
||
355 | } |
||
356 | |||
357 | if ( ! defined( 'MONSTERINSIGHTS_PLUGIN_URL' ) ) { |
||
358 | define( 'MONSTERINSIGHTS_PLUGIN_URL', plugin_dir_url( $this->file ) ); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Loads the plugin textdomain for translation. |
||
364 | * |
||
365 | * @access public |
||
366 | * @since 6.0.0 |
||
367 | * |
||
368 | * @return void |
||
369 | */ |
||
370 | public function load_plugin_textdomain() { |
||
371 | |||
372 | $mi_locale = get_locale(); |
||
373 | if ( function_exists( 'get_user_locale' ) ) { |
||
374 | $mi_locale = get_user_locale(); |
||
375 | } |
||
376 | |||
377 | // Traditional WordPress plugin locale filter. |
||
378 | $mi_locale = apply_filters( 'plugin_locale', $mi_locale, 'google-analytics-for-wordpress' ); |
||
379 | $mi_mofile = sprintf( '%1$s-%2$s.mo', 'google-analytics-for-wordpress', $mi_locale ); |
||
380 | |||
381 | // Look for wp-content/languages/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
382 | $mi_mofile1 = WP_LANG_DIR . '/google-analytics-for-wordpress/' . $mi_mofile; |
||
383 | |||
384 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress/google-analytics-for-wordpress-{lang}_{country}.mo |
||
385 | $mi_mofile2 = WP_LANG_DIR . '/plugins/google-analytics-for-wordpress/' . $mi_mofile; |
||
386 | |||
387 | // Look in wp-content/languages/plugins/google-analytics-for-wordpress-{lang}_{country}.mo |
||
388 | $mi_mofile3 = WP_LANG_DIR . '/plugins/' . $mi_mofile; |
||
389 | |||
390 | // Look in wp-content/plugins/google-analytics-for-wordpress/languages/google-analytics-for-wordpress-{lang}_{country}.mo |
||
391 | $mi_mofile4 = dirname( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ) . '/languages/'; |
||
392 | $mi_mofile4 = apply_filters( 'monsterinsights_lite_languages_directory', $mi_mofile4 ); |
||
393 | |||
394 | if ( file_exists( $mi_mofile1 ) ) { |
||
395 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile1 ); |
||
396 | } elseif ( file_exists( $mi_mofile2 ) ) { |
||
397 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile2 ); |
||
398 | } elseif ( file_exists( $mi_mofile3 ) ) { |
||
399 | load_textdomain( 'google-analytics-for-wordpress', $mi_mofile3 ); |
||
400 | } else { |
||
401 | load_plugin_textdomain( 'google-analytics-for-wordpress', false, $mi_mofile4 ); |
||
402 | } |
||
403 | |||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Output a nag notice if the user has an out of date WP version installed |
||
408 | * |
||
409 | * @access public |
||
410 | * @since 6.0.0 |
||
411 | * |
||
412 | * @return void |
||
413 | */ |
||
414 | public function monsterinsights_wp_notice() { |
||
415 | $url = admin_url( 'plugins.php' ); |
||
416 | // Check for MS dashboard |
||
417 | if( is_network_admin() ) { |
||
418 | $url = network_admin_url( 'plugins.php' ); |
||
419 | } |
||
420 | ?> |
||
421 | <div class="error"> |
||
422 | <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> |
||
423 | </div> |
||
424 | <?php |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Output a nag notice if the user has both Lite and Pro activated |
||
429 | * |
||
430 | * @access public |
||
431 | * @since 6.0.0 |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | public function monsterinsights_pro_notice() { |
||
436 | $url = admin_url( 'plugins.php' ); |
||
437 | // Check for MS dashboard |
||
438 | if( is_network_admin() ) { |
||
439 | $url = network_admin_url( 'plugins.php' ); |
||
440 | } |
||
441 | ?> |
||
442 | <div class="error"> |
||
443 | <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> |
||
444 | </div> |
||
445 | <?php |
||
446 | |||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Loads MonsterInsights settings |
||
451 | * |
||
452 | * Adds the items to the base object, and adds the helper functions. |
||
453 | * |
||
454 | * @since 6.0.0 |
||
455 | * @access public |
||
456 | * |
||
457 | * @return void |
||
458 | */ |
||
459 | public function load_settings() { |
||
460 | global $monsterinsights_settings; |
||
461 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/options.php'; |
||
462 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/helpers.php'; |
||
463 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/deprecated.php'; |
||
464 | $monsterinsights_settings = monsterinsights_get_options(); |
||
465 | } |
||
466 | |||
467 | |||
468 | /** |
||
469 | * Loads MonsterInsights License |
||
470 | * |
||
471 | * Loads license class used by MonsterInsights |
||
472 | * |
||
473 | * @since 7.0.0 |
||
474 | * @access public |
||
475 | * |
||
476 | * @return void |
||
477 | */ |
||
478 | public function load_licensing(){ |
||
479 | if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
||
480 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/license.php'; |
||
481 | self::$instance->license = new MonsterInsights_License(); |
||
482 | } |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Loads MonsterInsights Auth |
||
487 | * |
||
488 | * Loads auth used by MonsterInsights |
||
489 | * |
||
490 | * @since 7.0.0 |
||
491 | * @access public |
||
492 | * |
||
493 | * @return void |
||
494 | */ |
||
495 | public function load_auth() { |
||
499 | } |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Loads all files into scope. |
||
504 | * |
||
505 | * @access public |
||
506 | * @since 6.0.0 |
||
507 | * |
||
508 | * @return void |
||
509 | */ |
||
510 | public function require_files() { |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Loads all updater related files and functions into scope. |
||
560 | * |
||
561 | * @access public |
||
562 | * @since 6.0.0 |
||
563 | * |
||
564 | * @return null Return early if the license key is not set or there are key errors. |
||
565 | */ |
||
566 | public function require_updater() { |
||
579 | } |
||
580 | } |
||
581 | |||
792 |
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