PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/admin.php

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