Jetpack_JSON_API_Plugins_Modify_Endpoint

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

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-plugins-modify-endpoint.php  
  1. class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { 
  2. // POST /sites/%s/plugins/%s 
  3. // POST /sites/%s/plugins 
  4.  
  5. protected $needed_capabilities = 'activate_plugins'; 
  6. protected $action = 'default_action'; 
  7. protected $expected_actions = array( 'update', 'install', 'delete' ); 
  8.  
  9. public function callback( $path = '', $blog_id = 0, $object = null ) { 
  10. Jetpack_JSON_API_Endpoint::validate_input( $object ); 
  11. switch ( $this->action ) { 
  12. case 'update' : 
  13. $this->needed_capabilities = 'update_plugins'; 
  14. break; 
  15. case 'install' : 
  16. $this->needed_capabilities = 'install_plugins'; 
  17. break; 
  18. if ( isset( $args['autoupdate'] ) ) { 
  19. $this->needed_capabilities = 'update_plugins'; 
  20.  
  21. return parent::callback( $path, $blog_id, $object ); 
  22.  
  23. public function default_action() { 
  24. $args = $this->input(); 
  25.  
  26. if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) { 
  27. if ( $args['autoupdate'] ) { 
  28. $this->autoupdate_on(); 
  29. } else { 
  30. $this->autoupdate_off(); 
  31.  
  32. if ( isset( $args['active'] ) && is_bool( $args['active'] ) ) { 
  33. if ( $args['active'] ) { 
  34. return $this->activate(); 
  35. } else { 
  36. return $this->deactivate(); 
  37.  
  38. return true; 
  39.  
  40. protected function autoupdate_on() { 
  41. $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); 
  42. $autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) ); 
  43. Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins ); 
  44.  
  45. protected function autoupdate_off() { 
  46. $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); 
  47. $autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins ); 
  48. Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins ); 
  49.  
  50. protected function activate() { 
  51. foreach ( $this->plugins as $plugin ) { 
  52. if ( ( ! $this->network_wide && Jetpack::is_plugin_active( $plugin ) ) || is_plugin_active_for_network( $plugin ) ) { 
  53. $this->log[ $plugin ]['error'] = __( 'The Plugin is already active.', 'jetpack' ); 
  54. $has_errors = true; 
  55. continue; 
  56.  
  57. if ( ! $this->network_wide && is_network_only_plugin( $plugin ) && is_multisite() ) { 
  58. $this->log[ $plugin ]['error'] = __( 'Plugin can only be Network Activated', 'jetpack' ); 
  59. $has_errors = true; 
  60. continue; 
  61.  
  62. $result = activate_plugin( $plugin, '', $this->network_wide ); 
  63.  
  64. if ( is_wp_error( $result ) ) { 
  65. $this->log[ $plugin ]['error'] = $result->get_error_messages(); 
  66. $has_errors = true; 
  67. continue; 
  68.  
  69. $success = Jetpack::is_plugin_active( $plugin ); 
  70. if ( $success && $this->network_wide ) { 
  71. $success &= is_plugin_active_for_network( $plugin ); 
  72.  
  73. if ( ! $success ) { 
  74. $this->log[ $plugin ]['error'] = $result->get_error_messages; 
  75. $has_errors = true; 
  76. continue; 
  77. $this->log[ $plugin ][] = __( 'Plugin activated.', 'jetpack' ); 
  78. if ( ! $this->bulk && isset( $has_errors ) ) { 
  79. $plugin = $this->plugins[0]; 
  80. return new WP_Error( 'activation_error', $this->log[ $plugin ]['error'] ); 
  81.  
  82. protected function deactivate() { 
  83. foreach ( $this->plugins as $plugin ) { 
  84. if ( ! Jetpack::is_plugin_active( $plugin ) ) { 
  85. $error = $this->log[ $plugin ]['error'] = __( 'The Plugin is already deactivated.', 'jetpack' ); 
  86. continue; 
  87.  
  88. deactivate_plugins( $plugin, false, $this->network_wide ); 
  89.  
  90. $success = ! Jetpack::is_plugin_active( $plugin ); 
  91. if ( $success && $this->network_wide ) { 
  92. $success &= ! is_plugin_active_for_network( $plugin ); 
  93.  
  94. if ( ! $success ) { 
  95. $error = $this->log[ $plugin ]['error'] = __( 'There was an error deactivating your plugin', 'jetpack' ); 
  96. continue; 
  97. $this->log[ $plugin ][] = __( 'Plugin deactivated.', 'jetpack' ); 
  98. if ( ! $this->bulk && isset( $error ) ) { 
  99. return new WP_Error( 'deactivation_error', $error ); 
  100.  
  101. protected function update() { 
  102.  
  103. wp_clean_plugins_cache(); 
  104. ob_start(); 
  105. wp_update_plugins(); // Check for Plugin updates 
  106. ob_end_clean(); 
  107.  
  108. $update_plugins = get_site_transient( 'update_plugins' ); 
  109.  
  110. if ( isset( $update_plugins->response ) ) { 
  111. $plugin_updates_needed = array_keys( $update_plugins->response ); 
  112. } else { 
  113. $plugin_updates_needed = array(); 
  114.  
  115. $update_attempted = false; 
  116.  
  117. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 
  118.  
  119. // unhook this functions that output things before we send our response header. 
  120. remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); 
  121. remove_action( 'upgrader_process_complete', 'wp_version_check' ); 
  122. remove_action( 'upgrader_process_complete', 'wp_update_themes' ); 
  123.  
  124. foreach ( $this->plugins as $plugin ) { 
  125.  
  126. if ( ! in_array( $plugin, $plugin_updates_needed ) ) { 
  127. $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' ); 
  128. continue; 
  129.  
  130. $update_attempted = true; 
  131.  
  132. // Object created inside the for loop to clean the messages for each plugin 
  133. $skin = new Automatic_Upgrader_Skin(); 
  134. // The Automatic_Upgrader_Skin skin shouldn't output anything. 
  135. $upgrader = new Plugin_Upgrader( $skin ); 
  136. $upgrader->init(); 
  137. // This avoids the plugin to be deactivated. 
  138. defined( 'DOING_CRON' ) or define( 'DOING_CRON', true ); 
  139. $result = $upgrader->upgrade( $plugin ); 
  140.  
  141. $this->log[ $plugin ][] = $upgrader->skin->get_upgrade_messages(); 
  142.  
  143. if ( ! $this->bulk && ! $result && $update_attempted ) { 
  144. return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 ); 
  145.  
  146. return $this->default_action();