network-settings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Network class.
  4. *
  5. * @since 6.0.0
  6. *
  7. * @package MonsterInsights
  8. * @subpackage network
  9. * @author Chris Christoff
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * Callback to output the MonsterInsights network page.
  17. *
  18. * @since 6.0.0
  19. * @access public
  20. *
  21. * @return void
  22. */
  23. function monsterinsights_network_page() {
  24. /**
  25. * Developer Alert:
  26. *
  27. * Per the README, this is considered an internal hook and should
  28. * not be used by other developers. This hook's behavior may be modified
  29. * or the hook may be removed at any time, without warning.
  30. */
  31. do_action( 'monsterinsights_head' );
  32. ?>
  33. <?php echo monsterinsights_ublock_notice(); ?>
  34. <div id="monsterinsights-settings" class="wrap">
  35. <div id="monsterinsights-settings-pages" class="monsterinsights-main-nav-tabs monsterinsights-nav-tabs wrap" data-navigation="#monsterinsights-network-settings-page-main-nav">
  36. <div id="monsterinsights-main-tab-general" class="monsterinsights-main-nav-tab monsterinsights-nav-tab monsterinsights-active">
  37. <div id="monsterinsights-network-settings-general">
  38. <?php
  39. // Output any notices now
  40. /**
  41. * Developer Alert:
  42. *
  43. * Per the README, this is considered an internal hook and should
  44. * not be used by other developers. This hook's behavior may be modified
  45. * or the hook may be removed at any time, without warning.
  46. */
  47. do_action( 'monsterinsights_network_settings_general_tab_notice' );
  48. ?>
  49. <h1><?php esc_html_e( 'Network Settings', 'google-analytics-for-wordpress'); ?></h1>
  50. <p><?php esc_html_e( 'Activate your MonsterInsights license key on this panel to hide the license key settings and addon pages for subsites.', 'google-analytics-for-wordpress'); ?></p>
  51. <table class="form-table">
  52. <tbody>
  53. <?php if ( monsterinsights_is_pro_version() ) {
  54. $license_key = MonsterInsights()->license->get_network_license_key();
  55. $license_key = $license_key ? $license_key : MonsterInsights()->license->get_default_license_key();
  56. $license_type = MonsterInsights()->license->get_network_license_type();
  57. ?>
  58. <tr id="monsterinsights-settings-key-box">
  59. <th scope="row">
  60. <label for="monsterinsights-settings-key"><?php esc_html_e( 'License Key', 'google-analytics-for-wordpress' ); ?></label>
  61. </th>
  62. <td>
  63. <form id="monsterinsights-settings-verify-key" method="post">
  64. <input type="password" name="monsterinsights-license-key" id="monsterinsights-settings-key" value="<?php echo esc_attr( $license_key ); ?>" />
  65. <?php wp_nonce_field( 'monsterinsights-key-nonce', 'monsterinsights-key-nonce' ); ?>
  66. <?php if ( MonsterInsights()->license->get_network_license_key() ) { ?>
  67. <?php submit_button( esc_html__( 'Verify Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-verify-submit', false ); ?>
  68. <?php submit_button( esc_html__( 'Deactivate Key', 'google-analytics-for-wordpress' ), 'button-danger', 'monsterinsights-deactivate-submit', false ); ?>
  69. <?php } else { ?>
  70. <?php submit_button( esc_html__( 'Activate Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-verify-submit', false ); ?>
  71. <?php } ?>
  72. <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>
  73. </form>
  74. <?php if ( MonsterInsights()->license->network_license_has_error() ) { ?>
  75. <?php echo monsterinsights_get_message( 'error', MonsterInsights()->license->get_network_license_error() ); ?>
  76. <?php } ?>
  77. </td>
  78. </tr>
  79. <tr id="monsterinsights-settings-key-type-box">
  80. <th scope="row">
  81. <label for="monsterinsights-settings-key-type"><?php esc_html_e( 'License Key Type', 'google-analytics-for-wordpress' ); ?></label>
  82. </th>
  83. <td>
  84. <form id="monsterinsights-settings-key-type" method="post">
  85. <?php if ( $license_type ) { ?>
  86. <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>' ); ?>
  87. <?php } else { ?>
  88. <span class="monsterinsights-license-type"><?php esc_html_e( 'No license key activated.', 'google-analytics-for-wordpress' ); ?>
  89. <?php } ?>
  90. <input type="hidden" name="monsterinsights-license-key" value="<?php echo esc_attr( $license_key ); ?>" />
  91. <?php wp_nonce_field( 'monsterinsights-key-nonce', 'monsterinsights-key-nonce' ); ?>
  92. <?php submit_button( esc_html__( 'Refresh Key', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-refresh-submit', false ); ?>
  93. <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>
  94. </form>
  95. </td>
  96. </tr>
  97. <?php } ?>
  98. <tr id="monsterinsights-google-authenticate-box">
  99. <th scope="row">
  100. <?php if ( MonsterInsights()->auth->get_network_viewname() && MonsterInsights()->auth->get_network_ua() ) { ?>
  101. <label for="monsterinsights-google-authenticate"><?php esc_html_e( 'Analytics Profile', 'google-analytics-for-wordpress' ); ?></label>
  102. <?php } else { ?>
  103. <label for="monsterinsights-google-authenticate"><?php esc_html_e( 'Google Authentication', 'google-analytics-for-wordpress' ); ?></label>
  104. <?php } ?>
  105. </th>
  106. <td>
  107. <form id="monsterinsights-google-authenticate" method="post">
  108. <?php if ( MonsterInsights()->auth->get_network_viewname() && MonsterInsights()->auth->get_network_ua() ) { ?>
  109. <?php if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->is_network_licensed() ) { ?>
  110. <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>
  111. <?php } else { ?>
  112. <p><?php echo esc_html__( 'Profile Active: ', 'google-analytics-for-wordpress' ) . MonsterInsights()->auth->get_network_viewname(); ?></p>
  113. <p><?php wp_nonce_field( 'monsterinsights-google-authenticated-nonce', 'monsterinsights-google-authenticated-nonce' ); ?>
  114. <?php submit_button( esc_html__( 'Re-Authenticate with your Google Account', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-google-reauthenticate-submit', false ); ?>
  115. <?php submit_button( esc_html__( 'Verify Credentials', 'google-analytics-for-wordpress' ), 'button-primary', 'monsterinsights-google-verify-submit', false ); ?>
  116. <?php submit_button( esc_html__( 'Deauthenticate', 'google-analytics-for-wordpress' ), 'button-danger', 'monsterinsights-google-deauthenticate-submit', false ); ?></p>
  117. <?php } ?>
  118. <?php } else { ?>
  119. <?php if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->is_network_licensed() ) { ?>
  120. <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>
  121. <?php } else { ?>
  122. <?php wp_nonce_field( 'monsterinsights-google-authenticate-nonce', 'monsterinsights-google-authenticate-nonce' ); ?>
  123. <?php submit_button( esc_html__( 'Authenticate with your Google account', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-google-authenticate-submit', false ); ?>
  124. <p class="description"><?php printf( esc_html__( 'This is the default Google profile to use for subsites of a network (can be overriden at the single site level). 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>
  125. <?php } ?>
  126. <?php } ?>
  127. </form>
  128. </td>
  129. </tr>
  130. <?php if ( ! MonsterInsights()->auth->get_network_viewname() ) {
  131. $network_ua = MonsterInsights()->auth->get_network_manual_ua();
  132. ?>
  133. <tr id="monsterinsights-google-ua-box" <?php echo (empty( $network_ua ) ? 'class="monsterinsights-hideme"' : ''); ?> >
  134. <form id="monsterinsights-network-general-tab" method="post">
  135. <th scope="row">
  136. <label for="monsterinsights-google-ua"><?php esc_html_e( 'Network UA code', 'google-analytics-for-wordpress' ); ?></label>
  137. </th>
  138. <td>
  139. <input type="text" id="monsterinsights-network-ua-code" name="network_manual_ua_code" value="<?php echo esc_html( MonsterInsights()->auth->get_network_manual_ua() ); ?>" /><?php submit_button( esc_html__( 'Save Network UA code', 'google-analytics-for-wordpress' ), 'button-action', 'monsterinsights-network-settings-submit', false ); ?>
  140. <p class="description"><?php esc_html_e( 'This is the default UA code to use for subsites of a network (can be overriden at the single site level).', 'google-analytics-for-wordpress' ); ?></p>
  141. <?php wp_nonce_field( 'monsterinsights-network-settings-nonce', 'monsterinsights-network-settings-nonce' ); ?>
  142. </td>
  143. </form>
  144. </tr>
  145. <?php } ?>
  146. <!-- Upgrade Doc -->
  147. <?php if ( ! monsterinsights_is_pro_version() ) { ?>
  148. <tr id="monsterinsights-upgrade-link">
  149. <th scope="row">
  150. <label for="monsterinsights-upgrade-link"><?php esc_html_e( 'Unlock MonsterInsights Pro', 'google-analytics-for-wordpress' ); ?></label>
  151. </th>
  152. <td>
  153. <p>
  154. <?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( 'network-settings-page', 'go-lite-pro-link', "https://www.monsterinsights.com/docs/go-lite-pro" ) .'">', '</a>' ); ?>
  155. </p>
  156. <p>
  157. <?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( 'network-settings-page', 'upgrade-to-pro-link' ) .'">', '</a>' ); ?>
  158. </p>
  159. </td>
  160. </tr>
  161. <?php } ?>
  162. <?php
  163. $title = esc_html__( 'Hide Announcements', 'google-analytics-for-wordpress' );
  164. $description = esc_html__( 'Hides plugin announcements and update details. This includes critical notices we use to inform about deprecations and important required configuration changes.', 'google-analytics-for-wordpress' );
  165. echo monsterinsights_make_checkbox( 'network_hide_am_notices', $title, $description );
  166. ?>
  167. </tbody>
  168. </table>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. <?php
  174. }
  175. /**
  176. * Callback for saving the general settings tab.
  177. *
  178. * @since 6.1.0
  179. * @access public
  180. *
  181. * @return void
  182. */
  183. function monsterinsights_network_settings_save_general() {
  184. // Check if user pressed the 'Update' button and nonce is valid
  185. if ( ! isset( $_POST['monsterinsights-network-settings-submit'] ) ) {
  186. return;
  187. }
  188. if ( ! wp_verify_nonce( $_POST['monsterinsights-network-settings-nonce'], 'monsterinsights-network-settings-nonce' ) ) {
  189. return;
  190. }
  191. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  192. return;
  193. }
  194. $throw_notice = false;
  195. $manual_ua_code = isset( $_POST['network_manual_ua_code'] ) ? $_POST['network_manual_ua_code'] : '';
  196. $manual_ua_code = monsterinsights_is_valid_ua( $manual_ua_code ); // also sanitizes the string
  197. $manual_ua_code_old = MonsterInsights()->auth->get_network_manual_ua();
  198. if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old === $manual_ua_code ) {
  199. // Same code we had before
  200. // Do nothing
  201. } else if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old !== $manual_ua_code ) {
  202. // Different UA code
  203. MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code );
  204. } else if ( $manual_ua_code && empty( $manual_ua_code_old ) ) {
  205. // Move to manual
  206. MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code );
  207. } else if ( empty( $manual_ua_code ) && $manual_ua_code_old ) {
  208. // Deleted manual
  209. MonsterInsights()->auth->delete_network_manual_ua();
  210. } else if ( isset( $_POST['network_manual_ua_code'] ) && empty( $manual_ua_code ) ) {
  211. $throw_notice = true;
  212. } else {
  213. // Not UA before or after
  214. // Do nothing
  215. }
  216. $network_hide_am_notices = isset( $_POST['network_hide_am_notices'] ) ? 1 : 0;
  217. $network_hide_am_notices_old = monsterinsights_get_option( 'network_hide_am_notices', false );
  218. if ( $network_hide_am_notices != $network_hide_am_notices_old ) {
  219. monsterinsights_update_option( 'network_hide_am_notices', $network_hide_am_notices );
  220. $throw_notice = true;
  221. }
  222. // Output an admin notice so the user knows what happened
  223. if ( $throw_notice ) {
  224. add_action( 'monsterinsights_network_settings_general_tab_notice', 'monsterinsights_invalid_ua_code' );
  225. } else {
  226. add_action( 'monsterinsights_network_settings_general_tab_notice', 'monsterinsights_updated_settings' );
  227. }
  228. }
  229. add_action( 'admin_init', 'monsterinsights_network_settings_save_general', 11 );