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

587
			monsterinsights_update_option( 'monsterinsights_first_run_notice', /** @scrutinizer ignore-type */ true );
Loading history...
588
589
			// If they are already tracking when they upgrade, mark connected time as now.
590
			$over_time = get_option( 'monsterinsights_over_time', array() );
591
			if ( empty( $over_time['connected_date'] ) ) {
592
				$over_time['connected_date'] = time();
593
				update_option( 'monsterinsights_over_time', $over_time );
594
			}
595
		}
596
597
	}
598
599
	/**
600
	 * Upgrade routine for version 7.9.0
601
	 */
602
	public function v790_upgrades() {
603
604
		// If they are already tracking, don't show the notice.
605
		if ( monsterinsights_get_ua() ) {
606
			update_option( 'monsterinsights_frontend_tracking_notice_viewed', true );
607
608
			// If they are already tracking when they upgrade & not already marked mark connected time as now.
609
			// Adding this here again as 7.8.0 upgrade didn't run for all users.
610
			$over_time = get_option( 'monsterinsights_over_time', array() );
611
			if ( empty( $over_time['connected_date'] ) ) {
612
				$over_time['connected_date'] = time();
613
				update_option( 'monsterinsights_over_time', $over_time );
614
			}
615
		}
616
617
	}
618
619
	/**
620
	 * Upgrade routine for version 8.0.0
621
	 */
622
	public function v7100_upgrades() {
623
624
		// Remove exe js and tgz from file downloads tracking.
625
		$current_downloads = isset( $this->new_settings['extensions_of_files'] ) ? $this->new_settings['extensions_of_files'] : array();
626
627
		if ( ! empty( $current_downloads ) ) {
628
			$extensions_to_remove = array(
629
				'exe',
630
				'js',
631
				'tgz'
632
			);
633
			$extensions_to_add    = array(
634
				'docx',
635
				'pptx',
636
				'xlsx',
637
			);
638
639
			$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

639
			$extensions         = explode( ',', /** @scrutinizer ignore-type */ $current_downloads );
Loading history...
640
			$updated_extensions = array();
641
642
			if ( ! empty( $extensions ) && is_array( $extensions ) ) {
643
				foreach ( $extensions as $extension ) {
644
					if ( ! in_array( $extension, $extensions_to_remove ) ) {
645
						$updated_extensions[] = $extension;
646
					}
647
				}
648
			}
649
650
			foreach ( $extensions_to_add as $extension_to_add ) {
651
				if ( ! in_array( $extension_to_add, $updated_extensions ) ) {
652
					$updated_extensions[] = $extension_to_add;
653
				}
654
			}
655
		} else {
656
			$updated_extensions = array(
657
				'pdf',
658
				'doc',
659
				'ppt',
660
				'xls',
661
				'zip',
662
				'docx',
663
				'pptx',
664
				'xlsx',
665
			);
666
		}
667
668
		$updated_extensions = implode( ',', $updated_extensions );
669
670
		$this->new_settings['extensions_of_files'] = $updated_extensions;
671
672
	}
673
}
674