options.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. function monsterinsights_get_options() {
  16. $settings = array();
  17. $option_name = monsterinsights_get_option_name();
  18. //$settings = get_site_option( $option_name );
  19. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
  20. //$is_network = is_multisite();
  21. //if ( $is_network && $use_network_settings ) {
  22. // return $settings;
  23. //} else if ( $is_network ) {
  24. $settings = get_option( $option_name );
  25. //} else {
  26. // return $settings;
  27. //}
  28. if ( empty( $settings ) || ! is_array( $settings ) ) {
  29. $settings = array();
  30. }
  31. return $settings;
  32. }
  33. /**
  34. * Helper method for getting a setting's value. Falls back to the default
  35. * setting value if none exists in the options table.
  36. *
  37. * @since 6.0.0
  38. * @access public
  39. *
  40. * @param string $key The setting key to retrieve.
  41. * @param mixed $default The default value of the setting key to retrieve.
  42. * @return string The value of the setting.
  43. */
  44. function monsterinsights_get_option( $key = '', $default = false ) {
  45. global $monsterinsights_settings;
  46. $value = ! empty( $monsterinsights_settings[ $key ] ) ? $monsterinsights_settings[ $key ] : $default;
  47. $value = apply_filters( 'monsterinsights_get_option', $value, $key, $default );
  48. return apply_filters( 'monsterinsights_get_option_' . $key, $value, $key, $default );
  49. }
  50. /**
  51. * Helper method for getting the UA string.
  52. *
  53. * @since 6.0.0
  54. * @access public
  55. *
  56. * @return string The UA to use.
  57. */
  58. function monsterinsights_get_ua() {
  59. // Try getting it from the auth UA
  60. $ua = MonsterInsights()->auth->get_ua();
  61. // If that didn't work, try the manual UA at the site level
  62. if ( empty( $ua ) ) {
  63. $ua = MonsterInsights()->auth->get_manual_ua();
  64. // If that didn't work try getting it from the network
  65. if ( empty( $ua ) ) {
  66. $ua = monsterinsights_get_network_ua();
  67. // If that didn't work, try getting it from the overall constant. If it's not there, leave it blank
  68. if ( empty( $ua ) ) {
  69. $ua = defined( 'MONSTERINSIGHTS_GA_UA' ) && MONSTERINSIGHTS_GA_UA ? monsterinsights_is_valid_ua( MONSTERINSIGHTS_GA_UA ) : '';
  70. }
  71. }
  72. }
  73. // Feed through the filter
  74. $pre_filter = $ua;
  75. $ua = apply_filters( 'monsterinsights_get_ua', $ua );
  76. // Only run through monsterinsights_is_valid_ua if it's different than pre-filter
  77. return $pre_filter === $ua ? $ua : monsterinsights_is_valid_ua( $ua );
  78. }
  79. /**
  80. * Helper method for getting the network UA string.
  81. *
  82. * @since 6.0.0
  83. * @access public
  84. *
  85. * @return string The UA to use.
  86. */
  87. function monsterinsights_get_network_ua() {
  88. if ( ! is_multisite() ) {
  89. return '';
  90. }
  91. // First try network auth UA
  92. $ua = MonsterInsights()->auth->get_network_ua();
  93. if ( ! empty( $ua ) ) {
  94. return $ua;
  95. }
  96. // Then try manual network UA
  97. $ua = MonsterInsights()->auth->get_network_manual_ua();
  98. if ( ! empty( $ua ) ) {
  99. return $ua;
  100. }
  101. // See if the constant is defined
  102. if ( defined( 'MONSTERINSIGHTS_MS_GA_UA' ) && monsterinsights_is_valid_ua( MONSTERINSIGHTS_MS_GA_UA ) ) {
  103. return MONSTERINSIGHTS_MS_GA_UA;
  104. }
  105. return '';
  106. }
  107. /**
  108. * Helper method for getting the UA string that's output on the frontend.
  109. *
  110. * @since 6.0.0
  111. * @access public
  112. *
  113. * @param array $args Allow calling functions to give args to use in future applications.
  114. * @return string The UA to use on frontend.
  115. */
  116. function monsterinsights_get_ua_to_output( $args = array() ) {
  117. $ua = monsterinsights_get_ua();
  118. $ua = apply_filters( 'monsterinsights_get_ua_to_output', $ua, $args );
  119. return monsterinsights_is_valid_ua( $ua );
  120. }
  121. /**
  122. * Helper method for updating a setting's value.
  123. *
  124. * @since 6.0.0
  125. * @access public
  126. *
  127. * @param string $key The setting key.
  128. * @param string $value The value to set for the key.
  129. * @return boolean True if updated, false if not.
  130. */
  131. function monsterinsights_update_option( $key = '', $value = false ) {
  132. // If no key, exit
  133. if ( empty( $key ) ){
  134. return false;
  135. }
  136. if ( empty( $value ) ) {
  137. $remove_option = monsterinsights_delete_option( $key );
  138. return $remove_option;
  139. }
  140. $option_name = monsterinsights_get_option_name();
  141. // First let's grab the current settings
  142. // if on network panel or if on single site using network settings
  143. //$settings = get_site_option( $option_name );
  144. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
  145. //$is_network = is_multisite();
  146. //$update_network_option = true;
  147. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
  148. $settings = get_option( $option_name );
  149. // $update_network_option = false;
  150. //}
  151. if ( ! is_array( $settings ) ) {
  152. $settings = array();
  153. }
  154. // Let's let devs alter that value coming in
  155. $value = apply_filters( 'monsterinsights_update_option', $value, $key );
  156. // Next let's try to update the value
  157. $settings[ $key ] = $value;
  158. $did_update = false;
  159. //if ( $update_network_option ) {
  160. // $did_update = update_site_option( $option_name, $settings );
  161. //} else {
  162. $did_update = update_option( $option_name, $settings );
  163. //}
  164. // If it updated, let's update the global variable
  165. if ( $did_update ){
  166. global $monsterinsights_settings;
  167. $monsterinsights_settings[ $key ] = $value;
  168. }
  169. return $did_update;
  170. }
  171. /**
  172. * Helper method for deleting a setting's value.
  173. *
  174. * @since 6.0.0
  175. * @access public
  176. *
  177. * @param string $key The setting key.
  178. * @return boolean True if removed, false if not.
  179. */
  180. function monsterinsights_delete_option( $key = '' ) {
  181. // If no key, exit
  182. if ( empty( $key ) ){
  183. return false;
  184. }
  185. $option_name = monsterinsights_get_option_name();
  186. // First let's grab the current settings
  187. // if on network panel or if on single site using network settings
  188. //$settings = get_site_option( $option_name );
  189. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
  190. //$is_network = is_multisite();
  191. //$update_network_option = true;
  192. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
  193. $settings = get_option( $option_name );
  194. // $update_network_option = false;
  195. //}
  196. // Next let's try to remove the key
  197. if( isset( $settings[ $key ] ) ) {
  198. unset( $settings[ $key ] );
  199. }
  200. $did_update = false;
  201. //if ( $update_network_option ) {
  202. // $did_update = update_site_option( 'monsterinsights_settings', $settings );
  203. //} else {
  204. $did_update = update_option( $option_name, $settings );
  205. //}
  206. // If it updated, let's update the global variable
  207. if ( $did_update ){
  208. global $monsterinsights_settings;
  209. $monsterinsights_settings = $settings;
  210. }
  211. return $did_update;
  212. }
  213. /**
  214. * Helper method for deleting multiple settings value.
  215. *
  216. * @since 6.0.0
  217. * @access public
  218. *
  219. * @param string $key The setting key.
  220. * @return boolean True if removed, false if not.
  221. */
  222. function monsterinsights_delete_options( $keys = array() ) {
  223. // If no keys, exit
  224. if ( empty( $keys ) || ! is_array( $keys ) ){
  225. return false;
  226. }
  227. $option_name = monsterinsights_get_option_name();
  228. // First let's grab the current settings
  229. // if on network panel or if on single site using network settings
  230. //$settings = get_site_option( $option_name );
  231. //$use_network_settings = ! empty( $use_network_settings['use_network_settings'] ) ? true : false;
  232. //$is_network = is_multisite();
  233. //$update_network_option = true;
  234. //if ( ! is_network_admin() && ! ( $is_network && $use_network_settings ) ) {
  235. $settings = get_option( $option_name );
  236. // $update_network_option = false;
  237. //}
  238. // Next let's try to remove the keys
  239. foreach ( $keys as $key ) {
  240. if( isset( $settings[ $key ] ) ) {
  241. unset( $settings[ $key ] );
  242. }
  243. }
  244. $did_update = false;
  245. //if ( $update_network_option ) {
  246. // $did_update = update_site_option( 'monsterinsights_settings', $settings );
  247. //} else {
  248. $did_update = update_option( $option_name, $settings );
  249. //}
  250. // If it updated, let's update the global variable
  251. if ( $did_update ){
  252. global $monsterinsights_settings;
  253. $monsterinsights_settings = $settings;
  254. }
  255. return $did_update;
  256. }
  257. /**
  258. * Is valid ua code.
  259. *
  260. * @access public
  261. * @since 6.0.0
  262. *
  263. * @param string $ua_code UA code to check validity for.
  264. *
  265. * @return string|false Return cleaned ua string if valid, else returns false.
  266. */
  267. function monsterinsights_is_valid_ua( $ua_code = '' ) {
  268. $ua_code = (string) $ua_code; // Rare case, but let's make sure it never happens.
  269. $ua_code = trim( $ua_code );
  270. if ( empty( $ua_code ) ) {
  271. return '';
  272. }
  273. // Replace all type of dashes (n-dash, m-dash, minus) with normal dashes.
  274. $ua_code = str_replace( array( '–', '—', '−' ), '-', $ua_code );
  275. if ( preg_match( "/^(UA|YT|MO)-\d{4,}-\d+$/", strval( $ua_code ) ) ) {
  276. return $ua_code;
  277. } else {
  278. return '';
  279. }
  280. }
  281. /**
  282. * Helper method for getting the license information.
  283. *
  284. * @since 6.0.0
  285. * @access public
  286. *
  287. * @param string $key The setting key to retrieve.
  288. * @param mixed $default_value The default value of the setting key to retrieve.
  289. * @return string The value of the setting.
  290. */
  291. function monsterinsights_get_license() {
  292. $license = MonsterInsights()->license->get_site_license();
  293. $license = $license ? $license : MonsterInsights()->license->get_network_license();
  294. $default = MonsterInsights()->license->get_default_license_key();
  295. if ( empty( $license ) && ! empty( $default ) ) {
  296. $license = array();
  297. $license['key'] = MonsterInsights()->license->get_default_license_key();
  298. }
  299. return $license;
  300. }
  301. /**
  302. * Helper method for getting the license key.
  303. *
  304. * @since 6.0.0
  305. * @access public
  306. *
  307. * @param string $key The setting key to retrieve.
  308. * @param mixed $default_value The default value of the setting key to retrieve.
  309. * @return string The value of the setting.
  310. */
  311. function monsterinsights_get_license_key() {
  312. return MonsterInsights()->license->get_license_key();
  313. }
  314. function monsterinsights_get_option_name() {
  315. //if ( monsterinsights_is_pro_version() ) {
  316. return 'monsterinsights_settings';
  317. //} else {
  318. // return 'monsterinsights_settings';
  319. //}
  320. }
  321. function monsterinsights_export_settings() {
  322. $settings = monsterinsights_get_options();
  323. $exclude = array(
  324. 'analytics_profile',
  325. 'analytics_profile_code',
  326. 'analytics_profile_name',
  327. 'oauth_version',
  328. 'cron_last_run',
  329. 'monsterinsights_oauth_status',
  330. );
  331. foreach ( $exclude as $e ) {
  332. if ( ! empty( $settings[ $e ] ) ) {
  333. unset( $settings[ $e ] );
  334. }
  335. }
  336. return wp_json_encode( $settings );
  337. }