PageRenderTime 76ms CodeModel.GetById 41ms RepoModel.GetById 7ms app.codeStats 0ms

/wp-admin/update.php

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