Brak opisu

update.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * Update/Install Plugin/Theme administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. if ( ! defined( 'IFRAME_REQUEST' )
  9. && isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'update-selected', 'activate-plugin', 'update-selected-themes' ), true )
  10. ) {
  11. define( 'IFRAME_REQUEST', true );
  12. }
  13. /** WordPress Administration Bootstrap */
  14. require_once __DIR__ . '/admin.php';
  15. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  16. wp_enqueue_script( 'wp-a11y' );
  17. if ( isset( $_GET['action'] ) ) {
  18. $plugin = isset( $_REQUEST['plugin'] ) ? trim( $_REQUEST['plugin'] ) : '';
  19. $theme = isset( $_REQUEST['theme'] ) ? urldecode( $_REQUEST['theme'] ) : '';
  20. $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
  21. if ( 'update-selected' === $action ) {
  22. if ( ! current_user_can( 'update_plugins' ) ) {
  23. wp_die( __( 'Sorry, you are not allowed to update plugins for this site.' ) );
  24. }
  25. check_admin_referer( 'bulk-update-plugins' );
  26. if ( isset( $_GET['plugins'] ) ) {
  27. $plugins = explode( ',', stripslashes( $_GET['plugins'] ) );
  28. } elseif ( isset( $_POST['checked'] ) ) {
  29. $plugins = (array) $_POST['checked'];
  30. } else {
  31. $plugins = array();
  32. }
  33. $plugins = array_map( 'urldecode', $plugins );
  34. $url = 'update.php?action=update-selected&amp;plugins=' . urlencode( implode( ',', $plugins ) );
  35. $nonce = 'bulk-update-plugins';
  36. wp_enqueue_script( 'updates' );
  37. iframe_header();
  38. $upgrader = new Plugin_Upgrader( new Bulk_Plugin_Upgrader_Skin( compact( 'nonce', 'url' ) ) );
  39. $upgrader->bulk_upgrade( $plugins );
  40. iframe_footer();
  41. } elseif ( 'upgrade-plugin' === $action ) {
  42. if ( ! current_user_can( 'update_plugins' ) ) {
  43. wp_die( __( 'Sorry, you are not allowed to update plugins for this site.' ) );
  44. }
  45. check_admin_referer( 'upgrade-plugin_' . $plugin );
  46. $title = __( 'Update Plugin' );
  47. $parent_file = 'plugins.php';
  48. $submenu_file = 'plugins.php';
  49. wp_enqueue_script( 'updates' );
  50. require_once ABSPATH . 'wp-admin/admin-header.php';
  51. $nonce = 'upgrade-plugin_' . $plugin;
  52. $url = 'update.php?action=upgrade-plugin&plugin=' . urlencode( $plugin );
  53. $upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) ) );
  54. $upgrader->upgrade( $plugin );
  55. require_once ABSPATH . 'wp-admin/admin-footer.php';
  56. } elseif ( 'activate-plugin' === $action ) {
  57. if ( ! current_user_can( 'update_plugins' ) ) {
  58. wp_die( __( 'Sorry, you are not allowed to update plugins for this site.' ) );
  59. }
  60. check_admin_referer( 'activate-plugin_' . $plugin );
  61. if ( ! isset( $_GET['failure'] ) && ! isset( $_GET['success'] ) ) {
  62. wp_redirect( admin_url( 'update.php?action=activate-plugin&failure=true&plugin=' . urlencode( $plugin ) . '&_wpnonce=' . $_GET['_wpnonce'] ) );
  63. activate_plugin( $plugin, '', ! empty( $_GET['networkwide'] ), true );
  64. wp_redirect( admin_url( 'update.php?action=activate-plugin&success=true&plugin=' . urlencode( $plugin ) . '&_wpnonce=' . $_GET['_wpnonce'] ) );
  65. die();
  66. }
  67. iframe_header( __( 'Plugin Reactivation' ), true );
  68. if ( isset( $_GET['success'] ) ) {
  69. echo '<p>' . __( 'Plugin reactivated successfully.' ) . '</p>';
  70. }
  71. if ( isset( $_GET['failure'] ) ) {
  72. echo '<p>' . __( 'Plugin failed to reactivate due to a fatal error.' ) . '</p>';
  73. error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
  74. ini_set( 'display_errors', true ); // Ensure that fatal errors are displayed.
  75. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  76. include WP_PLUGIN_DIR . '/' . $plugin;
  77. }
  78. iframe_footer();
  79. } elseif ( 'install-plugin' === $action ) {
  80. if ( ! current_user_can( 'install_plugins' ) ) {
  81. wp_die( __( 'Sorry, you are not allowed to install plugins on this site.' ) );
  82. }
  83. include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // For plugins_api().
  84. check_admin_referer( 'install-plugin_' . $plugin );
  85. $api = plugins_api(
  86. 'plugin_information',
  87. array(
  88. 'slug' => $plugin,
  89. 'fields' => array(
  90. 'sections' => false,
  91. ),
  92. )
  93. );
  94. if ( is_wp_error( $api ) ) {
  95. wp_die( $api );
  96. }
  97. $title = __( 'Plugin Installation' );
  98. $parent_file = 'plugins.php';
  99. $submenu_file = 'plugin-install.php';
  100. require_once ABSPATH . 'wp-admin/admin-header.php';
  101. /* translators: %s: Plugin name and version. */
  102. $title = sprintf( __( 'Installing Plugin: %s' ), $api->name . ' ' . $api->version );
  103. $nonce = 'install-plugin_' . $plugin;
  104. $url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin );
  105. if ( isset( $_GET['from'] ) ) {
  106. $url .= '&from=' . urlencode( stripslashes( $_GET['from'] ) );
  107. }
  108. $type = 'web'; // Install plugin type, From Web or an Upload.
  109. $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
  110. $upgrader->install( $api->download_link );
  111. require_once ABSPATH . 'wp-admin/admin-footer.php';
  112. } elseif ( 'upload-plugin' === $action ) {
  113. if ( ! current_user_can( 'upload_plugins' ) ) {
  114. wp_die( __( 'Sorry, you are not allowed to install plugins on this site.' ) );
  115. }
  116. check_admin_referer( 'plugin-upload' );
  117. $file_upload = new File_Upload_Upgrader( 'pluginzip', 'package' );
  118. $title = __( 'Upload Plugin' );
  119. $parent_file = 'plugins.php';
  120. $submenu_file = 'plugin-install.php';
  121. require_once ABSPATH . 'wp-admin/admin-header.php';
  122. /* translators: %s: File name. */
  123. $title = sprintf( __( 'Installing plugin from uploaded file: %s' ), esc_html( basename( $file_upload->filename ) ) );
  124. $nonce = 'plugin-upload';
  125. $url = add_query_arg( array( 'package' => $file_upload->id ), 'update.php?action=upload-plugin' );
  126. $type = 'upload'; // Install plugin type, From Web or an Upload.
  127. $overwrite = isset( $_GET['overwrite'] ) ? sanitize_text_field( $_GET['overwrite'] ) : '';
  128. $overwrite = in_array( $overwrite, array( 'update-plugin', 'downgrade-plugin' ), true ) ? $overwrite : '';
  129. $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact( 'type', 'title', 'nonce', 'url', 'overwrite' ) ) );
  130. $result = $upgrader->install( $file_upload->package, array( 'overwrite_package' => $overwrite ) );
  131. if ( $result || is_wp_error( $result ) ) {
  132. $file_upload->cleanup();
  133. }
  134. require_once ABSPATH . 'wp-admin/admin-footer.php';
  135. } elseif ( 'upload-plugin-cancel-overwrite' === $action ) {
  136. if ( ! current_user_can( 'upload_plugins' ) ) {
  137. wp_die( __( 'Sorry, you are not allowed to install plugins on this site.' ) );
  138. }
  139. check_admin_referer( 'plugin-upload-cancel-overwrite' );
  140. // Make sure the attachment still exists, or File_Upload_Upgrader will call wp_die()
  141. // that shows a generic "Please select a file" error.
  142. if ( ! empty( $_GET['package'] ) ) {
  143. $attachment_id = (int) $_GET['package'];
  144. if ( get_post( $attachment_id ) ) {
  145. $file_upload = new File_Upload_Upgrader( 'pluginzip', 'package' );
  146. $file_upload->cleanup();
  147. }
  148. }
  149. wp_redirect( self_admin_url( 'plugin-install.php' ) );
  150. exit;
  151. } elseif ( 'upgrade-theme' === $action ) {
  152. if ( ! current_user_can( 'update_themes' ) ) {
  153. wp_die( __( 'Sorry, you are not allowed to update themes for this site.' ) );
  154. }
  155. check_admin_referer( 'upgrade-theme_' . $theme );
  156. wp_enqueue_script( 'updates' );
  157. $title = __( 'Update Theme' );
  158. $parent_file = 'themes.php';
  159. $submenu_file = 'themes.php';
  160. require_once ABSPATH . 'wp-admin/admin-header.php';
  161. $nonce = 'upgrade-theme_' . $theme;
  162. $url = 'update.php?action=upgrade-theme&theme=' . urlencode( $theme );
  163. $upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'theme' ) ) );
  164. $upgrader->upgrade( $theme );
  165. require_once ABSPATH . 'wp-admin/admin-footer.php';
  166. } elseif ( 'update-selected-themes' === $action ) {
  167. if ( ! current_user_can( 'update_themes' ) ) {
  168. wp_die( __( 'Sorry, you are not allowed to update themes for this site.' ) );
  169. }
  170. check_admin_referer( 'bulk-update-themes' );
  171. if ( isset( $_GET['themes'] ) ) {
  172. $themes = explode( ',', stripslashes( $_GET['themes'] ) );
  173. } elseif ( isset( $_POST['checked'] ) ) {
  174. $themes = (array) $_POST['checked'];
  175. } else {
  176. $themes = array();
  177. }
  178. $themes = array_map( 'urldecode', $themes );
  179. $url = 'update.php?action=update-selected-themes&amp;themes=' . urlencode( implode( ',', $themes ) );
  180. $nonce = 'bulk-update-themes';
  181. wp_enqueue_script( 'updates' );
  182. iframe_header();
  183. $upgrader = new Theme_Upgrader( new Bulk_Theme_Upgrader_Skin( compact( 'nonce', 'url' ) ) );
  184. $upgrader->bulk_upgrade( $themes );
  185. iframe_footer();
  186. } elseif ( 'install-theme' === $action ) {
  187. if ( ! current_user_can( 'install_themes' ) ) {
  188. wp_die( __( 'Sorry, you are not allowed to install themes on this site.' ) );
  189. }
  190. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // For themes_api().
  191. check_admin_referer( 'install-theme_' . $theme );
  192. $api = themes_api(
  193. 'theme_information',
  194. array(
  195. 'slug' => $theme,
  196. 'fields' => array(
  197. 'sections' => false,
  198. 'tags' => false,
  199. ),
  200. )
  201. ); // Save on a bit of bandwidth.
  202. if ( is_wp_error( $api ) ) {
  203. wp_die( $api );
  204. }
  205. $title = __( 'Install Themes' );
  206. $parent_file = 'themes.php';
  207. $submenu_file = 'themes.php';
  208. require_once ABSPATH . 'wp-admin/admin-header.php';
  209. /* translators: %s: Theme name and version. */
  210. $title = sprintf( __( 'Installing Theme: %s' ), $api->name . ' ' . $api->version );
  211. $nonce = 'install-theme_' . $theme;
  212. $url = 'update.php?action=install-theme&theme=' . urlencode( $theme );
  213. $type = 'web'; // Install theme type, From Web or an Upload.
  214. $upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
  215. $upgrader->install( $api->download_link );
  216. require_once ABSPATH . 'wp-admin/admin-footer.php';
  217. } elseif ( 'upload-theme' === $action ) {
  218. if ( ! current_user_can( 'upload_themes' ) ) {
  219. wp_die( __( 'Sorry, you are not allowed to install themes on this site.' ) );
  220. }
  221. check_admin_referer( 'theme-upload' );
  222. $file_upload = new File_Upload_Upgrader( 'themezip', 'package' );
  223. $title = __( 'Upload Theme' );
  224. $parent_file = 'themes.php';
  225. $submenu_file = 'theme-install.php';
  226. require_once ABSPATH . 'wp-admin/admin-header.php';
  227. /* translators: %s: File name. */
  228. $title = sprintf( __( 'Installing theme from uploaded file: %s' ), esc_html( basename( $file_upload->filename ) ) );
  229. $nonce = 'theme-upload';
  230. $url = add_query_arg( array( 'package' => $file_upload->id ), 'update.php?action=upload-theme' );
  231. $type = 'upload'; // Install theme type, From Web or an Upload.
  232. $overwrite = isset( $_GET['overwrite'] ) ? sanitize_text_field( $_GET['overwrite'] ) : '';
  233. $overwrite = in_array( $overwrite, array( 'update-theme', 'downgrade-theme' ), true ) ? $overwrite : '';
  234. $upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact( 'type', 'title', 'nonce', 'url', 'overwrite' ) ) );
  235. $result = $upgrader->install( $file_upload->package, array( 'overwrite_package' => $overwrite ) );
  236. if ( $result || is_wp_error( $result ) ) {
  237. $file_upload->cleanup();
  238. }
  239. require_once ABSPATH . 'wp-admin/admin-footer.php';
  240. } elseif ( 'upload-theme-cancel-overwrite' === $action ) {
  241. if ( ! current_user_can( 'upload_themes' ) ) {
  242. wp_die( __( 'Sorry, you are not allowed to install themes on this site.' ) );
  243. }
  244. check_admin_referer( 'theme-upload-cancel-overwrite' );
  245. // Make sure the attachment still exists, or File_Upload_Upgrader will call wp_die()
  246. // that shows a generic "Please select a file" error.
  247. if ( ! empty( $_GET['package'] ) ) {
  248. $attachment_id = (int) $_GET['package'];
  249. if ( get_post( $attachment_id ) ) {
  250. $file_upload = new File_Upload_Upgrader( 'themezip', 'package' );
  251. $file_upload->cleanup();
  252. }
  253. }
  254. wp_redirect( self_admin_url( 'theme-install.php' ) );
  255. exit;
  256. } else {
  257. /**
  258. * Fires when a custom plugin or theme update request is received.
  259. *
  260. * The dynamic portion of the hook name, `$action`, refers to the action
  261. * provided in the request for wp-admin/update.php. Can be used to
  262. * provide custom update functionality for themes and plugins.
  263. *
  264. * @since 2.8.0
  265. */
  266. do_action( "update-custom_{$action}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  267. }
  268. }