/includes/options.php

  1. <?php 
  2. /** 
  3. * Option functions. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Options 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. function monsterinsights_get_options() { 
  17. $settings = array(); 
  18. $option_name = monsterinsights_get_option_name(); 
  19. //$settings = get_site_option( $option_name ); 
  20. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; 
  21. //$is_network = is_multisite(); 
  22.  
  23. //if ( $is_network && $use_network_settings ) { 
  24. // return $settings; 
  25. //} else if ( $is_network ) { 
  26. $settings = get_option( $option_name ); 
  27. //} else { 
  28. // return $settings; 
  29. //} 
  30. if ( empty( $settings ) ) { 
  31. $settings = array(); 
  32. return $settings; 
  33.  
  34. /** 
  35. * Helper method for getting a setting's value. Falls back to the default 
  36. * setting value if none exists in the options table. 
  37. * 
  38. * @since 6.0.0 
  39. * @access public 
  40. * 
  41. * @param string $key The setting key to retrieve. 
  42. * @param mixed $default The default value of the setting key to retrieve. 
  43. * @return string The value of the setting. 
  44. */ 
  45. function monsterinsights_get_option( $key = '', $default = false ) { 
  46. global $monsterinsights_settings; 
  47. $value = ! empty( $monsterinsights_settings[ $key ] ) ? $monsterinsights_settings[ $key ] : $default; 
  48. $value = apply_filters( 'monsterinsights_get_option', $value, $key, $default ); 
  49. return apply_filters( 'monsterinsights_get_option_' . $key, $value, $key, $default ); 
  50.  
  51. /** 
  52. * Helper method for getting the UA string. 
  53. * 
  54. * @since 6.0.0 
  55. * @access public 
  56. * 
  57. * @return string The UA to use. 
  58. */ 
  59. function monsterinsights_get_ua() { 
  60. $ua = ''; 
  61. if ( is_multisite() ) { 
  62. if ( defined( 'MONSTERINSIGHTS_MS_GA_UA' ) && monsterinsights_is_valid_ua( MONSTERINSIGHTS_MS_GA_UA ) ) { 
  63. $ua = MONSTERINSIGHTS_MS_GA_UA; 
  64.  
  65. if ( is_multisite() ) { 
  66. $ua_code = monsterinsights_is_valid_ua( get_site_option( 'monsterinsights_network_manual_ua_code', '' ) ); 
  67. if ( $ua_code ) { 
  68. $ua = $ua_code; 
  69.  
  70. if ( defined( 'MONSTERINSIGHTS_GA_UA' ) && monsterinsights_is_valid_ua( MONSTERINSIGHTS_GA_UA ) ) { 
  71. $ua = MONSTERINSIGHTS_GA_UA; 
  72.  
  73. $ua_code = monsterinsights_is_valid_ua( monsterinsights_get_option( 'analytics_profile_code', '' ) ); 
  74.  
  75. if ( $ua_code ) { 
  76. $ua = $ua_code; 
  77.  
  78. $manual_ua_code = monsterinsights_is_valid_ua( monsterinsights_get_option( 'manual_ua_code', '' ) ); 
  79.  
  80. if ( $manual_ua_code ) { 
  81. $ua = $manual_ua_code; 
  82.  
  83. $ua = apply_filters( 'monsterinsights_get_ua', $ua ); 
  84.  
  85. return monsterinsights_is_valid_ua( $ua ); 
  86.  
  87. /** 
  88. * Helper method for getting the UA string that's output on the frontend. 
  89. * 
  90. * @since 6.0.0 
  91. * @access public 
  92. * 
  93. * @param array $args Allow calling functions to give args to use in future applications. 
  94. * @return string The UA to use on frontend. 
  95. */ 
  96. function monsterinsights_get_ua_to_output( $args = array() ) { 
  97. $ua = monsterinsights_get_ua(); 
  98. $ua = apply_filters( 'monsterinsights_get_ua_to_output', $ua, $args ); 
  99. return monsterinsights_is_valid_ua( $ua ); 
  100.  
  101. /** 
  102. * Helper method for updating a setting's value. 
  103. * 
  104. * @since 6.0.0 
  105. * @access public 
  106. * 
  107. * @param string $key The setting key. 
  108. * @param string $value The value to set for the key. 
  109. * @return boolean True if updated, false if not. 
  110. */ 
  111. function monsterinsights_update_option( $key = '', $value = false ) { 
  112.  
  113. // If no key, exit 
  114. if ( empty( $key ) ) { 
  115. return false; 
  116.  
  117. if ( empty( $value ) ) { 
  118. $remove_option = monsterinsights_delete_option( $key ); 
  119. return $remove_option; 
  120.  
  121. $option_name = monsterinsights_get_option_name(); 
  122.  
  123. // First let's grab the current settings 
  124.  
  125. // if on network panel or if on single site using network settings 
  126. //$settings = get_site_option( $option_name ); 
  127. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; 
  128. //$is_network = is_multisite(); 
  129. //$update_network_option = true; 
  130. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { 
  131. $settings = get_option( $option_name ); 
  132. // $update_network_option = false; 
  133. //} 
  134.  
  135. // Let's let devs alter that value coming in 
  136. $value = apply_filters( 'monsterinsights_update_option', $value, $key ); 
  137.  
  138. // Next let's try to update the value 
  139. $settings[ $key ] = $value; 
  140. $did_update = false; 
  141. //if ( $update_network_option ) { 
  142. // $did_update = update_site_option( $option_name, $settings ); 
  143. //} else { 
  144. $did_update = update_option( $option_name, $settings ); 
  145. //} 
  146.  
  147. // If it updated, let's update the global variable 
  148. if ( $did_update ) { 
  149. global $monsterinsights_settings; 
  150. $monsterinsights_settings[ $key ] = $value; 
  151.  
  152. return $did_update; 
  153.  
  154. /** 
  155. * Helper method for deleting a setting's value. 
  156. * 
  157. * @since 6.0.0 
  158. * @access public 
  159. * 
  160. * @param string $key The setting key. 
  161. * @return boolean True if removed, false if not. 
  162. */ 
  163. function monsterinsights_delete_option( $key = '' ) { 
  164. // If no key, exit 
  165. if ( empty( $key ) ) { 
  166. return false; 
  167.  
  168. $option_name = monsterinsights_get_option_name(); 
  169.  
  170. // First let's grab the current settings 
  171.  
  172. // if on network panel or if on single site using network settings 
  173. //$settings = get_site_option( $option_name ); 
  174. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; 
  175. //$is_network = is_multisite(); 
  176. //$update_network_option = true; 
  177. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { 
  178. $settings = get_option( $option_name ); 
  179. // $update_network_option = false; 
  180. //} 
  181.  
  182. // Next let's try to remove the key 
  183. if( isset( $settings[ $key ] ) ) { 
  184. unset( $settings[ $key ] ); 
  185.  
  186. $did_update = false; 
  187. //if ( $update_network_option ) { 
  188. // $did_update = update_site_option( 'monsterinsights_settings', $settings ); 
  189. //} else { 
  190. $did_update = update_option( $option_name, $settings ); 
  191. //} 
  192.  
  193. // If it updated, let's update the global variable 
  194. if ( $did_update ) { 
  195. global $monsterinsights_settings; 
  196. $monsterinsights_settings = $settings; 
  197.  
  198. return $did_update; 
  199.  
  200. /** 
  201. * Helper method for deleting multiple settings value. 
  202. * 
  203. * @since 6.0.0 
  204. * @access public 
  205. * 
  206. * @param string $key The setting key. 
  207. * @return boolean True if removed, false if not. 
  208. */ 
  209. function monsterinsights_delete_options( $keys = array() ) { 
  210. // If no keys, exit 
  211. if ( empty( $keys ) || ! is_array( $keys ) ) { 
  212. return false; 
  213.  
  214. $option_name = monsterinsights_get_option_name(); 
  215.  
  216. // First let's grab the current settings 
  217.  
  218. // if on network panel or if on single site using network settings 
  219. //$settings = get_site_option( $option_name ); 
  220. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false; 
  221. //$is_network = is_multisite(); 
  222. //$update_network_option = true; 
  223. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) { 
  224. $settings = get_option( $option_name ); 
  225. // $update_network_option = false; 
  226. //} 
  227.  
  228. // Next let's try to remove the key
  229. foreach ( $keys as $key ) { 
  230. if( isset( $settings[ $key ] ) ) { 
  231. unset( $settings[ $key ] ); 
  232.  
  233. $did_update = false; 
  234. //if ( $update_network_option ) { 
  235. // $did_update = update_site_option( 'monsterinsights_settings', $settings ); 
  236. //} else { 
  237. $did_update = update_option( $option_name, $settings ); 
  238. //} 
  239.  
  240. // If it updated, let's update the global variable 
  241. if ( $did_update ) { 
  242. global $monsterinsights_settings; 
  243. $monsterinsights_settings = $settings; 
  244.  
  245. return $did_update; 
  246.  
  247. /** 
  248. * Helper method for getting the license information. 
  249. * 
  250. * @since 6.0.0 
  251. * @access public 
  252. * 
  253. * @param string $key The setting key to retrieve. 
  254. * @param mixed $default_value The default value of the setting key to retrieve. 
  255. * @return string The value of the setting. 
  256. */ 
  257. function monsterinsights_get_license() { 
  258. $license = false; 
  259. if ( defined( 'MONSTERINSIGHTS_LICENSE_KEY' ) && is_string( MONSTERINSIGHTS_LICENSE_KEY ) && strlen( MONSTERINSIGHTS_LICENSE_KEY ) > 10 ) { 
  260. $license = array( 'key' => MONSTERINSIGHTS_LICENSE_KEY ); 
  261. } else if ( is_multisite() && monsterinsights_is_network_active() ) { 
  262. $network_license = get_site_option( 'monsterinsights_license' ); 
  263. if ( ! empty( $network_license['key'] ) && is_string( $network_license['key'] ) && strlen( $network_license['key'] ) > 10 ) { 
  264. $license = $network_license; 
  265. } else { 
  266. $site_license = get_option( 'monsterinsights_license' ); 
  267. if ( ! empty( $site_license['key'] ) && is_string( $site_license['key'] ) && strlen( $site_license['key'] ) > 10 ) { 
  268. $license = $site_license; 
  269. } else { 
  270. $site_license = get_option( 'monsterinsights_license' ); 
  271. if ( ! empty( $site_license['key'] ) && is_string( $site_license['key'] ) && strlen( $site_license['key'] ) > 10 ) { 
  272. $license = $site_license; 
  273.  
  274. return apply_filters( 'monsterinsights_license', $license ); 
  275.  
  276. /** 
  277. * Helper method for getting the license key. 
  278. * 
  279. * @since 6.0.0 
  280. * @access public 
  281. * 
  282. * @param string $key The setting key to retrieve. 
  283. * @param mixed $default_value The default value of the setting key to retrieve. 
  284. * @return string The value of the setting. 
  285. */ 
  286. function monsterinsights_get_license_key() { 
  287. $license = false; 
  288. if ( defined( 'MONSTERINSIGHTS_LICENSE_KEY' ) && is_string( MONSTERINSIGHTS_LICENSE_KEY ) && strlen( MONSTERINSIGHTS_LICENSE_KEY ) > 10 ) { 
  289. $license = MONSTERINSIGHTS_LICENSE_KEY; 
  290. } else { 
  291. $license = monsterinsights_get_license(); 
  292. if ( ! empty( $license['key'] ) && is_string( $license['key'] ) && strlen( $license['key'] ) > 10 ) { 
  293. $license = $license['key']; 
  294. } else { 
  295. $license = false; 
  296.  
  297. return apply_filters( 'monsterinsights_license_key', $license ); 
  298.  
  299. /** 
  300. * Helper method for updating the license key. 
  301. * 
  302. * @since 6.0.0 
  303. * @access public 
  304. * 
  305. * @param string $key The setting key. 
  306. * @param string $value The value to set for the key. 
  307. * @return boolean True if updated, false if not. 
  308. */ 
  309. function monsterinsights_update_license_key( $license_key = false, $network = false, $override = false ) { 
  310. if ( ! $license_key || ! is_string( $license_key ) || ! strlen( $license_key ) > 10 ) { 
  311. return false; 
  312.  
  313. if ( $network && is_multisite() && ( is_network_admin() || $override ) ) { 
  314. update_site_option( 'monsterinsights_license', $license_key ); 
  315. return true; 
  316. } else { 
  317. update_option( 'monsterinsights_license', $license_key ); 
  318. return true; 
  319.  
  320. /** 
  321. * Helper method for deleting the license key. 
  322. * 
  323. * @since 6.0.0 
  324. * @access public 
  325. * 
  326. * @param string $key The setting key. 
  327. * @return boolean True if removed, false if not. 
  328. */ 
  329. function monsterinsights_delete_license_key( $network = false, $override = false ) { 
  330. if ( $network && is_multisite() && ( is_network_admin() || $override ) ) { 
  331. delete_site_option( 'monsterinsights_license' ); 
  332. return true; 
  333. } else { 
  334. delete_option( 'monsterinsights_license' ); 
  335. return true; 
  336.  
  337.  
  338. /** 
  339. * Returns the license key type for MonsterInsights. 
  340. * 
  341. * @access public 
  342. * @since 6.0.0 
  343. * 
  344. * @return string $type The user's license key type for MonsterInsights. 
  345. */ 
  346. function monsterinsights_get_license_key_type() { 
  347. $type = false; 
  348. $license = monsterinsights_get_license(); 
  349. if ( ! empty( $license['type'] ) && is_string( $license['type'] ) ) { 
  350. if ( in_array( $license['type'], array( 'master', 'pro', 'plus', 'basic' ) ) ) { 
  351. $type = $license['type']; 
  352. return $type; 
  353.  
  354. /** 
  355. * Returns possible license key error flag. 
  356. * 
  357. * @access public 
  358. * @since 6.0.0 
  359. * 
  360. * @return bool True if there are license key errors, false otherwise. 
  361. */ 
  362. function monsterinsights_get_license_key_errors() { 
  363. $errors = false; 
  364. $license = monsterinsights_get_license(); 
  365. if ( ! empty( $license['type'] ) && is_string( $license['type'] ) && strlen( $license['type'] ) > 3 ) { 
  366. if ( ( isset( $license['is_expired'] ) && $license['is_expired'] )  
  367. || ( isset( $license['is_disabled'] ) && $license['is_disabled'] ) 
  368. || ( isset( $license['is_invalid'] ) && $license['is_invalid'] ) ) { 
  369. $errors = true; 
  370. return $errors; 
  371.  
  372. /** 
  373. * Is valid ua code. 
  374. * 
  375. * @access public 
  376. * @since 6.0.0 
  377. * 
  378. * @param string $ua_code UA code to check validity for. 
  379. * 
  380. * @return string|false Return cleaned ua string if valid, else returns false. 
  381. */ 
  382. function monsterinsights_is_valid_ua( $ua_code = '' ) { 
  383. $ua_code = (string) $ua_code; // Rare case, but let's make sure it never happens. 
  384. $ua_code = trim( $ua_code ); 
  385.  
  386. if ( empty( $ua_code ) ) { 
  387. return ''; 
  388.  
  389. // Replace all type of dashes (n-dash, m-dash, minus) with normal dashes. 
  390. $ua_code = str_replace( array( '*', '*', '*' ), '-', $ua_code ); 
  391.  
  392. if ( preg_match( "/^(UA|YT|MO)-\d{4, }-\d+$/", strval( $ua_code ) ) ) { 
  393. return $ua_code; 
  394. } else { 
  395. return ''; 
  396.  
  397. function monsterinsights_get_option_name() { 
  398. //if ( monsterinsights_is_pro_version() ) { 
  399. return 'monsterinsights_settings'; 
  400. //} else { 
  401. // return 'monsterinsights_settings'; 
  402. //} 
  403.  
  404. function monsterinsights_export_settings() { 
  405. $settings = monsterinsights_get_options(); 
  406. $exclude = array(  
  407. 'analytics_profile',  
  408. 'analytics_profile_code',  
  409. 'analytics_profile_name',  
  410. 'oauth_version',  
  411. 'cron_last_run',  
  412. 'monsterinsights_oauth_status',  
  413. ); 
  414.  
  415. foreach ( $exclude as $e ) { 
  416. if ( ! empty( $settings[ $e ] ) ) { 
  417. unset( $settings[ $e ] ); 
  418. return wp_json_encode( $settings ); 
.