wp_ajax_delete_plugin

Ajax handler for deleting a plugin.

Description

wp_ajax_delete_plugin(); 

Usage

  1. if ( !function_exists( 'wp_ajax_delete_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_delete_plugin(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_delete_plugin() { 
  2. check_ajax_referer( 'updates' ); 
  3.  
  4. if ( empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) { 
  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. 'delete' => 'plugin',  
  14. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  15. ); 
  16.  
  17. if ( ! current_user_can( 'delete_plugins' ) || 0 !== validate_file( $plugin ) ) { 
  18. $status['errorMessage'] = __( 'Sorry, you are not allowed to delete plugins for this site.' ); 
  19. wp_send_json_error( $status ); 
  20.  
  21. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  22. $status['plugin'] = $plugin; 
  23. $status['pluginName'] = $plugin_data['Name']; 
  24.  
  25. if ( is_plugin_active( $plugin ) ) { 
  26. $status['errorMessage'] = __( 'You cannot delete a plugin while it is active on the main site.' ); 
  27. wp_send_json_error( $status ); 
  28.  
  29. // Check filesystem credentials. `delete_plugins()` will bail otherwise. 
  30. $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&checked[]=' . $plugin, 'bulk-plugins' ); 
  31. ob_start(); 
  32. $credentials = request_filesystem_credentials( $url ); 
  33. ob_end_clean(); 
  34. if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 
  35. global $wp_filesystem; 
  36.  
  37. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  38. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  39.  
  40. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  41. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  42. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  43.  
  44. wp_send_json_error( $status ); 
  45.  
  46. $result = delete_plugins( array( $plugin ) ); 
  47.  
  48. if ( is_wp_error( $result ) ) { 
  49. $status['errorMessage'] = $result->get_error_message(); 
  50. wp_send_json_error( $status ); 
  51. } elseif ( false === $result ) { 
  52. $status['errorMessage'] = __( 'Plugin could not be deleted.' ); 
  53. wp_send_json_error( $status ); 
  54.  
  55. wp_send_json_success( $status );