Passed
Push — master ( bfbffa...ac9aa8 )
by
unknown
09:32
created

MonsterInsights_Install::v7150_upgrades()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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
	 * @since 6.0.0
51
	 * @access public
52
	 *
53
	 * @return void
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
	 * @since 6.0.0
234
	 * @access public
235
	 *
236
	 * @return void
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(),
247
		);
248
249
		update_option( 'monsterinsights_over_time', $data );
250
251
		// Let addons + MI Pro/Lite hook in here. @todo: doc as nonpublic
252
		do_action( 'monsterinsights_after_new_install_routine', MONSTERINSIGHTS_VERSION );
253
	}
254
255
	public function get_monsterinsights_default_values() {
256
257
		$admin_email       = get_option( 'admin_email' );
258
		$admin_email_array = array(
259
			array(
260
				'email' => $admin_email,
261
			),
262
		);
263
264
		return array(
265
			'enable_affiliate_links'                   => true,
266
			'affiliate_links'                          => array(
267
				array(
268
					'path'  => '/go/',
269
					'label' => 'affiliate',
270
				),
271
				array(
272
					'path'  => '/recommend/',
273
					'label' => 'affiliate',
274
				)
275
			),
276
			'demographics'                             => 1,
277
			'ignore_users'                             => array( 'administrator', 'editor' ),
278
			'dashboards_disabled'                      => 0,
279
			'anonymize_ips'                            => 0,
280
			'extensions_of_files'                      => 'doc,pdf,ppt,zip,xls,docx,pptx,xlsx',
281
			'subdomain_tracking'                       => '',
282
			'link_attribution'                         => true,
283
			'tag_links_in_rss'                         => true,
284
			'allow_anchor'                             => 0,
285
			'add_allow_linker'                         => 0,
286
			'custom_code'                              => '',
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
			'gtagtracker_compatibility_mode'           => true,
292
			'email_summaries'                          => 'on',
293
			'summaries_html_template'                  => 'yes',
294
			'summaries_email_addresses'                => $admin_email_array,
295
			'automatic_updates'                        => 'none',
296
			'popular_posts_inline_theme'               => 'alpha',
297
			'popular_posts_widget_theme'               => 'alpha',
298
			'popular_posts_products_theme'             => 'alpha',
299
			'popular_posts_inline_placement'           => 'manual',
300
			'popular_posts_widget_theme_columns'       => '2',
301
			'popular_posts_products_theme_columns'     => '2',
302
			'popular_posts_widget_count'               => '4',
303
			'popular_posts_products_count'             => '4',
304
			'popular_posts_widget_theme_meta_author'   => 'on',
305
			'popular_posts_widget_theme_meta_date'     => 'on',
306
			'popular_posts_widget_theme_meta_comments' => 'on',
307
			'popular_posts_products_theme_meta_price'  => 'on',
308
			'popular_posts_products_theme_meta_rating' => 'on',
309
			'popular_posts_products_theme_meta_image'  => 'on',
310
			'popular_posts_inline_after_count'         => '150',
311
			'popular_posts_inline_multiple_number'     => '3',
312
			'popular_posts_inline_multiple_distance'   => '250',
313
			'popular_posts_inline_multiple_min_words'  => '100',
314
			'popular_posts_inline_post_types'          => array( 'post' ),
315
			'popular_posts_widget_post_types'          => array( 'post' ),
316
		);
317
	}
318
319
	/**
320
	 * Check if ThirstyAffiliates plugin is installed and use the link prefix value in the affiliate settings.
321
	 *
322
	 * @return void
323
	 */
324
	public function maybe_import_thirstyaffiliates_options() {
325
326
		// Check if ThirstyAffiliates is installed.
327
		if ( ! function_exists( 'ThirstyAffiliates' ) ) {
328
			return;
329
		}
330
331
		$link_prefix = get_option( 'ta_link_prefix', 'recommends' );
332
333
		if ( $link_prefix === 'custom' ) {
334
			$link_prefix = get_option( 'ta_link_prefix_custom', 'recommends' );
335
		}
336
337
		if ( ! empty( $link_prefix ) ) {
338
339
			// Check if prefix exists.
340
			$prefix_set = false;
341
			foreach ( $this->new_settings['affiliate_links'] as $affiliate_link ) {
342
				if ( $link_prefix === trim( $affiliate_link['path'], '/' ) ) {
343
					$prefix_set = true;
344
					break;
345
				}
346
			}
347
348
			if ( ! $prefix_set ) {
349
				$this->new_settings['affiliate_links'][] = array(
350
					'path'  => '/' . $link_prefix . '/',
351
					'label' => 'affiliate',
352
				);
353
			}
354
		}
355
	}
356
357
	/**
358
	 * MonsterInsights Version 7.0 upgrades.
359
	 *
360
	 * This function does the
361
	 * upgrade routine from MonsterInsights 6.2->7.0.
362
	 *
363
	 * @since 7.0.0
364
	 * @access public
365
	 *
366
	 * @return void
367
	 */
368
	public function v700_upgrades() {
369
		// 1. Default all event tracking and tracking to GA + JS respectively
370
		// 3a Set tracking_mode to use analytics.js
371
		$this->new_settings['tracking_mode' ] = 'analytics';
372
373
374
		// 3b Set events mode to use JS if the events mode is not set explicitly to none
375
		if ( empty( $this->new_settings['events_mode' ] ) || $this->new_settings['events_mode' ] !== 'none' ) {
376
			$this->new_settings['events_mode' ] = 'js';
377
		}
378
379
		// 2. Migrate manual UA codes
380
		// 2a Manual UA has the lowest priority
381
		if ( ! empty( $this->new_settings['manual_ua_code' ] ) ) {
382
			// Set as manual UA code
383
			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' ] ) );
384
		}
385
386
		// 2b Then try the oAuth UA code
387
		if ( ! empty( $this->new_settings['analytics_profile_code' ] ) ) {
388
			// Set as manual UA code
389
			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' ] ) );
390
		}
391
392
		// 3. Migrate License keys
393
		if ( is_multisite() ) {
394
			$ms_license = get_site_option( 'monsterinsights_license', '' );
395
			if ( $ms_license ) {
396
				update_site_option( 'monsterinsights_network_license_updates', get_site_option( 'monsterinsights_license_updates', '' ) );
397
				update_site_option( 'monsterinsights_network_license', $ms_license );
398
			}
399
		}
400
	}
401
402
	/**
403
	 * Upgrade routine for the new settings panel, onboarding wizard, and the internal-as-outbound v2 settings system.
404
	 */
405
	public function v740_upgrades() {
406
407
		// 1. Settings Conversions:
408
		// Convert affiliate field to repeater format
409
		if ( ! empty( $this->new_settings['track_internal_as_outbound'] ) ) {
410
			$affiliate_old_paths = $this->new_settings['track_internal_as_outbound'];
411
			$affiliate_old_label = isset( $this->new_settings['track_internal_as_label'] ) ? $this->new_settings['track_internal_as_label'] : '';
412
413
			$new_paths = explode( ',', $affiliate_old_paths );
414
415
			$this->new_settings['affiliate_links'] = array();
416
			if ( ! empty( $new_paths ) ) {
417
				$this->new_settings['enable_affiliate_links'] = true;
418
				foreach ( $new_paths as $new_path ) {
419
					$this->new_settings['affiliate_links'][] = array(
420
						'path'  => $new_path,
421
						'label' => $affiliate_old_label,
422
					);
423
				}
424
			}
425
426
			$settings = array(
427
				'track_internal_as_outbound',
428
				'track_internal_as_label',
429
			);
430
			foreach ( $settings as $setting ) {
431
				if ( ! empty( $this->new_settings[ $setting ] ) ) {
432
					unset( $this->new_settings[ $setting ] );
433
				}
434
			}
435
		}
436
437
		// Update option to disable just reports or also the dashboard widget.
438
		if ( isset( $this->new_settings['dashboards_disabled'] ) && $this->new_settings['dashboards_disabled'] ) {
439
			$this->new_settings['dashboards_disabled'] = 'disabled';
440
		}
441
442
		$this->new_settings['tracking_mode'] = 'analytics';
443
		$this->new_settings['events_mode']   = 'js';
444
445
		// If opted in during allow_tracking era, move that over
446
		if ( ! empty( $this->new_settings['allow_tracking'] ) ) {
447
			$this->new_settings['anonymous_data'] = 1;
448
		}
449
450
		// 2. Remove Yoast stuff
451
		delete_option( 'yoast-ga-access_token' );
452
		delete_option( 'yoast-ga-refresh_token' );
453
		delete_option( 'yst_ga' );
454
		delete_option( 'yst_ga_api' );
455
456
457
		// 3. Remove fake settings from other plugins using our key for some reason and old settings of ours
458
		$settings = array(
459
			'debug_mode',
460
			'track_download_as',
461
			'analytics_profile',
462
			'analytics_profile_code',
463
			'analytics_profile_name',
464
			'manual_ua_code',
465
			'track_outbound',
466
			'track_download_as',
467
			'enhanced_link_attribution',
468
			'oauth_version',
469
			'monsterinsights_oauth_status',
470
			'firebug_lite',
471
			'google_auth_code',
472
			'allow_tracking',
473
		);
474
475
		foreach ( $settings as $setting ) {
476
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
477
				unset( $this->new_settings[ $setting ] );
478
			}
479
		}
480
481
		$settings = array(
482
			'_repeated',
483
			'ajax',
484
			'asmselect0',
485
			'bawac_force_nonce',
486
			'icl_post_language',
487
			'saved_values',
488
			'mlcf_email',
489
			'mlcf_name',
490
			'cron_failed',
491
			'undefined',
492
			'cf_email',
493
			'cf_message',
494
			'cf_name',
495
			'cf_number',
496
			'cf_phone',
497
			'cf_subject',
498
			'content',
499
			'credentials',
500
			'cron_failed',
501
			'cron_last_run',
502
			'global-css',
503
			'grids',
504
			'page',
505
			'punch-fonts',
506
			'return_tab',
507
			'skins',
508
			'navigation-skins',
509
			'title',
510
			'type',
511
			'wpcf_email',
512
			'wpcf_your_name',
513
		);
514
515
		foreach ( $settings as $setting ) {
516
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
517
				unset( $this->new_settings[ $setting ] );
518
			}
519
		}
520
521
		// 4. Remove old crons
522
		if ( wp_next_scheduled( 'monsterinsights_daily_cron' ) ) {
523
			wp_clear_scheduled_hook( 'monsterinsights_daily_cron' );
524
		}
525
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_data' ) ) {
526
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_data' );
527
		}
528
529
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_checkin' ) ) {
530
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_checkin' );
531
		}
532
533
		if ( wp_next_scheduled( 'monsterinsights_weekly_cron' ) ) {
534
			wp_clear_scheduled_hook( 'monsterinsights_weekly_cron' );
535
		}
536
537
		if ( wp_next_scheduled( 'yst_ga_aggregate_data' ) ) {
538
			wp_clear_scheduled_hook( 'yst_ga_aggregate_data' );
539
		}
540
541
		delete_option( 'monsterinsights_tracking_last_send' );
542
		delete_option( 'mi_tracking_last_send' );
543
544
		// 5. Remove old option
545
		delete_option( 'monsterinsights_settings_version' );
546
	}
547
548
549
	/**
550
	 * Upgrade routine
551
	 */
552
	public function v750_upgrades() {
553
		// 1. One time re-prompt for anonymous data (due to migration bug now fixed)
554
		// if ( ! monsterinsights_is_pro_version() ) {
555
		// 	if ( empty( $this->new_settings[ 'anonymous_data' ] ) ) {
556
		// 		update_option( 'monsterinsights_tracking_notice', 0 );
557
		// 	}
558
		// }
559
		//
560
		// 2. Clear old settings ( 	'tracking_mode','events_mode',)
561
562
563
		// 3. Attempt to extract the cross-domain settings from the Custom Code area and use in the new option.
564
		$custom_code = isset( $this->new_settings['custom_code'] ) ? $this->new_settings['custom_code'] : '';
565
		if ( ! empty( $custom_code ) ) {
566
			$pattern = '/(?:\'linker:autoLink\', )(?:\[)(.*)(?:\])/m';
567
			preg_match_all( $pattern, $custom_code, $matches, PREG_SET_ORDER, 0 );
568
			if ( ! empty( $matches ) && isset( $matches[0] ) && isset( $matches[0][1] ) ) {
569
				$cross_domains = array();
570
				$domains       = explode( ',', $matches[0][1] );
571
				foreach ( $domains as $key => $domain ) {
572
					$domain          = trim( $domain );
573
					$cross_domains[] = array(
574
						'domain' => trim( $domain, '\'\"' ),
575
					);
576
				}
577
				$this->new_settings['add_allow_linker'] = true;
578
				$this->new_settings['cross_domains']    = $cross_domains;
579
580
				$notices = get_option( 'monsterinsights_notices' );
581
				if ( ! is_array( $notices ) ) {
582
					$notices = array();
583
				}
584
				$notices['monsterinsights_cross_domains_extracted'] = false;
585
				update_option( 'monsterinsights_notices', $notices );
586
			}
587
		}
588
	}
589
590
	/**
591
	 * Upgrade routine for version 7.6.0
592
	 */
593
	public function v760_upgrades() {
594
595
		$cross_domains = isset( $this->new_settings['cross_domains'] ) ? $this->new_settings['cross_domains'] : array();
596
597
		if ( ! empty( $cross_domains ) && is_array( $cross_domains ) ) {
598
			$current_domain = wp_parse_url( home_url() );
599
			$current_domain = isset( $current_domain['host'] ) ? $current_domain['host'] : '';
600
			if ( ! empty( $current_domain ) ) {
601
				$regex = '/^(?:' . $current_domain . '|(?:.+)\.' . $current_domain . ')$/m';
602
				foreach ( $cross_domains as $key => $cross_domain ) {
603
					if ( ! isset( $cross_domain['domain'] ) ) {
604
						continue;
605
					}
606
					preg_match( $regex, $cross_domain['domain'], $matches );
607
					if ( count( $matches ) > 0 ) {
608
						unset( $this->new_settings['cross_domains'][ $key ] );
609
					}
610
				}
611
			}
612
		}
613
614
	}
615
616
	/**
617
	 * Upgrade routine for version 7.7.1
618
	 */
619
	public function v771_upgrades() {
620
621
		if ( ! monsterinsights_is_pro_version() ) {
622
			// We only need to run this for the Pro version.
623
			return;
624
		}
625
		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
626
627
		$plugin = 'wp-scroll-depth/wp-scroll-depth.php';
628
		// Check if wp-scroll-depth is active and deactivate to avoid conflicts with the pro scroll tracking feature.
629
		if ( is_plugin_active( $plugin ) ) {
630
			deactivate_plugins( $plugin );
631
		}
632
633
	}
634
635
	/**
636
	 * Upgrade routine for version 7.8.0
637
	 */
638
	public function v780_upgrades() {
639
640
		if ( monsterinsights_get_ua() ) {
641
			// If we have a UA, don't show the first run notice.
642
			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

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

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