tools.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // Exit if accessed directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. /**
  7. * Callback to output the MonsterInsights settings page.
  8. *
  9. * @since 6.0.0
  10. * @access public
  11. *
  12. * @return void
  13. */
  14. function monsterinsights_tools_page() {
  15. /**
  16. * Developer Alert:
  17. *
  18. * Per the README, this is considered an internal hook and should
  19. * not be used by other developers. This hook's behavior may be modified
  20. * or the hook may be removed at any time, without warning.
  21. */
  22. do_action( 'monsterinsights_head' );
  23. ?>
  24. <?php echo monsterinsights_ublock_notice(); ?>
  25. <!-- Tabs -->
  26. <h1 id="monsterinsights-tools-page-main-nav" class="monsterinsights-main-nav-container monsterinsights-nav-container" data-container="#monsterinsights-tools-pages">
  27. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-spacing-item" href="#">&nbsp;</a>
  28. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-active" href="#monsterinsights-main-tab-settings" title="<?php echo esc_attr( __( 'Import/Export', 'google-analytics-for-wordpress' ) ); ?>">
  29. <?php echo esc_html__( 'Import/Export', 'google-analytics-for-wordpress' ); ?>
  30. </a>
  31. <a class="monsterinsights-main-nav-item monsterinsights-nav-item" href="#monsterinsights-main-tab-url-builder" title="<?php echo esc_attr( __( 'Campaign URL Builder', 'google-analytics-for-wordpress' ) ); ?>">
  32. <?php echo esc_html__( 'URL Builder', 'google-analytics-for-wordpress' ); ?>
  33. </a>
  34. </h1>
  35. <!-- Tab Panels -->
  36. <div id="monsterinsights-tools-pages" class="monsterinsights-main-nav-tabs monsterinsights-nav-tabs wrap" data-navigation="#monsterinsights-tools-page-main-nav">
  37. <h1 class="monsterinsights-hideme"></h1><!-- so wp notices are below the nav bar -->
  38. <div id="monsterinsights-main-tab-settings" class="monsterinsights-main-nav-tab monsterinsights-nav-tab monsterinsights-active">
  39. <?php monsterinsights_tools_settings_tab(); ?>
  40. </div>
  41. <div id="monsterinsights-main-tab-url-builder" class="monsterinsights-main-nav-tab monsterinsights-nav-tab">
  42. <?php monsterinsights_tools_url_builder_tab(); ?>
  43. </div>
  44. </div>
  45. <?php
  46. }
  47. function monsterinsights_tools_url_builder_tab(){
  48. do_action( 'monsterinsights_tools_url_builder_tab' );
  49. }
  50. function monsterinsights_tools_settings_tab() {
  51. ob_start();?>
  52. <h2><?php echo esc_html__( 'Setting Tools', 'google-analytics-for-wordpress' );?></h2>
  53. <p><?php echo esc_html__( 'You can use the below tools to import settings from other MonsterInsights websites or export settings to import into another MonsterInsights install.', 'google-analytics-for-wordpress' ); ?> </p>
  54. <br />
  55. <table class="form-table">
  56. <tbody>
  57. <tr>
  58. <th scope="row">
  59. <label for="monsterinsights-import-settings">
  60. <?php echo esc_html__( 'Import Settings', 'google-analytics-for-wordpress' );?>
  61. </label>
  62. </th>
  63. <td>
  64. <?php
  65. if ( ! empty( $_REQUEST['action'] ) && ! empty( $_REQUEST['result'] ) && $_REQUEST['action'] === 'import' && $_REQUEST['result'] === 'success' ){
  66. echo MonsterInsights()->notices->display_inline_notice( 'monsterinsights_standard_notice', '', __( 'Successfully imported settings!','google-analytics-for-wordpress'), 'success', false, array() );
  67. }
  68. ?>
  69. <form method="post" enctype="multipart/form-data">
  70. <p>
  71. <input type="file" name="import_file"/>
  72. <input type="hidden" name="monsterinsights_action" value="monsterinsights_import_settings" />
  73. </p>
  74. <p class="description"><?php echo esc_html__( 'Paste the import field content from another MonsterInsights site in above.', 'google-analytics-for-wordpress' );?></p>
  75. <p>
  76. <?php wp_nonce_field( 'monsterinsights_import_settings', 'monsterinsights_import_settings' ); ?>
  77. </p>
  78. <p>
  79. <?php submit_button( __( 'Import', 'google-analytics-for-wordpress' ), 'monsterinsights-action-button button button-action', 'submit', false ); ?>
  80. </p>
  81. </form>
  82. </td>
  83. </tr>
  84. <tr>
  85. <th scope="row">
  86. <label for="monsterinsights-export-settings">
  87. <?php echo esc_html__( 'Export Settings:', 'google-analytics-for-wordpress' );?>
  88. </label>
  89. </th>
  90. <td>
  91. <form method="post" enctype="multipart/form-data">
  92. <p>
  93. <input type="hidden" name="monsterinsights_action" value="monsterinsights_export_settings" />
  94. </p>
  95. <p>
  96. <?php wp_nonce_field( 'monsterinsights_export_settings', 'monsterinsights_export_settings' ); ?>
  97. </p>
  98. <p>
  99. <?php submit_button( __( 'Export', 'google-analytics-for-wordpress' ), 'monsterinsights-settings-export monsterinsights-action-button button button-action', 'submit', false ); ?>
  100. </p>
  101. </form>
  102. </td>
  103. </tr>
  104. </tbody>
  105. </table>
  106. <?php
  107. echo ob_get_clean();
  108. }
  109. /**
  110. * MonsterInsights settings export.
  111. *
  112. * @since 6.0.0
  113. * @access public
  114. *
  115. * @return void
  116. */
  117. function monsterinsights_process_export_settings() {
  118. if ( !isset( $_POST['monsterinsights_action'] ) || empty( $_POST['monsterinsights_action'] ) ) {
  119. return;
  120. }
  121. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  122. return;
  123. }
  124. if ( $_POST['monsterinsights_action'] !== 'monsterinsights_export_settings' ){
  125. return;
  126. }
  127. if ( empty( $_POST['monsterinsights_export_settings'] ) || ! wp_verify_nonce( $_POST['monsterinsights_export_settings'], 'monsterinsights_export_settings' ) ) {
  128. return;
  129. }
  130. $settings = monsterinsights_export_settings();
  131. ignore_user_abort( true );
  132. nocache_headers();
  133. header( 'Content-Type: application/json; charset=utf-8' );
  134. header( 'Content-Disposition: attachment; filename=monsterinsights-settings-export-' . date( 'm-d-Y' ) . '.json' );
  135. header( "Expires: 0" );
  136. echo $settings;
  137. exit;
  138. }
  139. add_action( 'admin_init', 'monsterinsights_process_export_settings' );
  140. function monsterinsights_import_settings() {
  141. if ( !isset( $_POST['monsterinsights_action'] ) || empty( $_POST['monsterinsights_action'] ) ) {
  142. return;
  143. }
  144. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  145. return;
  146. }
  147. if ( $_POST['monsterinsights_action'] !== 'monsterinsights_import_settings' ){
  148. return;
  149. }
  150. if ( !wp_verify_nonce( $_POST['monsterinsights_import_settings'], 'monsterinsights_import_settings' ) ) {
  151. return;
  152. }
  153. $extension = explode( '.', $_FILES['import_file']['name'] );
  154. $extension = end( $extension );
  155. if ( $extension != 'json' ) {
  156. wp_die( __( 'Please upload a valid .json file', 'google-analytics-for-wordpress' ) );
  157. }
  158. $import_file = $_FILES['import_file']['tmp_name'];
  159. if ( empty( $import_file ) ) {
  160. wp_die( __( 'Please upload a file to import', 'google-analytics-for-wordpress' ) );
  161. }
  162. $file = file_get_contents( $import_file );
  163. if ( empty( $file ) ) {
  164. wp_die( __( 'Please upload a real settings export file to import', 'google-analytics-for-wordpress' ) );
  165. }
  166. // Retrieve the settings from the file and convert the json object to an array.
  167. $new_settings = json_decode( wp_json_encode( json_decode( $file ) ), true );
  168. $settings = monsterinsights_get_options();
  169. $exclude = array(
  170. 'analytics_profile',
  171. 'analytics_profile_code',
  172. 'analytics_profile_name',
  173. 'oauth_version',
  174. 'cron_last_run',
  175. 'monsterinsights_oauth_status',
  176. );
  177. foreach ( $exclude as $e ) {
  178. if ( ! empty( $new_settings[ $e ] ) ) {
  179. unset( $new_settings[ $e ] );
  180. }
  181. }
  182. if ( ! is_super_admin() ) {
  183. if ( ! empty( $new_settings[ 'custom_code' ] ) ) {
  184. unset( $new_settings[ 'custom_code' ] );
  185. }
  186. }
  187. foreach ( $exclude as $e ) {
  188. if ( ! empty( $settings[ $e ] ) ) {
  189. $new_settings = $settings[ $e ];
  190. }
  191. }
  192. global $monsterinsights_settings;
  193. $monsterinsights_settings = $new_settings;
  194. update_option( monsterinsights_get_option_name(), $new_settings );
  195. wp_safe_redirect( admin_url( 'admin.php?page=monsterinsights_tools&action=import&result=success#monsterinsights-main-tab-settings' ) ); exit;
  196. }
  197. add_action( 'admin_init', 'monsterinsights_import_settings' );