config.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * Author: ExactMetrics
  4. * Author URI: https://exactmetrics.com
  5. * Copyright 2018 ExactMetrics team
  6. * License: GPLv2 or later
  7. * License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9. // Exit if accessed directly
  10. if ( ! defined( 'ABSPATH' ) )
  11. exit();
  12. if ( ! class_exists( 'GADWP_Config' ) ) {
  13. final class GADWP_Config {
  14. public $options;
  15. public function __construct() {
  16. // Rename old option keys
  17. $this->option_keys_rename(); // v5.2
  18. // Get plugin options
  19. $this->get_plugin_options();
  20. // Automatic updates
  21. add_filter( 'auto_update_plugin', array( $this, 'automatic_update' ), 10, 2 );
  22. // Provide language packs for all available Network languages
  23. if ( is_multisite() ) {
  24. add_filter( 'plugins_update_check_locales', array( $this, 'translation_updates' ), 10, 1 );
  25. }
  26. }
  27. public function get_major_version( $version ) {
  28. $exploded_version = explode( '.', $version );
  29. if ( isset( $exploded_version[2] ) ) {
  30. return $exploded_version[0] . '.' . $exploded_version[1] . '.' . $exploded_version[2];
  31. } else {
  32. return $exploded_version[0] . '.' . $exploded_version[1] . '.0';
  33. }
  34. }
  35. public function automatic_update( $update, $item ) {
  36. $item = (array) $item;
  37. if ( is_multisite() && ! is_main_site() ) {
  38. return;
  39. }
  40. if ( ! isset( $item['new_version'] ) || ! isset( $item['plugin'] ) || ! $this->options['automatic_updates_minorversion'] ) {
  41. return $update;
  42. }
  43. if ( isset( $item['slug'] ) && 'google-analytics-dashboard-for-wp' == $item['slug'] ) {
  44. // Only when a minor update is available
  45. if ( $this->get_major_version( GADWP_CURRENT_VERSION ) == $this->get_major_version( $item['new_version'] ) ) {
  46. return ( $this->get_major_version( GADWP_CURRENT_VERSION ) == $this->get_major_version( $item['new_version'] ) );
  47. }
  48. }
  49. return $update;
  50. }
  51. public function translation_updates( $locales ) {
  52. $languages = get_available_languages();
  53. return array_values( $languages );
  54. }
  55. // Validates data before storing
  56. private function validate_data( $options ) {
  57. /* @formatter:off */
  58. $numerics = array( 'ga_realtime_pages',
  59. 'ga_enhanced_links',
  60. 'ga_crossdomain_tracking',
  61. 'ga_author_dimindex',
  62. 'ga_category_dimindex',
  63. 'ga_tag_dimindex',
  64. 'ga_user_dimindex',
  65. 'ga_pubyear_dimindex',
  66. 'ga_pubyearmonth_dimindex',
  67. 'tm_author_var',
  68. 'tm_category_var',
  69. 'tm_tag_var',
  70. 'tm_user_var',
  71. 'tm_pubyear_var',
  72. 'tm_pubyearmonth_var',
  73. 'ga_aff_tracking',
  74. 'amp_tracking_analytics',
  75. 'amp_tracking_clientidapi',
  76. 'amp_tracking_tagmanager',
  77. 'optimize_tracking',
  78. 'optimize_pagehiding',
  79. 'trackingcode_infooter',
  80. 'trackingevents_infooter',
  81. 'ga_formsubmit_tracking',
  82. 'superadmin_tracking',
  83. 'ga_pagescrolldepth_tracking',
  84. 'tm_pagescrolldepth_tracking',
  85. 'ga_speed_samplerate',
  86. 'ga_user_samplerate',
  87. 'ga_event_precision',
  88. 'with_endpoint',
  89. 'backend_realtime_report',
  90. 'ga_optout',
  91. 'ga_dnt_optout',
  92. 'tm_optout',
  93. 'tm_dnt_optout',
  94. 'ga_with_gtag',
  95. 'usage_tracking',
  96. 'hide_am_notices',
  97. 'network_hide_am_notices',
  98. );
  99. foreach ( $numerics as $key ) {
  100. if ( isset( $options[$key] ) ) {
  101. $options[$key] = (int) $options[$key];
  102. }
  103. }
  104. $texts = array( 'ga_crossdomain_list',
  105. 'client_id',
  106. 'client_secret',
  107. 'theme_color',
  108. 'ga_target_geomap',
  109. 'ga_cookiedomain',
  110. 'ga_cookiename',
  111. 'pagetitle_404',
  112. 'maps_api_key',
  113. 'web_containerid',
  114. 'amp_containerid',
  115. 'optimize_containerid',
  116. 'ga_event_downloads',
  117. 'ga_event_affiliates',
  118. 'ecommerce_mode',
  119. 'tracking_type',
  120. );
  121. foreach ( $texts as $key ) {
  122. if ( isset( $options[$key] ) ) {
  123. $options[$key] = trim (sanitize_text_field( $options[$key] ));
  124. }
  125. }
  126. /* @formatter:on */
  127. if ( isset( $options['ga_event_downloads'] ) && empty( $options['ga_event_downloads'] ) ) {
  128. $options['ga_event_downloads'] = 'zip|mp3*|mpe*g|pdf|docx*|pptx*|xlsx*|rar*';
  129. }
  130. if ( isset( $options['pagetitle_404'] ) && empty( $options['pagetitle_404'] ) ) {
  131. $options['pagetitle_404'] = 'Page Not Found';
  132. }
  133. if ( isset( $options['ga_event_affiliates'] ) && empty( $options['ga_event_affiliates'] ) ) {
  134. $options['ga_event_affiliates'] = '/out/';
  135. }
  136. if ( isset( $options['ga_speed_samplerate'] ) && ( $options['ga_speed_samplerate'] < 1 || $options['ga_speed_samplerate'] > 100 ) ) {
  137. $options['ga_speed_samplerate'] = 1;
  138. }
  139. if ( isset( $options['ga_user_samplerate'] ) && ( $options['ga_user_samplerate'] < 1 || $options['ga_user_samplerate'] > 100 ) ) {
  140. $options['ga_user_samplerate'] = 100;
  141. }
  142. if ( isset( $options['ga_cookieexpires'] ) && $options['ga_cookieexpires'] ) { // v4.9
  143. $options['ga_cookieexpires'] = (int) $options['ga_cookieexpires'];
  144. }
  145. return $options;
  146. }
  147. public function set_plugin_options( $network_settings = false ) {
  148. // Handle Network Mode
  149. $options = $this->options;
  150. $get_network_options = get_site_option( 'gadwp_network_options' );
  151. $old_network_options = (array) json_decode( $get_network_options );
  152. if ( is_multisite() ) {
  153. if ( $network_settings ) { // Retrieve network options, clear blog options, store both to db
  154. $network_options['token'] = $this->options['token'];
  155. $options['token'] = '';
  156. if ( is_network_admin() ) {
  157. $network_options['ga_profiles_list'] = $this->options['ga_profiles_list'];
  158. $options['ga_profiles_list'] = array();
  159. $network_options['client_id'] = $this->options['client_id'];
  160. $options['client_id'] = '';
  161. $network_options['client_secret'] = $this->options['client_secret'];
  162. $options['client_secret'] = '';
  163. $network_options['user_api'] = $this->options['user_api'];
  164. $options['user_api'] = 0;
  165. $network_options['network_mode'] = $this->options['network_mode'];
  166. $network_options['superadmin_tracking'] = $this->options['superadmin_tracking'];
  167. $network_options['automatic_updates_minorversion'] = $this->options['automatic_updates_minorversion'];
  168. unset( $options['network_mode'] );
  169. if ( isset( $this->options['network_tableid'] ) ) {
  170. $network_options['network_tableid'] = $this->options['network_tableid'];
  171. unset( $options['network_tableid'] );
  172. }
  173. }
  174. $merged_options = array_merge( $old_network_options, $network_options );
  175. update_site_option( 'gadwp_network_options', json_encode( $this->validate_data( $merged_options ) ) );
  176. }
  177. }
  178. update_option( 'gadwp_options', json_encode( $this->validate_data( $options ) ) );
  179. }
  180. private function get_plugin_options() {
  181. /*
  182. * Get plugin options
  183. */
  184. global $blog_id;
  185. if ( ! get_option( 'gadwp_options' ) ) {
  186. GADWP_Install::install();
  187. }
  188. $this->options = (array) json_decode( get_option( 'gadwp_options' ) );
  189. // Maintain Compatibility
  190. $this->maintain_compatibility();
  191. // Handle Network Mode
  192. if ( is_multisite() ) {
  193. $get_network_options = get_site_option( 'gadwp_network_options' );
  194. $network_options = (array) json_decode( $get_network_options );
  195. if ( isset( $network_options['network_mode'] ) && ( $network_options['network_mode'] ) ) {
  196. if ( ! is_network_admin() && ! empty( $network_options['ga_profiles_list'] ) && isset( $network_options['network_tableid']->$blog_id ) ) {
  197. $network_options['ga_profiles_list'] = array( 0 => GADWP_Tools::get_selected_profile( $network_options['ga_profiles_list'], $network_options['network_tableid']->$blog_id ) );
  198. $network_options['tableid_jail'] = $network_options['ga_profiles_list'][0][1];
  199. }
  200. $this->options = array_merge( $this->options, $network_options );
  201. } else {
  202. $this->options['network_mode'] = 0;
  203. }
  204. }
  205. }
  206. private function maintain_compatibility() {
  207. $flag = false;
  208. $prevver = get_option( 'gadwp_version' );
  209. if ( $prevver && GADWP_CURRENT_VERSION != $prevver ) {
  210. $flag = true;
  211. update_option( 'gadwp_version', GADWP_CURRENT_VERSION );
  212. update_option( 'gadwp_got_updated', true );
  213. GADWP_Tools::clear_cache();
  214. GADWP_Tools::delete_cache( 'last_error' );
  215. if ( is_multisite() ) { // Cleanup errors and cookies on the entire network
  216. foreach ( GADWP_Tools::get_sites( array( 'number' => apply_filters( 'gadwp_sites_limit', 100 ) ) ) as $blog ) {
  217. switch_to_blog( $blog['blog_id'] );
  218. GADWP_Tools::delete_cache( 'gapi_errors' );
  219. restore_current_blog();
  220. }
  221. } else {
  222. GADWP_Tools::delete_cache( 'gapi_errors' );
  223. }
  224. // Enable GADWP EndPoint for those updating from a version lower than 5.2, introduced in GADWP v5.3
  225. if (version_compare( $prevver, '5.2', '<' ) ) {
  226. $this->options['with_endpoint'] = 2;
  227. }
  228. }
  229. if ( isset( $this->options['item_reports'] ) ) { // v4.8
  230. $this->options['backend_item_reports'] = $this->options['item_reports'];
  231. }
  232. if ( isset( $this->options['ga_dash_frontend_stats'] ) ) { // v4.8
  233. $this->options['frontend_item_reports'] = $this->options['ga_dash_frontend_stats'];
  234. }
  235. /* @formatter:off */
  236. $zeros = array( 'ga_enhanced_links',
  237. 'network_mode',
  238. 'ga_enhanced_excludesa',
  239. 'ga_remarketing',
  240. 'ga_event_bouncerate',
  241. 'ga_author_dimindex',
  242. 'ga_tag_dimindex',
  243. 'ga_category_dimindex',
  244. 'ga_user_dimindex',
  245. 'ga_pubyear_dimindex',
  246. 'ga_pubyearmonth_dimindex',
  247. 'tm_author_var', // v5.0
  248. 'tm_category_var', // v5.0
  249. 'tm_tag_var', // v5.0
  250. 'tm_user_var', // v5.0
  251. 'tm_pubyear_var', // v5.0
  252. 'tm_pubyearmonth_var', // v5.0
  253. 'ga_crossdomain_tracking',
  254. 'api_backoff', // v4.8.1.3
  255. 'ga_aff_tracking',
  256. 'ga_hash_tracking',
  257. 'switch_profile', // V4.7
  258. 'amp_tracking_analytics', //v5.0
  259. 'amp_tracking_clientidapi', //v5.1.2
  260. 'optimize_tracking', //v5.0
  261. 'optimize_pagehiding', //v5.0
  262. 'amp_tracking_tagmanager', //v5.0
  263. 'trackingcode_infooter', //v5.0
  264. 'trackingevents_infooter', //v5.0
  265. 'ga_formsubmit_tracking', //v5.0
  266. 'superadmin_tracking', //v5.0
  267. 'ga_pagescrolldepth_tracking', //v5.0
  268. 'tm_pagescrolldepth_tracking', //v5.0
  269. 'ga_event_precision', //v5.1.1.1
  270. 'ga_force_ssl', //v5.1.2
  271. 'with_endpoint', //v5.2
  272. 'backend_realtime_report', //v5.2
  273. 'ga_optout', //v5.2.3
  274. 'ga_dnt_optout', //v5.2.3
  275. 'ga_with_gtag', //v5.3
  276. 'frontend_item_reports',
  277. 'tm_optout', //v5.3.1.2
  278. 'tm_dnt_optout', //v5.3.1.2
  279. 'usage_tracking', //v5.3.4
  280. 'hide_am_notices', //v5.3.4
  281. 'network_hide_am_notices', //v5.3.4
  282. );
  283. foreach ( $zeros as $key ) {
  284. if ( ! isset( $this->options[$key] ) ) {
  285. $this->options[$key] = 0;
  286. $flag = true;
  287. }
  288. }
  289. if ( isset($this->options['ga_dash_tracking']) && 0 == $this->options['ga_dash_tracking'] ) { // v5.0.1
  290. $this->options['tracking_type'] = 'disabled';
  291. $flag = true;
  292. }
  293. $unsets = array( 'ga_dash_jailadmins', // v4.7
  294. 'ga_tracking_code',
  295. 'ga_dash_tableid', // v4.9
  296. 'ga_dash_frontend_keywords', // v4.8
  297. 'ga_dash_apikey', // v4.9.1.3
  298. 'ga_dash_default_metric', // v4.8.1
  299. 'ga_dash_default_dimension', // v4.8.1
  300. 'ga_dash_adsense', // v5.0
  301. 'ga_dash_frontend_stats', // v4.8
  302. 'item_reports', // v4.8
  303. 'ga_dash_tracking', // v5.0
  304. 'ga_dash_cachetime', // v5.2
  305. 'ga_dash_default_ua', // v5.2
  306. 'ga_dash_hidden', // v5.2
  307. );
  308. foreach ( $unsets as $key ) {
  309. if ( isset( $this->options[$key] ) ) {
  310. unset( $this->options[$key] );
  311. $flag = true;
  312. }
  313. }
  314. $empties = array( 'ga_crossdomain_list',
  315. 'ga_cookiedomain', // v4.9.4
  316. 'ga_cookiename', // v4.9.4
  317. 'ga_cookieexpires', // v4.9.4
  318. 'maps_api_key', // v4.9.4
  319. 'web_containerid', // v5.0
  320. 'amp_containerid', // v5.0
  321. 'optimize_containerid', // v5.0
  322. );
  323. foreach ( $empties as $key ) {
  324. if ( ! isset( $this->options[$key] ) ) {
  325. $this->options[$key] = '';
  326. $flag = true;
  327. }
  328. }
  329. $ones = array( 'ga_speed_samplerate',
  330. 'automatic_updates_minorversion',
  331. 'backend_item_reports', // v4.8
  332. 'dashboard_widget', // v4.7
  333. );
  334. foreach ( $ones as $key ) {
  335. if ( ! isset( $this->options[$key] ) ) {
  336. $this->options[$key] = 1;
  337. $flag = true;
  338. }
  339. }
  340. $arrays = array( 'access_front',
  341. 'access_back',
  342. 'ga_profiles_list',
  343. 'track_exclude',
  344. );
  345. foreach ( $arrays as $key ) {
  346. if ( ! is_array( $this->options[$key] ) ) {
  347. $this->options[$key] = array();
  348. $flag = true;
  349. }
  350. }
  351. if ( empty( $this->options['access_front'] ) ) {
  352. $this->options['access_front'][] = 'administrator';
  353. }
  354. if ( empty( $this->options['access_back'] ) ) {
  355. $this->options['access_back'][] = 'administrator';
  356. }
  357. /* @formatter:on */
  358. if ( ! isset( $this->options['ga_event_affiliates'] ) ) {
  359. $this->options['ga_event_affiliates'] = '/out/';
  360. $flag = true;
  361. }
  362. if ( ! isset( $this->options['ga_user_samplerate'] ) ) {
  363. $this->options['ga_user_samplerate'] = 100;
  364. }
  365. if ( ! isset( $this->options['ga_event_downloads'] ) ) {
  366. $this->options['ga_event_downloads'] = 'zip|mp3*|mpe*g|pdf|docx*|pptx*|xlsx*|rar*';
  367. $flag = true;
  368. }
  369. if ( ! isset( $this->options['pagetitle_404'] ) ) { // v4.9.4
  370. $this->options['pagetitle_404'] = 'Page Not Found';
  371. $flag = true;
  372. }
  373. if ( ! isset( $this->options['ecommerce_mode'] ) ) { // v5.0
  374. $this->options['ecommerce_mode'] = 'disabled';
  375. $flag = true;
  376. }
  377. if ( isset( $this->options['ga_dash_tracking'] ) && 'classic' == $this->options['ga_dash_tracking'] ) { // v5.0
  378. $this->options['tracking_type'] = 'universal';
  379. $flag = true;
  380. }
  381. if ( $flag ) {
  382. $this->set_plugin_options( false );
  383. }
  384. }
  385. private function option_keys_rename() {
  386. /* @formatter:off */
  387. $batch = array( 'ga_dash_token' => 'token',
  388. 'ga_dash_clientid' => 'client_id',
  389. 'ga_dash_clientsecret' => 'client_secret',
  390. 'ga_dash_access_front' => 'access_front',
  391. 'ga_dash_access_back' => 'access_back',
  392. 'ga_dash_tableid_jail' => 'tableid_jail',
  393. 'ga_dash_tracking_type' => 'tracking_type',
  394. 'ga_dash_userapi' => 'user_api',
  395. 'ga_dash_network' => 'network_mode',
  396. 'ga_dash_tableid_network' => 'network_tableid',
  397. 'ga_dash_anonim' => 'ga_anonymize_ip',
  398. 'ga_dash_profile_list' => 'ga_profiles_list',
  399. 'ga_dash_remarketing' => 'ga_remarketing',
  400. 'ga_dash_excludesa' => 'superadmin_tracking',
  401. 'ga_track_exclude' => 'track_exclude',
  402. 'ga_dash_style' => 'theme_color',
  403. );
  404. /* @formatter:on */
  405. if ( is_multisite() ) {
  406. $options = get_site_option( 'gadash_network_options' );
  407. if ( $options ) {
  408. $options = (array) json_decode( $options );
  409. $options = GADWP_Tools::array_keys_rename( $options, $batch );
  410. update_site_option( 'gadwp_network_options', json_encode( $this->validate_data( $options ) ) );
  411. delete_site_option( 'gadash_network_options' );
  412. }
  413. }
  414. $options = get_option( 'gadash_options' );
  415. if ( $options ) {
  416. $options = (array) json_decode( $options );
  417. $options = GADWP_Tools::array_keys_rename( $options, $batch );
  418. update_option( 'gadwp_options', json_encode( $this->validate_data( $options ) ) );
  419. delete_option( 'gadash_options' );
  420. }
  421. }
  422. }
  423. }