Passed
Push — master ( 31e4e3...e9b806 )
by Chris
03:06
created

MonsterInsights_Install::v720_upgrades()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 12
nop 0
dl 0
loc 22
rs 9.5222
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
	 * @todo  I'd like to add preflight checks here.
54
	 * @todo  I'd like to add a recovery system here.
55
	 * 
56
	 * @return void
57
	 */
58
	public function init() {
59
60
		// Get a copy of the current MI settings.
61
		$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...
62
63
64
		$version = get_option( 'monsterinsights_current_version', false );
65
		$yoast   = get_option( 'yst_ga', false );
66
		$cachec  = false; // have we forced an object cache to be cleared already (so we don't clear it unnecessarily)
67
68
		// if new install and have not used Yoast previously
69
		if ( ! $version && ! $yoast ) {
70
71
			$this->new_install();
72
			// This is the version used for MI upgrade routines.
73
			update_option( 'monsterinsights_db_version', '6.2.0' );
74
			
75
		} else if ( ! $version && $yoast ) { // if new install and has used Yoast previously
76
77
			$this->upgrade_from_yoast();
78
			// This is the version used for MI upgrade routines.
79
			update_option( 'monsterinsights_db_version', '6.2.0' );
80
81
			if ( ! $cachec ) {
0 ignored issues
show
introduced by
The condition $cachec is always false.
Loading history...
82
				wp_cache_flush();
83
				$cachec = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $cachec is dead and can be removed.
Loading history...
84
			}
85
			
86
		} else { // if existing install
87
			if ( version_compare( $version, '6.0.2', '<' ) ) {
88
				$this->v602_upgrades();
89
			}
90
			
91
			if ( version_compare( $version, '6.0.11', '<' ) ) {
92
				$this->v6011_upgrades();
93
94
				if ( ! $cachec ) {
0 ignored issues
show
introduced by
The condition $cachec is always false.
Loading history...
95
					wp_cache_flush();
96
					$cachec = true;
97
				}
98
			}
99
			if ( version_compare( $version, '6.2.0', '<' ) ) {
100
				$this->v620_upgrades();
101
			}
102
103
			if ( version_compare( $version, '7.0.0', '<' ) ) {
104
				$this->v700_upgrades();
105
			}
106
107
			update_option( 'monsterinsights_db_version', '7.0.0' );
108
			
109
			// @todo: doc as nonpublic
110
			
111
			update_option( 'monsterinsights_version_upgraded_from', $version );
112
			do_action( 'monsterinsights_after_existing_upgrade_routine', $version );
113
		}
114
115
		// This hook is used primarily by the Pro version to run some Pro
116
		// specific install stuff. Please do not use this hook. It is not 
117
		// considered a public hook by MI's dev team and can/will be removed, 
118
		// relocated, and/or altered without warning at any time. You've been warned.
119
		// As this hook is not for public use, we've intentionally not docbloc'd this
120
		// hook to avoid developers seeing it future public dev docs.
121
		do_action( 'monsterinsights_after_install_routine', $version );
122
123
		// This is the version of the MI settings themselves
124
		update_option( 'monsterinsights_settings_version', '7.0.0' );
125
126
		// This is the version of MI installed
127
		update_option( 'monsterinsights_current_version', MONSTERINSIGHTS_VERSION );
128
129
		// This is where we save MI settings
130
		update_option( monsterinsights_get_option_name(), $this->new_settings );
131
132
		// This is where we redirect to the MI welcome page
133
		//set_transient( '_monsterinsights_activation_redirect', true, 30 ); @todo: Investigate
134
135
		// There's no code for this function below this. Just an explanation
136
		// of the MI core options.
137
138
		/** 
139
		 * Explanation of MonsterInsights core options
140
		 *
141
		 * By now your head is probably spinning trying to figure
142
		 * out what all of these version options are for. Note, I've abbreviated
143
		 * "monsterinsights" to "mi" in the options names to make this table easier
144
		 * to read.
145
		 *
146
		 * Here's a basic rundown:
147
		 *
148
		 * mi_settings_version: Used to store the version 
149
		 * 						of the MI settings. We use this
150
		 * 						so we can do upgrade routines where
151
		 * 						we'd have to do different actions based
152
		 * 						on the version the settings were installed
153
		 * 						in. For example: if we made a mistake with 
154
		 * 						the value we saved as the default for 
155
		 * 						a select setting, we can detect the version
156
		 * 						containing this mistake and correct it.
157
		 *
158
		 * mi_current_version:  This starts with the actual version MI was
159
		 * 						installed on. We use this version to 
160
		 * 						determine whether or not a site needs
161
		 * 						to run one of the behind the scenes
162
		 * 						MI upgrade routines. This version is updated
163
		 * 						every time a minor or major background upgrade
164
		 * 						routine is run. Generally lags behind the 
165
		 * 						MONSTERINSIGHTS_VERSION constant by at most a couple minor
166
		 * 						versions. Never lags behind by 1 major version
167
		 * 						or more.
168
		 *
169
		 * mi_db_version: 		This is different from mi_current_version.
170
		 * 						Unlike the former, this is used to determine
171
		 * 						if a site needs to run a *user* initiated
172
		 * 						upgrade routine (see MI_Upgrade class). This
173
		 * 						value is only update when a user initiated
174
		 * 						upgrade routine is done. Because we do very
175
		 * 						few user initiated upgrades compared to 
176
		 * 						automatic ones, this version can lag behind by
177
		 * 						2 or even 3 major versions. Generally contains
178
		 * 						the current major version.
179
		 *
180
		 * mi_settings:		    Returned by monsterinsights_get_option_name(), this
181
		 * 						is actually "monsterinsights_settings" for both pro
182
		 * 						and lite version. However we use a helper function to 
183
		 * 						retrieve the option name in case we ever decide down the
184
		 * 						road to maintain seperate options for the Lite and Pro versions.
185
		 * 					 	If you need to access MI's settings directly, (as opposed to our
186
		 * 					 	monsterinsights_get_option helper which uses the option name helper
187
		 * 					 	automatically), you should use this function to get the 
188
		 * 					 	name of the option to retrieve.
189
		 *
190
		 * yst_ga:			    Yoast's old settings option. We no longer use this, though
191
		 * 						for backwards compatibility reasons we store the updated settings
192
		 * 						in this just for a little while longer. These settings are migrated
193
		 * 						to the new settings option when you upgrade to MonsterInsights
194
		 * 						6.0 or higher automatically.
195
		 *
196
		 * yst_* & yoast_*:		These are options from when the plugin was developed by
197
		 * 						Yoast, and also of the few point releases we did after
198
		 * 						the acquisition. Note, while we currently do backcompat
199
		 * 						on some of these options so other plugins will continue working
200
		 * 						please realize there will be a point in the near future that we 
201
		 * 						will no longer support them. Please do not use them anymore.
202
		 */			
203
	}
204
205
206
	/**
207
	 * New MonsterInsights Install routine.
208
	 *
209
	 * This function installs all of the default
210
	 * things on new MI installs. Flight 5476 with 
211
	 * non-stop service to a whole world of 
212
	 * possibilities is now boarding.
213
	 *
214
	 * @since 6.0.0
215
	 * @access public
216
	 * 
217
	 * @return void
218
	 */
219
	public function new_install() {
220
221
		// Add default settings values
222
		$this->new_settings = $this->get_monsterinsights_default_values();
223
224
		$data = array(
225
			'installed_version' => MONSTERINSIGHTS_VERSION,
226
			'installed_date'    => time(),
227
			'installed_pro'     => monsterinsights_is_pro_version(),
228
		);
229
230
		update_option( 'monsterinsights_over_time', $data );
231
232
		// Let addons + MI Pro/Lite hook in here. @todo: doc as nonpublic
233
		do_action( 'monsterinsights_after_new_install_routine', MONSTERINSIGHTS_VERSION );
234
	}
235
236
	/**
237
	 * Upgrade from Yoast.
238
	 *
239
	 * This function does the upgrade routine from Yoast to this plugin version.
240
	 * Includes all of Yoast's previous routines.
241
	 *
242
	 * @since 6.0.0
243
	 * @access public
244
	 * 
245
	 * @return void
246
	 */
247
	public function upgrade_from_yoast() {
248
		// Do Yoast's Old Routines
249
		$options = get_option( 'yst_ga', array() );
250
		if ( ! empty( $options['ga_general'] ) ) {
251
			$options = $options['ga_general'];
252
		}
253
254
		$tracking_code = null;
255
		if ( ! empty( $options['analytics_profile'] ) && ! empty( $options['analytics_profile_code'] ) ) {
256
			$tracking_code = $options['analytics_profile_code'];
257
		} else if ( ! empty( $options['analytics_profile'] ) && empty( $options['analytics_profile_code'] ) ) {
258
			// Analytics profile is still holding the UA code
259
			$tracking_code = $options['analytics_profile'];
260
		}
261
262
		if ( ! empty( $options['manual_ua_code_field'] ) && ! empty( $options['manual_ua_code'] ) ) {
263
			$tracking_code = $options['manual_ua_code_field'];
264
		}
265
266
		if ( ! isset( $options['version'] ) && is_null( $tracking_code ) ) {
267
			$old_options = get_option( 'Yoast_Google_Analytics' );
268
			if ( isset( $old_options ) && is_array( $old_options ) ) {
269
				if ( isset( $old_options['uastring'] ) && '' !== trim( $old_options['uastring'] ) ) {
270
					// Save UA as manual UA, instead of saving all the old GA crap
271
					$options['manual_ua_code']       = 1;
272
					$options['manual_ua_code_field'] = $old_options['uastring'];
273
				}
274
				// Other settings
275
				$options['allow_anchor']               = $old_options['allowanchor'];
276
				$options['add_allow_linker']           = $old_options['allowlinker'];
277
				$options['anonymous_data']             = $old_options['anonymizeip'];
278
				$options['track_outbound']             = $old_options['trackoutbound'];
279
				$options['track_internal_as_outbound'] = $old_options['internallink'];
280
				$options['track_internal_as_label']    = $old_options['internallinklabel'];
281
				$options['extensions_of_files']        = $old_options['dlextensions'];
282
			}
283
			delete_option( 'Yoast_Google_Analytics' );
284
		}
285
		// 5.0.0 to 5.0.1 fix of ignore users array
286
		if ( ! isset( $options['version'] ) || version_compare( $options['version'], '5.0.1', '<' ) ) {
287
			if ( isset( $options['ignore_users'] ) && ! is_array( $options['ignore_users'] ) ) {
288
				$options['ignore_users'] = (array) $options['ignore_users'];
289
			}
290
		}
291
		// 5.1.2+ Remove firebug_lite from options, if set
292
		if ( ! isset ( $options['version'] ) || version_compare( $options['version'], '5.1.2', '<' ) ) {
293
			if ( isset( $options['firebug_lite'] ) ) {
294
				unset( $options['firebug_lite'] );
295
			}
296
		}
297
		// 5.2.8+ Add disabled dashboards option
298
		if ( ! isset ( $options['dashboards_disabled'] ) || version_compare( $options['version'], '5.2.8', '>' ) ) {
299
			$options['dashboards_disabled'] = 0;
300
		}
301
		// Check is API option already exists - if not add it
302
		$yst_ga_api = get_option( 'yst_ga_api' );
303
		if ( $yst_ga_api === false ) {
304
			add_option( 'yst_ga_api', array(), '', 'no' );
305
		}
306
		// Fallback to make sure every default option has a value
307
		$defaults = $this->get_yoast_default_values();
308
		if ( is_array( $defaults ) ) {
309
			foreach ( $defaults[ 'ga_general' ] as $key => $value ) {
310
				if ( ! isset( $options[ $key ] ) ) {
311
					$options[ $key ] = $value;
312
				}
313
			}
314
		}
315
316
		// Set to the current version now that we've done all needed upgrades
317
		$options['version'] = '5.5.3'; // Last Yoast codebase version
318
		$saved_options = get_option( 'yst_ga' );
319
		$saved_options[ 'ga_general' ] = $options;
320
		update_option( 'yst_ga', $saved_options );
321
322
323
		// Do license key switchover 
324
			$key     = '';
325
			$found   = false;
326
			$network = false;
327
			// Try network active Premium
328
			$is_key = get_site_option( 'google-analytics-by-yoast-premium_license', array() );
329
			if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){
330
				$key    = $is_key['key'];
331
				$found = true;
332
				$network = true;
333
			}
334
335
			// Try single site Premium
336
			if ( ! $found ) {
337
				$is_key = get_option( 'google-analytics-by-yoast-premium_license', array() );
338
				if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){
339
					$key    = $is_key['key'];
340
					$found = true;
341
				}				
342
			}
343
344
			// Try network active Premium
345
			if ( ! $found ) {
346
				$is_key = get_site_option( 'monsterinsights-pro_license', array() );
347
				if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){
348
					$key    = $is_key['key'];
349
					$found = true;
350
					$network = true;
351
				}				
352
			}
353
			
354
			// Try single site Premium
355
			if ( ! $found ) {
356
				$is_key = get_option( 'monsterinsights-pro_license', array() );
357
				if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){
358
					$key    = $is_key['key'];
359
					$found = true;
360
				}				
361
			}
362
			
363
			// Try network active ecommmerce
364
			if ( ! $found ) {
365
				$is_key = get_site_option( 'ecommerce-addon_license', array() );
366
				if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){
367
					$key    = $is_key['key'];
368
					$found = true;
369
					$network = true;
370
				}
371
			}
372
			// Try single site ecommerce
373
			if ( ! $found ) {
374
				$is_key = get_option( 'ecommerce-addon_license', array() );
375
				if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){
376
					$key    = $is_key['key'];
377
					$found = true;
378
				}				
379
			}
380
381
			// set as new key for monsterinsights
382
			if ( $found && ! empty( $key ) ) {
383
				// In pro, install custom dimensions + ads. In lite, just save the key
384
				do_action( 'monsterinsights_upgrade_from_yoast', $key, $network );
385
			}
386
387
		// Next up: Settings Migration
388
		
389
			$options = get_option( 'yst_ga', array() );
390
			if ( ! empty( $options['ga_general'] ) ) {
391
				$options = $options['ga_general'];
392
			}
393
394
395
			// Let's remove the defaults
396
			if ( isset( $options['ga_general'] ) ) {
397
				unset( $options['ga_general'] );
398
			}
399
400
			// Let's remove unused options
401
			if ( isset( $options['yoast_ga_nonce'] ) ) {
402
				unset( $options['yoast_ga_nonce'] );
403
			}			
404
			if ( isset( $options['ga-form-settings'] ) ) {
405
				unset( $options['ga-form-settings'] );
406
			}
407
			if ( isset( $options['string_error_custom_dimensions'] ) ) {
408
				unset( $options['string_error_custom_dimensions'] );
409
			}
410
			if ( isset( $options['custom_metrics'] ) ) {
411
				unset( $options['custom_metrics'] );
412
			}	
413
			if ( isset( $options['track_full_url'] ) ) {
414
				unset( $options['track_full_url'] );
415
			}	
416
			if ( isset( $options['version'] ) ) {
417
				unset( $options['version'] );
418
			}
419
420
			// Migrate universal to tracking_mode
421
			if ( isset( $options['enable_universal'] ) ) {
422
				unset( $options['enable_universal'] );
423
				$options['tracking_mode'] = 'analytics';
424
			} else {
425
				$options['tracking_mode'] = 'ga';
426
			}
427
428
			// Migrate events tracking
429
			if ( isset( $options['track_outbound'] ) ) {
430
				unset( $options['track_outbound'] );
431
				$options['events_mode'] = 'php';
432
			} else {
433
				$options['events_mode'] = 'none';
434
			}
435
436
			// Migrate anonymous_data to allow tracking
437
			if ( isset( $options['anonymous_data'] ) ) {
438
				unset( $options['anonymous_data'] );
439
				$options['allow_tracking'] = 1;
440
			} else {
441
				$options['allow_tracking'] = 0;
442
			}
443
444
		
445
		// Migrate GA profile data if there
446
			// first let's try to salvage the current profile
447
			$access_token  = get_option( 'yoast-ga-access_token', array() );
448
			$refresh_token = get_option( 'yoast-ga-refresh_token', array() );
449
			$profiles      = get_option( 'yst_ga_api', array() );
450
451
			$profile_name  = ! empty( $options['analytics_profile'] ) ? $options['analytics_profile'] : ''; 
452
453
				if ( empty( $refresh_token ) && ! empty( $access_token['refresh_token'] ) ) {
454
					$refresh_token = $access_token['refresh_token'];
455
				}
456
	
457
				// We need a name and a profile
458
				if ( ! empty( $refresh_token ) && ! empty( $options['analytics_profile'] ) && ! empty( $profiles['ga_api_response_accounts'] ) ) {
459
					// See if we have an access token
460
					if ( ! empty( $access_token['access_token'] ) ) {
461
						if ( monsterinsights_is_pro_version() ) {
462
							update_option( 'monsterinsights_pro_access_token', $access_token['access_token'] );
463
						} else {
464
							update_option( 'monsterinsights_lite_access_token', $access_token['access_token'] );
465
						}
466
					}
467
468
					// We need a refresh token
469
					if ( monsterinsights_is_pro_version() ) {
470
						update_option( 'monsterinsights_pro_refresh_token', $refresh_token );
471
					} else {
472
						update_option( 'monsterinsights_lite_refresh_token', $refresh_token );
473
					}
474
475
					// If we can find the profile in the response save the name
476
					if ( ! empty( $profiles['ga_api_response_accounts'] ) && is_array( $profiles['ga_api_response_accounts'] ) ) {
477
						foreach ( $profiles['ga_api_response_accounts'] as $account ) {
478
							foreach ( $account['items'] as $profile ) {
479
								foreach ( $profile['items'] as $subprofile ) {
480
									if ( isset( $subprofile['id'] ) && $subprofile['id'] == $profile_name ) {
481
										$options['analytics_profile_name'] = $subprofile['name'];
482
										if ( empty( $options['analytics_profile_code'] ) ) {
483
											$options['analytics_profile_code'] = $subprofile['ua_code'];
484
										}
485
										break 3;
486
									}
487
								}
488
							}
489
						}
490
					}
491
					$options['cron_last_run'] = strtotime("-25 hours");
492
				} else {
493
					// if UA in manual code field, remove analytics profile fields if set
494
					if ( ! empty( $options['manual_ua_code_field' ] ) ) {
495
						if ( isset( $options['analytics_profile_code'] ) ) {
496
							unset( $options['analytics_profile_code'] );
497
						}
498
						if ( isset( $options['analytics_profile'] ) ) {
499
							unset( $options['analytics_profile'] );
500
						}
501
						$options['manual_ua_code'] = $options['manual_ua_code_field'];
502
						delete_option( 'yoast-ga-access_token' );
503
						delete_option( 'yoast-ga-refresh_token' );
504
						delete_option( 'yst_ga_api' );
505
					} else if ( ! empty( $options['analytics_profile_code' ] ) ) {
506
					// if UA in profile fields, remove others and use that
507
						$options['manual_ua_code'] = $options['analytics_profile_code'];
508
						if ( isset( $options['analytics_profile_code'] ) ) {
509
							unset( $options['analytics_profile_code'] );
510
						}
511
						if ( isset( $options['analytics_profile'] ) ) {
512
							unset( $options['analytics_profile'] );
513
						}
514
						delete_option( 'yoast-ga-access_token' );
515
						delete_option( 'yoast-ga-refresh_token' );
516
						delete_option( 'yst_ga_api' );
517
					}  else {
518
					// if UA in profile profiles, remove others and use that
519
						if ( ! empty( $options['analytics_profile' ] ) && ! empty( $profiles['ga_api_response_accounts'] ) && is_array( $profiles['ga_api_response_accounts'] ) ) {
520
							foreach ( $profiles as $account ) {
521
								foreach ( $account['items'] as $profile ) {
522
									foreach ( $profile['items'] as $subprofile ) {
523
										if ( isset( $subprofile['id'] ) && $subprofile['id'] == $options['analytics_profile' ] ) {
524
											$options['manual_ua_code'] = $subprofile['ua_code'];
525
											break 3;
526
										}
527
									}
528
								}
529
							}
530
						}
531
						delete_option( 'yoast-ga-access_token' );
532
						delete_option( 'yoast-ga-refresh_token' );
533
						delete_option( 'yst_ga_api' );
534
					}
535
				}
536
			
537
			if ( isset( $options['manual_ua_code_field'] ) ) {
538
				unset( $options['manual_ua_code_field'] );
539
			}
540
541
		// oAuth Stir Data Tank
542
			// Will happen automatically as cron_last_run set to 25 hours ago
543
544
		// Add oAuth version 
545
				$options['oauth_version'] = '1.0.0';
546
547
		$data = array(
548
			'installed_version' => MONSTERINSIGHTS_VERSION,
549
			'installed_date'    => time(),
550
			'installed_pro'     => monsterinsights_is_pro_version(),
551
		);
552
553
		update_option( 'monsterinsights_over_time', $data );
554
		
555
		// Add the cron job
556
		//if ( ! wp_next_scheduled( 'monsterinsights_daily_cron' ) ) {
557
			// Set the next event of fetching data
558
			//wp_schedule_event( strtotime( date( 'Y-m-d', strtotime( 'tomorrow' ) ) . ' 00:05:00 ' ), 'daily', 'monsterinsights_daily_cron' );
559
		//}
560
		
561
		// Finish up
562
			// Save the new settings
563
			$this->new_settings = $options;
564
	}
565
566
	public function get_yoast_default_values() {
567
		$options = array(
568
			'ga_general' => array(
569
				'analytics_profile'          => null,
570
				'analytics_profile_code'     => null,
571
				'manual_ua_code'             => 0,
572
				'manual_ua_code_field'       => null,
573
				'track_internal_as_outbound' => null,
574
				'track_internal_as_label'    => null,
575
				'track_outbound'             => 0,
576
				'anonymous_data'             => 0,
577
				'enable_universal'           => 1,
578
				'demographics'               => 0,
579
				'ignore_users'               => array( 'administrator', 'editor' ),
580
				'dashboards_disabled'        => 0,
581
				'anonymize_ips'              => 0,
582
				'track_download_as'          => 'event',
583
				'extensions_of_files'        => 'doc,exe,js,pdf,ppt,tgz,zip,xls',
584
				'track_full_url'             => 'domain',
585
				'subdomain_tracking'         => null,
586
				'tag_links_in_rss'           => 0,
587
				'allow_anchor'               => 0,
588
				'add_allow_linker'           => 0,
589
				'enhanced_link_attribution'  => 0,
590
				'custom_code'                => null,
591
				'debug_mode'                 => 0,
592
			)
593
		);
594
		$options = apply_filters( 'yst_ga_default-ga-values', $options, 'ga_general' );
595
		return $options;
596
	}
597
598
	public function get_monsterinsights_default_values() {
599
		return array(
600
			'analytics_profile'          => '',
601
			'analytics_profile_code'     => '',
602
			'manual_ua_code'             => '',
603
			'track_internal_as_outbound' => 0,
604
			'track_internal_as_label'    => '',
605
			'track_outbound'             => 1,
606
			'allow_tracking'             => 0,
607
			'tracking_mode'              => 'analytics',
608
			'events_mode'                => 'js',
609
			'demographics'               => 1,
610
			'ignore_users'               => array( 'administrator', 'editor' ),
611
			'dashboards_disabled'        => 0,
612
			'anonymize_ips'              => 0,
613
			'track_download_as'          => 'event',
614
			'extensions_of_files'        => 'doc,exe,js,pdf,ppt,tgz,zip,xls',
615
			'subdomain_tracking'         => '',
616
			'tag_links_in_rss'           => 0,
617
			'allow_anchor'               => 0,
618
			'add_allow_linker'           => 0,
619
			'enhanced_link_attribution'  => 1,
620
			'custom_code'                => '',
621
			'debug_mode'                 => 0,
622
			'anonymous_data'             => 0,
623
			'save_settings'              => array(),
624
			'view_reports'               => array(),
625
		);
626
	}
627
628
	/**
629
	 * MonsterInsights Version 6.0.2 upgrades.
630
	 *
631
	 * This detects if a manual auth code is in the Yoast settings, and not
632
	 * in the MI settings, and that oAuth hasn't been performed (caused by the
633
	 * manual ua code not being transferred during the 6.0 upgrade routine)
634
	 * and automatically fixes it.
635
	 *
636
	 * @since 6.0.2
637
	 * @access public
638
	 * 
639
	 * @return void
640
	 */
641
	public function v602_upgrades() {
642
		$options = get_option( 'yst_ga', array() );
643
		if ( ( empty( $this->new_settings[ 'manual_ua_code'] ) || $this->new_settings[ 'manual_ua_code']  === '1' )  &&  
644
			 empty( $this->new_settings[ 'analytics_profile_code'] ) &&
645
			 ! empty( $options ) &&
646
			 is_array( $options ) &&
647
			 ! empty( $options['ga_general']['manual_ua_code_field'] )
648
		) {
649
			$this->new_settings['manual_ua_code'] = $options['ga_general']['manual_ua_code_field'];
650
		}
651
	}
652
653
	/**
654
	 * MonsterInsights Version 6.0.11 upgrades.
655
	 *
656
	 * This upgrade routine finds and removes the old crons if they exist.
657
	 *
658
	 * @since 6.0.11
659
	 * @access public
660
	 * 
661
	 * @return void
662
	 */
663
	public function v6011_upgrades() {
664
		// If old tracking checkin exists, remove it
665
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_checkin' ) ) {
666
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_checkin' );
667
		}
668
669
		// Remove Weekly cron
670
		if ( wp_next_scheduled( 'monsterinsights_weekly_cron' ) ) {
671
			wp_clear_scheduled_hook( 'monsterinsights_weekly_cron' );
672
		}
673
674
		// Remove Yoast cron
675
		if ( wp_next_scheduled( 'yst_ga_aggregate_data' ) ) {
676
			wp_clear_scheduled_hook( 'yst_ga_aggregate_data' );
677
		}
678
	}
679
680
	/**
681
	 * MonsterInsights Version 6.2.0 upgrades.
682
	 *
683
	 * Turns off debug mode if its on.
684
	 *
685
	 * @since 6.2.0
686
	 * @access public
687
	 * 
688
	 * @return void
689
	 */
690
	public function v620_upgrades() {
691
		// Turns off debug mode if its on.
692
		if ( empty( $this->new_settings['debug_mode' ] ) ) {
693
			$this->new_settings['debug_mode' ] = 0;
694
		}
695
	}
696
697
	/**
698
	 * MonsterInsights Version 7.0 upgrades.
699
	 *
700
	 * This function does the
701
	 * upgrade routine from MonsterInsights 6.2->7.0.
702
	 *
703
	 * @since 7.0.0
704
	 * @access public
705
	 * 
706
	 * @return void
707
	 */
708
	public function v700_upgrades() {
709
		// 1. Remove old Yoast GA options
710
		delete_option( 'yst_ga' );
711
712
		// 2. Remove old cron jobs
713
			// 2a Remove Yoast cron
714
			if ( wp_next_scheduled( 'yst_ga_aggregate_data' ) ) {
715
				wp_clear_scheduled_hook( 'yst_ga_aggregate_data' );
716
			}
717
718
			// 2b Remove Weekly cron
719
			if ( wp_next_scheduled( 'monsterinsights_weekly_cron' ) ) {
720
				wp_clear_scheduled_hook( 'monsterinsights_weekly_cron' );
721
			}
722
723
			// 2c Remove old tracking checkin
724
			if ( wp_next_scheduled( 'monsterinsights_send_tracking_checkin' ) ) {
725
				wp_clear_scheduled_hook( 'monsterinsights_send_tracking_checkin' );
726
			}
727
728
		// 3. Default all event tracking and tracking to GA + JS respectively
729
			// 3a Set tracking_mode to use analytics.js
730
			$this->new_settings['tracking_mode' ] = 'analytics';
731
			
732
733
			// 3b Set events mode to use JS if the events mode is not set explicitly to none
734
			if ( empty( $this->new_settings['events_mode' ] ) || $this->new_settings['events_mode' ] !== 'none' ) {
735
				$this->new_settings['events_mode' ] = 'js';
736
			}
737
738
		// 4. Migrate manual UA codes
739
			// 4a Manual UA has the lowest priority
740
			if ( ! empty( $this->new_settings['manual_ua_code' ] ) ) {
741
				// Set as manual UA code
742
				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' ] ) );
743
			}
744
745
			// 4b Then try the oAuth UA code
746
			if ( ! empty( $this->new_settings['analytics_profile_code' ] ) ) {
747
				// Set as manual UA code
748
				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' ] ) );
749
			}
750
751
		// 5. Migrate License keys
752
		if ( is_multisite() ) {
753
			$ms_license = get_site_option( 'monsterinsights_license', '' );
754
			if ( $ms_license ) {
755
				update_site_option( 'monsterinsights_network_license_updates', get_site_option( 'monsterinsights_license_updates', '' ) );
756
				update_site_option( 'monsterinsights_network_license', $ms_license );
757
			}
758
		}
759
760
		// 6. Remove old cron
761
		if ( wp_next_scheduled( 'monsterinsights_daily_cron' ) ) {
762
			wp_clear_scheduled_hook( 'monsterinsights_daily_cron' );
763
		}
764
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_data' ) ) {
765
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_data' );
766
		}
767
		delete_option( 'monsterinsights_tracking_last_send' );
768
		delete_option( 'mi_tracking_last_send' );
769
770
		// 7. Remove deprecated settings
771
		$settings = array( '_repeated', 'ajax', 'asmselect0', 'bawac_force_nonce', 'cf_email', 'cf_message', 'cf_name', 'cf_number',
772
							'cf_phone', 'cf_subject', 'credentials', 'cron_failed', 'cron_last_run', 'firebug_lite', 'global-css', 'google_auth_code', 
773
							'grids', 'icl_post_language', 'mlcf_email', 'mlcf_name', 'navigation-skins', 'page', 'punch-fonts', 'return_tab', 'skins',
774
							'wpcf_email', 'wpcf_your_name' );
775
		foreach ( $settings as $setting ) {
776
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
777
				unset( $this->new_settings[ $setting ] );
778
			}
779
		}
780
781
	}
782
783
	public function v720_upgrades() {
784
		// 1. remove old API keys from MI settings & manual UA code
785
		// 2. Comprehensively review old settings and meta keys from pre-relay and consider removing them
786
		// 3. Consider removing all install back compat to yoast.
787
		// 4. Remove old cron
788
		if ( wp_next_scheduled( 'monsterinsights_daily_cron' ) ) {
789
			wp_clear_scheduled_hook( 'monsterinsights_daily_cron' );
790
		}
791
		if ( wp_next_scheduled( 'monsterinsights_send_tracking_data' ) ) {
792
			wp_clear_scheduled_hook( 'monsterinsights_send_tracking_data' );
793
		}
794
		delete_option( 'monsterinsights_tracking_last_send' );
795
		delete_option( 'mi_tracking_last_send' );
796
797
		// 5. Remove deprecated settings
798
		$settings = array( '_repeated', 'ajax', 'asmselect0', 'bawac_force_nonce', 'cf_email', 'cf_message', 'cf_name', 'cf_number',
799
							'cf_phone', 'cf_subject', 'credentials', 'cron_failed', 'cron_last_run', 'firebug_lite', 'global-css', 'google_auth_code', 
800
							'grids', 'icl_post_language', 'mlcf_email', 'mlcf_name','navigation-skins', 'page', 'punch-fonts', 'return_tab', 'skins',
801
							'wpcf_email', 'wpcf_your_name' );
802
		foreach ( $settings as $setting ) {
803
			if ( ! empty( $this->new_settings[ $setting ] ) ) {
804
				unset( $this->new_settings[ $setting ] );
805
			}
806
		}
807
808
		// 5. Remove old GA settings like oauth_version
809
	}
810
}
811