monsterinsights_ajax_install_addon

Installs a MonsterInsights addon.

Description

monsterinsights_ajax_install_addon(); 

Usage

  1. if ( !function_exists( 'monsterinsights_ajax_install_addon' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-analytics-for-wordpress-by-monsterinsights/includes/admin/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = monsterinsights_ajax_install_addon(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/admin/ajax.php  
  1. function monsterinsights_ajax_install_addon() { 
  2.  
  3. // Run a security check first. 
  4. check_ajax_referer( 'monsterinsights-install', 'nonce' ); 
  5.  
  6. if ( ! current_user_can( 'install_plugins' ) ) { 
  7. echo json_encode( true ); 
  8.  
  9. // Install the addon. 
  10. if ( isset( $_POST['plugin'] ) ) { 
  11. $download_url = $_POST['plugin']; 
  12. global $hook_suffix; 
  13.  
  14. // Set the current screen to avoid undefined notices. 
  15. set_current_screen(); 
  16.  
  17. // Prepare variables. 
  18. $method = ''; 
  19. $url = add_query_arg( 
  20. array( 
  21. 'page' => 'monsterinsights-settings' 
  22. ),  
  23. admin_url( 'admin.php' ) 
  24. ); 
  25. $url = esc_url( $url ); 
  26.  
  27. // Start output bufferring to catch the filesystem form if credentials are needed. 
  28. ob_start(); 
  29. if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) { 
  30. $form = ob_get_clean(); 
  31. echo json_encode( array( 'form' => $form ) ); 
  32. wp_die(); 
  33.  
  34. // If we are not authenticated, make it happen now. 
  35. if ( ! WP_Filesystem( $creds ) ) { 
  36. ob_start(); 
  37. request_filesystem_credentials( $url, $method, true, false, null ); 
  38. $form = ob_get_clean(); 
  39. echo json_encode( array( 'form' => $form ) ); 
  40. wp_die(); 
  41.  
  42. // We do not need any extra credentials if we have gotten this far, so let's install the plugin. 
  43. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  44. $base = MonsterInsights(); 
  45. require_once plugin_dir_path( $base->file ) . '/includes/admin/licensing/skin.php'; 
  46.  
  47. // Create the plugin upgrader with our custom skin. 
  48. $installer = new Plugin_Upgrader( $skin = new MonsterInsights_Skin() ); 
  49. $installer->install( $download_url ); 
  50.  
  51. // Flush the cache and return the newly installed plugin basename. 
  52. wp_cache_flush(); 
  53. if ( $installer->plugin_info() ) { 
  54. $plugin_basename = $installer->plugin_info(); 
  55. echo json_encode( array( 'plugin' => $plugin_basename ) ); 
  56. wp_die(); 
  57.  
  58. // Send back a response. 
  59. echo json_encode( true ); 
  60. wp_die(); 
  61.