/includes/admin/ajax.php

  1. <?php 
  2. /** 
  3. * Handles all admin ajax interactions for the MonsterInsights plugin. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @subpackage Ajax 
  9. * @author Chris Christoff 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. /** 
  17. * Stores a user setting for the logged in WordPress User 
  18. * 
  19. * @access public 
  20. * @since 6.0.0 
  21. */ 
  22. function monsterinsights_ajax_set_user_setting() { 
  23.  
  24. // Run a security check first. 
  25. check_ajax_referer( 'monsterinsights-set-user-setting', 'nonce' ); 
  26.  
  27. // Prepare variables. 
  28. $name = stripslashes( $_POST['name'] ); 
  29. $value = stripslashes( $_POST['value'] ); 
  30.  
  31. // Set user setting. 
  32. set_user_setting( $name, $value ); 
  33.  
  34. // Send back the response. 
  35. wp_send_json_success(); 
  36. wp_die(); 
  37.  
  38. add_action( 'wp_ajax_monsterinsights_install_addon', 'monsterinsights_ajax_install_addon' ); 
  39.  
  40. /** 
  41. * Installs a MonsterInsights addon. 
  42. * 
  43. * @access public 
  44. * @since 6.0.0 
  45. */ 
  46. function monsterinsights_ajax_install_addon() { 
  47.  
  48. // Run a security check first. 
  49. check_ajax_referer( 'monsterinsights-install', 'nonce' ); 
  50.  
  51. if ( ! current_user_can( 'install_plugins' ) ) { 
  52. echo json_encode( true ); 
  53.  
  54. // Install the addon. 
  55. if ( isset( $_POST['plugin'] ) ) { 
  56. $download_url = $_POST['plugin']; 
  57. global $hook_suffix; 
  58.  
  59. // Set the current screen to avoid undefined notices. 
  60. set_current_screen(); 
  61.  
  62. // Prepare variables. 
  63. $method = ''; 
  64. $url = add_query_arg( 
  65. array( 
  66. 'page' => 'monsterinsights-settings' 
  67. ),  
  68. admin_url( 'admin.php' ) 
  69. ); 
  70. $url = esc_url( $url ); 
  71.  
  72. // Start output bufferring to catch the filesystem form if credentials are needed. 
  73. ob_start(); 
  74. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) { 
  75. $form = ob_get_clean(); 
  76. echo json_encode( array( 'form' => $form ) ); 
  77. wp_die(); 
  78.  
  79. // If we are not authenticated, make it happen now. 
  80. if ( ! WP_Filesystem( $creds ) ) { 
  81. ob_start(); 
  82. request_filesystem_credentials( $url, $method, true, false, null ); 
  83. $form = ob_get_clean(); 
  84. echo json_encode( array( 'form' => $form ) ); 
  85. wp_die(); 
  86.  
  87. // We do not need any extra credentials if we have gotten this far, so let's install the plugin. 
  88. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  89. $base = MonsterInsights(); 
  90. require_once plugin_dir_path( $base->file ) . '/includes/admin/licensing/skin.php'; 
  91.  
  92. // Create the plugin upgrader with our custom skin. 
  93. $installer = new Plugin_Upgrader( $skin = new MonsterInsights_Skin() ); 
  94. $installer->install( $download_url ); 
  95.  
  96. // Flush the cache and return the newly installed plugin basename. 
  97. wp_cache_flush(); 
  98. if ( $installer->plugin_info() ) { 
  99. $plugin_basename = $installer->plugin_info(); 
  100. echo json_encode( array( 'plugin' => $plugin_basename ) ); 
  101. wp_die(); 
  102.  
  103. // Send back a response. 
  104. echo json_encode( true ); 
  105. wp_die(); 
  106.  
  107.  
  108. add_action( 'wp_ajax_monsterinsights_activate_addon', 'monsterinsights_ajax_activate_addon' ); 
  109. /** 
  110. * Activates a MonsterInsights addon. 
  111. * 
  112. * @access public 
  113. * @since 6.0.0 
  114. */ 
  115. function monsterinsights_ajax_activate_addon() { 
  116.  
  117. // Run a security check first. 
  118. check_ajax_referer( 'monsterinsights-activate', 'nonce' ); 
  119.  
  120. if ( ! current_user_can( 'activate_plugins' ) ) { 
  121. echo json_encode( true ); 
  122.  
  123. // Activate the addon. 
  124. if ( isset( $_POST['plugin'] ) ) { 
  125. if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) { 
  126. $activate = activate_plugin( $_POST['plugin'], NULL, true ); 
  127. } else { 
  128. $activate = activate_plugin( $_POST['plugin'] ); 
  129.  
  130. if ( is_wp_error( $activate ) ) { 
  131. echo json_encode( array( 'error' => $activate->get_error_message() ) ); 
  132. wp_die(); 
  133.  
  134. echo json_encode( true ); 
  135. wp_die(); 
  136.  
  137.  
  138. add_action( 'wp_ajax_monsterinsights_deactivate_addon', 'monsterinsights_ajax_deactivate_addon' ); 
  139. /** 
  140. * Deactivates a MonsterInsights addon. 
  141. * 
  142. * @access public 
  143. * @since 6.0.0 
  144. */ 
  145. function monsterinsights_ajax_deactivate_addon() { 
  146.  
  147. // Run a security check first. 
  148. check_ajax_referer( 'monsterinsights-deactivate', 'nonce' ); 
  149.  
  150. if ( ! current_user_can( 'activate_plugins' ) ) { 
  151. echo json_encode( true ); 
  152.  
  153. // Deactivate the addon. 
  154. if ( isset( $_POST['plugin'] ) ) { 
  155. if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) {  
  156. $deactivate = deactivate_plugins( $_POST['plugin'], false, true ); 
  157. } else { 
  158. $deactivate = deactivate_plugins( $_POST['plugin'] ); 
  159.  
  160. echo json_encode( true ); 
  161. wp_die(); 
  162.  
  163. /** 
  164. * Called whenever a notice is dismissed in MonsterInsights or its Addons. 
  165. * 
  166. * Updates a key's value in the options table to mark the notice as dismissed,  
  167. * preventing it from displaying again 
  168. * 
  169. * @access public 
  170. * @since 6.0.0 
  171. */ 
  172. function monsterinsights_ajax_dismiss_notice() { 
  173.  
  174. // Run a security check first. 
  175. check_ajax_referer( 'monsterinsights-dismiss-notice', 'nonce' ); 
  176.  
  177. // Deactivate the notice 
  178. if ( isset( $_POST['notice'] ) ) { 
  179. // Init the notice class and mark notice as deactivated 
  180. MonsterInsights()->notices->dismiss( $_POST['notice'] ); 
  181.  
  182. // Return true 
  183. echo json_encode( true ); 
  184. wp_die(); 
  185.  
  186. // If here, an error occured 
  187. echo json_encode( false ); 
  188. wp_die(); 
  189.  
  190. add_action( 'wp_ajax_monsterinsights_ajax_dismiss_notice', 'monsterinsights_ajax_dismiss_notice' ); 
  191.  
  192.  
  193. function monsterinsights_get_shortlink() { 
  194. // Run a security check first. 
  195. check_ajax_referer( 'mi-admin-nonce', 'nonce' ); 
  196.  
  197. $shorten = ! empty( $_POST['url'] ) ? esc_url_raw( $_POST['url'] ) : ''; 
  198. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  199. echo $shorten; 
  200. wp_die(); 
  201.  
  202. $url = 'https://www.googleapis.com/urlshortener/v1/url'; 
  203.  
  204. // If no url passed die 
  205. if ( ! $shorten ) { 
  206. echo $shorten; 
  207. wp_die(); 
  208.  
  209. // if the url is already shortened, don't re-run 
  210. if ( strpos( $shorten, 'goo.g') !== false ) { 
  211. echo $shorten; 
  212. wp_die(); 
  213.  
  214. $result = wp_remote_post( 
  215. add_query_arg( 
  216. 'key',  
  217. 'AIzaSyCfHOlx8NbBVSpmHPqxophzULWSAzWDyio',  
  218. 'https://www.googleapis.com/urlshortener/v1/url' 
  219. ),  
  220. array( 
  221. 'body' => json_encode( array('longUrl' => esc_url_raw( $shorten ) ) ),  
  222. 'headers' => array( 'Content-Type' => 'application/json') 
  223. ); 
  224.  
  225. if ( is_wp_error( $result ) ) { 
  226. echo $shorten; 
  227. wp_die(); 
  228. $result = json_decode( $result['body'] ); 
  229. $shortlink = $result->id; 
  230. echo $shortlink; 
  231. wp_die(); 
  232. add_action( 'wp_ajax_monsterinsights_get_shortlink', 'monsterinsights_get_shortlink' ); 
.