Passed
Push — master ( 2bd1cf...bfbffa )
by
unknown
05:03
created

MonsterInsights_Install::v790_upgrades()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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