/json-endpoints/jetpack/class.jetpack-json-api-plugins-modify-endpoint.php

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