/includes/admin/pages/settings.php

  1. <?php 
  2. /** 
  3. * Settings class. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Settings 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. function monsterinsights_is_settings_page() { 
  17. $current_screen = get_current_screen(); 
  18. global $admin_page_hooks; 
  19.  
  20. if ( ! is_object( $current_screen ) || empty( $current_screen->id ) || empty( $admin_page_hooks ) ) { 
  21. return false; 
  22.  
  23. $settings_page = false; 
  24. if ( ! empty( $admin_page_hooks['monsterinsights_dashboard'] ) && $current_screen->id === $admin_page_hooks['monsterinsights_dashboard'] . '_page_monsterinsights_settings' ) { 
  25. $settings_page = true; 
  26.  
  27. if ( $current_screen->id === 'toplevel_page_monsterinsights_settings' ) { 
  28. $settings_page = true; 
  29.  
  30. return $settings_page; 
  31.  
  32.  
  33. /** 
  34. * Callback to output the MonsterInsights settings page. 
  35. * 
  36. * @since 6.0.0 
  37. * @access public 
  38. * 
  39. * @return void 
  40. */ 
  41. function monsterinsights_settings_page() { 
  42. /**  
  43. * Developer Alert: 
  44. * 
  45. * Per the README, this is considered an internal hook and should 
  46. * not be used by other developers. This hook's behavior may be modified 
  47. * or the hook may be removed at any time, without warning. 
  48. */ 
  49. do_action( 'monsterinsights_head' ); 
  50. ?> 
  51. <?php echo monsterinsights_ublock_notice(); ?> 
  52.  
  53. <!-- Tabs --> 
  54. <h1 id="monsterinsights-settings-page-main-nav" class="monsterinsights-main-nav-container monsterinsights-nav-container" data-container="#monsterinsights-settings-pages"> 
  55. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-spacing-item" href="#"> </a> 
  56.  
  57. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-active" href="#monsterinsights-main-tab-general" title="<?php echo esc_attr( __( 'General', 'google-analytics-for-wordpress' ) ); ?>"> 
  58. <?php echo esc_html__( 'General', 'google-analytics-for-wordpress' ); ?> 
  59. </a> 
  60.  
  61. <a class="monsterinsights-main-nav-item monsterinsights-nav-item" href="#monsterinsights-main-tab-tracking" title="<?php echo esc_attr( __( 'Tracking', 'google-analytics-for-wordpress' ) ); ?>"> 
  62. <?php echo esc_html__( 'Tracking', 'google-analytics-for-wordpress' ); ?> 
  63. </a> 
  64. <!-- 
  65. <a class="monsterinsights-main-nav-item monsterinsights-nav-item" href="#monsterinsights-main-tab-status" title="<?php //echo esc_attr( __( 'Status', 'google-analytics-for-wordpress' ) ); ?>"> 
  66. <?php //echo esc_html__( 'Status', 'google-analytics-for-wordpress' ); ?> 
  67. </a> 
  68.  
  69. <a class="monsterinsights-main-nav-item monsterinsights-nav-item" href="#monsterinsights-main-tab-support" title="<?php //echo esc_attr( __( 'Support', 'google-analytics-for-wordpress' ) ); ?>"> 
  70. <?php //echo esc_html__( 'Support', 'google-analytics-for-wordpress' ); ?> 
  71. </a> 
  72. --> 
  73. </h1> 
  74.  
  75.  
  76. <!-- Tab Panels --> 
  77. <div id="monsterinsights-settings-pages" class="monsterinsights-main-nav-tabs monsterinsights-nav-tabs wrap" data-navigation="#monsterinsights-settings-page-main-nav"> 
  78. <h1 class="monsterinsights-hideme"></h1><!-- so wp notices are below the nav bar --> 
  79. <div id="monsterinsights-main-tab-general" class="monsterinsights-main-nav-tab monsterinsights-nav-tab monsterinsights-active"> 
  80. <?php monsterinsights_settings_general_tab(); ?> 
  81. </div> 
  82. <div id="monsterinsights-main-tab-tracking" class="monsterinsights-main-nav-tab monsterinsights-nav-tab"> 
  83. <?php monsterinsights_settings_tracking_tab(); ?> 
  84. </div> 
  85. <!-- 
  86. <div id="monsterinsights-main-tab-status" class="monsterinsights-main-nav-tab monsterinsights-nav-tab"> 
  87. <?php //monsterinsights_settings_status_tab(); ?> 
  88. </div> 
  89. <div id="monsterinsights-main-tab-support" class="monsterinsights-main-nav-tab monsterinsights-nav-tab"> 
  90. <?php //monsterinsights_settings_support_tab(); ?> 
  91. </div> 
  92. --> 
  93. </div> 
  94. <?php 
  95.  
  96. /** 
  97. * Saves Settings 
  98. * 
  99. * @since 6.0.0 
  100. * @access public 
  101. * 
  102. * @return null Return early if not fixing the broken migration 
  103. */ 
  104. function monsterinsights_save_general_settings_page() { 
  105.  
  106. if ( ! monsterinsights_is_settings_page() ) { 
  107. return; 
  108.  
  109. // Check if user pressed the 'Update' button and nonce is valid 
  110. if ( ! isset( $_POST['monsterinsights-settings-submit'] ) ) { 
  111. return; 
  112.  
  113. if ( ! wp_verify_nonce( $_POST['monsterinsights-settings-nonce'], 'monsterinsights-settings-nonce' ) ) { 
  114. return; 
  115.  
  116. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  117. return; 
  118.  
  119. if ( ! empty( $_POST['monsterinsights_settings_tab'] ) && $_POST['monsterinsights_settings_tab'] === 'general' ) { 
  120. /**  
  121. * Developer Alert: 
  122. * 
  123. * Per the README, this is considered an internal hook and should 
  124. * not be used by other developers. This hook's behavior may be modified 
  125. * or the hook may be removed at any time, without warning. 
  126. */ 
  127. do_action( 'monsterinsights_settings_save_general' ); 
  128. add_action( 'current_screen', 'monsterinsights_save_general_settings_page' ); 
  129.  
  130. /** 
  131. * Outputs a WordPress style notification to tell the user settings were saved 
  132. * 
  133. * @since 6.0.0 
  134. * @access public 
  135. * 
  136. * @return void 
  137. */ 
  138. function monsterinsights_updated_settings() { 
  139. echo monsterinsights_get_message( 'success', esc_html__( 'Settings saved successfully.', 'google-analytics-for-wordpress' ) );  
  140.  
  141. /** 
  142. * Outputs a WordPress style notification to tell the user their UA code was bad. 
  143. * 
  144. * @since 6.0.3 
  145. * @access public 
  146. * 
  147. * @return void 
  148. */ 
  149. function monsterinsights_invalid_ua_code() { 
  150. echo monsterinsights_get_message( 'error', esc_html__( 'Invalid UA code.', 'google-analytics-for-wordpress' ) );  
  151.  
  152. /** 
  153. * Outputs a checkbox for settings.  
  154. * 
  155. * Do not use this in other plugins. We may remove this at any time 
  156. * without forwarning and without consideration for backwards compatibility. 
  157. * 
  158. * This is to be considered a private function, for MonsterInsights use only. 
  159. * 
  160. * @since 6.0.0 
  161. * @access public 
  162. * 
  163. * @return void 
  164. */ 
  165. function monsterinsights_make_checkbox( $option_id, $title = '', $description = '' ) { 
  166. $option_value = monsterinsights_get_option( $option_id, 0 ); 
  167. $option_class = str_replace( '_', '-', $option_id ); 
  168. ob_start(); 
  169. ?> 
  170. <tr id="monsterinsights-input-<?php echo esc_attr( $option_class ); ?>"> 
  171. <?php if ( !empty ( $title ) ) { ?> 
  172. <th scope="row"> 
  173. <label for="monsterinsights-<?php echo esc_attr( $option_class ); ?>"><?php echo $title; ?></label> 
  174. </th> 
  175. <?php } ?> 
  176. <td> 
  177. <input type="checkbox" id="monsterinsights-<?php echo esc_attr( $option_class ); ?>" name="<?php echo esc_attr( $option_id ); ?>" <?php checked( $option_value, 1 ); ?> /> 
  178. <?php if ( ! empty ( $description ) ) { ?> 
  179. <p class="description"> 
  180. <?php echo $description; ?> 
  181. </p> 
  182. <?php } ?> 
  183. </td> 
  184. </tr> 
  185. <?php 
  186. $input_field = ob_get_contents(); 
  187. ob_end_clean(); 
  188. return $input_field; 
  189.  
  190. function monsterinsights_switch_to_analyticsjs() { 
  191. $nonce = ''; 
  192. if ( ! empty( $_REQUEST['monsterinsights-switch-to-analyticsjs-nonce'] ) ) { 
  193. $nonce = 'monsterinsights-switch-to-analyticsjs-nonce'; 
  194. } else if ( ! empty( $_REQUEST['_wpnonce'] ) ) { 
  195. $nonce = '_wpnonce'; 
  196.  
  197. if ( empty( $nonce ) ) { 
  198. return; 
  199.  
  200. if ( ! wp_verify_nonce( $_REQUEST[$nonce], 'monsterinsights-switch-to-analyticsjs-nonce' ) ) { 
  201. return; 
  202.  
  203. if ( empty( $_REQUEST['monsterinsights-action'] ) || $_REQUEST['monsterinsights-action'] !== 'switch_to_analyticsjs' ) { 
  204. return; 
  205.  
  206. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  207. wp_die( esc_html__( 'You do not have permission to manage MonsterInsights settings', 'google-analytics-for-wordpress' ), esc_html__( 'Error', 'google-analytics-for-wordpress' ), array( 'response' => 403 ) ); 
  208.  
  209. $return = ''; 
  210. $allowed_tabs = array( 'engagement', 'performance', 'ecommerce', 'demographics', 'dimensions' ); 
  211. if ( ! empty( $_REQUEST['return'] ) && in_array( $_REQUEST['return'], $allowed_tabs ) ) { 
  212. $return = admin_url( 'admin.php?page=monsterinsights_settings&monsterinsights-message=tracking_mode_switched#monsterinsights-main-tab-tracking?monsterinsights-sub-tab-') . $_REQUEST['return']; 
  213. $return = add_query_arg( 'return', $_REQUEST['return'], $return ); 
  214. } else { 
  215. $return = admin_url( 'admin.php?page=monsterinsights_settings&monsterinsights-message=tracking_mode_switched'); 
  216. monsterinsights_update_option( 'tracking_mode', 'analytics' ); 
  217. wp_safe_redirect( $return );exit; 
  218. add_action( 'admin_init', 'monsterinsights_switch_to_analyticsjs', 9 ); 
  219.  
  220. function monsterinsights_switched_to_analyticsjs() { 
  221. echo monsterinsights_get_message( 'success', esc_html__( 'Successfully migrated to Universal Analytics (analytics.js)!', 'google-analytics-for-wordpress' ) ); 
  222.  
  223. function monsterinsights_switch_to_analyticsjs_show_notice() { 
  224. if ( empty( $_REQUEST['monsterinsights-message'] ) || $_REQUEST['monsterinsights-message'] !== 'tracking_mode_switched' ) { 
  225. return; 
  226.  
  227. $allowed_tabs = array( 'engagement', 'performance', 'ecommerce', 'demographics', 'dimensions' ); 
  228. if ( ! empty( $_REQUEST['return'] ) && in_array( $_REQUEST['return'], $allowed_tabs ) ) { 
  229. add_action( 'monsterinsights_tracking_' . $_REQUEST['return'] . '_tab_notice', 'monsterinsights_switched_to_analyticsjs' ); 
  230. } else { 
  231. add_action( 'monsterinsights_settings_general_tab_notice', 'monsterinsights_switched_to_analyticsjs' ); 
  232. add_action( 'admin_init', 'monsterinsights_switch_to_analyticsjs_show_notice', 11 );  
  233.  
  234.  
  235.  
  236. function monsterinsights_switch_to_jsevents() { 
  237. $nonce = ''; 
  238. if ( ! empty( $_REQUEST['monsterinsights-switch-to-jsevents-nonce'] ) ) { 
  239. $nonce = 'monsterinsights-switch-to-jsevents-nonce'; 
  240. } else if ( ! empty( $_REQUEST['_wpnonce'] ) ) { 
  241. $nonce = '_wpnonce'; 
  242.  
  243. if ( empty( $nonce ) ) { 
  244. return; 
  245.  
  246. if ( ! wp_verify_nonce( $_REQUEST[$nonce], 'monsterinsights-switch-to-jsevents-nonce' ) ) { 
  247. return; 
  248.  
  249. if ( empty( $_REQUEST['monsterinsights-action'] ) || $_REQUEST['monsterinsights-action'] !== 'switch_to_jsevents' ) { 
  250. return; 
  251.  
  252. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  253. wp_die( esc_html__( 'You do not have permission to manage MonsterInsights settings', 'google-analytics-for-wordpress' ), esc_html__( 'Error', 'google-analytics-for-wordpress' ), array( 'response' => 403 ) ); 
  254.  
  255. $return = ''; 
  256. $allowed_tabs = array( 'engagement', 'performance', 'ecommerce', 'demographics', 'dimensions' ); 
  257. if ( ! empty( $_REQUEST['return'] ) && in_array( $_REQUEST['return'], $allowed_tabs ) ) { 
  258. $return = admin_url( 'admin.php?page=monsterinsights_settings&monsterinsights-message=jsvents_mode_switched#monsterinsights-main-tab-tracking?monsterinsights-sub-tab-') . $_REQUEST['return']; 
  259. $return = add_query_arg( 'return', $_REQUEST['return'], $return ); 
  260. } else { 
  261. $return = admin_url( 'admin.php?page=monsterinsights_settings&monsterinsights-message=jsvents_mode_switched'); 
  262. monsterinsights_update_option( 'events_mode', 'js' ); 
  263. wp_safe_redirect( $return );exit; 
  264. add_action( 'admin_init', 'monsterinsights_switch_to_jsevents', 9 ); 
  265.  
  266. function monsterinsights_switched_to_jsevents() { 
  267. echo monsterinsights_get_message( 'success', esc_html__( 'Successfully migrated to JS events tracking!', 'google-analytics-for-wordpress' ) ); 
  268.  
  269. function monsterinsights_switch_to_jsevents_show_notice() { 
  270. if ( empty( $_REQUEST['monsterinsights-message'] ) || $_REQUEST['monsterinsights-message'] !== 'jsvents_mode_switched' ) { 
  271. return; 
  272.  
  273. $allowed_tabs = array( 'engagement', 'performance', 'ecommerce', 'demographics', 'dimensions' ); 
  274. if ( ! empty( $_REQUEST['return'] ) && in_array( $_REQUEST['return'], $allowed_tabs ) ) { 
  275. add_action( 'monsterinsights_tracking_' . $_REQUEST['return'] . '_tab_notice', 'monsterinsights_switched_to_jsevents' ); 
  276. } else { 
  277. add_action( 'monsterinsights_settings_general_tab_notice', 'monsterinsights_switched_to_jsevents' ); 
  278. add_action( 'admin_init', 'monsterinsights_switch_to_jsevents_show_notice', 11 ); 
.