Passed
Push — master ( 8403c0...3ede7f )
by Chris
08:42
created

MonsterInsights_Lite::get_instance()   B

Complexity

Conditions 11
Paths 19

Size

Total Lines 65
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 11
eloc 32
c 4
b 0
f 0
nc 19
nop 0
dl 0
loc 65
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Plugin Name:         Google Analytics for WordPress by MonsterInsights
4
 * Plugin URI:          https://www.monsterinsights.com/?utm_source=liteplugin&utm_medium=pluginheader&utm_campaign=pluginurl&utm_content=7%2E0%2E0
5
 * Description:         The best Google Analytics plugin for WordPress. See how visitors find and use your website, so you can keep them coming back.
6
 * Author:              MonsterInsights
7
 * Author URI:          https://www.monsterinsights.com/?utm_source=liteplugin&utm_medium=pluginheader&utm_campaign=authoruri&utm_content=7%2E0%2E0
8
 *
9
 * Version:             8.0.0
10
 * Requires at least:   4.8.0
11
 * Requires PHP:        5.5
12
 *
13
 * License:             GPL v3
14
 *
15
 * Text Domain:         google-analytics-for-wordpress
16
 * Domain Path:         /languages
17
 *
18
 * MonsterInsights Lite
19
 * Copyright (C) 2008-2018, MonsterInsights, [email protected]
20
 *
21
 * This program is free software: you can redistribute it and/or modify
22
 * it under the terms of the GNU General Public License as published by
23
 * the Free Software Foundation, either version 3 of the License, or
24
 * (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33
 *
34
 * @category            Plugin
35
 * @copyright           Copyright © 2018 Chris Christoff
36
 * @author              Chris Christoff
37
 * @package             MonsterInsights
38
 */
39
40
// Exit if accessed directly.
41
if ( ! defined( 'ABSPATH' ) ) {
42
	exit;
43
}
44
45
/**
46
 * Main plugin class.
47
 *
48
 * @since 6.0.0
49
 *
50
 * @package MonsterInsights
51
 * @author  Chris Christoff
52
 * @access public
53
 */
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.
0 ignored issues
show
Bug introduced by
The type MonsterInsights_Admin_Notice was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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', '<' ) ) {
0 ignored issues
show
Bug introduced by
It seems like $mi_version can also be of type false; however, parameter $version1 of version_compare() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
			if ( version_compare( /** @scrutinizer ignore-type */ $mi_version, '7.15.0', '<' ) ) {
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new MonsterInsights_Notice_Admin() of type MonsterInsights_Notice_Admin is incompatible with the declared type MonsterInsights_Admin_Notice of property $notices.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
247
				self::$instance->reporting          = new MonsterInsights_Reporting();
248
				self::$instance->api_auth           = new MonsterInsights_API_Auth();
0 ignored issues
show
Documentation Bug introduced by
It seems like new MonsterInsights_API_Auth() of type MonsterInsights_API_Auth is incompatible with the declared type MonsterInsights_Auth of property $api_auth.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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&#8217; 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&#8217; 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 ) {
0 ignored issues
show
Bug introduced by
The constant MONSTERINSIGHTS_FORCE_ACTIVATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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() {
494
		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
495
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/auth.php';
496
			self::$instance->auth = new MonsterInsights_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() {
509
510
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/capabilities.php';
511
512
		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
513
514
			// Lite and Pro files
515
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/pandora/class-am-deactivation-survey.php';
516
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/ajax.php';
517
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/admin.php';
518
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/common.php';
519
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notice.php';
520
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/autoupdate.php';
521
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/review.php';
522
523
			// Pages
524
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/settings.php';
525
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/tools.php';
526
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/reports.php';
527
				require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/pages/addons.php';
528
529
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/api-auth.php';
530
531
			// Reports
532
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reports/abstract-report.php';
533
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reports/overview.php';
534
535
			// Reporting Functionality
536
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reporting.php';
537
538
			// Routes used by Vue
539
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/routes.php';
540
541
			// Load gutenberg editor functions
542
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/gutenberg/gutenberg.php';
543
544
			// Emails
545
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/emails/class-emails.php';
546
547
			// Notifications class.
548
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notifications.php';
549
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notification-event.php';
550
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notification-event-runner.php';
551
			// Add notification manual events for lite version.
552
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/notifications/notification-events.php';
553
		}
554
555
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/api-request.php';
556
557
		if ( is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
558
			// Late loading classes (self instantiating)
559
			require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/tracking.php';
560
		}
561
562
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/frontend/frontend.php';
563
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/frontend/seedprod.php';
564
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/measurement-protocol.php';
565
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/measurement-protocol-v4.php';
566
	}
567
568
	/**
569
	 * Get the tracking mode for the frontend scripts.
570
	 *
571
	 * @return string
572
	 */
573
	public function get_tracking_mode() {
574
575
		if ( ! isset( $this->tracking_mode ) ) {
576
			// This will already be set to 'analytics' to anybody already using the plugin before 7.15.0.
577
			$this->tracking_mode = monsterinsights_get_option( 'tracking_mode', 'gtag' );
578
		}
579
580
		return $this->tracking_mode;
581
	}
582
}
583
584
/**
585
 * Fired when the plugin is activated.
586
 *
587
 * @access public
588
 * @since 6.0.0
589
 *
590
 * @global int $wp_version      The version of WordPress for this install.
591
 * @global object $wpdb         The WordPress database object.
592
 * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false otherwise.
593
 *
594
 * @return void
595
 */
596
function monsterinsights_lite_activation_hook( $network_wide ) {
0 ignored issues
show
Unused Code introduced by
The parameter $network_wide is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

596
function monsterinsights_lite_activation_hook( /** @scrutinizer ignore-unused */ $network_wide ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
597
	$url = admin_url( 'plugins.php' );
598
	// Check for MS dashboard
599
	if ( is_network_admin() ) {
600
		$url = network_admin_url( 'plugins.php' );
601
	}
602
603
	if ( class_exists( 'MonsterInsights' ) ) {
604
		deactivate_plugins( plugin_basename( __FILE__ ) );
605
		wp_die( sprintf( esc_html__( 'Please uninstall and remove MonsterInsights Pro before activating Google Analytics for WordPress by MonsterInsights. The Lite version has not been activated. %1$sClick here to return to the Dashboard%2$s.', 'google-analytics-by-wordpress' ), '<a href="' . $url . '">', '</a>' ) );
606
	}
607
608
	require_once plugin_dir_path( __FILE__ ) . 'includes/compatibility-check.php';
609
	$compatibility = MonsterInsights_Compatibility_Check::get_instance();
610
	$compatibility->maybe_deactivate_plugin( plugin_basename( __FILE__ ) );
611
612
	// Add transient to trigger redirect.
613
	set_transient( '_monsterinsights_activation_redirect', 1, 30 );
614
}
615
register_activation_hook( __FILE__, 'monsterinsights_lite_activation_hook' );
616
617
/**
618
 * Fired when the plugin is uninstalled.
619
 *
620
 * @access public
621
 * @since 6.0.0
622
 *
623
 * @return 	void
624
 */
625
function monsterinsights_lite_uninstall_hook() {
626
	wp_cache_flush();
627
628
	// Note, if both MI Pro and Lite are active, this is an MI Pro instance
629
	// Therefore MI Lite can only use functions of the instance common to
630
	// both plugins. If it needs to be pro specific, then include a file that
631
	// has that method.
632
	$instance = MonsterInsights();
633
634
	// If uninstalling via wp-cli load admin-specific files only here.
635
	if ( defined( 'WP_CLI' ) && WP_CLI ) {
0 ignored issues
show
Bug introduced by
The constant WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
636
		define( 'WP_ADMIN', true );
637
		$instance->require_files();
638
		$instance->load_auth();
639
		$instance->notices         = new MonsterInsights_Notice_Admin();
0 ignored issues
show
Documentation Bug introduced by
It seems like new MonsterInsights_Notice_Admin() of type MonsterInsights_Notice_Admin is incompatible with the declared type MonsterInsights_Admin_Notice of property $notices.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
640
		$instance->reporting       = new MonsterInsights_Reporting();
641
		$instance->api_auth        = new MonsterInsights_API_Auth();
0 ignored issues
show
Documentation Bug introduced by
It seems like new MonsterInsights_API_Auth() of type MonsterInsights_API_Auth is incompatible with the declared type MonsterInsights_Auth of property $api_auth.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
642
	}
643
644
	// Don't delete any data if the PRO version is already active.
645
	if ( monsterinsights_is_pro_version() ) {
646
		return;
647
	}
648
649
	require_once 'includes/admin/uninstall.php';
650
651
	if ( is_multisite() ) {
652
		$site_list = get_sites();
653
		foreach ( (array) $site_list as $site ) {
654
			switch_to_blog( $site->blog_id );
655
656
			// Deauthenticate.
657
			$instance->api_auth->delete_auth();
0 ignored issues
show
Bug introduced by
The method delete_auth() does not exist on MonsterInsights_Auth. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

657
			$instance->api_auth->/** @scrutinizer ignore-call */ 
658
                        delete_auth();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
658
659
			// Delete report cache.
660
			$instance->reporting->delete_aggregate_data();
661
662
			// Delete options.
663
			$instance->api_auth->uninstall_auth();
0 ignored issues
show
Bug introduced by
The method uninstall_auth() does not exist on MonsterInsights_Auth. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

663
			$instance->api_auth->/** @scrutinizer ignore-call */ 
664
                        uninstall_auth();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
664
665
			restore_current_blog();
666
		}
667
		// Delete network auth using a custom function as some variables are not initiated.
668
		$instance->api_auth->uninstall_network_auth();
0 ignored issues
show
Bug introduced by
The method uninstall_network_auth() does not exist on MonsterInsights_Auth. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

668
		$instance->api_auth->/** @scrutinizer ignore-call */ 
669
                       uninstall_network_auth();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
669
670
		// Delete network data.
671
		$instance->reporting->delete_aggregate_data( 'network' );
672
	} else {
673
		// Delete auth.
674
		$instance->api_auth->delete_auth();
675
676
		// Delete report cache.
677
		$instance->reporting->delete_aggregate_data();
678
679
		// Delete options.
680
		$instance->api_auth->uninstall_auth();
681
	}
682
683
	// Clear notification cron schedules.
684
	$schedules = wp_get_schedules();
685
686
	if ( is_array( $schedules ) && ! empty( $schedules ) ) {
687
		foreach ( $schedules as $key => $value ) {
688
			if ( 0 === strpos( $key, "monsterinsights_notification_" ) ) {
689
				$cron_hook = implode( "_", explode( "_", $key, - 2 ) ) . '_cron';
690
				wp_clear_scheduled_hook( $cron_hook );
691
			}
692
		}
693
	}
694
695
	// Delete the notifications data.
696
	$instance->notifications->delete_notifications_data();
697
698
	// Delete Popular Posts data.
699
	MonsterInsights_Popular_Posts_Inline()->get_cache()->delete_data();
700
	MonsterInsights_Popular_Posts_Widget()->get_cache()->delete_data();
701
702
	// Delete other options.
703
	monsterinsights_uninstall_remove_options();
704
705
}
706
register_uninstall_hook( __FILE__, 'monsterinsights_lite_uninstall_hook' );
707
708
/**
709
 * The main function responsible for returning the one true MonsterInsights_Lite
710
 * Instance to functions everywhere.
711
 *
712
 * Use this function like you would a global variable, except without needing
713
 * to declare the global.
714
 *
715
 * Example: <?php $monsterinsights = MonsterInsights_Lite(); ?>
716
 *
717
 * @since 6.0.0
718
 *
719
 * @uses MonsterInsights_Lite::get_instance() Retrieve MonsterInsights_Lite instance.
720
 *
721
 * @return MonsterInsights_Lite The singleton MonsterInsights_Lite instance.
722
 */
723
function MonsterInsights_Lite() {
724
	return MonsterInsights_Lite::get_instance();
725
}
726
727
/**
728
 * MonsterInsights Install and Updates.
729
 *
730
 * This function is used install and upgrade MonsterInsights. This is used for upgrade routines
731
 * that can be done automatically, behind the scenes without the need for user interaction
732
 * (for example pagination or user input required), as well as the initial install.
733
 *
734
 * @since 6.0.0
735
 * @access public
736
 *
737
 * @global string $wp_version WordPress version (provided by WordPress core).
738
 * @uses MonsterInsights_Lite::load_settings() Loads MonsterInsights settings
739
 * @uses MonsterInsights_Install::init() Runs upgrade process
740
 *
741
 * @return void
742
 */
743
function monsterinsights_lite_install_and_upgrade() {
744
	require_once plugin_dir_path( __FILE__ ) . 'includes/compatibility-check.php';
745
	$compatibility = MonsterInsights_Compatibility_Check::get_instance();
746
747
	// If the WordPress site doesn't meet the correct WP or PHP version requirements, don't activate MonsterInsights
748
	if ( ! $compatibility->is_php_compatible() || ! $compatibility->is_wp_compatible() ) {
749
		if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
750
			return;
751
		}
752
	}
753
754
	// Don't run if MI Pro is installed
755
	if ( class_exists( 'MonsterInsights' ) ) {
756
		if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
757
			return;
758
		}
759
	}
760
761
762
	// Load settings and globals (so we can use/set them during the upgrade process)
763
	MonsterInsights_Lite()->define_globals();
764
	MonsterInsights_Lite()->load_settings();
765
766
	// Load in Auth
767
	MonsterInsights()->load_auth();
768
769
	// Load upgrade file
770
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/install.php';
771
772
	// Run the MonsterInsights upgrade routines
773
	$updates = new MonsterInsights_Install();
774
	$updates->init();
775
}
776
777
/**
778
 * MonsterInsights check for install and update processes.
779
 *
780
 * This function is used to call the MonsterInsights automatic upgrade class, which in turn
781
 * checks to see if there are any update procedures to be run, and if
782
 * so runs them. Also installs MonsterInsights for the first time.
783
 *
784
 * @since 6.0.0
785
 * @access public
786
 *
787
 * @uses MonsterInsights_Install() Runs install and upgrade process.
788
 *
789
 * @return void
790
 */
791
function monsterinsights_lite_call_install_and_upgrade(){
792
	add_action( 'wp_loaded', 'monsterinsights_lite_install_and_upgrade' );
793
}
794
795
/**
796
 * Returns the MonsterInsights combined object that you can use for both
797
 * MonsterInsights Lite and Pro Users. When both plugins active, defers to the
798
 * more complete Pro object.
799
 *
800
 * Warning: Do not use this in Lite or Pro specific code (use the individual objects instead).
801
 * Also do not use in the MonsterInsights Lite/Pro upgrade and install routines.
802
 *
803
 * Use this function like you would a global variable, except without needing
804
 * to declare the global.
805
 *
806
 * Prevents the need to do conditional global object logic when you have code that you want to work with
807
 * both Pro and Lite.
808
 *
809
 * Example: <?php $monsterinsights = MonsterInsights(); ?>
810
 *
811
 * @since 6.0.0
812
 *
813
 * @uses MonsterInsights::get_instance() Retrieve MonsterInsights Pro instance.
814
 * @uses MonsterInsights_Lite::get_instance() Retrieve MonsterInsights Lite instance.
815
 *
816
 * @return MonsterInsights The singleton MonsterInsights instance.
817
 */
818
if ( ! function_exists( 'MonsterInsights' ) ) {
819
	function MonsterInsights() {
820
		return ( class_exists( 'MonsterInsights' ) ? MonsterInsights_Pro() : MonsterInsights_Lite() );
0 ignored issues
show
Bug introduced by
The function MonsterInsights_Pro was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

820
		return ( class_exists( 'MonsterInsights' ) ? /** @scrutinizer ignore-call */ MonsterInsights_Pro() : MonsterInsights_Lite() );
Loading history...
821
	}
822
	add_action( 'plugins_loaded', 'MonsterInsights' );
823
}
824