PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/hunt9310/ras
PHP | 191 lines | 140 code | 36 blank | 15 comment | 34 complexity | ac841edde66eda7652f686429d6457fd MD5 | raw file
  1. <?php
  2. class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint {
  3. // POST /sites/%s/plugins/%s
  4. // POST /sites/%s/plugins
  5. protected $needed_capabilities = 'activate_plugins';
  6. protected $action = 'default_action';
  7. protected $expected_actions = array( 'update', 'install', 'delete' );
  8. public function callback( $path = '', $blog_id = 0, $object = null ) {
  9. Jetpack_JSON_API_Endpoint::validate_input( $object );
  10. switch ( $this->action ) {
  11. case 'update' :
  12. $this->needed_capabilities = 'update_plugins';
  13. break;
  14. case 'install' :
  15. $this->needed_capabilities = 'install_plugins';
  16. break;
  17. }
  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. if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) {
  26. if ( $args['autoupdate'] ) {
  27. $this->autoupdate_on();
  28. } else {
  29. $this->autoupdate_off();
  30. }
  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. }
  39. return true;
  40. }
  41. protected function autoupdate_on() {
  42. $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
  43. $autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) );
  44. Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins );
  45. }
  46. protected function autoupdate_off() {
  47. $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
  48. $autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins );
  49. Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins );
  50. }
  51. protected function activate() {
  52. foreach ( $this->plugins as $plugin ) {
  53. if ( ( ! $this->network_wide && Jetpack::is_plugin_active( $plugin ) ) || is_plugin_active_for_network( $plugin ) ) {
  54. $this->log[ $plugin ]['error'] = __( 'The Plugin is already active.', 'jetpack' );
  55. $has_errors = true;
  56. continue;
  57. }
  58. if ( ! $this->network_wide && is_network_only_plugin( $plugin ) && is_multisite() ) {
  59. $this->log[ $plugin ]['error'] = __( 'Plugin can only be Network Activated', 'jetpack' );
  60. $has_errors = true;
  61. continue;
  62. }
  63. $result = activate_plugin( $plugin, '', $this->network_wide );
  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. }
  78. $this->log[ $plugin ][] = __( 'Plugin activated.', 'jetpack' );
  79. }
  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. }
  85. protected function deactivate() {
  86. foreach ( $this->plugins as $plugin ) {
  87. if ( ! Jetpack::is_plugin_active( $plugin ) ) {
  88. $error = $this->log[ $plugin ]['error'] = __( 'The Plugin is already deactivated.', 'jetpack' );
  89. continue;
  90. }
  91. deactivate_plugins( $plugin, false, $this->network_wide );
  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. }
  100. $this->log[ $plugin ][] = __( 'Plugin deactivated.', 'jetpack' );
  101. }
  102. if ( ! $this->bulk && isset( $error ) ) {
  103. return new WP_Error( 'deactivation_error', $error );
  104. }
  105. }
  106. protected function update() {
  107. wp_clean_plugins_cache();
  108. ob_start();
  109. wp_update_plugins(); // Check for Plugin updates
  110. ob_end_clean();
  111. $update_plugins = get_site_transient( 'update_plugins' );
  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. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  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. $result = false;
  124. foreach ( $this->plugins as $plugin ) {
  125. if ( ! in_array( $plugin, $plugin_updates_needed ) ) {
  126. $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' );
  127. continue;
  128. }
  129. /**
  130. * Pre-upgrade action
  131. *
  132. * @since 3.9.3
  133. *
  134. * @param array $plugin Plugin data
  135. * @param array $plugin Array of plugin objects
  136. * @param bool $updated_attempted false for the first update, true subsequently
  137. */
  138. do_action('jetpack_pre_plugin_upgrade', $plugin, $this->plugins, $update_attempted);
  139. $update_attempted = true;
  140. // Object created inside the for loop to clean the messages for each plugin
  141. $skin = new Automatic_Upgrader_Skin();
  142. // The Automatic_Upgrader_Skin skin shouldn't output anything.
  143. $upgrader = new Plugin_Upgrader( $skin );
  144. $upgrader->init();
  145. // This avoids the plugin to be deactivated.
  146. defined( 'DOING_CRON' ) or define( 'DOING_CRON', true );
  147. $result = $upgrader->upgrade( $plugin );
  148. $this->log[ $plugin ][] = $upgrader->skin->get_upgrade_messages();
  149. }
  150. if ( ! $this->bulk && ! $result && $update_attempted ) {
  151. return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 );
  152. }
  153. return $this->default_action();
  154. }
  155. }