ajax.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * Stores a user setting for the logged in WordPress User
  17. *
  18. * @access public
  19. * @since 6.0.0
  20. */
  21. function monsterinsights_ajax_set_user_setting() {
  22. // Run a security check first.
  23. check_ajax_referer( 'monsterinsights-set-user-setting', 'nonce' );
  24. // Prepare variables.
  25. $name = stripslashes( $_POST['name'] );
  26. $value = stripslashes( $_POST['value'] );
  27. // Set user setting.
  28. set_user_setting( $name, $value );
  29. // Send back the response.
  30. wp_send_json_success();
  31. wp_die();
  32. }
  33. add_action( 'wp_ajax_monsterinsights_install_addon', 'monsterinsights_ajax_install_addon' );
  34. /**
  35. * Installs a MonsterInsights addon.
  36. *
  37. * @access public
  38. * @since 6.0.0
  39. */
  40. function monsterinsights_ajax_install_addon() {
  41. // Run a security check first.
  42. check_ajax_referer( 'monsterinsights-install', 'nonce' );
  43. if ( ! current_user_can( 'install_plugins' ) ) {
  44. echo json_encode( true );
  45. }
  46. // Install the addon.
  47. if ( isset( $_POST['plugin'] ) ) {
  48. $download_url = $_POST['plugin'];
  49. global $hook_suffix;
  50. // Set the current screen to avoid undefined notices.
  51. set_current_screen();
  52. // Prepare variables.
  53. $method = '';
  54. $url = add_query_arg(
  55. array(
  56. 'page' => 'monsterinsights-settings'
  57. ),
  58. admin_url( 'admin.php' )
  59. );
  60. $url = esc_url( $url );
  61. // Start output bufferring to catch the filesystem form if credentials are needed.
  62. ob_start();
  63. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) {
  64. $form = ob_get_clean();
  65. echo json_encode( array( 'form' => $form ) );
  66. wp_die();
  67. }
  68. // If we are not authenticated, make it happen now.
  69. if ( ! WP_Filesystem( $creds ) ) {
  70. ob_start();
  71. request_filesystem_credentials( $url, $method, true, false, null );
  72. $form = ob_get_clean();
  73. echo json_encode( array( 'form' => $form ) );
  74. wp_die();
  75. }
  76. // We do not need any extra credentials if we have gotten this far, so let's install the plugin.
  77. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  78. $base = MonsterInsights();
  79. require_once plugin_dir_path( $base->file ) . '/includes/admin/licensing/skin.php';
  80. // Create the plugin upgrader with our custom skin.
  81. $installer = new Plugin_Upgrader( $skin = new MonsterInsights_Skin() );
  82. $installer->install( $download_url );
  83. // Flush the cache and return the newly installed plugin basename.
  84. wp_cache_flush();
  85. if ( $installer->plugin_info() ) {
  86. $plugin_basename = $installer->plugin_info();
  87. echo json_encode( array( 'plugin' => $plugin_basename ) );
  88. wp_die();
  89. }
  90. }
  91. // Send back a response.
  92. echo json_encode( true );
  93. wp_die();
  94. }
  95. add_action( 'wp_ajax_monsterinsights_activate_addon', 'monsterinsights_ajax_activate_addon' );
  96. /**
  97. * Activates a MonsterInsights addon.
  98. *
  99. * @access public
  100. * @since 6.0.0
  101. */
  102. function monsterinsights_ajax_activate_addon() {
  103. // Run a security check first.
  104. check_ajax_referer( 'monsterinsights-activate', 'nonce' );
  105. if ( ! current_user_can( 'activate_plugins' ) ) {
  106. echo json_encode( true );
  107. }
  108. // Activate the addon.
  109. if ( isset( $_POST['plugin'] ) ) {
  110. if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) {
  111. $activate = activate_plugin( $_POST['plugin'], NULL, true );
  112. } else {
  113. $activate = activate_plugin( $_POST['plugin'] );
  114. }
  115. if ( is_wp_error( $activate ) ) {
  116. echo json_encode( array( 'error' => $activate->get_error_message() ) );
  117. wp_die();
  118. }
  119. }
  120. echo json_encode( true );
  121. wp_die();
  122. }
  123. add_action( 'wp_ajax_monsterinsights_deactivate_addon', 'monsterinsights_ajax_deactivate_addon' );
  124. /**
  125. * Deactivates a MonsterInsights addon.
  126. *
  127. * @access public
  128. * @since 6.0.0
  129. */
  130. function monsterinsights_ajax_deactivate_addon() {
  131. // Run a security check first.
  132. check_ajax_referer( 'monsterinsights-deactivate', 'nonce' );
  133. if ( ! current_user_can( 'activate_plugins' ) ) {
  134. echo json_encode( true );
  135. }
  136. // Deactivate the addon.
  137. if ( isset( $_POST['plugin'] ) ) {
  138. if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) {
  139. $deactivate = deactivate_plugins( $_POST['plugin'], false, true );
  140. } else {
  141. $deactivate = deactivate_plugins( $_POST['plugin'] );
  142. }
  143. }
  144. echo json_encode( true );
  145. wp_die();
  146. }
  147. /**
  148. * Called whenever a notice is dismissed in MonsterInsights or its Addons.
  149. *
  150. * Updates a key's value in the options table to mark the notice as dismissed,
  151. * preventing it from displaying again
  152. *
  153. * @access public
  154. * @since 6.0.0
  155. */
  156. function monsterinsights_ajax_dismiss_notice() {
  157. // Run a security check first.
  158. check_ajax_referer( 'monsterinsights-dismiss-notice', 'nonce' );
  159. // Deactivate the notice
  160. if ( isset( $_POST['notice'] ) ) {
  161. // Init the notice class and mark notice as deactivated
  162. MonsterInsights()->notices->dismiss( $_POST['notice'] );
  163. // Return true
  164. echo json_encode( true );
  165. wp_die();
  166. }
  167. // If here, an error occurred
  168. echo json_encode( false );
  169. wp_die();
  170. }
  171. add_action( 'wp_ajax_monsterinsights_ajax_dismiss_notice', 'monsterinsights_ajax_dismiss_notice' );