Jetpack_JSON_API_Plugins_New_Endpoint

The WordPress Core Jetpack JSON API Plugins New Endpoint class.

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-plugins-new-endpoint.php  
  1. class Jetpack_JSON_API_Plugins_New_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { 
  2.  
  3. // POST /sites/%s/plugins/new 
  4. protected $needed_capabilities = 'install_plugins'; 
  5. protected $action = 'install'; 
  6.  
  7. protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { 
  8. $validate = parent::validate_call( $_blog_id, $capability, $check_manage_active ); 
  9. if ( is_wp_error( $validate ) ) { 
  10.  
  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. // no need to try to validate the plugin since we didn't pass one in. 
  19. protected function validate_input( $plugin ) { 
  20. $this->bulk = false; 
  21. $this->plugins = array(); 
  22.  
  23. function install() { 
  24. $args = $this->input(); 
  25.  
  26. if ( isset( $args['zip'][0]['id'] ) ) { 
  27. $plugin_attachment_id = $args['zip'][0]['id']; 
  28. $local_file = get_attached_file( $plugin_attachment_id ); 
  29. if ( ! $local_file ) { 
  30. return new WP_Error( 'local-file-does-not-exist' ); 
  31. $skin = new Jetpack_Automatic_Install_Skin(); 
  32. $upgrader = new Plugin_Upgrader( $skin ); 
  33.  
  34. $pre_install_plugin_list = get_plugins(); 
  35. $result = $upgrader->install( $local_file ); 
  36.  
  37. // clean up. 
  38. wp_delete_attachment( $plugin_attachment_id, true ); 
  39.  
  40. if ( is_wp_error( $result ) ) { 
  41. return $result; 
  42.  
  43. $after_install_plugin_list = get_plugins(); 
  44. $plugin = array_values( array_diff( array_keys( $after_install_plugin_list ), array_keys( $pre_install_plugin_list ) ) ); 
  45.  
  46. if ( ! $result ) { 
  47. $error_code = $upgrader->skin->get_main_error_code(); 
  48. $message = $upgrader->skin->get_main_error_message(); 
  49. if ( empty( $message ) ) { 
  50. $message = __( 'An unknown error occurred during installation', 'jetpack' ); 
  51.  
  52. if ( 'download_failed' === $error_code ) { 
  53. $error_code = 'no_package'; 
  54.  
  55. return new WP_Error( $error_code, $message, 400 ); 
  56.  
  57. if ( empty( $plugin ) ) { 
  58. return new WP_Error( 'plugin_already_installed' ); 
  59.  
  60. $this->plugins = $plugin; 
  61. $this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages(); 
  62.  
  63. return true; 
  64.  
  65. return new WP_Error( 'no_plugin_installed' );