Jetpack_JSON_API_Plugins_Install_Endpoint

The Jetpack by WordPress.com Jetpack JSON API Plugins Install Endpoint class.

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-plugins-install-endpoint.php  
  1. class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { 
  2.  
  3. // POST /sites/%s/plugins/%s/new 
  4. protected $needed_capabilities = 'install_plugins'; 
  5. protected $action = 'install'; 
  6. protected $download_links = array(); 
  7.  
  8. protected function install() { 
  9. foreach ( $this->plugins as $index => $slug ) { 
  10.  
  11. $skin = new Automatic_Upgrader_Skin(); 
  12. $upgrader = new Plugin_Upgrader( $skin ); 
  13.  
  14. $result = $upgrader->install( $this->download_links[ $slug ] ); 
  15.  
  16. if ( ! $this->bulk && is_wp_error( $result ) ) { 
  17. return $result; 
  18.  
  19. $plugin = self::get_plugin_id_by_slug( $slug ); 
  20.  
  21. if ( ! $plugin ) { 
  22. $error = $this->log[ $slug ]['error'] = __( 'There was an error installing your plugin', 'jetpack' ); 
  23.  
  24. if ( ! $this->bulk && ! $result ) { 
  25. $error = $this->log[ $slug ]['error'] = __( 'An unknown error occurred during installation', 'jetpack' ); 
  26.  
  27. $this->log[ $plugin ][] = $upgrader->skin->get_upgrade_messages(); 
  28.  
  29. if ( ! $this->bulk && isset( $error ) ) { 
  30. return new WP_Error( 'install_error', $this->log[ $slug ]['error'], 400 ); 
  31.  
  32. // replace the slug with the actual plugin id 
  33. $this->plugins[ $index ] = $plugin; 
  34.  
  35. return true; 
  36.  
  37. protected function validate_plugins() { 
  38. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { 
  39. return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ) ); 
  40. foreach( $this->plugins as $index => $slug ) { 
  41.  
  42. // make sure it is not already installed 
  43. if ( self::get_plugin_id_by_slug( $slug ) ) { 
  44. return new WP_Error( 'plugin_already_installed', __( 'The plugin is already installed', 'jetpack' ) ); 
  45.  
  46. $response = wp_remote_get( "http://api.wordpress.org/plugins/info/1.0/$slug" ); 
  47. $plugin_data = unserialize( $response['body'] ); 
  48. if ( is_wp_error( $plugin_data ) ) { 
  49. return $plugin_data; 
  50.  
  51. $this->download_links[ $slug ] = $plugin_data->download_link; 
  52.  
  53. return true; 
  54.  
  55. protected static function get_plugin_id_by_slug( $slug ) { 
  56. $plugins = get_plugins(); 
  57. if( ! is_array( $plugins ) ) { 
  58. return false; 
  59. foreach( $plugins as $id => $plugin_data ) { 
  60. if( strpos( $id, $slug ) !== false ) { 
  61. return $id; 
  62. return false;