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. check_ajax_referer( 'updates' ); 
  3.  
  4. if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) { 
  5. 'slug' => '',  
  6. 'errorCode' => 'no_plugin_specified',  
  7. 'errorMessage' => __( 'No plugin specified.' ),  
  8. ) ); 
  9.  
  10. $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ); 
  11.  
  12. $status = array( 
  13. 'update' => 'plugin',  
  14. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  15. 'oldVersion' => '',  
  16. 'newVersion' => '',  
  17. ); 
  18.  
  19. if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) { 
  20. $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' ); 
  21. wp_send_json_error( $status ); 
  22.  
  23. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  24. $status['plugin'] = $plugin; 
  25. $status['pluginName'] = $plugin_data['Name']; 
  26.  
  27. if ( $plugin_data['Version'] ) { 
  28. /** translators: %s: Plugin version */ 
  29. $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  30.  
  31. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  32.  
  33.  
  34. $skin = new WP_Ajax_Upgrader_Skin(); 
  35. $upgrader = new Plugin_Upgrader( $skin ); 
  36. $result = $upgrader->bulk_upgrade( array( $plugin ) ); 
  37.  
  38. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  39. $status['debug'] = $skin->get_upgrade_messages(); 
  40.  
  41. if ( is_wp_error( $skin->result ) ) { 
  42. $status['errorCode'] = $skin->result->get_error_code(); 
  43. $status['errorMessage'] = $skin->result->get_error_message(); 
  44. wp_send_json_error( $status ); 
  45. } elseif ( $skin->get_errors()->get_error_code() ) { 
  46. $status['errorMessage'] = $skin->get_error_messages(); 
  47. wp_send_json_error( $status ); 
  48. } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) { 
  49. $plugin_update_data = current( $result ); 
  50.  
  51. /** 
  52. * If the `update_plugins` site transient is empty (e.g. when you update 
  53. * two plugins in quick succession before the transient repopulates),  
  54. * this may be the return. 
  55. * Preferably something can be done to ensure `update_plugins` isn't empty. 
  56. * For now, surface some sort oferrorhere. 
  57. */ 
  58. if ( true === $plugin_update_data ) { 
  59. $status['errorMessage'] = __( 'Plugin update failed.' ); 
  60. wp_send_json_error( $status ); 
  61.  
  62. $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] ); 
  63. $plugin_data = reset( $plugin_data ); 
  64.  
  65. if ( $plugin_data['Version'] ) { 
  66. /** translators: %s: Plugin version */ 
  67. $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 
  68. wp_send_json_success( $status ); 
  69. } elseif ( false === $result ) { 
  70. global $wp_filesystem; 
  71.  
  72. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  73. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  74.  
  75. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  76. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  77. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  78.  
  79. wp_send_json_error( $status ); 
  80.  
  81. // An unhandlederroroccurred. 
  82. $status['errorMessage'] = __( 'Plugin update failed.' ); 
  83. wp_send_json_error( $status );