tab-general.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * General Settings Tab.
  4. *
  5. * @since 6.0.0
  6. *
  7. * @package MonsterInsights
  8. * @subpackage Settings
  9. * @author Chris Christoff
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * Callback for displaying the UI for general settings tab.
  17. *
  18. * @since 6.0.0
  19. * @access public
  20. *
  21. * @return void
  22. */
  23. function monsterinsights_settings_general_tab() {
  24. $automatic_updates = monsterinsights_get_option( 'automatic_updates', false );
  25. $anon_tracking = monsterinsights_get_option( 'anonymous_data', false );
  26. ?>
  27. <div id="monsterinsights-settings-general">
  28. <div class="monsterinsights-tab-settings-notices">
  29. <?php
  30. // Output any notices now
  31. /**
  32. * Developer Alert:
  33. *
  34. * Per the README, this is considered an internal hook and should
  35. * not be used by other developers. This hook's behavior may be modified
  36. * or the hook may be removed at any time, without warning.
  37. */
  38. do_action( 'monsterinsights_settings_general_tab_notice' );
  39. ?>
  40. </div>
  41. <table class="form-table">
  42. <tbody>
  43. <?php if ( monsterinsights_is_pro_version() ) {
  44. $license_key = MonsterInsights()->license->get_site_license_key();
  45. $license_key = $license_key ? $license_key : MonsterInsights()->license->get_network_license_key();
  46. $license_key = $license_key ? $license_key : MonsterInsights()->license->get_default_license_key();
  47. $license_type = MonsterInsights()->license->get_site_license_type();
  48. ?>
  49. <tr id="monsterinsights-settings-key-box">
  50. <th scope="row">
  51. <label for="monsterinsights-settings-key"><?php esc_html_e( 'License Key', 'google-analytics-for-wordpress' ); ?></label>
  52. </th>
  53. <td>
  54. <form id="monsterinsights-settings-verify-key" method="post">
  55. <input type="password" name="monsterinsights-license-key" id="monsterinsights-settings-key" value="<?php echo $license_key; ?>" />
  56. <?php wp_nonce_field( 'monsterinsights-key-nonce', 'monsterinsights-key-nonce' ); ?>
  57. <?php if ( MonsterInsights()->license->get_site_license_key() ) { ?>
  58. <?php submit_button( esc_html__( 'Verify Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-verify-submit', false ); ?>
  59. <?php submit_button( esc_html__( 'Deactivate Key', 'google-analytics-for-wordpress' ), 'button-danger', 'monsterinsights-deactivate-submit', false ); ?>
  60. <?php } else { ?>
  61. <?php submit_button( esc_html__( 'Activate Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-verify-submit', false ); ?>
  62. <?php } ?>
  63. <?php if ( MonsterInsights()->license->is_network_licensed() ) { ?>
  64. <p class="description"><?php esc_html_e( 'Your website is network licensed for MonsterInsights. Enter a license above only if you want to use a different Google Analytics profile for the reports and tracking on this subsite.', 'google-analytics-for-wordpress' ); ?></p>
  65. <?php } else { ?>
  66. <p class="description"><?php esc_html_e( 'The license key is used to enable updates for MonsterInsights Pro & addons, as well enable the ability to view reports. Deactivate your license if you want to use it on another WordPress site.', 'google-analytics-for-wordpress' ); ?></p>
  67. <?php } ?>
  68. </form>
  69. <?php if ( MonsterInsights()->license->site_license_has_error() ) { ?>
  70. <?php echo monsterinsights_get_message( 'error', MonsterInsights()->license->get_site_license_error() ); ?>
  71. <?php } ?>
  72. </td>
  73. </tr>
  74. <tr id="monsterinsights-settings-key-type-box">
  75. <th scope="row">
  76. <label for="monsterinsights-settings-key-type"><?php esc_html_e( 'License Key Type', 'google-analytics-for-wordpress' ); ?></label>
  77. </th>
  78. <td>
  79. <form id="monsterinsights-settings-key-type" method="post">
  80. <?php if ( $license_type ) { ?>
  81. <span class="monsterinsights-license-type"><?php printf( esc_html__( 'Your license key type for this site is %s.', 'google-analytics-for-wordpress' ), '<strong>' . $license_type . '</strong>' ); ?>
  82. <?php } else { ?>
  83. <?php if ( is_multisite() ) { ?>
  84. <span class="monsterinsights-license-type"><?php esc_html_e( 'No license key activated on this subsite.', 'google-analytics-for-wordpress' ); ?>
  85. <?php } else { ?>
  86. <span class="monsterinsights-license-type"><?php esc_html_e( 'No license key activated.', 'google-analytics-for-wordpress' ); ?>
  87. <?php } ?>
  88. <?php } ?>
  89. <input type="hidden" name="monsterinsights-license-key" value="<?php echo $license_key; ?>" />
  90. <?php wp_nonce_field( 'monsterinsights-key-nonce', 'monsterinsights-key-nonce' ); ?>
  91. <?php submit_button( esc_html__( 'Refresh Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-refresh-submit', false ); ?>
  92. <p class="description"><?php esc_html_e( 'Click refresh if your license has been upgraded or the type is incorrect.', 'google-analytics-for-wordpress' ); ?></p>
  93. </form>
  94. </td>
  95. </tr>
  96. <?php } ?>
  97. <tr id="monsterinsights-google-authenticate-box">
  98. <th scope="row">
  99. <?php if ( MonsterInsights()->auth->get_viewname() && MonsterInsights()->auth->get_ua() ) { ?>
  100. <label for="monsterinsights-google-authenticate"><?php esc_html_e( 'Analytics Profile', 'google-analytics-for-wordpress' ); ?></label>
  101. <?php } else { ?>
  102. <label for="monsterinsights-google-authenticate"><?php esc_html_e( 'Google Authentication', 'google-analytics-for-wordpress' ); ?></label>
  103. <?php } ?>
  104. </th>
  105. <td>
  106. <form id="monsterinsights-google-authenticate" method="post">
  107. <?php if ( MonsterInsights()->auth->get_viewname() && MonsterInsights()->auth->get_ua() ) { ?>
  108. <?php if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->is_site_licensed() ) { ?>
  109. <p><?php echo esc_html__( 'Please activate MonsterInsights Pro with an active, valid license key in order to use MonsterInsights Pro.' , 'google-analytics-for-wordpress' ); ?></p>
  110. <?php } else { ?>
  111. <p><?php echo esc_html__( 'Profile Active: ', 'google-analytics-for-wordpress' ) . MonsterInsights()->auth->get_viewname(); ?></p>
  112. <p><?php wp_nonce_field( 'monsterinsights-google-authenticated-nonce', 'monsterinsights-google-authenticated-nonce' ); ?>
  113. <?php submit_button( esc_html__( 'Re-Authenticate with your Google Account', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-google-reauthenticate-submit', false ); ?>
  114. <?php submit_button( esc_html__( 'Verify Credentials', 'google-analytics-for-wordpress' ), 'button-primary', 'monsterinsights-google-verify-submit', false ); ?>
  115. <?php submit_button( esc_html__( 'Deauthenticate', 'google-analytics-for-wordpress' ), 'button-danger', 'monsterinsights-google-deauthenticate-submit', false ); ?></p>
  116. <?php } ?>
  117. <?php } else { ?>
  118. <?php if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->is_site_licensed() ) { ?>
  119. <p><?php echo esc_html__( 'Please activate MonsterInsights Pro with an active, valid license key in order to use MonsterInsights Pro.' , 'google-analytics-for-wordpress' ); ?></p>
  120. <?php } else { ?>
  121. <?php wp_nonce_field( 'monsterinsights-google-authenticate-nonce', 'monsterinsights-google-authenticate-nonce' ); ?>
  122. <?php submit_button( esc_html__( 'Authenticate with your Google account', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-google-authenticate-submit', false ); ?>
  123. <p class="description"><?php printf( esc_html__( 'Already have a Google Account, but don’t know if you’ve setup Google Analytics? %s Click here to login and find out%s.', 'google-analytics-for-wordpress' ), '<a href="'. monsterinsights_get_url( 'settings-page', 'already-have-ga-account-doc-link', 'https://www.monsterinsights.com/docs/find-out-if-you-already-have-a-google-analytics-account/?utm_source=monsterinsights-settings' ) .'">', '</a>' ); ?>
  124. <br />
  125. <?php printf( esc_html__( 'Having issues automatically authenticating? %s Click here to authenticate manually%s.', 'google-analytics-for-wordpress' ), '<a href="javascript:monsterinsights_show_manual()" data-action="show-manual-ua-box">', '</a>' ); ?></p>
  126. <?php } ?>
  127. <?php } ?>
  128. </form>
  129. </td>
  130. </tr>
  131. </tbody>
  132. </table>
  133. <!-- <hr /> -->
  134. <!-- Settings Form -->
  135. <form id="monsterinsights-general-tab" method="post">
  136. <table class="form-table">
  137. <tbody>
  138. <?php if ( ! MonsterInsights()->auth->get_viewname() ) { ?>
  139. <!-- Manual UA -->
  140. <tr id="monsterinsights-google-ua-box" <?php echo ( ! MonsterInsights()->auth->get_manual_ua() ? 'class="monsterinsights-hideme"' : ''); ?> >
  141. <th scope="row">
  142. <label for="monsterinsights-google-ua"><?php esc_html_e( 'Manually enter your UA code', 'google-analytics-for-wordpress' ); ?></label>
  143. </th>
  144. <td>
  145. <input type="text" id="monsterinsights-google-ua" name="manual_ua_code" value="<?php echo esc_html( MonsterInsights()->auth->get_manual_ua() ); ?>" />
  146. <p class="description"><?php esc_html_e( 'Warning: If you use a manual UA code, you won\'t be able to use the reports.', 'google-analytics-for-wordpress' ); ?></p>
  147. </td>
  148. </tr>
  149. <?php } ?>
  150. <?php
  151. $manual_ua = MonsterInsights()->auth->get_manual_ua();
  152. $auth_ua = MonsterInsights()->auth->get_ua();
  153. if ( empty( $manual_ua ) && empty( $auth_ua ) && monsterinsights_get_network_ua() ) { ?>
  154. <!-- If we don't have a manual or auth UA but we have a valid default set (from network), explain that -->
  155. <tr id="monsterinsights-default-google-authenticate-box">
  156. <th scope="row">
  157. <label><?php esc_html_e( 'Default Configuration:', 'google-analytics-for-wordpress' ); ?></label>
  158. </th>
  159. <td>
  160. <p class="description"><?php printf( esc_html__( 'If you do not authenticate with MonsterInsights above, the network default %s will be used as the Google UA code.', 'google-analytics-for-wordpress' ), monsterinsights_get_network_ua() ); ?></p>
  161. </td>
  162. </tr>
  163. <?php } ?>
  164. <!-- Upgrade Doc -->
  165. <?php if ( ! monsterinsights_is_pro_version() ) { ?>
  166. <tr id="monsterinsights-upgrade-link">
  167. <th scope="row">
  168. <label for="monsterinsights-upgrade-link"><?php esc_html_e( 'Unlock MonsterInsights Pro', 'google-analytics-for-wordpress' ); ?></label>
  169. </th>
  170. <td>
  171. <p>
  172. <?php echo sprintf( esc_html__( 'Already purchased an upgrade to MonsterInsights Pro? To unlock your Pro features and addons, %sfollow our upgrade guide%s to install MonsterInsights Pro.' ), '<a href="'. monsterinsights_get_url( 'settings-page', 'general-tab-upgrade-link', 'https://www.monsterinsights.com/docs/go-lite-pro/' ) .'">', '</a>' ); ?>
  173. </p>
  174. <p>
  175. <?php echo sprintf( esc_html__( "Don't yet have a Pro license? %sVisit our website%s to upgrade and learn more about all the amazing features, expanded report and powerful addons you unlock when you go Pro." ), '<a href="'. monsterinsights_get_upgrade_link( 'settings-page', 'general-tab-upgrade-link' ) .'">', '</a>' ); ?>
  176. </p>
  177. </td>
  178. </tr>
  179. <?php } ?>
  180. <!-- Disable Dashboard -->
  181. <?php
  182. $title = esc_html__( 'Disable Reports', 'google-analytics-for-wordpress' );
  183. $description = esc_html__( 'Hide the reports page.', 'google-analytics-for-wordpress' );
  184. echo monsterinsights_make_checkbox( 'dashboards_disabled', $title, $description );
  185. ?>
  186. <?php if ( $automatic_updates !== 'all' && $automatic_updates !== 'minor' ){ ?>
  187. <?php $automatic_updates = $automatic_updates ? $automatic_updates : 'none'; ?>
  188. <tr id="monsterinsights-automatic-updates-mode">
  189. <th scope="row">
  190. <label for="monsterinsights-automatic-updates-mode"><?php esc_html_e( 'Automatic Updates', 'google-analytics-for-wordpress' ); ?></label>
  191. </th>
  192. <td>
  193. <label><input type="radio" name="automatic_updates" value="all" <?php checked( $automatic_updates, 'all' ); ?> ><?php esc_html_e('Yes (Recommended) - Get the latest features, bugfixes, and security updates as they are released.', 'google-analytics-for-wordpress'); ?> </label>
  194. <label><input type="radio" name="automatic_updates" value="minor" <?php checked( $automatic_updates, 'minor' ); ?> ><?php esc_html_e( 'Minor Only - Only get bugfixes and security updates, but not major features.', 'google-analytics-for-wordpress'); ?> </label>
  195. <label><input type="radio" name="automatic_updates" value="none" <?php checked( $automatic_updates, 'none' ); ?> ><?php esc_html_e( 'None - Manually update everything.', 'google-analytics-for-wordpress'); ?> </label>
  196. </td>
  197. </tr>
  198. <?php } ?>
  199. <!-- Tracking -->
  200. <?php
  201. $title = esc_html__( 'Allow Usage Tracking', 'google-analytics-for-wordpress' );
  202. $description = esc_html__( 'By allowing us to track usage data we can better help you, because we know with which WordPress configurations, themes and plugins we should test.', 'google-analytics-for-wordpress' );
  203. if ( ( ! $anon_tracking || monsterinsights_is_debug_mode() ) && ! monsterinsights_is_pro_version() ){
  204. echo monsterinsights_make_checkbox( 'anonymous_data', $title, $description );
  205. }
  206. ?>
  207. <?php
  208. /**
  209. * Developer Alert:
  210. *
  211. * Per the README, this is considered an internal hook and should
  212. * not be used by other developers. This hook's behavior may be modified
  213. * or the hook may be removed at any time, without warning.
  214. */
  215. do_action( 'monsterinsights_settings_general_box' );
  216. ?>
  217. </tbody>
  218. </table>
  219. <input type="hidden" name="monsterinsights_settings_tab" value="general"/>
  220. <?php wp_nonce_field( 'monsterinsights-settings-nonce', 'monsterinsights-settings-nonce' ); ?>
  221. <?php submit_button( esc_html__( 'Save Changes', 'google-analytics-for-wordpress' ), 'primary', 'monsterinsights-settings-submit', false ); ?>
  222. </form>
  223. </div>
  224. <?php
  225. }
  226. add_action( 'monsterinsights_tab_settings_general', 'monsterinsights_settings_general_tab' );
  227. /**
  228. * Callback for saving the general settings tab.
  229. *
  230. * @since 6.0.0
  231. * @access public
  232. *
  233. * @return void
  234. */
  235. function monsterinsights_settings_save_general() {
  236. $throw_notice = false;
  237. $manual_ua_code = isset( $_POST['manual_ua_code'] ) ? $_POST['manual_ua_code'] : '';
  238. $manual_ua_code = monsterinsights_is_valid_ua( $manual_ua_code ); // also sanitizes the string
  239. $manual_ua_code_old = MonsterInsights()->auth->get_manual_ua();
  240. if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old === $manual_ua_code ) {
  241. // Same code we had before
  242. // Do nothing
  243. } else if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old !== $manual_ua_code ) {
  244. // Different UA code
  245. MonsterInsights()->auth->set_manual_ua( $manual_ua_code );
  246. } else if ( $manual_ua_code && empty( $manual_ua_code_old ) ) {
  247. // Move to manual
  248. MonsterInsights()->auth->set_manual_ua( $manual_ua_code );
  249. } else if ( empty( $manual_ua_code ) && $manual_ua_code_old ) {
  250. // Deleted manual
  251. MonsterInsights()->auth->delete_manual_ua();
  252. } else if ( isset( $_POST['manual_ua_code'] ) && empty( $manual_ua_code ) ) {
  253. $throw_notice = true;
  254. } else {
  255. // Not UA before or after
  256. // Do nothing
  257. }
  258. $dashboards_disabled = isset( $_POST['dashboards_disabled'] ) ? 1 : 0;
  259. $dashboards_disabled_old = monsterinsights_get_option( 'dashboards_disabled', false );
  260. if ( $dashboards_disabled && ! $dashboards_disabled_old ) {
  261. do_action( 'monsterinsights_reports_delete_aggregate_data' );
  262. }
  263. monsterinsights_update_option( 'dashboards_disabled', $dashboards_disabled );
  264. monsterinsights_update_option( 'tracking_mode', 'analytics' );
  265. $automatic_updates = isset( $_POST['automatic_updates'] ) && in_array( $_POST['automatic_updates'], array( 'all', 'minor', 'none' ) ) ? $_POST['automatic_updates'] : false;
  266. if ( $automatic_updates ) {
  267. monsterinsights_update_option( 'automatic_updates', $automatic_updates );
  268. }
  269. $anonymous_data = isset( $_POST['anonymous_data'] ) ? 1 : 0;
  270. if ( $anonymous_data ) {
  271. if ( monsterinsights_is_pro_version() ) {
  272. monsterinsights_update_option( 'anonymous_data', 1 );
  273. } else {
  274. monsterinsights_update_option( 'anonymous_data', $anonymous_data );
  275. }
  276. }
  277. /**
  278. * Developer Alert:
  279. *
  280. * Per the README, this is considered an internal hook and should
  281. * not be used by other developers. This hook's behavior may be modified
  282. * or the hook may be removed at any time, without warning.
  283. */
  284. do_action( 'monsterinsights_settings_save_general_end' );
  285. // Output an admin notice so the user knows what happened
  286. if ( $throw_notice ) {
  287. add_action( 'monsterinsights_settings_general_tab_notice', 'monsterinsights_invalid_ua_code' );
  288. } else {
  289. add_action( 'monsterinsights_settings_general_tab_notice', 'monsterinsights_updated_settings' );
  290. }
  291. }
  292. add_action( 'monsterinsights_settings_save_general', 'monsterinsights_settings_save_general', 11 );