Passed
Push — master ( 90a9ca...fc67d4 )
by
unknown
07:30
created

MonsterInsights_Install::v780_upgrades()   A

Complexity

Conditions 3
Paths 3

Size

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

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

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