/includes/admin/pages/tools.php

  1. <?php 
  2. /** 
  3. * Tools class. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Tools 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. /** 
  17. * Callback to output the MonsterInsights settings page. 
  18. * 
  19. * @since 6.0.0 
  20. * @access public 
  21. * 
  22. * @return void 
  23. */ 
  24. function monsterinsights_tools_page() { 
  25. /**  
  26. * Developer Alert: 
  27. * 
  28. * Per the README, this is considered an internal hook and should 
  29. * not be used by other developers. This hook's behavior may be modified 
  30. * or the hook may be removed at any time, without warning. 
  31. */ 
  32. do_action( 'monsterinsights_head' ); 
  33. ?> 
  34. <?php echo monsterinsights_ublock_notice(); ?> 
  35.  
  36. <!-- Tabs --> 
  37. <h1 id="monsterinsights-tools-page-main-nav" class="monsterinsights-main-nav-container monsterinsights-nav-container" data-container="#monsterinsights-tools-pages"> 
  38. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-spacing-item" href="#"> </a> 
  39.  
  40. <a class="monsterinsights-main-nav-item monsterinsights-nav-item monsterinsights-active" href="#monsterinsights-main-tab-url-builder" title="<?php echo esc_attr( __( 'Campaign URL Builder', 'google-analytics-for-wordpress' ) ); ?>"> 
  41. <?php echo esc_html__( 'URL Builder', 'google-analytics-for-wordpress' ); ?> 
  42. </a> 
  43.  
  44. <a class="monsterinsights-main-nav-item monsterinsights-nav-item" href="#monsterinsights-main-tab-settings" title="<?php echo esc_attr( __( 'Settings', 'google-analytics-for-wordpress' ) ); ?>"> 
  45. <?php echo esc_html__( 'Settings', 'google-analytics-for-wordpress' ); ?> 
  46. </a> 
  47. </h1> 
  48.  
  49.  
  50. <!-- Tab Panels --> 
  51. <div id="monsterinsights-tools-pages" class="monsterinsights-main-nav-tabs monsterinsights-nav-tabs wrap" data-navigation="#monsterinsights-tools-page-main-nav"> 
  52. <h1 class="monsterinsights-hideme"></h1><!-- so wp notices are below the nav bar --> 
  53. <div id="monsterinsights-main-tab-url-builder" class="monsterinsights-main-nav-tab monsterinsights-nav-tab monsterinsights-active"> 
  54. <?php monsterinsights_tools_url_builder_tab(); ?> 
  55. </div> 
  56. <div id="monsterinsights-main-tab-settings" class="monsterinsights-main-nav-tab monsterinsights-nav-tab"> 
  57. <?php monsterinsights_tools_settings_tab(); ?> 
  58. </div> 
  59. </div> 
  60. <?php 
  61.  
  62. function monsterinsights_tools_url_builder_tab() { 
  63. ob_start();?> 
  64. <h2><?php echo esc_html__( 'Generate custom campaign parameters for your advertising URLS.', 'google-analytics-for-wordpress' );?></h2> 
  65. <p><?php echo esc_html__( 'The URL builder helps you add parameters to your URLs you use in custom web-based or email ad campaigns. A custom campaign is any ad campaign not using the AdWords auto-tagging feature. When users click one of the custom links, the unique parameters are sent to your Analytics account, so you can identify the urls that are the most effective in attracting users to your content.', 'google-analytics-for-wordpress' ); ?> </p> 
  66. <p><?php echo esc_html__('Fill out the required fields (marked with *) in the form below, and as you make changes the full campaign URL will be generated for you.', 'google-analytics-for-wordpress' ); ?></p> 
  67. <br /> 
  68. <form id="monsterinsights-url-builder" action="javascript:void(0);"> 
  69. <table class="form-table"> 
  70. <tbody> 
  71. <tr> 
  72. <th scope="row"> 
  73. <label for="monsterinsights-url-builer-domain"> 
  74. <?php echo esc_html__( 'Website URL', 'google-analytics-for-wordpress' );?><span class="monsterinsights-required-indicator">*</span> 
  75. </label> 
  76. </th> 
  77. <td> 
  78. <input type="url" name="domain" id="monsterinsights-url-builer-domain" value="" /> 
  79. <p class="description"><?php echo sprintf( esc_html__( 'The full website URL (e.g. %1$s)', 'google-analytics-for-wordpress' ), home_url() );?></p> 
  80. </td> 
  81. </tr> 
  82. <tr> 
  83. <th scope="row"> 
  84. <label for="monsterinsights-url-builer-source"> 
  85. <?php echo esc_html__( 'Campaign Source', 'google-analytics-for-wordpress' );?><span class="monsterinsights-required-indicator">*</span> 
  86. </label> 
  87. </th> 
  88. <td> 
  89. <input type="text" name="source" id="monsterinsights-url-builer-source" value="" /> 
  90. <p class="description"><?php echo sprintf( esc_html__( 'Enter a referrer (e.g. %1$s, %2$s, %3$s)', 'google-analytics-for-wordpress' ), '<code>facebook</code>', '<code>newsletter</code>', '<code>google</code>' );?></p> 
  91. </td> 
  92. </tr> 
  93. <tr> 
  94. <th scope="row"> 
  95. <label for="monsterinsights-url-builer-medium"> 
  96. <?php echo esc_html__( 'Campaign Medium', 'google-analytics-for-wordpress' );?> 
  97. </label> 
  98. </th> 
  99. <td> 
  100. <input type="text" name="medium" id="monsterinsights-url-builer-medium" value="" /> 
  101. <p class="description"><?php echo sprintf( esc_html__( 'Enter a marketing medium (e.g. %1$s, %2$s, %3$s)', 'google-analytics-for-wordpress' ), '<code>cpc</code>', '<code>banner</code>', '<code>email</code>' );?></p> 
  102. </td> 
  103. </tr> 
  104. <tr> 
  105. <th scope="row"> 
  106. <label for="monsterinsights-url-builer-name"> 
  107. <?php echo esc_html__( 'Campaign Name', 'google-analytics-for-wordpress' );?> 
  108. </label> 
  109. </th> 
  110. <td> 
  111. <input type="text" name="name" id="monsterinsights-url-builer-name" value="" /> 
  112. <p class="description"><?php echo sprintf( esc_html__( 'Enter a name to identify the campaign (e.g. %1$s)', 'google-analytics-for-wordpress' ), '<code>spring_sale</code>' );?></p> 
  113. </td> 
  114. </tr> 
  115. <tr> 
  116. <th scope="row"> 
  117. <label for="monsterinsights-url-builer-term"> 
  118. <?php echo esc_html__( 'Campaign Term', 'google-analytics-for-wordpress' );?> 
  119. </label> 
  120. </th> 
  121. <td> 
  122. <input type="text" name="term" id="monsterinsights-url-builer-term" value="" /> 
  123. <p class="description"><?php echo esc_html__( 'Enter the paid keyword', 'google-analytics-for-wordpress' );?></p> 
  124. </td> 
  125. </tr> 
  126. <tr> 
  127. <th scope="row"> 
  128. <label for="monsterinsights-url-builer-content"> 
  129. <?php echo esc_html__( 'Campaign Content', 'google-analytics-for-wordpress' );?> 
  130. </label> 
  131. </th> 
  132. <td> 
  133. <input type="text" name="content" id="monsterinsights-url-builer-content" value="" /> 
  134. <p class="description"><?php echo esc_html__( 'Enter something to differentiate ads', 'google-analytics-for-wordpress' );?></p> 
  135. </td> 
  136. </tr> 
  137. <tr> 
  138. <th scope="row"> 
  139. <label for="monsterinsights-url-builer-fragment"> 
  140. <?php echo esc_html__( 'Use Fragment', 'google-analytics-for-wordpress' );?> 
  141. </label> 
  142. </th> 
  143. <td> 
  144. <input type="checkbox" name="fragment" id="monsterinsights-url-builer-fragment" value="" /> 
  145. <p class="description"><?php echo esc_html__( 'Set the parameters in the fragment portion of the URL (not recommended).', 'google-analytics-for-wordpress' );?></p> 
  146. </td> 
  147. </tr> 
  148. <tr> 
  149. <th scope="row"> 
  150. <label for="monsterinsights-url-builer-url"> 
  151. <?php echo esc_html__( 'URL to use (updates automatically):', 'google-analytics-for-wordpress' );?> 
  152. </label> 
  153. </th> 
  154. <td> 
  155. <textarea name="url" id="monsterinsights-url-builer-url" value="" readonly="readonly"></textarea> 
  156. <p> 
  157. <button class="monsterinsights-copy-to-clipboard monsterinsights-action-button button button-action" data-clipboard-target="#monsterinsights-url-builer-url"> 
  158. <?php echo esc_html__( 'Copy to clipboard' , 'google-analytics-for-wordpress');?> 
  159. </button> 
  160. <button id="monsterinsights-shorten-url" class="monsterinsights-action-button button button-secondary" style="margin-left: 20px;"> 
  161. <?php echo esc_html__( 'Shorten URL' , 'google-analytics-for-wordpress');?> 
  162. </button> 
  163. </p> 
  164. </td> 
  165. </tr> 
  166.  
  167. </tbody> 
  168. </table> 
  169. </form> 
  170. <h2><?php echo esc_html__( 'More information and examples for each option', 'google-analytics-for-wordpress');?></h2> 
  171. <p><?php echo esc_html__( 'The following table gives a detailed explanation and example of each of the campaign parameters.', 'google-analytics-for-wordpress');?></p> 
  172. <table class="wp-list-table widefat striped"> 
  173. <tbody> 
  174. <tr> 
  175. <td> 
  176. <p><strong><?php echo esc_html__( 'Campaign Source', 'google-analytics-for-wordpress');?></strong></p> 
  177. <p><code>utm_source</code></p> 
  178. </td> 
  179. <td> 
  180. <p><strong><?php echo esc_html__( 'Required.', 'google-analytics-for-wordpress');?></strong></p> 
  181. <p><?php echo sprintf( esc_html__( 'Use %1$s to identify a search engine, newsletter name, or other source.', 'google-analytics-for-wordpress'), '<code>utm_source</code>');?></p> 
  182. <p><em><?php echo esc_html__( 'Example:', 'google-analytics-for-wordpress');?></em> <code>google</code></p> 
  183. </td> 
  184. </tr> 
  185. <tr> 
  186. <td> 
  187. <p><strong><?php echo esc_html__( 'Campaign Medium', 'google-analytics-for-wordpress');?></strong></p> 
  188. <p><code>utm_medium</code></p> 
  189. </td> 
  190. <td> 
  191. <p><?php echo sprintf(esc_html__( 'Use %1$s to identify a medium such as email or cost-per- click.', 'google-analytics-for-wordpress'), '<code>utm_medium</code>');?></p> 
  192. <p><em><?php echo esc_html__( 'Example:', 'google-analytics-for-wordpress');?></em> <code>cpc</code></p> 
  193. </td> 
  194. </tr> 
  195. <tr> 
  196. <td> 
  197. <p><strong><?php echo esc_html__( 'Campaign Name', 'google-analytics-for-wordpress');?></strong></p> 
  198. <p><code>utm_campaign</code></p> 
  199. </td> 
  200. <td> 
  201. <p><?php echo sprintf(esc_html__( 'Used for keyword analysis. Use %1$s to identify a specific product promotion or strategic campaign.', 'google-analytics-for-wordpress'), '<code>utm_campaign</code>');?></p> 
  202. <p><em><?php echo esc_html__( 'Example:', 'google-analytics-for-wordpress');?></em> <code>utm_campaign=spring_sale</code></p> 
  203. </td> 
  204. </tr> 
  205. <tr> 
  206. <td> 
  207. <p><strong><?php echo esc_html__( 'Campaign Term', 'google-analytics-for-wordpress');?></strong></p> 
  208. <p><code>utm_term</code></p> 
  209. </td> 
  210. <td> 
  211. <p><?php echo sprintf( esc_html__( 'Used for paid search. Use %1$s to note the keywords for this ad.', 'google-analytics-for-wordpress'), '<code>utm_term</code>');?></p> 
  212. <p><em><?php echo esc_html__( 'Example:', 'google-analytics-for-wordpress');?></em> <code>running+shoes</code></p> 
  213. </td> 
  214. </tr> 
  215. <tr> 
  216. <td> 
  217. <p><strong><?php echo esc_html__( 'Campaign Content', 'google-analytics-for-wordpress');?></strong></p> 
  218. <p><code>utm_content</code></p> 
  219. </td> 
  220. <td> 
  221. <p><?php echo sprintf(esc_html__( 'Used for A/B testing and content-targeted ads. Use %1$s to differentiate ads or links that point to the same URL.', 'google-analytics-for-wordpress'), '<code>utm_content</code>');?></p> 
  222. <p><em><?php echo esc_html__( 'Examples:', 'google-analytics-for-wordpress');?></em> <code>logolink</code> <em><?php echo esc_html__( 'or', 'google-analytics-for-wordpress');?></em> <code>textlink</code></p> 
  223. </td> 
  224. </tr> 
  225. </tbody> 
  226. </table> 
  227.  
  228. <h2 id="monsterinsights-related-resources"><?php echo esc_html__( 'More information:', 'google-analytics-for-wordpress');?></h2> 
  229.  
  230. <ul id="monsterinsights-related-resources-list"> 
  231. <li><a href="https://support.google.com/analytics/answer/1247851"><?php echo esc_html__( 'About Campaigns', 'google-analytics-for-wordpress');?></a></li> 
  232. <li><a href="https://support.google.com/analytics/answer/1033863"><?php echo esc_html__( 'About Custom Campaigns', 'google-analytics-for-wordpress');?></a></li> 
  233. <li><a href="https://support.google.com/analytics/answer/1037445"><?php echo esc_html__( 'Best practices for creating Custom Campaigns', 'google-analytics-for-wordpress');?></a></li> 
  234. <li><a href="https://support.google.com/analytics/answer/1247839"><?php echo esc_html__( 'About the Referral Traffic report', 'google-analytics-for-wordpress');?></a></li> 
  235. <li><a href="https://support.google.com/analytics/answer/1033173"><?php echo esc_html__( 'About traffic source dimensions', 'google-analytics-for-wordpress');?></a></li> 
  236. <li><a href="https://support.google.com/adwords/answer/1752125"><?php echo esc_html__( 'AdWords Auto-Tagging', 'google-analytics-for-wordpress');?></a></li> 
  237. </ul> 
  238. <?php 
  239. echo ob_get_clean(); 
  240.  
  241. function monsterinsights_tools_settings_tab() { 
  242. ob_start();?> 
  243. <h2><?php echo esc_html__( 'Setting Tools', 'google-analytics-for-wordpress' );?></h2> 
  244. <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> 
  245. <br /> 
  246. <table class="form-table"> 
  247. <tbody> 
  248. <tr> 
  249. <th scope="row"> 
  250. <label for="monsterinsights-import-settings"> 
  251. <?php echo esc_html__( 'Import Settings', 'google-analytics-for-wordpress' );?> 
  252. </label> 
  253. </th> 
  254. <td> 
  255. <?php  
  256. if ( ! empty( $_REQUEST['action'] ) && ! empty( $_REQUEST['result'] ) && $_REQUEST['action'] === 'import' && $_REQUEST['result'] === 'success' ) { 
  257. echo MonsterInsights()->notices->display_inline_notice( 'monsterinsights_standard_notice', '', __( 'Successfully imported settings!', 'google-analytics-for-wordpress'), 'success', false, array() ); 
  258. ?> 
  259. <form method="post" enctype="multipart/form-data"> 
  260. <p> 
  261. <input type="file" name="import_file"/> 
  262. <input type="hidden" name="monsterinsights_action" value="monsterinsights_import_settings" /> 
  263. </p> 
  264. <p class="description"><?php echo esc_html__( 'Paste the import field content from another MonsterInsights site in above.', 'google-analytics-for-wordpress' );?></p> 
  265. <p> 
  266. <?php wp_nonce_field( 'monsterinsights_import_settings', 'monsterinsights_import_settings' ); ?> 
  267. </p> 
  268. <p> 
  269. <?php submit_button( __( 'Import', 'google-analytics-for-wordpress' ), 'monsterinsights-action-button button button-action', 'submit', false ); ?> 
  270. </p> 
  271. </form> 
  272. </td> 
  273. </tr> 
  274. <tr> 
  275. <th scope="row"> 
  276. <label for="monsterinsights-export-settings"> 
  277. <?php echo esc_html__( 'Export Settings:', 'google-analytics-for-wordpress' );?> 
  278. </label> 
  279. </th> 
  280. <td> 
  281. <form method="post" enctype="multipart/form-data"> 
  282. <p> 
  283. <input type="hidden" name="monsterinsights_action" value="monsterinsights_export_settings" /> 
  284. </p> 
  285. <p> 
  286. <?php wp_nonce_field( 'monsterinsights_export_settings', 'monsterinsights_export_settings' ); ?> 
  287. </p> 
  288. <p> 
  289. <?php submit_button( __( 'Export', 'google-analytics-for-wordpress' ), 'monsterinsights-settings-export monsterinsights-action-button button button-action', 'submit', false ); ?> 
  290. </p> 
  291. </form> 
  292. </td> 
  293. </tr> 
  294.  
  295. </tbody> 
  296. </table> 
  297. <?php 
  298. echo ob_get_clean(); 
  299.  
  300. /** 
  301. * MonsterInsights settings export. 
  302. * 
  303. * @since 6.0.0 
  304. * @access public 
  305. * 
  306. * @return void 
  307. */ 
  308. function monsterinsights_process_export_settings() { 
  309. if ( !isset( $_POST['monsterinsights_action'] ) || empty( $_POST['monsterinsights_action'] ) ) { 
  310. return; 
  311.  
  312. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  313. return; 
  314.  
  315. if ( $_POST['monsterinsights_action'] !== 'monsterinsights_export_settings' ) { 
  316. return; 
  317.  
  318. if ( empty( $_POST['monsterinsights_export_settings'] ) || ! wp_verify_nonce( $_POST['monsterinsights_export_settings'], 'monsterinsights_export_settings' ) ) { 
  319. return; 
  320.  
  321. $settings = monsterinsights_export_settings(); 
  322. ignore_user_abort( true ); 
  323.  
  324. nocache_headers(); 
  325. header( 'Content-Type: application/json; charset=utf-8' ); 
  326. header( 'Content-Disposition: attachment; filename=monsterinsights-settings-export-' . date( 'm-d-Y' ) . '.json' ); 
  327. header( "Expires: 0" ); 
  328.  
  329. echo $settings; 
  330. exit; 
  331. add_action( 'admin_init', 'monsterinsights_process_export_settings' ); 
  332.  
  333. function monsterinsights_import_settings() { 
  334.  
  335. if ( !isset( $_POST['monsterinsights_action'] ) || empty( $_POST['monsterinsights_action'] ) ) { 
  336. return; 
  337.  
  338. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  339. return; 
  340.  
  341. if ( $_POST['monsterinsights_action'] !== 'monsterinsights_import_settings' ) { 
  342. return; 
  343.  
  344. if ( !wp_verify_nonce( $_POST['monsterinsights_import_settings'], 'monsterinsights_import_settings' ) ) { 
  345. return; 
  346.  
  347. $extension = explode( '.', $_FILES['import_file']['name'] ); 
  348. $extension = end( $extension ); 
  349.  
  350. if ( $extension != 'json' ) { 
  351. wp_die( __( 'Please upload a valid .json file', 'google-analytics-for-wordpress' ) ); 
  352.  
  353. $import_file = $_FILES['import_file']['tmp_name']; 
  354.  
  355. if ( empty( $import_file ) ) { 
  356. wp_die( __( 'Please upload a file to import', 'google-analytics-for-wordpress' ) ); 
  357.  
  358. $file = file_get_contents( $import_file ); 
  359. if ( empty( $file ) ) { 
  360. wp_die( __( 'Please upload a real settings export file to import', 'google-analytics-for-wordpress' ) ); 
  361.  
  362. // Retrieve the settings from the file and convert the json object to an array. 
  363. $new_settings = json_decode( wp_json_encode( json_decode( $file ) ), true ); 
  364. $settings = monsterinsights_get_options(); 
  365. $exclude = array(  
  366. 'analytics_profile',  
  367. 'analytics_profile_code',  
  368. 'analytics_profile_name',  
  369. 'oauth_version',  
  370. 'cron_last_run',  
  371. 'monsterinsights_oauth_status',  
  372. ); 
  373.  
  374. foreach ( $exclude as $e ) { 
  375. if ( ! empty( $new_settings[ $e ] ) ) { 
  376. unset( $new_settings[ $e ] ); 
  377.  
  378. if ( ! is_super_admin() ) { 
  379. if ( ! empty( $new_settings[ 'custom_code' ] ) ) { 
  380. unset( $new_settings[ 'custom_code' ] ); 
  381.  
  382. foreach ( $exclude as $e ) { 
  383. if ( ! empty( $settings[ $e ] ) ) { 
  384. $new_settings = $settings[ $e ]; 
  385.  
  386. global $monsterinsights_settings; 
  387. $monsterinsights_settings = $new_settings; 
  388.  
  389. update_option( monsterinsights_get_option_name(), $new_settings ); 
  390. wp_safe_redirect( admin_url( 'admin.php?page=monsterinsights_tools&action=import&result=success#monsterinsights-main-tab-settings' ) ); exit; 
  391. add_action( 'admin_init', 'monsterinsights_import_settings' ); 
.