Passed
Push — master ( 6f0a6d...90a9ca )
by
unknown
09:41
created

MonsterInsights_Install::v750_upgrades()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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