Jetpack_JSON_API_Themes_New_Endpoint

The WordPress Core Jetpack JSON API Themes New Endpoint class.

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-themes-new-endpoint.php  
  1. class Jetpack_JSON_API_Themes_New_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { 
  2.  
  3. // POST /sites/%s/themes/%s/install 
  4. protected $needed_capabilities = 'install_themes'; 
  5. protected $action = 'install'; 
  6. protected $download_links = array(); 
  7.  
  8. protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { 
  9. $validate = parent::validate_call( $_blog_id, $capability, $check_manage_active ); 
  10. if ( is_wp_error( $validate ) ) { 
  11. // Lets delete the attachment... if the user doesn't have the right permissions to do things. 
  12. $args = $this->input(); 
  13. if ( isset( $args['zip'][0]['id'] ) ) { 
  14. wp_delete_attachment( $args['zip'][0]['id'], true ); 
  15.  
  16. return $validate; 
  17.  
  18. protected function validate_input( $theme ) { 
  19. $this->bulk = false; 
  20. $this->themes = array(); 
  21.  
  22. function install() { 
  23. $args = $this->input(); 
  24.  
  25. if ( isset( $args['zip'][0]['id'] ) ) { 
  26. $attachment_id = $args['zip'][0]['id']; 
  27. $local_file = get_attached_file( $attachment_id ); 
  28. if ( ! $local_file ) { 
  29. return new WP_Error( 'local-file-does-not-exist' ); 
  30. $skin = new Jetpack_Automatic_Install_Skin(); 
  31. $upgrader = new Theme_Upgrader( $skin ); 
  32.  
  33. $pre_install_list = wp_get_themes(); 
  34. $result = $upgrader->install( $local_file ); 
  35.  
  36. // clean up. 
  37. wp_delete_attachment( $attachment_id, true ); 
  38.  
  39. if ( is_wp_error( $result ) ) { 
  40. return $result; 
  41.  
  42. $after_install_list = wp_get_themes(); 
  43. $plugin = array_values( array_diff( array_keys( $after_install_list ), array_keys( $pre_install_list ) ) ); 
  44.  
  45. if ( ! $result ) { 
  46. $error_code = $upgrader->skin->get_main_error_code(); 
  47. $message = $upgrader->skin->get_main_error_message(); 
  48. if ( empty( $message ) ) { 
  49. $message = __( 'An unknown error occurred during installation', 'jetpack' ); 
  50.  
  51. if ( 'download_failed' === $error_code ) { 
  52. $error_code = 'no_package'; 
  53.  
  54. return new WP_Error( $error_code, $message, 400 ); 
  55.  
  56. if ( empty( $plugin ) ) { 
  57. return new WP_Error( 'theme_already_installed' ); 
  58.  
  59. $this->themes = $plugin; 
  60. $this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages(); 
  61.  
  62. return true; 
  63.  
  64. return new WP_Error( 'no_theme_installed' );