wp_ajax_update_plugin

Ajax handler for updating a plugin.

Description

wp_ajax_update_plugin(); 

Usage

  1. if ( !function_exists( 'wp_ajax_update_plugin' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_update_plugin(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_update_plugin() { 
  2. global $wp_filesystem; 
  3.  
  4. $plugin = urldecode( $_POST['plugin'] ); 
  5.  
  6. $status = array( 
  7. 'update' => 'plugin',  
  8. 'plugin' => $plugin,  
  9. 'slug' => sanitize_key( $_POST['slug'] ),  
  10. 'oldVersion' => '',  
  11. 'newVersion' => '',  
  12. ); 
  13.  
  14. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  15. if ( $plugin_data['Version'] ) { 
  16. $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  17.  
  18. if ( ! current_user_can( 'update_plugins' ) ) { 
  19. $status[error] = __( 'You do not have sufficient permissions to update plugins for this site.' ); 
  20. wp_send_json_error( $status ); 
  21.  
  22. check_ajax_referer( 'updates' ); 
  23.  
  24. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  25.  
  26.  
  27. $skin = new Automatic_Upgrader_Skin(); 
  28. $upgrader = new Plugin_Upgrader( $skin ); 
  29. $result = $upgrader->bulk_upgrade( array( $plugin ) ); 
  30.  
  31. if ( is_array( $result ) && empty( $result[$plugin] ) && is_wp_error( $skin->result ) ) { 
  32. $result = $skin->result; 
  33.  
  34. if ( is_array( $result ) && !empty( $result[ $plugin ] ) ) { 
  35. $plugin_update_data = current( $result ); 
  36.  
  37. /** 
  38. * If the `update_plugins` site transient is empty (e.g. when you update 
  39. * two plugins in quick succession before the transient repopulates),  
  40. * this may be the return. 
  41. * Preferably something can be done to ensure `update_plugins` isn't empty. 
  42. * For now, surface some sort oferrorhere. 
  43. */ 
  44. if ( $plugin_update_data === true ) { 
  45. $status[error] = __( 'Plugin update failed.' ); 
  46. wp_send_json_error( $status ); 
  47.  
  48. $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] ); 
  49. $plugin_data = reset( $plugin_data ); 
  50.  
  51. if ( $plugin_data['Version'] ) { 
  52. $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  53.  
  54. wp_send_json_success( $status ); 
  55. } else if ( is_wp_error( $result ) ) { 
  56. $status[error] = $result->get_error_message(); 
  57. wp_send_json_error( $status ); 
  58.  
  59. } else if ( is_bool( $result ) && ! $result ) { 
  60. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  61. $status[error] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  62.  
  63. // Pass through theerrorfrom WP_Filesystem if one was raised 
  64. if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  65. $status[error] = $wp_filesystem->errors->get_error_message(); 
  66.  
  67. wp_send_json_error( $status ); 
  68.  
  69. } else { 
  70. // An unhandlederroroccured 
  71. $status[error] = __( 'Plugin update failed.' ); 
  72. wp_send_json_error( $status );