install.php 29 KB

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