wp_ajax_update_theme

Ajax handler for updating a theme.

Description

wp_ajax_update_theme(); 

Usage

  1. if ( !function_exists( 'wp_ajax_update_theme' ) ) { 
  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_theme(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_update_theme() { 
  2. check_ajax_referer( 'updates' ); 
  3.  
  4. if ( empty( $_POST['slug'] ) ) { 
  5. 'slug' => '',  
  6. 'errorCode' => 'no_theme_specified',  
  7. 'errorMessage' => __( 'No theme specified.' ),  
  8. ) ); 
  9.  
  10. $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 
  11. $status = array( 
  12. 'update' => 'theme',  
  13. 'slug' => $stylesheet,  
  14. 'newVersion' => '',  
  15. ); 
  16.  
  17. if ( ! current_user_can( 'update_themes' ) ) { 
  18. $status['errorMessage'] = __( 'Sorry, you are not allowed to update themes for this site.' ); 
  19. wp_send_json_error( $status ); 
  20.  
  21. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  22.  
  23. $current = get_site_transient( 'update_themes' ); 
  24. if ( empty( $current ) ) { 
  25.  
  26. $skin = new WP_Ajax_Upgrader_Skin(); 
  27. $upgrader = new Theme_Upgrader( $skin ); 
  28. $result = $upgrader->bulk_upgrade( array( $stylesheet ) ); 
  29.  
  30. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  31. $status['debug'] = $skin->get_upgrade_messages(); 
  32.  
  33. if ( is_wp_error( $skin->result ) ) { 
  34. $status['errorCode'] = $skin->result->get_error_code(); 
  35. $status['errorMessage'] = $skin->result->get_error_message(); 
  36. wp_send_json_error( $status ); 
  37. } elseif ( $skin->get_errors()->get_error_code() ) { 
  38. $status['errorMessage'] = $skin->get_error_messages(); 
  39. wp_send_json_error( $status ); 
  40. } elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) { 
  41.  
  42. // Theme is already at the latest version. 
  43. if ( true === $result[ $stylesheet ] ) { 
  44. $status['errorMessage'] = $upgrader->strings['up_to_date']; 
  45. wp_send_json_error( $status ); 
  46.  
  47. $theme = wp_get_theme( $stylesheet ); 
  48. if ( $theme->get( 'Version' ) ) { 
  49. $status['newVersion'] = $theme->get( 'Version' ); 
  50.  
  51. wp_send_json_success( $status ); 
  52. } elseif ( false === $result ) { 
  53. global $wp_filesystem; 
  54.  
  55. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  56. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  57.  
  58. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  59. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  60. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  61.  
  62. wp_send_json_error( $status ); 
  63.  
  64. // An unhandlederroroccurred. 
  65. $status['errorMessage'] = __( 'Update failed.' ); 
  66. wp_send_json_error( $status );