admin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * WordPress Administration Bootstrap
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * In WordPress Administration Screens
  10. *
  11. * @since 2.3.2
  12. */
  13. if ( ! defined( 'WP_ADMIN' ) ) {
  14. define( 'WP_ADMIN', true );
  15. }
  16. if ( ! defined( 'WP_NETWORK_ADMIN' ) ) {
  17. define( 'WP_NETWORK_ADMIN', false );
  18. }
  19. if ( ! defined( 'WP_USER_ADMIN' ) ) {
  20. define( 'WP_USER_ADMIN', false );
  21. }
  22. if ( ! WP_NETWORK_ADMIN && ! WP_USER_ADMIN ) {
  23. define( 'WP_BLOG_ADMIN', true );
  24. }
  25. if ( isset( $_GET['import'] ) && ! defined( 'WP_LOAD_IMPORTERS' ) ) {
  26. define( 'WP_LOAD_IMPORTERS', true );
  27. }
  28. require_once dirname( __DIR__ ) . '/wp-load.php';
  29. nocache_headers();
  30. if ( get_option( 'db_upgraded' ) ) {
  31. flush_rewrite_rules();
  32. update_option( 'db_upgraded', false );
  33. /**
  34. * Fires on the next page load after a successful DB upgrade.
  35. *
  36. * @since 2.8.0
  37. */
  38. do_action( 'after_db_upgrade' );
  39. } elseif ( ! wp_doing_ajax() && empty( $_POST )
  40. && (int) get_option( 'db_version' ) !== $wp_db_version
  41. ) {
  42. if ( ! is_multisite() ) {
  43. wp_redirect( admin_url( 'upgrade.php?_wp_http_referer=' . urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
  44. exit;
  45. }
  46. /**
  47. * Filters whether to attempt to perform the multisite DB upgrade routine.
  48. *
  49. * In single site, the user would be redirected to wp-admin/upgrade.php.
  50. * In multisite, the DB upgrade routine is automatically fired, but only
  51. * when this filter returns true.
  52. *
  53. * If the network is 50 sites or less, it will run every time. Otherwise,
  54. * it will throttle itself to reduce load.
  55. *
  56. * @since MU (3.0.0)
  57. *
  58. * @param bool $do_mu_upgrade Whether to perform the Multisite upgrade routine. Default true.
  59. */
  60. if ( apply_filters( 'do_mu_upgrade', true ) ) {
  61. $c = get_blog_count();
  62. /*
  63. * If there are 50 or fewer sites, run every time. Otherwise, throttle to reduce load:
  64. * attempt to do no more than threshold value, with some +/- allowed.
  65. */
  66. if ( $c <= 50 || ( $c > 50 && mt_rand( 0, (int) ( $c / 50 ) ) === 1 ) ) {
  67. require_once ABSPATH . WPINC . '/http.php';
  68. $response = wp_remote_get(
  69. admin_url( 'upgrade.php?step=1' ),
  70. array(
  71. 'timeout' => 120,
  72. 'httpversion' => '1.1',
  73. )
  74. );
  75. /** This action is documented in wp-admin/network/upgrade.php */
  76. do_action( 'after_mu_upgrade', $response );
  77. unset( $response );
  78. }
  79. unset( $c );
  80. }
  81. }
  82. require_once ABSPATH . 'wp-admin/includes/admin.php';
  83. auth_redirect();
  84. // Schedule Trash collection.
  85. if ( ! wp_next_scheduled( 'wp_scheduled_delete' ) && ! wp_installing() ) {
  86. wp_schedule_event( time(), 'daily', 'wp_scheduled_delete' );
  87. }
  88. // Schedule transient cleanup.
  89. if ( ! wp_next_scheduled( 'delete_expired_transients' ) && ! wp_installing() ) {
  90. wp_schedule_event( time(), 'daily', 'delete_expired_transients' );
  91. }
  92. set_screen_options();
  93. $date_format = __( 'F j, Y' );
  94. $time_format = __( 'g:i a' );
  95. wp_enqueue_script( 'common' );
  96. /**
  97. * $pagenow is set in vars.php.
  98. * $wp_importers is sometimes set in wp-admin/includes/import.php.
  99. * The remaining variables are imported as globals elsewhere, declared as globals here.
  100. *
  101. * @global string $pagenow The filename of the current screen.
  102. * @global array $wp_importers
  103. * @global string $hook_suffix
  104. * @global string $plugin_page
  105. * @global string $typenow The post type of the current screen.
  106. * @global string $taxnow The taxonomy of the current screen.
  107. */
  108. global $pagenow, $wp_importers, $hook_suffix, $plugin_page, $typenow, $taxnow;
  109. $page_hook = null;
  110. $editing = false;
  111. if ( isset( $_GET['page'] ) ) {
  112. $plugin_page = wp_unslash( $_GET['page'] );
  113. $plugin_page = plugin_basename( $plugin_page );
  114. }
  115. if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
  116. $typenow = $_REQUEST['post_type'];
  117. } else {
  118. $typenow = '';
  119. }
  120. if ( isset( $_REQUEST['taxonomy'] ) && taxonomy_exists( $_REQUEST['taxonomy'] ) ) {
  121. $taxnow = $_REQUEST['taxonomy'];
  122. } else {
  123. $taxnow = '';
  124. }
  125. if ( WP_NETWORK_ADMIN ) {
  126. require ABSPATH . 'wp-admin/network/menu.php';
  127. } elseif ( WP_USER_ADMIN ) {
  128. require ABSPATH . 'wp-admin/user/menu.php';
  129. } else {
  130. require ABSPATH . 'wp-admin/menu.php';
  131. }
  132. if ( current_user_can( 'manage_options' ) ) {
  133. wp_raise_memory_limit( 'admin' );
  134. }
  135. /**
  136. * Fires as an admin screen or script is being initialized.
  137. *
  138. * Note, this does not just run on user-facing admin screens.
  139. * It runs on admin-ajax.php and admin-post.php as well.
  140. *
  141. * This is roughly analogous to the more general {@see 'init'} hook, which fires earlier.
  142. *
  143. * @since 2.5.0
  144. */
  145. do_action( 'admin_init' );
  146. if ( isset( $plugin_page ) ) {
  147. if ( ! empty( $typenow ) ) {
  148. $the_parent = $pagenow . '?post_type=' . $typenow;
  149. } else {
  150. $the_parent = $pagenow;
  151. }
  152. $page_hook = get_plugin_page_hook( $plugin_page, $the_parent );
  153. if ( ! $page_hook ) {
  154. $page_hook = get_plugin_page_hook( $plugin_page, $plugin_page );
  155. // Back-compat for plugins using add_management_page().
  156. if ( empty( $page_hook ) && 'edit.php' === $pagenow && get_plugin_page_hook( $plugin_page, 'tools.php' ) ) {
  157. // There could be plugin specific params on the URL, so we need the whole query string.
  158. if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
  159. $query_string = $_SERVER['QUERY_STRING'];
  160. } else {
  161. $query_string = 'page=' . $plugin_page;
  162. }
  163. wp_redirect( admin_url( 'tools.php?' . $query_string ) );
  164. exit;
  165. }
  166. }
  167. unset( $the_parent );
  168. }
  169. $hook_suffix = '';
  170. if ( isset( $page_hook ) ) {
  171. $hook_suffix = $page_hook;
  172. } elseif ( isset( $plugin_page ) ) {
  173. $hook_suffix = $plugin_page;
  174. } elseif ( isset( $pagenow ) ) {
  175. $hook_suffix = $pagenow;
  176. }
  177. set_current_screen();
  178. // Handle plugin admin pages.
  179. if ( isset( $plugin_page ) ) {
  180. if ( $page_hook ) {
  181. /**
  182. * Fires before a particular screen is loaded.
  183. *
  184. * The load-* hook fires in a number of contexts. This hook is for plugin screens
  185. * where a callback is provided when the screen is registered.
  186. *
  187. * The dynamic portion of the hook name, `$page_hook`, refers to a mixture of plugin
  188. * page information including:
  189. * 1. The page type. If the plugin page is registered as a submenu page, such as for
  190. * Settings, the page type would be 'settings'. Otherwise the type is 'toplevel'.
  191. * 2. A separator of '_page_'.
  192. * 3. The plugin basename minus the file extension.
  193. *
  194. * Together, the three parts form the `$page_hook`. Citing the example above,
  195. * the hook name used would be 'load-settings_page_pluginbasename'.
  196. *
  197. * @see get_plugin_page_hook()
  198. *
  199. * @since 2.1.0
  200. */
  201. do_action( "load-{$page_hook}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  202. if ( ! isset( $_GET['noheader'] ) ) {
  203. require_once ABSPATH . 'wp-admin/admin-header.php';
  204. }
  205. /**
  206. * Used to call the registered callback for a plugin screen.
  207. *
  208. * This hook uses a dynamic hook name, `$page_hook`, which refers to a mixture of plugin
  209. * page information including:
  210. * 1. The page type. If the plugin page is registered as a submenu page, such as for
  211. * Settings, the page type would be 'settings'. Otherwise the type is 'toplevel'.
  212. * 2. A separator of '_page_'.
  213. * 3. The plugin basename minus the file extension.
  214. *
  215. * Together, the three parts form the `$page_hook`. Citing the example above,
  216. * the hook name used would be 'settings_page_pluginbasename'.
  217. *
  218. * @see get_plugin_page_hook()
  219. *
  220. * @since 1.5.0
  221. */
  222. do_action( $page_hook );
  223. } else {
  224. if ( validate_file( $plugin_page ) ) {
  225. wp_die( __( 'Invalid plugin page.' ) );
  226. }
  227. if ( ! ( file_exists( WP_PLUGIN_DIR . "/$plugin_page" ) && is_file( WP_PLUGIN_DIR . "/$plugin_page" ) )
  228. && ! ( file_exists( WPMU_PLUGIN_DIR . "/$plugin_page" ) && is_file( WPMU_PLUGIN_DIR . "/$plugin_page" ) )
  229. ) {
  230. /* translators: %s: Admin page generated by a plugin. */
  231. wp_die( sprintf( __( 'Cannot load %s.' ), htmlentities( $plugin_page ) ) );
  232. }
  233. /**
  234. * Fires before a particular screen is loaded.
  235. *
  236. * The load-* hook fires in a number of contexts. This hook is for plugin screens
  237. * where the file to load is directly included, rather than the use of a function.
  238. *
  239. * The dynamic portion of the hook name, `$plugin_page`, refers to the plugin basename.
  240. *
  241. * @see plugin_basename()
  242. *
  243. * @since 1.5.0
  244. */
  245. do_action( "load-{$plugin_page}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  246. if ( ! isset( $_GET['noheader'] ) ) {
  247. require_once ABSPATH . 'wp-admin/admin-header.php';
  248. }
  249. if ( file_exists( WPMU_PLUGIN_DIR . "/$plugin_page" ) ) {
  250. include WPMU_PLUGIN_DIR . "/$plugin_page";
  251. } else {
  252. include WP_PLUGIN_DIR . "/$plugin_page";
  253. }
  254. }
  255. require_once ABSPATH . 'wp-admin/admin-footer.php';
  256. exit;
  257. } elseif ( isset( $_GET['import'] ) ) {
  258. $importer = $_GET['import'];
  259. if ( ! current_user_can( 'import' ) ) {
  260. wp_die( __( 'Sorry, you are not allowed to import content into this site.' ) );
  261. }
  262. if ( validate_file( $importer ) ) {
  263. wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
  264. exit;
  265. }
  266. if ( ! isset( $wp_importers[ $importer ] ) || ! is_callable( $wp_importers[ $importer ][2] ) ) {
  267. wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
  268. exit;
  269. }
  270. /**
  271. * Fires before an importer screen is loaded.
  272. *
  273. * The dynamic portion of the hook name, `$importer`, refers to the importer slug.
  274. *
  275. * Possible hook names include:
  276. *
  277. * - `load-importer-blogger`
  278. * - `load-importer-wpcat2tag`
  279. * - `load-importer-livejournal`
  280. * - `load-importer-mt`
  281. * - `load-importer-rss`
  282. * - `load-importer-tumblr`
  283. * - `load-importer-wordpress`
  284. *
  285. * @since 3.5.0
  286. */
  287. do_action( "load-importer-{$importer}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  288. // Used in the HTML title tag.
  289. $title = __( 'Import' );
  290. $parent_file = 'tools.php';
  291. $submenu_file = 'import.php';
  292. if ( ! isset( $_GET['noheader'] ) ) {
  293. require_once ABSPATH . 'wp-admin/admin-header.php';
  294. }
  295. require_once ABSPATH . 'wp-admin/includes/upgrade.php';
  296. define( 'WP_IMPORTING', true );
  297. /**
  298. * Whether to filter imported data through kses on import.
  299. *
  300. * Multisite uses this hook to filter all data through kses by default,
  301. * as a super administrator may be assisting an untrusted user.
  302. *
  303. * @since 3.1.0
  304. *
  305. * @param bool $force Whether to force data to be filtered through kses. Default false.
  306. */
  307. if ( apply_filters( 'force_filtered_html_on_import', false ) ) {
  308. kses_init_filters(); // Always filter imported data with kses on multisite.
  309. }
  310. call_user_func( $wp_importers[ $importer ][2] );
  311. require_once ABSPATH . 'wp-admin/admin-footer.php';
  312. // Make sure rules are flushed.
  313. flush_rewrite_rules( false );
  314. exit;
  315. } else {
  316. /**
  317. * Fires before a particular screen is loaded.
  318. *
  319. * The load-* hook fires in a number of contexts. This hook is for core screens.
  320. *
  321. * The dynamic portion of the hook name, `$pagenow`, is a global variable
  322. * referring to the filename of the current screen, such as 'admin.php',
  323. * 'post-new.php' etc. A complete hook for the latter would be
  324. * 'load-post-new.php'.
  325. *
  326. * @since 2.1.0
  327. */
  328. do_action( "load-{$pagenow}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  329. /*
  330. * The following hooks are fired to ensure backward compatibility.
  331. * In all other cases, 'load-' . $pagenow should be used instead.
  332. */
  333. if ( 'page' === $typenow ) {
  334. if ( 'post-new.php' === $pagenow ) {
  335. do_action( 'load-page-new.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  336. } elseif ( 'post.php' === $pagenow ) {
  337. do_action( 'load-page.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  338. }
  339. } elseif ( 'edit-tags.php' === $pagenow ) {
  340. if ( 'category' === $taxnow ) {
  341. do_action( 'load-categories.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  342. } elseif ( 'link_category' === $taxnow ) {
  343. do_action( 'load-edit-link-categories.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  344. }
  345. } elseif ( 'term.php' === $pagenow ) {
  346. do_action( 'load-edit-tags.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  347. }
  348. }
  349. if ( ! empty( $_REQUEST['action'] ) ) {
  350. $action = $_REQUEST['action'];
  351. /**
  352. * Fires when an 'action' request variable is sent.
  353. *
  354. * The dynamic portion of the hook name, `$action`, refers to
  355. * the action derived from the `GET` or `POST` request.
  356. *
  357. * @since 2.6.0
  358. */
  359. do_action( "admin_action_{$action}" );
  360. }