MonsterInsights_Install::v780_upgrades()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * MonsterInsights Installation and Automatic Upgrades.
4
 *
5
 * This file handles setting up new
6
 * MonsterInsights installs as well as performing
7
 * behind the scene upgrades between
8
 * MonsterInsights versions.
9
 *
10
 * @package MonsterInsights
11
 * @subpackage Install/Upgrade
12
 * @since 6.0.0
13
 */
14
15
// Exit if accessed directly
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * MonsterInsights Install.
22
 *
23
 * This class handles a new MI install
24
 * as well as automatic (non-user initiated)
25
 * upgrade routines.
26
 *
27
 * @since 6.0.0
28
 * @access public
29
 */
30
class MonsterInsights_Install {
31
32
	/**
33
	 * MI Settings.
34
	 *
35
	 * @since 6.0.0
36
	 * @access public
37
	 * @var array $new_settings When the init() function starts, initially
38
	 *                        contains the original settings. At the end
39
	 *                        of init() contains the settings to save.
40
	 */
41
	public $new_settings = array();
42
43
	/**
44
	 * Install/Upgrade routine.
45
	 *
46
	 * This function is what is called to actually install MI data on new installs and to do
47
	 * behind the scenes upgrades on MI upgrades. If this function contains a bug, the results
48
	 * can be catastrophic. This function gets the highest priority in all of MI for unit tests.
49
	 *
50
	 * @return void
51
	 * @since 6.0.0
52
	 * @access public
53
	 *
54
	 */
55
	public function init() {
56
57
		// Get a copy of the current MI settings.
58
		$this->new_settings = get_option( monsterinsights_get_option_name() );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_option(monsterinsights_get_option_name()) can also be of type false. However, the property $new_settings is declared as type array. 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...
59
60
		$version = get_option( 'monsterinsights_current_version', false );
61
		$cachec  = false; // have we forced an object cache to be cleared already (so we don't clear it unnecessarily)
62
63
		// if new install or Yoast Era instal
64
		if ( ! $version ) {
65
			// See if from Yoast
66
			$yoast = get_option( 'yst_ga', false );
67
68
			// In case from Yoast, start from scratch
69
			delete_option( 'yoast-ga-access_token' );
70
			delete_option( 'yoast-ga-refresh_token' );
71
			delete_option( 'yst_ga' );
72
			delete_option( 'yst_ga_api' );
73
74
			$this->new_install();
75
76
			// set db version (Do not increment! See below large comment)
77
			update_option( 'monsterinsights_db_version', '7.4.0' );
78
79
			// Remove Yoast hook if present
80
			if ( wp_next_scheduled( 'yst_ga_aggregate_data' ) ) {
81
				wp_clear_scheduled_hook( 'yst_ga_aggregate_data' );
82
			}
83
84
			// Clear cache since coming from Yoast
85
			if ( ! $cachec && ! empty( $yoast ) ) {
0 ignored issues
show
introduced by
The condition $cachec is always false.
Loading history...
86
				wp_cache_flush();
87
				$cachec = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $cachec is dead and can be removed.
Loading history...
88
			}
89
		} else { // if existing install
90
			if ( version_compare( $version, '6.0.11', '<' ) ) {
91
				if ( ! $cachec ) {
0 ignored issues
show
introduced by
The condition $cachec is always false.
Loading history...
92
					wp_cache_flush();
93
					$cachec = true;
94
				}
95
			}
96
97
			if ( version_compare( $version, '7.0.0', '<' ) ) {
98
				$this->v700_upgrades();
99
			}
100
101
			if ( version_compare( $version, '7.4.0', '<' ) ) {
102
				$this->v740_upgrades();
103
				// Do not increment! See below large comment
104
				update_option( 'monsterinsights_db_version', '7.4.0' );
105
			}
106
107
			if ( version_compare( $version, '7.5.0', '<' ) ) {
108
				$this->v750_upgrades();
109
			}
110
111
			if ( version_compare( $version, '7.6.0', '<' ) ) {
112
				$this->v760_upgrades();
113
			}
114
115
			if ( version_compare( $version, '7.7.1', '<' ) ) {
116
				$this->v771_upgrades();
117
			}
118
119
			if ( version_compare( $version, '7.8.0', '<' ) ) {
120
				$this->v780_upgrades();
121
			}
122
123
			if ( version_compare( $version, '7.9.0', '<' ) ) {
124
				$this->v790_upgrades();
125
			}
126
127
			if ( version_compare( $version, '7.10.0', '<' ) ) {
128
				$this->v7100_upgrades();
129
			}
130
131
			if ( version_compare( $version, '7.11.0', '<' ) ) {
132
				$this->v7110_upgrades();
133
			}
134
135
			if ( version_compare( $version, '7.12.0', '<' ) ) {
136
				$this->v7120_upgrades();
137
			}
138
139
			if ( version_compare( $version, '7.13.0', '<' ) ) {
140
				$this->v7130_upgrades();
141
			}
142
143
			if ( version_compare( $version, '7.13.1', '<' ) ) {
144
				$this->v7131_upgrades();
145
			}
146
147
			if ( version_compare( $version, '7.14.0', '<' ) ) {
148
				$this->v7140_upgrades();
149
			}
150
151
			if ( version_compare( $version, '7.15.0', '<' ) ) {
152
				$this->v7150_upgrades();
153
			}
154
155
			// Do not use. See monsterinsights_after_install_routine comment below.
156
			do_action( 'monsterinsights_after_existing_upgrade_routine', $version );
157
			$version = get_option( 'monsterinsights_current_version', $version );
158
			update_option( 'monsterinsights_version_upgraded_from', $version );
159
		}
160
161
		// This hook is used primarily by the Pro version to run some Pro
162
		// specific install stuff. Please do not use this hook. It is not
163
		// considered a public hook by MI's dev team and can/will be removed,
164
		// relocated, and/or altered without warning at any time. You've been warned.
165
		// As this hook is not for public use, we've intentionally not docbloc'd this
166
		// hook to avoid developers seeing it future public dev docs.
167
		do_action( 'monsterinsights_after_install_routine', $version );
168
169
		// This is the version of MI installed
170
		update_option( 'monsterinsights_current_version', MONSTERINSIGHTS_VERSION );
171
172
		// This is where we save MI settings
173
		update_option( monsterinsights_get_option_name(), $this->new_settings );
174
175
		// There's no code for this function below this. Just an explanation
176
		// of the MI core options.
177
178
		/**
179
		 * Explanation of MonsterInsights core options
180
		 *
181
		 * By now your head is probably spinning trying to figure
182
		 * out what all of these version options are for. Note, I've abbreviated
183
		 * "monsterinsights" to "mi" in the options names to make this table easier
184
		 * to read.
185
		 *
186
		 * Here's a basic rundown:
187
		 *
188
		 * mi_current_version:  This starts with the actual version MI was
189
		 *                        installed on. We use this version to
190
		 *                        determine whether or not a site needs
191
		 *                        to run one of the behind the scenes
192
		 *                        MI upgrade routines. This version is updated
193
		 *                        every time a minor or major background upgrade
194
		 *                        routine is run. Generally lags behind the
195
		 *                        MONSTERINSIGHTS_VERSION constant by at most a couple minor
196
		 *                        versions. Never lags behind by 1 major version
197
		 *                        or more generally.
198
		 *
199
		 * mi_db_version:        This is different from mi_current_version.
200
		 *                        Unlike the former, this is used to determine
201
		 *                        if a site needs to run a *user* initiated
202
		 *                        upgrade routine (incremented in MI_Upgrade class). This
203
		 *                        value is only update when a user initiated
204
		 *                        upgrade routine is done. Because we do very
205
		 *                        few user initiated upgrades compared to
206
		 *                        automatic ones, this version can lag behind by
207
		 *                        2 or even 3 major versions. Generally contains
208
		 *                        the current major version.
209
		 *
210
		 * mi_settings:            Returned by monsterinsights_get_option_name(), this
211
		 *                        is actually "monsterinsights_settings" for both pro
212
		 *                        and lite version. However we use a helper function to
213
		 *                        retrieve the option name in case we ever decide down the
214
		 *                        road to maintain seperate options for the Lite and Pro versions.
215
		 *                        If you need to access MI's settings directly, (as opposed to our
216
		 *                        monsterinsights_get_option helper which uses the option name helper
217
		 *                        automatically), you should use this function to get the
218
		 *                        name of the option to retrieve.
219
		 *
220
		 * Therefore you should never increment mi_db_version in this file and always increment mi_current_version.
221
		 */
222
	}
223
224
225
	/**
226
	 * New MonsterInsights Install routine.
227
	 *
228
	 * This function installs all of the default
229
	 * things on new MI installs. Flight 5476 with
230
	 * non-stop service to a whole world of
231
	 * possibilities is now boarding.
232
	 *
233
	 * @return void
234
	 * @since 6.0.0
235
	 * @access public
236
	 *
237
	 */
238
	public function new_install() {
239
		$this->new_settings = $this->get_monsterinsights_default_values();
240
241
		$this->maybe_import_thirstyaffiliates_options();
242
243
		$data = array(
244
			'installed_version' => MONSTERINSIGHTS_VERSION,
245
			'installed_date'    => time(),
246
			'installed_pro'     => monsterinsights_is_pro_version() ? time() : false,
247
			'installed_lite'    => monsterinsights_is_pro_version() ? false : time(),
248
		);
249
250
		update_option( 'monsterinsights_over_time', $data, false );
251
252
		// Let addons + MI Pro/Lite hook in here. @todo: doc as nonpublic
253
		do_action( 'monsterinsights_after_new_install_routine', MONSTERINSIGHTS_VERSION );
254
	}
255
256
	public function get_monsterinsights_default_values() {
257
258
		$admin_email       = get_option( 'admin_email' );
259
		$admin_email_array = array(
260
			array(
261
				'email' => $admin_email,
262
			),
263
		);
264
265
		return array(
266
			'enable_affiliate_links'                   => true,
267
			'affiliate_links'                          => array(
268
				array(
269
					'path'  => '/go/',
270
					'label' => 'affiliate',
271
				),
272
				array(
273
					'path'  => '/recommend/',
274
					'label' => 'affiliate',
275
				)
276
			),
277
			'demographics'                             => 1,
278
			'ignore_users'                             => array( 'administrator', 'editor' ),
279
			'dashboards_disabled'                      => 0,
280
			'anonymize_ips'                            => 0,
281
			'extensions_of_files'                      => 'doc,pdf,ppt,zip,xls,docx,pptx,xlsx',
282
			'subdomain_tracking'                       => '',
283
			'link_attribution'                         => true,
284
			'tag_links_in_rss'                         => true,
285
			'allow_anchor'                             => 0,
286
			'add_allow_linker'                         => 0,
287
			'save_settings'                            => array( 'administrator' ),
288
			'view_reports'                             => array( 'administrator', 'editor' ),
289
			'events_mode'                              => 'js',
290
			'tracking_mode'                            => 'gtag', // Default new users to gtag.
291
			'email_summaries'                          => 'on',
292
			'summaries_html_template'                  => 'yes',
293
			'summaries_email_addresses'                => $admin_email_array,
294
			'exception_alert_email_addresses'          => $admin_email_array,
295
			'automatic_updates'                        => 'all',
296
			'anonymous_data'                           => 0,
297
			'verified_automatic'                       => 0,
298
			'popular_posts_inline_theme'               => 'alpha',
299
			'popular_posts_widget_theme'               => 'alpha',
300
			'popular_posts_products_theme'             => 'alpha',
301
			'popular_posts_inline_placement'           => 'manual',
302
			'popular_posts_widget_theme_columns'       => '2',
303
			'popular_posts_products_theme_columns'     => '2',
304
			'popular_posts_widget_count'               => '4',
305
			'popular_posts_products_count'             => '4',
306
			'popular_posts_widget_theme_meta_author'   => 'on',
307
			'popular_posts_widget_theme_meta_date'     => 'on',
308
			'popular_posts_widget_theme_meta_comments' => 'on',
309
			'popular_posts_products_theme_meta_price'  => 'on',
310
			'popular_posts_products_theme_meta_rating' => 'on',
311
			'popular_posts_products_theme_meta_image'  => 'on',
312
			'popular_posts_inline_after_count'         => '150',
313
			'popular_posts_inline_multiple_number'     => '3',
314
			'popular_posts_inline_multiple_distance'   => '250',
315
			'popular_posts_inline_multiple_min_words'  => '100',
316
			'popular_posts_inline_post_types'          => array( 'post' ),
317
			'popular_posts_widget_post_types'          => array( 'post' ),
318
		);
319
	}
320
321
	/**
322
	 * Check if ThirstyAffiliates plugin is installed and use the link prefix value in the affiliate settings.
323
	 *
324
	 * @return void
325
	 */
326
	public function maybe_import_thirstyaffiliates_options() {
327
328
		// Check if ThirstyAffiliates is installed.
329
		if ( ! function_exists( 'ThirstyAffiliates' ) ) {
330
			return;
331
		}
332
333
		$link_prefix = get_option( 'ta_link_prefix', 'recommends' );
334
335
		if ( $link_prefix === 'custom' ) {
336
			$link_prefix = get_option( 'ta_link_prefix_custom', 'recommends' );
337
		}
338
339
		if ( ! empty( $link_prefix ) ) {
340
341
			// Check if prefix exists.
342
			$prefix_set = false;
343
			foreach ( $this->new_settings['affiliate_links'] as $affiliate_link ) {
344
				if ( $link_prefix === trim( $affiliate_link['path'], '/' ) ) {
345
					$prefix_set = true;
346
					break;
347
				}
348
			}
349
350
			if ( ! $prefix_set ) {
351
				$this->new_settings['affiliate_links'][] = array(
352
					'path'  => '/' . $link_prefix . '/',
353
					'label' => 'affiliate',
354
				);
355
			}
356
		}
357
	}
358
359
	/**
360
	 * MonsterInsights Version 7.0 upgrades.
361
	 *
362
	 * This function does the
363
	 * upgrade routine from MonsterInsights 6.2->7.0.
364
	 *
365
	 * @return void
366
	 * @since 7.0.0
367
	 * @access public
368
	 *
369
	 */
370
	public function v700_upgrades() {
371
		// 1. Default all event tracking and tracking to GA + JS respectively
372
		// 3a Set tracking_mode to use analytics.js
373
		$this->new_settings['tracking_mode'] = 'analytics';
374
375
376
		// 3b Set events mode to use JS if the events mode is not set explicitly to none
377
		if ( empty( $this->new_settings['events_mode'] ) || $this->new_settings['events_mode'] !== 'none' ) {
378
			$this->new_settings['events_mode'] = 'js';
379
		}
380
381
		// 2. Migrate manual UA codes
382
		// 2a Manual UA has the lowest priority
383
		if ( ! empty( $this->new_settings['manual_ua_code'] ) ) {
384
			// Set as manual UA code
385
			is_network_admin() ? update_site_option( 'monsterinsights_network_profile', array( 'manual' => $this->new_settings['manual_ua_code'] ) ) : update_option( 'monsterinsights_site_profile', array( 'manual' => $this->new_settings['manual_ua_code'] ) );
386
		}
387
388
		// 2b Then try the oAuth UA code
389
		if ( ! empty( $this->new_settings['analytics_profile_code'] ) ) {
390
			// Set as manual UA code
391
			is_network_admin() ? update_site_option( 'monsterinsights_network_profile', array( 'manual' => $this->new_settings['analytics_profile_code'] ) ) : update_option( 'monsterinsights_site_profile', array( 'manual' => $this->new_settings['analytics_profile_code'] ) );
392
		}
393
394
		// 3. Migrate License keys
395
		if ( is_multisite() ) {
396
			$ms_license = get_site_option( 'monsterinsights_license', '' );
397
			if ( $ms_license ) {
398
				update_site_option( 'monsterinsights_network_license_updates', get_site_option( 'monsterinsights_license_updates', '' ) );
399
				update_site_option( 'monsterinsights_network_license', $ms_license );
400
			}
401
		}
402
	}
403
404
	/**
405
	 * Upgrade routine for the new settings panel, onboarding wizard, and the internal-as-outbound v2 settings system.
406
	 */
407
	public function v740_upgrades() {
408
409
		// 1. Settings Conversions:
410
		// Convert affiliate field to repeater format
411
		if ( ! empty( $this->new_settings['track_internal_as_outbound'] ) ) {
412
			$affiliate_old_paths = $this->new_settings['track_internal_as_outbound'];
413
			$affiliate_old_label = isset( $this->new_settings['track_internal_as_label'] ) ? $this->new_settings['track_internal_as_label'] : '';
414
415
			$new_paths = explode( ',', $affiliate_old_paths );
416
417
			$this->new_settings['affiliate_links'] = array();
418
			if ( ! empty( $new_paths ) ) {
419
				$this->new_settings['enable_affiliate_links'] = true;
420
				foreach ( $new_paths as $new_path ) {
421
					$this->new_settings['affiliate_links'][] = array(
422
						'path'  => $new_path,
423
						'label' => $affiliate_old_label,
424
					);
425
				}
426
			}
427
428
			$settings = array(
429
				'track_internal_as_outbound',
430
				'track_internal_as_label',
431
			);
432
			foreach ( $settings as $setting ) {
433
				if ( ! empty( $this->new_settings[ $setting ] ) ) {
434
					unset( $this->new_settings[ $setting ] );
435
				}
436
			}
437
		}
438
439
		// Update option to disable just reports or also the dashboard widget.
440
		if ( isset( $this->new_settings['dashboards_disabled'] ) && $this->new_settings['dashboards_disabled'] ) {
441
			$this->new_settings['dashboards_disabled'] = 'disabled';
442
		}
443
444
		$this->new_settings['tracking_mode'] = 'analytics';
445
		$this->new_settings['events_mode']   = 'js';
446
447
		// If opted in during allow_tracking era, move that over
448
		if ( ! empty( $this->new_settings['allow_tracking'] ) ) {
449
			$this->new_settings['anonymous_data'] = 1;
450
		}
451
452
		// 2. Remove Yoast stuff
453
		delete_option( 'yoast-ga-access_token' );
454
		delete_option( 'yoast-ga-refresh_token' );
455
		delete_option( 'yst_ga' );
456
		delete_option( 'yst_ga_api' );
457
458
459
		// 3. Remove fake settings from other plugins using our key for some reason and old settings of ours
460
		$settings = array(
461
			'debug_mode',
462
			'track_download_as',
463
			'analytics_profile',
464
			'analytics_profile_code',
465
			'analytics_profile_name',
466
			'manual_ua_code',
467
			'track_outbound',
468
			'track_download_as',
469
			'enhanced_link_attribution',
470
			'oauth_version',
471
			'monsterinsights_oauth_status',
472
			'firebug_lite',
473
			'google_auth_code',
474
			'allow_tracking',
475
		);
476
477
		foreach ( $settings as $setting ) {
478
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
479
				unset( $this->new_settings[ $setting ] );
480
			}
481
		}
482
483
		$settings = array(
484
			'_repeated',
485
			'ajax',
486
			'asmselect0',
487
			'bawac_force_nonce',
488
			'icl_post_language',
489
			'saved_values',
490
			'mlcf_email',
491
			'mlcf_name',
492
			'cron_failed',
493
			'undefined',
494
			'cf_email',
495
			'cf_message',
496
			'cf_name',
497
			'cf_number',
498
			'cf_phone',
499
			'cf_subject',
500
			'content',
501
			'credentials',
502
			'cron_failed',
503
			'cron_last_run',
504
			'global-css',
505
			'grids',
506
			'page',
507
			'punch-fonts',
508
			'return_tab',
509
			'skins',
510
			'navigation-skins',
511
			'title',
512
			'type',
513
			'wpcf_email',
514
			'wpcf_your_name',
515
		);
516
517
		foreach ( $settings as $setting ) {
518
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
519
				unset( $this->new_settings[ $setting ] );
520
			}
521
		}
522
523
		// 4. Remove old crons
524
		if ( wp_next_scheduled( 'monsterinsights_daily_cron' ) ) {
525
			wp_clear_scheduled_hook( 'monsterinsights_daily_cron' );
526
		}
527
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_data' ) ) {
528
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_data' );
529
		}
530
531
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_checkin' ) ) {
532
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_checkin' );
533
		}
534
535
		if ( wp_next_scheduled( 'monsterinsights_weekly_cron' ) ) {
536
			wp_clear_scheduled_hook( 'monsterinsights_weekly_cron' );
537
		}
538
539
		if ( wp_next_scheduled( 'yst_ga_aggregate_data' ) ) {
540
			wp_clear_scheduled_hook( 'yst_ga_aggregate_data' );
541
		}
542
543
		delete_option( 'monsterinsights_tracking_last_send' );
544
		delete_option( 'mi_tracking_last_send' );
545
546
		// 5. Remove old option
547
		delete_option( 'monsterinsights_settings_version' );
548
	}
549
550
551
	/**
552
	 * Upgrade routine
553
	 */
554
	public function v750_upgrades() {
555
		// 1. One time re-prompt for anonymous data (due to migration bug now fixed)
556
		// if ( ! monsterinsights_is_pro_version() ) {
557
		// 	if ( empty( $this->new_settings[ 'anonymous_data' ] ) ) {
558
		// 		update_option( 'monsterinsights_tracking_notice', 0 );
559
		// 	}
560
		// }
561
		//
562
		// 2. Clear old settings ( 	'tracking_mode','events_mode',)
563
564
565
		// 3. Attempt to extract the cross-domain settings from the Custom Code area and use in the new option.
566
		$custom_code = isset( $this->new_settings['custom_code'] ) ? $this->new_settings['custom_code'] : '';
567
		if ( ! empty( $custom_code ) ) {
568
			$pattern = '/(?:\'linker:autoLink\', )(?:\[)(.*)(?:\])/m';
569
			preg_match_all( $pattern, $custom_code, $matches, PREG_SET_ORDER, 0 );
570
			if ( ! empty( $matches ) && isset( $matches[0] ) && isset( $matches[0][1] ) ) {
571
				$cross_domains = array();
572
				$domains       = explode( ',', $matches[0][1] );
573
				foreach ( $domains as $key => $domain ) {
574
					$domain          = trim( $domain );
575
					$cross_domains[] = array(
576
						'domain' => trim( $domain, '\'\"' ),
577
					);
578
				}
579
				$this->new_settings['add_allow_linker'] = true;
580
				$this->new_settings['cross_domains']    = $cross_domains;
581
582
				$notices = get_option( 'monsterinsights_notices' );
583
				if ( ! is_array( $notices ) ) {
584
					$notices = array();
585
				}
586
				$notices['monsterinsights_cross_domains_extracted'] = false;
587
				update_option( 'monsterinsights_notices', $notices );
588
			}
589
		}
590
	}
591
592
	/**
593
	 * Upgrade routine for version 7.6.0
594
	 */
595
	public function v760_upgrades() {
596
597
		$cross_domains = isset( $this->new_settings['cross_domains'] ) ? $this->new_settings['cross_domains'] : array();
598
599
		if ( ! empty( $cross_domains ) && is_array( $cross_domains ) ) {
600
			$current_domain = wp_parse_url( home_url() );
601
			$current_domain = isset( $current_domain['host'] ) ? $current_domain['host'] : '';
602
			if ( ! empty( $current_domain ) ) {
603
				$regex = '/^(?:' . $current_domain . '|(?:.+)\.' . $current_domain . ')$/m';
604
				foreach ( $cross_domains as $key => $cross_domain ) {
605
					if ( ! isset( $cross_domain['domain'] ) ) {
606
						continue;
607
					}
608
					preg_match( $regex, $cross_domain['domain'], $matches );
609
					if ( count( $matches ) > 0 ) {
610
						unset( $this->new_settings['cross_domains'][ $key ] );
611
					}
612
				}
613
			}
614
		}
615
616
	}
617
618
	/**
619
	 * Upgrade routine for version 7.7.1
620
	 */
621
	public function v771_upgrades() {
622
623
		if ( ! monsterinsights_is_pro_version() ) {
624
			// We only need to run this for the Pro version.
625
			return;
626
		}
627
		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
628
629
		$plugin = 'wp-scroll-depth/wp-scroll-depth.php';
630
		// Check if wp-scroll-depth is active and deactivate to avoid conflicts with the pro scroll tracking feature.
631
		if ( is_plugin_active( $plugin ) ) {
632
			deactivate_plugins( $plugin );
633
		}
634
635
	}
636
637
	/**
638
	 * Upgrade routine for version 7.8.0
639
	 */
640
	public function v780_upgrades() {
641
642
		if ( monsterinsights_get_v4_id() ) {
643
			// If we have a UA, don't show the first run notice.
644
			monsterinsights_update_option( 'monsterinsights_first_run_notice', true );
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of monsterinsights_update_option(). ( Ignorable by Annotation )

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

644
			monsterinsights_update_option( 'monsterinsights_first_run_notice', /** @scrutinizer ignore-type */ true );
Loading history...
645
646
			// If they are already tracking when they upgrade, mark connected time as now.
647
			$over_time = get_option( 'monsterinsights_over_time', array() );
648
			if ( empty( $over_time['connected_date'] ) ) {
649
				$over_time['connected_date'] = time();
650
				update_option( 'monsterinsights_over_time', $over_time, false );
651
			}
652
		}
653
654
	}
655
656
	/**
657
	 * Upgrade routine for version 7.9.0
658
	 */
659
	public function v790_upgrades() {
660
661
		// If they are already tracking, don't show the notice.
662
		if ( monsterinsights_get_v4_id() ) {
663
			update_option( 'monsterinsights_frontend_tracking_notice_viewed', true );
664
665
			// If they are already tracking when they upgrade & not already marked mark connected time as now.
666
			// Adding this here again as 7.8.0 upgrade didn't run for all users.
667
			$over_time = get_option( 'monsterinsights_over_time', array() );
668
			if ( empty( $over_time['connected_date'] ) ) {
669
				$over_time['connected_date'] = time();
670
				update_option( 'monsterinsights_over_time', $over_time, false );
671
			}
672
		}
673
674
	}
675
676
	/**
677
	 * Upgrade routine for version 8.0.0
678
	 */
679
	public function v7100_upgrades() {
680
681
		// Remove exe js and tgz from file downloads tracking.
682
		$current_downloads = isset( $this->new_settings['extensions_of_files'] ) ? $this->new_settings['extensions_of_files'] : array();
683
684
		if ( ! empty( $current_downloads ) ) {
685
			$extensions_to_remove = array(
686
				'exe',
687
				'js',
688
				'tgz'
689
			);
690
			$extensions_to_add    = array(
691
				'docx',
692
				'pptx',
693
				'xlsx',
694
			);
695
696
			$extensions         = explode( ',', $current_downloads );
0 ignored issues
show
Bug introduced by
It seems like $current_downloads can also be of type array; however, parameter $string of explode() 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

696
			$extensions         = explode( ',', /** @scrutinizer ignore-type */ $current_downloads );
Loading history...
697
			$updated_extensions = array();
698
699
			if ( ! empty( $extensions ) && is_array( $extensions ) ) {
700
				foreach ( $extensions as $extension ) {
701
					if ( ! in_array( $extension, $extensions_to_remove ) ) {
702
						$updated_extensions[] = $extension;
703
					}
704
				}
705
			}
706
707
			foreach ( $extensions_to_add as $extension_to_add ) {
708
				if ( ! in_array( $extension_to_add, $updated_extensions ) ) {
709
					$updated_extensions[] = $extension_to_add;
710
				}
711
			}
712
		} else {
713
			$updated_extensions = array(
714
				'pdf',
715
				'doc',
716
				'ppt',
717
				'xls',
718
				'zip',
719
				'docx',
720
				'pptx',
721
				'xlsx',
722
			);
723
		}
724
725
		$updated_extensions = implode( ',', $updated_extensions );
726
727
		$this->new_settings['extensions_of_files'] = $updated_extensions;
728
729
	}
730
731
	/**
732
	 * Upgrade routine for version 7.11.0
733
	 */
734
	public function v7110_upgrades() {
735
736
		if ( empty( $this->new_settings['email_summaries'] ) ) {
737
			$admin_email                                     = get_option( 'admin_email' );
738
			$admin_email_array                               = array(
739
				array(
740
					'email' => $admin_email,
741
				),
742
			);
743
			$this->new_settings['email_summaries']           = 'on';
744
			$this->new_settings['summaries_html_template']   = 'yes';
745
			$this->new_settings['summaries_email_addresses'] = $admin_email_array; // Not using wp_json_encode for backwards compatibility.
746
		}
747
748
	}
749
750
	/**
751
	 * Upgrade routine for version 7.12.0
752
	 */
753
	public function v7120_upgrades() {
754
755
		// Make sure the default for automatic updates is reflected correctly in the settings.
756
		if ( empty( $this->new_settings['automatic_updates'] ) ) {
757
			$this->new_settings['automatic_updates'] = 'none';
758
		}
759
760
	}
761
762
	/**
763
	 * Upgrade routine for version 7.13.0
764
	 */
765
	public function v7130_upgrades() {
766
767
		// Set default values for popular posts.
768
		$popular_posts_defaults = array(
769
			'popular_posts_inline_theme'               => 'alpha',
770
			'popular_posts_widget_theme'               => 'alpha',
771
			'popular_posts_products_theme'             => 'alpha',
772
			'popular_posts_inline_placement'           => 'manual',
773
			'popular_posts_widget_theme_columns'       => '2',
774
			'popular_posts_products_theme_columns'     => '2',
775
			'popular_posts_widget_count'               => '4',
776
			'popular_posts_products_count'             => '4',
777
			'popular_posts_widget_theme_meta_author'   => 'on',
778
			'popular_posts_widget_theme_meta_date'     => 'on',
779
			'popular_posts_widget_theme_meta_comments' => 'on',
780
			'popular_posts_products_theme_meta_price'  => 'on',
781
			'popular_posts_products_theme_meta_rating' => 'on',
782
			'popular_posts_products_theme_meta_image'  => 'on',
783
			'popular_posts_inline_after_count'         => '150',
784
			'popular_posts_inline_multiple_number'     => '3',
785
			'popular_posts_inline_multiple_distance'   => '250',
786
			'popular_posts_inline_multiple_min_words'  => '100',
787
			'popular_posts_inline_post_types'          => array( 'post' ),
788
			'popular_posts_widget_post_types'          => array( 'post' ),
789
		);
790
791
		foreach ( $popular_posts_defaults as $key => $value ) {
792
			if ( empty( $this->new_settings[ $key ] ) ) {
793
				$this->new_settings[ $key ] = $value;
794
			}
795
		}
796
797
		// Contextual education cleanup.
798
		$option_name             = 'monsterinsights_notifications';
799
		$notifications           = get_option( $option_name, array() );
800
		$dismissed_notifications = isset( $notifications['dismissed'] ) ? $notifications['dismissed'] : array();
801
802
		if ( is_array( $dismissed_notifications ) && ! empty( $dismissed_notifications ) ) {
803
			foreach ( $dismissed_notifications as $key => $dismiss_notification ) {
804
				$title   = isset( $dismiss_notification['title'] ) ? $dismiss_notification['title'] : '';
805
				$content = isset( $dismiss_notification['content'] ) ? $dismiss_notification['content'] : '';
806
807
				if ( empty( $title ) || empty( $content ) ) {
808
					unset( $dismissed_notifications[ $key ] );
809
				}
810
			}
811
812
			update_option(
813
				$option_name,
814
				array(
815
					'update'    => $notifications['update'],
816
					'feed'      => $notifications['feed'],
817
					'events'    => $notifications['events'],
818
					'dismissed' => $dismissed_notifications,
819
				)
820
			);
821
		}
822
	}
823
824
	/**
825
	 * Upgrade routine for version 7.13.1
826
	 */
827
	public function v7131_upgrades() {
828
829
		// Delete transient for GA data with wrong expiration date.
830
		delete_transient( 'monsterinsights_popular_posts_ga_data' );
831
832
	}
833
834
	/**
835
	 * Upgrade routine for version 7.14.0
836
	 */
837
	public function v7140_upgrades() {
838
839
		// Clear notification cron events no longer used.
840
		$cron = get_option( 'cron' );
841
842
		foreach ( $cron as $timestamp => $cron_array ) {
843
			if ( ! is_array( $cron_array ) ) {
844
				continue;
845
			}
846
			foreach ( $cron_array as $cron_key => $cron_data ) {
847
				if ( 0 === strpos( $cron_key, 'monsterinsights_notification_' ) ) {
848
					wp_unschedule_event( $timestamp, $cron_key, array() );
849
				}
850
			}
851
		}
852
853
		// Delete existing year in review report option.
854
		delete_option( 'monsterinsights_report_data_yearinreview' );
855
	}
856
857
	/**
858
	 * Upgrade routine for version 7.15.0
859
	 */
860
	public function v7150_upgrades() {
861
		// Enable gtag compatibility mode by default.
862
		if ( empty( $this->new_settings['gtagtracker_compatibility_mode'] ) ) {
863
			$this->new_settings['gtagtracker_compatibility_mode'] = true;
864
		}
865
	}
866
}
867