wp-login.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. <?php
  2. /**
  3. * WordPress User Page
  4. *
  5. * Handles authentication, registering, resetting passwords, forgot password,
  6. * and other user handling.
  7. *
  8. * @package WordPress
  9. */
  10. /** Make sure that the WordPress bootstrap has run before continuing. */
  11. require __DIR__ . '/wp-load.php';
  12. // Redirect to HTTPS login if forced to use SSL.
  13. if ( force_ssl_admin() && ! is_ssl() ) {
  14. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  15. wp_safe_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
  16. exit;
  17. } else {
  18. wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  19. exit;
  20. }
  21. }
  22. /**
  23. * Output the login page header.
  24. *
  25. * @since 2.1.0
  26. *
  27. * @global string $error Login error message set by deprecated pluggable wp_login() function
  28. * or plugins replacing it.
  29. * @global bool|string $interim_login Whether interim login modal is being displayed. String 'success'
  30. * upon successful login.
  31. * @global string $action The action that brought the visitor to the login page.
  32. *
  33. * @param string $title Optional. WordPress login Page title to display in the `<title>` element.
  34. * Default 'Log In'.
  35. * @param string $message Optional. Message to display in header. Default empty.
  36. * @param WP_Error $wp_error Optional. The error to pass. Default is a WP_Error instance.
  37. */
  38. function login_header( $title = 'Log In', $message = '', $wp_error = null ) {
  39. global $error, $interim_login, $action;
  40. // Don't index any of these forms.
  41. add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
  42. add_action( 'login_head', 'wp_strict_cross_origin_referrer' );
  43. add_action( 'login_head', 'wp_login_viewport_meta' );
  44. if ( ! is_wp_error( $wp_error ) ) {
  45. $wp_error = new WP_Error();
  46. }
  47. // Shake it!
  48. $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password', 'retrieve_password_email_failure' );
  49. /**
  50. * Filters the error codes array for shaking the login form.
  51. *
  52. * @since 3.0.0
  53. *
  54. * @param string[] $shake_error_codes Error codes that shake the login form.
  55. */
  56. $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
  57. if ( $shake_error_codes && $wp_error->has_errors() && in_array( $wp_error->get_error_code(), $shake_error_codes, true ) ) {
  58. add_action( 'login_footer', 'wp_shake_js', 12 );
  59. }
  60. $login_title = get_bloginfo( 'name', 'display' );
  61. /* translators: Login screen title. 1: Login screen name, 2: Network or site name. */
  62. $login_title = sprintf( __( '%1$s &lsaquo; %2$s &#8212; WordPress' ), $title, $login_title );
  63. if ( wp_is_recovery_mode() ) {
  64. /* translators: %s: Login screen title. */
  65. $login_title = sprintf( __( 'Recovery Mode &#8212; %s' ), $login_title );
  66. }
  67. /**
  68. * Filters the title tag content for login page.
  69. *
  70. * @since 4.9.0
  71. *
  72. * @param string $login_title The page title, with extra context added.
  73. * @param string $title The original page title.
  74. */
  75. $login_title = apply_filters( 'login_title', $login_title, $title );
  76. ?><!DOCTYPE html>
  77. <html <?php language_attributes(); ?>>
  78. <head>
  79. <meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php bloginfo( 'charset' ); ?>" />
  80. <title><?php echo $login_title; ?></title>
  81. <?php
  82. wp_enqueue_style( 'login' );
  83. /*
  84. * Remove all stored post data on logging out.
  85. * This could be added by add_action('login_head'...) like wp_shake_js(),
  86. * but maybe better if it's not removable by plugins.
  87. */
  88. if ( 'loggedout' === $wp_error->get_error_code() ) {
  89. ?>
  90. <script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script>
  91. <?php
  92. }
  93. /**
  94. * Enqueue scripts and styles for the login page.
  95. *
  96. * @since 3.1.0
  97. */
  98. do_action( 'login_enqueue_scripts' );
  99. /**
  100. * Fires in the login page header after scripts are enqueued.
  101. *
  102. * @since 2.1.0
  103. */
  104. do_action( 'login_head' );
  105. $login_header_url = __( 'https://wordpress.org/' );
  106. /**
  107. * Filters link URL of the header logo above login form.
  108. *
  109. * @since 2.1.0
  110. *
  111. * @param string $login_header_url Login header logo URL.
  112. */
  113. $login_header_url = apply_filters( 'login_headerurl', $login_header_url );
  114. $login_header_title = '';
  115. /**
  116. * Filters the title attribute of the header logo above login form.
  117. *
  118. * @since 2.1.0
  119. * @deprecated 5.2.0 Use {@see 'login_headertext'} instead.
  120. *
  121. * @param string $login_header_title Login header logo title attribute.
  122. */
  123. $login_header_title = apply_filters_deprecated(
  124. 'login_headertitle',
  125. array( $login_header_title ),
  126. '5.2.0',
  127. 'login_headertext',
  128. __( 'Usage of the title attribute on the login logo is not recommended for accessibility reasons. Use the link text instead.' )
  129. );
  130. $login_header_text = empty( $login_header_title ) ? __( 'Powered by WordPress' ) : $login_header_title;
  131. /**
  132. * Filters the link text of the header logo above the login form.
  133. *
  134. * @since 5.2.0
  135. *
  136. * @param string $login_header_text The login header logo link text.
  137. */
  138. $login_header_text = apply_filters( 'login_headertext', $login_header_text );
  139. $classes = array( 'login-action-' . $action, 'wp-core-ui' );
  140. if ( is_rtl() ) {
  141. $classes[] = 'rtl';
  142. }
  143. if ( $interim_login ) {
  144. $classes[] = 'interim-login';
  145. ?>
  146. <style type="text/css">html{background-color: transparent;}</style>
  147. <?php
  148. if ( 'success' === $interim_login ) {
  149. $classes[] = 'interim-login-success';
  150. }
  151. }
  152. $classes[] = ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
  153. /**
  154. * Filters the login page body classes.
  155. *
  156. * @since 3.5.0
  157. *
  158. * @param string[] $classes An array of body classes.
  159. * @param string $action The action that brought the visitor to the login page.
  160. */
  161. $classes = apply_filters( 'login_body_class', $classes, $action );
  162. ?>
  163. </head>
  164. <body class="login no-js <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
  165. <script type="text/javascript">
  166. document.body.className = document.body.className.replace('no-js','js');
  167. </script>
  168. <?php
  169. /**
  170. * Fires in the login page header after the body tag is opened.
  171. *
  172. * @since 4.6.0
  173. */
  174. do_action( 'login_header' );
  175. ?>
  176. <div id="login">
  177. <h1><a href="<?php echo esc_url( $login_header_url ); ?>"><?php echo $login_header_text; ?></a></h1>
  178. <?php
  179. /**
  180. * Filters the message to display above the login form.
  181. *
  182. * @since 2.1.0
  183. *
  184. * @param string $message Login message text.
  185. */
  186. $message = apply_filters( 'login_message', $message );
  187. if ( ! empty( $message ) ) {
  188. echo $message . "\n";
  189. }
  190. // In case a plugin uses $error rather than the $wp_errors object.
  191. if ( ! empty( $error ) ) {
  192. $wp_error->add( 'error', $error );
  193. unset( $error );
  194. }
  195. if ( $wp_error->has_errors() ) {
  196. $errors = '';
  197. $messages = '';
  198. foreach ( $wp_error->get_error_codes() as $code ) {
  199. $severity = $wp_error->get_error_data( $code );
  200. foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
  201. if ( 'message' === $severity ) {
  202. $messages .= ' ' . $error_message . "<br />\n";
  203. } else {
  204. $errors .= ' ' . $error_message . "<br />\n";
  205. }
  206. }
  207. }
  208. if ( ! empty( $errors ) ) {
  209. /**
  210. * Filters the error messages displayed above the login form.
  211. *
  212. * @since 2.1.0
  213. *
  214. * @param string $errors Login error message.
  215. */
  216. echo '<div id="login_error">' . apply_filters( 'login_errors', $errors ) . "</div>\n";
  217. }
  218. if ( ! empty( $messages ) ) {
  219. /**
  220. * Filters instructional messages displayed above the login form.
  221. *
  222. * @since 2.5.0
  223. *
  224. * @param string $messages Login messages.
  225. */
  226. echo '<p class="message" id="login-message">' . apply_filters( 'login_messages', $messages ) . "</p>\n";
  227. }
  228. }
  229. } // End of login_header().
  230. /**
  231. * Outputs the footer for the login page.
  232. *
  233. * @since 3.1.0
  234. *
  235. * @global bool|string $interim_login Whether interim login modal is being displayed. String 'success'
  236. * upon successful login.
  237. *
  238. * @param string $input_id Which input to auto-focus.
  239. */
  240. function login_footer( $input_id = '' ) {
  241. global $interim_login;
  242. // Don't allow interim logins to navigate away from the page.
  243. if ( ! $interim_login ) {
  244. ?>
  245. <p id="backtoblog">
  246. <?php
  247. $html_link = sprintf(
  248. '<a href="%s">%s</a>',
  249. esc_url( home_url( '/' ) ),
  250. sprintf(
  251. /* translators: %s: Site title. */
  252. _x( '&larr; Go to %s', 'site' ),
  253. get_bloginfo( 'title', 'display' )
  254. )
  255. );
  256. /**
  257. * Filter the "Go to site" link displayed in the login page footer.
  258. *
  259. * @since 5.7.0
  260. *
  261. * @param string $link HTML link to the home URL of the current site.
  262. */
  263. echo apply_filters( 'login_site_html_link', $html_link );
  264. ?>
  265. </p>
  266. <?php
  267. the_privacy_policy_link( '<div class="privacy-policy-page-link">', '</div>' );
  268. }
  269. ?>
  270. </div><?php // End of <div id="login">. ?>
  271. <?php
  272. if (
  273. ! $interim_login &&
  274. /**
  275. * Filters the Languages select input activation on the login screen.
  276. *
  277. * @since 5.9.0
  278. *
  279. * @param bool Whether to display the Languages select input on the login screen.
  280. */
  281. apply_filters( 'login_display_language_dropdown', true )
  282. ) {
  283. $languages = get_available_languages();
  284. if ( ! empty( $languages ) ) {
  285. ?>
  286. <div class="language-switcher">
  287. <form id="language-switcher" action="" method="get">
  288. <label for="language-switcher-locales">
  289. <span class="dashicons dashicons-translation" aria-hidden="true"></span>
  290. <span class="screen-reader-text"><?php _e( 'Language' ); ?></span>
  291. </label>
  292. <?php
  293. $args = array(
  294. 'id' => 'language-switcher-locales',
  295. 'name' => 'wp_lang',
  296. 'selected' => determine_locale(),
  297. 'show_available_translations' => false,
  298. 'explicit_option_en_us' => true,
  299. 'languages' => $languages,
  300. );
  301. /**
  302. * Filters default arguments for the Languages select input on the login screen.
  303. *
  304. * The arguments get passed to the wp_dropdown_languages() function.
  305. *
  306. * @since 5.9.0
  307. *
  308. * @param array $args Arguments for the Languages select input on the login screen.
  309. */
  310. wp_dropdown_languages( apply_filters( 'login_language_dropdown_args', $args ) );
  311. ?>
  312. <?php if ( $interim_login ) { ?>
  313. <input type="hidden" name="interim-login" value="1" />
  314. <?php } ?>
  315. <?php if ( isset( $_GET['redirect_to'] ) && '' !== $_GET['redirect_to'] ) { ?>
  316. <input type="hidden" name="redirect_to" value="<?php echo sanitize_url( $_GET['redirect_to'] ); ?>" />
  317. <?php } ?>
  318. <?php if ( isset( $_GET['action'] ) && '' !== $_GET['action'] ) { ?>
  319. <input type="hidden" name="action" value="<?php echo esc_attr( $_GET['action'] ); ?>" />
  320. <?php } ?>
  321. <input type="submit" class="button" value="<?php esc_attr_e( 'Change' ); ?>">
  322. </form>
  323. </div>
  324. <?php } ?>
  325. <?php } ?>
  326. <?php
  327. if ( ! empty( $input_id ) ) {
  328. ?>
  329. <script type="text/javascript">
  330. try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
  331. if(typeof wpOnload==='function')wpOnload();
  332. </script>
  333. <?php
  334. }
  335. /**
  336. * Fires in the login page footer.
  337. *
  338. * @since 3.1.0
  339. */
  340. do_action( 'login_footer' );
  341. ?>
  342. <div class="clear"></div>
  343. </body>
  344. </html>
  345. <?php
  346. }
  347. /**
  348. * Outputs the JavaScript to handle the form shaking on the login page.
  349. *
  350. * @since 3.0.0
  351. */
  352. function wp_shake_js() {
  353. ?>
  354. <script type="text/javascript">
  355. document.querySelector('form').classList.add('shake');
  356. </script>
  357. <?php
  358. }
  359. /**
  360. * Outputs the viewport meta tag for the login page.
  361. *
  362. * @since 3.7.0
  363. */
  364. function wp_login_viewport_meta() {
  365. ?>
  366. <meta name="viewport" content="width=device-width" />
  367. <?php
  368. }
  369. /*
  370. * Main part: check the request and redirect or display a form based on the current action.
  371. */
  372. $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'login';
  373. $errors = new WP_Error();
  374. if ( isset( $_GET['key'] ) ) {
  375. $action = 'resetpass';
  376. }
  377. if ( isset( $_GET['checkemail'] ) ) {
  378. $action = 'checkemail';
  379. }
  380. $default_actions = array(
  381. 'confirm_admin_email',
  382. 'postpass',
  383. 'logout',
  384. 'lostpassword',
  385. 'retrievepassword',
  386. 'resetpass',
  387. 'rp',
  388. 'register',
  389. 'checkemail',
  390. 'confirmaction',
  391. 'login',
  392. WP_Recovery_Mode_Link_Service::LOGIN_ACTION_ENTERED,
  393. );
  394. // Validate action so as to default to the login screen.
  395. if ( ! in_array( $action, $default_actions, true ) && false === has_filter( 'login_form_' . $action ) ) {
  396. $action = 'login';
  397. }
  398. nocache_headers();
  399. header( 'Content-Type: ' . get_bloginfo( 'html_type' ) . '; charset=' . get_bloginfo( 'charset' ) );
  400. if ( defined( 'RELOCATE' ) && RELOCATE ) { // Move flag is set.
  401. if ( isset( $_SERVER['PATH_INFO'] ) && ( $_SERVER['PATH_INFO'] !== $_SERVER['PHP_SELF'] ) ) {
  402. $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
  403. }
  404. $url = dirname( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ) );
  405. if ( get_option( 'siteurl' ) !== $url ) {
  406. update_option( 'siteurl', $url );
  407. }
  408. }
  409. // Set a cookie now to see if they are supported by the browser.
  410. $secure = ( 'https' === parse_url( wp_login_url(), PHP_URL_SCHEME ) );
  411. setcookie( TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN, $secure );
  412. if ( SITECOOKIEPATH !== COOKIEPATH ) {
  413. setcookie( TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
  414. }
  415. if ( isset( $_GET['wp_lang'] ) ) {
  416. setcookie( 'wp_lang', sanitize_text_field( $_GET['wp_lang'] ), 0, COOKIEPATH, COOKIE_DOMAIN, $secure );
  417. }
  418. /**
  419. * Fires when the login form is initialized.
  420. *
  421. * @since 3.2.0
  422. */
  423. do_action( 'login_init' );
  424. /**
  425. * Fires before a specified login form action.
  426. *
  427. * The dynamic portion of the hook name, `$action`, refers to the action
  428. * that brought the visitor to the login form.
  429. *
  430. * Possible hook names include:
  431. *
  432. * - `login_form_checkemail`
  433. * - `login_form_confirm_admin_email`
  434. * - `login_form_confirmaction`
  435. * - `login_form_entered_recovery_mode`
  436. * - `login_form_login`
  437. * - `login_form_logout`
  438. * - `login_form_lostpassword`
  439. * - `login_form_postpass`
  440. * - `login_form_register`
  441. * - `login_form_resetpass`
  442. * - `login_form_retrievepassword`
  443. * - `login_form_rp`
  444. *
  445. * @since 2.8.0
  446. */
  447. do_action( "login_form_{$action}" );
  448. $http_post = ( 'POST' === $_SERVER['REQUEST_METHOD'] );
  449. $interim_login = isset( $_REQUEST['interim-login'] );
  450. /**
  451. * Filters the separator used between login form navigation links.
  452. *
  453. * @since 4.9.0
  454. *
  455. * @param string $login_link_separator The separator used between login form navigation links.
  456. */
  457. $login_link_separator = apply_filters( 'login_link_separator', ' | ' );
  458. switch ( $action ) {
  459. case 'confirm_admin_email':
  460. /*
  461. * Note that `is_user_logged_in()` will return false immediately after logging in
  462. * as the current user is not set, see wp-includes/pluggable.php.
  463. * However this action runs on a redirect after logging in.
  464. */
  465. if ( ! is_user_logged_in() ) {
  466. wp_safe_redirect( wp_login_url() );
  467. exit;
  468. }
  469. if ( ! empty( $_REQUEST['redirect_to'] ) ) {
  470. $redirect_to = $_REQUEST['redirect_to'];
  471. } else {
  472. $redirect_to = admin_url();
  473. }
  474. if ( current_user_can( 'manage_options' ) ) {
  475. $admin_email = get_option( 'admin_email' );
  476. } else {
  477. wp_safe_redirect( $redirect_to );
  478. exit;
  479. }
  480. /**
  481. * Filters the interval for dismissing the admin email confirmation screen.
  482. *
  483. * If `0` (zero) is returned, the "Remind me later" link will not be displayed.
  484. *
  485. * @since 5.3.1
  486. *
  487. * @param int $interval Interval time (in seconds). Default is 3 days.
  488. */
  489. $remind_interval = (int) apply_filters( 'admin_email_remind_interval', 3 * DAY_IN_SECONDS );
  490. if ( ! empty( $_GET['remind_me_later'] ) ) {
  491. if ( ! wp_verify_nonce( $_GET['remind_me_later'], 'remind_me_later_nonce' ) ) {
  492. wp_safe_redirect( wp_login_url() );
  493. exit;
  494. }
  495. if ( $remind_interval > 0 ) {
  496. update_option( 'admin_email_lifespan', time() + $remind_interval );
  497. }
  498. $redirect_to = add_query_arg( 'admin_email_remind_later', 1, $redirect_to );
  499. wp_safe_redirect( $redirect_to );
  500. exit;
  501. }
  502. if ( ! empty( $_POST['correct-admin-email'] ) ) {
  503. if ( ! check_admin_referer( 'confirm_admin_email', 'confirm_admin_email_nonce' ) ) {
  504. wp_safe_redirect( wp_login_url() );
  505. exit;
  506. }
  507. /**
  508. * Filters the interval for redirecting the user to the admin email confirmation screen.
  509. *
  510. * If `0` (zero) is returned, the user will not be redirected.
  511. *
  512. * @since 5.3.0
  513. *
  514. * @param int $interval Interval time (in seconds). Default is 6 months.
  515. */
  516. $admin_email_check_interval = (int) apply_filters( 'admin_email_check_interval', 6 * MONTH_IN_SECONDS );
  517. if ( $admin_email_check_interval > 0 ) {
  518. update_option( 'admin_email_lifespan', time() + $admin_email_check_interval );
  519. }
  520. wp_safe_redirect( $redirect_to );
  521. exit;
  522. }
  523. login_header( __( 'Confirm your administration email' ), '', $errors );
  524. /**
  525. * Fires before the admin email confirm form.
  526. *
  527. * @since 5.3.0
  528. *
  529. * @param WP_Error $errors A `WP_Error` object containing any errors generated by using invalid
  530. * credentials. Note that the error object may not contain any errors.
  531. */
  532. do_action( 'admin_email_confirm', $errors );
  533. ?>
  534. <form class="admin-email-confirm-form" name="admin-email-confirm-form" action="<?php echo esc_url( site_url( 'wp-login.php?action=confirm_admin_email', 'login_post' ) ); ?>" method="post">
  535. <?php
  536. /**
  537. * Fires inside the admin-email-confirm-form form tags, before the hidden fields.
  538. *
  539. * @since 5.3.0
  540. */
  541. do_action( 'admin_email_confirm_form' );
  542. wp_nonce_field( 'confirm_admin_email', 'confirm_admin_email_nonce' );
  543. ?>
  544. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  545. <h1 class="admin-email__heading">
  546. <?php _e( 'Administration email verification' ); ?>
  547. </h1>
  548. <p class="admin-email__details">
  549. <?php _e( 'Please verify that the <strong>administration email</strong> for this website is still correct.' ); ?>
  550. <?php
  551. /* translators: URL to the WordPress help section about admin email. */
  552. $admin_email_help_url = __( 'https://wordpress.org/support/article/settings-general-screen/#email-address' );
  553. /* translators: Accessibility text. */
  554. $accessibility_text = sprintf( '<span class="screen-reader-text"> %s</span>', __( '(opens in a new tab)' ) );
  555. printf(
  556. '<a href="%s" rel="noopener" target="_blank">%s%s</a>',
  557. esc_url( $admin_email_help_url ),
  558. __( 'Why is this important?' ),
  559. $accessibility_text
  560. );
  561. ?>
  562. </p>
  563. <p class="admin-email__details">
  564. <?php
  565. printf(
  566. /* translators: %s: Admin email address. */
  567. __( 'Current administration email: %s' ),
  568. '<strong>' . esc_html( $admin_email ) . '</strong>'
  569. );
  570. ?>
  571. </p>
  572. <p class="admin-email__details">
  573. <?php _e( 'This email may be different from your personal email address.' ); ?>
  574. </p>
  575. <div class="admin-email__actions">
  576. <div class="admin-email__actions-primary">
  577. <?php
  578. $change_link = admin_url( 'options-general.php' );
  579. $change_link = add_query_arg( 'highlight', 'confirm_admin_email', $change_link );
  580. ?>
  581. <a class="button button-large" href="<?php echo esc_url( $change_link ); ?>"><?php _e( 'Update' ); ?></a>
  582. <input type="submit" name="correct-admin-email" id="correct-admin-email" class="button button-primary button-large" value="<?php esc_attr_e( 'The email is correct' ); ?>" />
  583. </div>
  584. <?php if ( $remind_interval > 0 ) : ?>
  585. <div class="admin-email__actions-secondary">
  586. <?php
  587. $remind_me_link = wp_login_url( $redirect_to );
  588. $remind_me_link = add_query_arg(
  589. array(
  590. 'action' => 'confirm_admin_email',
  591. 'remind_me_later' => wp_create_nonce( 'remind_me_later_nonce' ),
  592. ),
  593. $remind_me_link
  594. );
  595. ?>
  596. <a href="<?php echo esc_url( $remind_me_link ); ?>"><?php _e( 'Remind me later' ); ?></a>
  597. </div>
  598. <?php endif; ?>
  599. </div>
  600. </form>
  601. <?php
  602. login_footer();
  603. break;
  604. case 'postpass':
  605. if ( ! array_key_exists( 'post_password', $_POST ) ) {
  606. wp_safe_redirect( wp_get_referer() );
  607. exit;
  608. }
  609. require_once ABSPATH . WPINC . '/class-phpass.php';
  610. $hasher = new PasswordHash( 8, true );
  611. /**
  612. * Filters the life span of the post password cookie.
  613. *
  614. * By default, the cookie expires 10 days from creation. To turn this
  615. * into a session cookie, return 0.
  616. *
  617. * @since 3.7.0
  618. *
  619. * @param int $expires The expiry time, as passed to setcookie().
  620. */
  621. $expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
  622. $referer = wp_get_referer();
  623. if ( $referer ) {
  624. $secure = ( 'https' === parse_url( $referer, PHP_URL_SCHEME ) );
  625. } else {
  626. $secure = false;
  627. }
  628. setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
  629. wp_safe_redirect( wp_get_referer() );
  630. exit;
  631. case 'logout':
  632. check_admin_referer( 'log-out' );
  633. $user = wp_get_current_user();
  634. wp_logout();
  635. if ( ! empty( $_REQUEST['redirect_to'] ) ) {
  636. $redirect_to = $_REQUEST['redirect_to'];
  637. $requested_redirect_to = $redirect_to;
  638. } else {
  639. $redirect_to = add_query_arg(
  640. array(
  641. 'loggedout' => 'true',
  642. 'wp_lang' => get_user_locale( $user ),
  643. ),
  644. wp_login_url()
  645. );
  646. $requested_redirect_to = '';
  647. }
  648. /**
  649. * Filters the log out redirect URL.
  650. *
  651. * @since 4.2.0
  652. *
  653. * @param string $redirect_to The redirect destination URL.
  654. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  655. * @param WP_User $user The WP_User object for the user that's logging out.
  656. */
  657. $redirect_to = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $user );
  658. wp_safe_redirect( $redirect_to );
  659. exit;
  660. case 'lostpassword':
  661. case 'retrievepassword':
  662. if ( $http_post ) {
  663. $errors = retrieve_password();
  664. if ( ! is_wp_error( $errors ) ) {
  665. $redirect_to = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?checkemail=confirm';
  666. wp_safe_redirect( $redirect_to );
  667. exit;
  668. }
  669. }
  670. if ( isset( $_GET['error'] ) ) {
  671. if ( 'invalidkey' === $_GET['error'] ) {
  672. $errors->add( 'invalidkey', __( '<strong>Error:</strong> Your password reset link appears to be invalid. Please request a new link below.' ) );
  673. } elseif ( 'expiredkey' === $_GET['error'] ) {
  674. $errors->add( 'expiredkey', __( '<strong>Error:</strong> Your password reset link has expired. Please request a new link below.' ) );
  675. }
  676. }
  677. $lostpassword_redirect = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  678. /**
  679. * Filters the URL redirected to after submitting the lostpassword/retrievepassword form.
  680. *
  681. * @since 3.0.0
  682. *
  683. * @param string $lostpassword_redirect The redirect destination URL.
  684. */
  685. $redirect_to = apply_filters( 'lostpassword_redirect', $lostpassword_redirect );
  686. /**
  687. * Fires before the lost password form.
  688. *
  689. * @since 1.5.1
  690. * @since 5.1.0 Added the `$errors` parameter.
  691. *
  692. * @param WP_Error $errors A `WP_Error` object containing any errors generated by using invalid
  693. * credentials. Note that the error object may not contain any errors.
  694. */
  695. do_action( 'lost_password', $errors );
  696. login_header( __( 'Lost Password' ), '<p class="message">' . __( 'Please enter your username or email address. You will receive an email message with instructions on how to reset your password.' ) . '</p>', $errors );
  697. $user_login = '';
  698. if ( isset( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) {
  699. $user_login = wp_unslash( $_POST['user_login'] );
  700. }
  701. ?>
  702. <form name="lostpasswordform" id="lostpasswordform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
  703. <p>
  704. <label for="user_login"><?php _e( 'Username or Email Address' ); ?></label>
  705. <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr( $user_login ); ?>" size="20" autocapitalize="off" autocomplete="username" />
  706. </p>
  707. <?php
  708. /**
  709. * Fires inside the lostpassword form tags, before the hidden fields.
  710. *
  711. * @since 2.1.0
  712. */
  713. do_action( 'lostpassword_form' );
  714. ?>
  715. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  716. <p class="submit">
  717. <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e( 'Get New Password' ); ?>" />
  718. </p>
  719. </form>
  720. <p id="nav">
  721. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a>
  722. <?php
  723. if ( get_option( 'users_can_register' ) ) {
  724. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  725. echo esc_html( $login_link_separator );
  726. /** This filter is documented in wp-includes/general-template.php */
  727. echo apply_filters( 'register', $registration_url );
  728. }
  729. ?>
  730. </p>
  731. <?php
  732. login_footer( 'user_login' );
  733. break;
  734. case 'resetpass':
  735. case 'rp':
  736. list( $rp_path ) = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) );
  737. $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
  738. if ( isset( $_GET['key'] ) && isset( $_GET['login'] ) ) {
  739. $value = sprintf( '%s:%s', wp_unslash( $_GET['login'] ), wp_unslash( $_GET['key'] ) );
  740. setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  741. wp_safe_redirect( remove_query_arg( array( 'key', 'login' ) ) );
  742. exit;
  743. }
  744. if ( isset( $_COOKIE[ $rp_cookie ] ) && 0 < strpos( $_COOKIE[ $rp_cookie ], ':' ) ) {
  745. list( $rp_login, $rp_key ) = explode( ':', wp_unslash( $_COOKIE[ $rp_cookie ] ), 2 );
  746. $user = check_password_reset_key( $rp_key, $rp_login );
  747. if ( isset( $_POST['pass1'] ) && ! hash_equals( $rp_key, $_POST['rp_key'] ) ) {
  748. $user = false;
  749. }
  750. } else {
  751. $user = false;
  752. }
  753. if ( ! $user || is_wp_error( $user ) ) {
  754. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  755. if ( $user && $user->get_error_code() === 'expired_key' ) {
  756. wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=expiredkey' ) );
  757. } else {
  758. wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=invalidkey' ) );
  759. }
  760. exit;
  761. }
  762. $errors = new WP_Error();
  763. // Check if password is one or all empty spaces.
  764. if ( ! empty( $_POST['pass1'] ) ) {
  765. $_POST['pass1'] = trim( $_POST['pass1'] );
  766. if ( empty( $_POST['pass1'] ) ) {
  767. $errors->add( 'password_reset_empty_space', __( 'The password cannot be a space or all spaces.' ) );
  768. }
  769. }
  770. // Check if password fields do not match.
  771. if ( ! empty( $_POST['pass1'] ) && trim( $_POST['pass2'] ) !== $_POST['pass1'] ) {
  772. $errors->add( 'password_reset_mismatch', __( '<strong>Error:</strong> The passwords do not match.' ) );
  773. }
  774. /**
  775. * Fires before the password reset procedure is validated.
  776. *
  777. * @since 3.5.0
  778. *
  779. * @param WP_Error $errors WP Error object.
  780. * @param WP_User|WP_Error $user WP_User object if the login and reset key match. WP_Error object otherwise.
  781. */
  782. do_action( 'validate_password_reset', $errors, $user );
  783. if ( ( ! $errors->has_errors() ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) {
  784. reset_password( $user, $_POST['pass1'] );
  785. setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
  786. login_header( __( 'Password Reset' ), '<p class="message reset-pass">' . __( 'Your password has been reset.' ) . ' <a href="' . esc_url( wp_login_url() ) . '">' . __( 'Log in' ) . '</a></p>' );
  787. login_footer();
  788. exit;
  789. }
  790. wp_enqueue_script( 'utils' );
  791. wp_enqueue_script( 'user-profile' );
  792. login_header( __( 'Reset Password' ), '<p class="message reset-pass">' . __( 'Enter your new password below or generate one.' ) . '</p>', $errors );
  793. ?>
  794. <form name="resetpassform" id="resetpassform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=resetpass', 'login_post' ) ); ?>" method="post" autocomplete="off">
  795. <input type="hidden" id="user_login" value="<?php echo esc_attr( $rp_login ); ?>" autocomplete="off" />
  796. <div class="user-pass1-wrap">
  797. <p>
  798. <label for="pass1"><?php _e( 'New password' ); ?></label>
  799. </p>
  800. <div class="wp-pwd">
  801. <input type="password" data-reveal="1" data-pw="<?php echo esc_attr( wp_generate_password( 16 ) ); ?>" name="pass1" id="pass1" class="input password-input" size="24" value="" autocomplete="new-password" aria-describedby="pass-strength-result" />
  802. <button type="button" class="button button-secondary wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e( 'Hide password' ); ?>">
  803. <span class="dashicons dashicons-hidden" aria-hidden="true"></span>
  804. </button>
  805. <div id="pass-strength-result" class="hide-if-no-js" aria-live="polite"><?php _e( 'Strength indicator' ); ?></div>
  806. </div>
  807. <div class="pw-weak">
  808. <input type="checkbox" name="pw_weak" id="pw-weak" class="pw-checkbox" />
  809. <label for="pw-weak"><?php _e( 'Confirm use of weak password' ); ?></label>
  810. </div>
  811. </div>
  812. <p class="user-pass2-wrap">
  813. <label for="pass2"><?php _e( 'Confirm new password' ); ?></label>
  814. <input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="new-password" />
  815. </p>
  816. <p class="description indicator-hint"><?php echo wp_get_password_hint(); ?></p>
  817. <br class="clear" />
  818. <?php
  819. /**
  820. * Fires following the 'Strength indicator' meter in the user password reset form.
  821. *
  822. * @since 3.9.0
  823. *
  824. * @param WP_User $user User object of the user whose password is being reset.
  825. */
  826. do_action( 'resetpass_form', $user );
  827. ?>
  828. <input type="hidden" name="rp_key" value="<?php echo esc_attr( $rp_key ); ?>" />
  829. <p class="submit reset-pass-submit">
  830. <button type="button" class="button wp-generate-pw hide-if-no-js skip-aria-expanded"><?php _e( 'Generate Password' ); ?></button>
  831. <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e( 'Save Password' ); ?>" />
  832. </p>
  833. </form>
  834. <p id="nav">
  835. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a>
  836. <?php
  837. if ( get_option( 'users_can_register' ) ) {
  838. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  839. echo esc_html( $login_link_separator );
  840. /** This filter is documented in wp-includes/general-template.php */
  841. echo apply_filters( 'register', $registration_url );
  842. }
  843. ?>
  844. </p>
  845. <?php
  846. login_footer( 'pass1' );
  847. break;
  848. case 'register':
  849. if ( is_multisite() ) {
  850. /**
  851. * Filters the Multisite sign up URL.
  852. *
  853. * @since 3.0.0
  854. *
  855. * @param string $sign_up_url The sign up URL.
  856. */
  857. wp_redirect( apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ) );
  858. exit;
  859. }
  860. if ( ! get_option( 'users_can_register' ) ) {
  861. wp_redirect( site_url( 'wp-login.php?registration=disabled' ) );
  862. exit;
  863. }
  864. $user_login = '';
  865. $user_email = '';
  866. if ( $http_post ) {
  867. if ( isset( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) {
  868. $user_login = wp_unslash( $_POST['user_login'] );
  869. }
  870. if ( isset( $_POST['user_email'] ) && is_string( $_POST['user_email'] ) ) {
  871. $user_email = wp_unslash( $_POST['user_email'] );
  872. }
  873. $errors = register_new_user( $user_login, $user_email );
  874. if ( ! is_wp_error( $errors ) ) {
  875. $redirect_to = ! empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
  876. wp_safe_redirect( $redirect_to );
  877. exit;
  878. }
  879. }
  880. $registration_redirect = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  881. /**
  882. * Filters the registration redirect URL.
  883. *
  884. * @since 3.0.0
  885. * @since 5.9.0 Added the `$errors` parameter.
  886. *
  887. * @param string $registration_redirect The redirect destination URL.
  888. * @param int|WP_Error $errors User id if registration was successful,
  889. * WP_Error object otherwise.
  890. */
  891. $redirect_to = apply_filters( 'registration_redirect', $registration_redirect, $errors );
  892. login_header( __( 'Registration Form' ), '<p class="message register">' . __( 'Register For This Site' ) . '</p>', $errors );
  893. ?>
  894. <form name="registerform" id="registerform" action="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login_post' ) ); ?>" method="post" novalidate="novalidate">
  895. <p>
  896. <label for="user_login"><?php _e( 'Username' ); ?></label>
  897. <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr( wp_unslash( $user_login ) ); ?>" size="20" autocapitalize="off" autocomplete="username" />
  898. </p>
  899. <p>
  900. <label for="user_email"><?php _e( 'Email' ); ?></label>
  901. <input type="email" name="user_email" id="user_email" class="input" value="<?php echo esc_attr( wp_unslash( $user_email ) ); ?>" size="25" autocomplete="email" />
  902. </p>
  903. <?php
  904. /**
  905. * Fires following the 'Email' field in the user registration form.
  906. *
  907. * @since 2.1.0
  908. */
  909. do_action( 'register_form' );
  910. ?>
  911. <p id="reg_passmail">
  912. <?php _e( 'Registration confirmation will be emailed to you.' ); ?>
  913. </p>
  914. <br class="clear" />
  915. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  916. <p class="submit">
  917. <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e( 'Register' ); ?>" />
  918. </p>
  919. </form>
  920. <p id="nav">
  921. <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a>
  922. <?php
  923. echo esc_html( $login_link_separator );
  924. $html_link = sprintf( '<a href="%s">%s</a>', esc_url( wp_lostpassword_url() ), __( 'Lost your password?' ) );
  925. /** This filter is documented in wp-login.php */
  926. echo apply_filters( 'lost_password_html_link', $html_link );
  927. ?>
  928. </p>
  929. <?php
  930. login_footer( 'user_login' );
  931. break;
  932. case 'checkemail':
  933. $redirect_to = admin_url();
  934. $errors = new WP_Error();
  935. if ( 'confirm' === $_GET['checkemail'] ) {
  936. $errors->add(
  937. 'confirm',
  938. sprintf(
  939. /* translators: %s: Link to the login page. */
  940. __( 'Check your email for the confirmation link, then visit the <a href="%s">login page</a>.' ),
  941. wp_login_url()
  942. ),
  943. 'message'
  944. );
  945. } elseif ( 'registered' === $_GET['checkemail'] ) {
  946. $errors->add(
  947. 'registered',
  948. sprintf(
  949. /* translators: %s: Link to the login page. */
  950. __( 'Registration complete. Please check your email, then visit the <a href="%s">login page</a>.' ),
  951. wp_login_url()
  952. ),
  953. 'message'
  954. );
  955. }
  956. /** This action is documented in wp-login.php */
  957. $errors = apply_filters( 'wp_login_errors', $errors, $redirect_to );
  958. login_header( __( 'Check your email' ), '', $errors );
  959. login_footer();
  960. break;
  961. case 'confirmaction':
  962. if ( ! isset( $_GET['request_id'] ) ) {
  963. wp_die( __( 'Missing request ID.' ) );
  964. }
  965. if ( ! isset( $_GET['confirm_key'] ) ) {
  966. wp_die( __( 'Missing confirm key.' ) );
  967. }
  968. $request_id = (int) $_GET['request_id'];
  969. $key = sanitize_text_field( wp_unslash( $_GET['confirm_key'] ) );
  970. $result = wp_validate_user_request_key( $request_id, $key );
  971. if ( is_wp_error( $result ) ) {
  972. wp_die( $result );
  973. }
  974. /**
  975. * Fires an action hook when the account action has been confirmed by the user.
  976. *
  977. * Using this you can assume the user has agreed to perform the action by
  978. * clicking on the link in the confirmation email.
  979. *
  980. * After firing this action hook the page will redirect to wp-login a callback
  981. * redirects or exits first.
  982. *
  983. * @since 4.9.6
  984. *
  985. * @param int $request_id Request ID.
  986. */
  987. do_action( 'user_request_action_confirmed', $request_id );
  988. $message = _wp_privacy_account_request_confirmed_message( $request_id );
  989. login_header( __( 'User action confirmed.' ), $message );
  990. login_footer();
  991. exit;
  992. case 'login':
  993. default:
  994. $secure_cookie = '';
  995. $customize_login = isset( $_REQUEST['customize-login'] );
  996. if ( $customize_login ) {
  997. wp_enqueue_script( 'customize-base' );
  998. }
  999. // If the user wants SSL but the session is not SSL, force a secure cookie.
  1000. if ( ! empty( $_POST['log'] ) && ! force_ssl_admin() ) {
  1001. $user_name = sanitize_user( wp_unslash( $_POST['log'] ) );
  1002. $user = get_user_by( 'login', $user_name );
  1003. if ( ! $user && strpos( $user_name, '@' ) ) {
  1004. $user = get_user_by( 'email', $user_name );
  1005. }
  1006. if ( $user ) {
  1007. if ( get_user_option( 'use_ssl', $user->ID ) ) {
  1008. $secure_cookie = true;
  1009. force_ssl_admin( true );
  1010. }
  1011. }
  1012. }
  1013. if ( isset( $_REQUEST['redirect_to'] ) ) {
  1014. $redirect_to = $_REQUEST['redirect_to'];
  1015. // Redirect to HTTPS if user wants SSL.
  1016. if ( $secure_cookie && false !== strpos( $redirect_to, 'wp-admin' ) ) {
  1017. $redirect_to = preg_replace( '|^http://|', 'https://', $redirect_to );
  1018. }
  1019. } else {
  1020. $redirect_to = admin_url();
  1021. }
  1022. $reauth = empty( $_REQUEST['reauth'] ) ? false : true;
  1023. $user = wp_signon( array(), $secure_cookie );
  1024. if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
  1025. if ( headers_sent() ) {
  1026. $user = new WP_Error(
  1027. 'test_cookie',
  1028. sprintf(
  1029. /* translators: 1: Browser cookie documentation URL, 2: Support forums URL. */
  1030. __( '<strong>Error:</strong> Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try the <a href="%2$s">support forums</a>.' ),
  1031. __( 'https://wordpress.org/support/article/cookies/' ),
  1032. __( 'https://wordpress.org/support/forums/' )
  1033. )
  1034. );
  1035. } elseif ( isset( $_POST['testcookie'] ) && empty( $_COOKIE[ TEST_COOKIE ] ) ) {
  1036. // If cookies are disabled, we can't log in even with a valid user and password.
  1037. $user = new WP_Error(
  1038. 'test_cookie',
  1039. sprintf(
  1040. /* translators: %s: Browser cookie documentation URL. */
  1041. __( '<strong>Error:</strong> Cookies are blocked or not supported by your browser. You must <a href="%s">enable cookies</a> to use WordPress.' ),
  1042. __( 'https://wordpress.org/support/article/cookies/#enable-cookies-in-your-browser' )
  1043. )
  1044. );
  1045. }
  1046. }
  1047. $requested_redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
  1048. /**
  1049. * Filters the login redirect URL.
  1050. *
  1051. * @since 3.0.0
  1052. *
  1053. * @param string $redirect_to The redirect destination URL.
  1054. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  1055. * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  1056. */
  1057. $redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );
  1058. if ( ! is_wp_error( $user ) && ! $reauth ) {
  1059. if ( $interim_login ) {
  1060. $message = '<p class="message">' . __( 'You have logged in successfully.' ) . '</p>';
  1061. $interim_login = 'success';
  1062. login_header( '', $message );
  1063. ?>
  1064. </div>
  1065. <?php
  1066. /** This action is documented in wp-login.php */
  1067. do_action( 'login_footer' );
  1068. if ( $customize_login ) {
  1069. ?>
  1070. <script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
  1071. <?php
  1072. }
  1073. ?>
  1074. </body></html>
  1075. <?php
  1076. exit;
  1077. }
  1078. // Check if it is time to add a redirect to the admin email confirmation screen.
  1079. if ( is_a( $user, 'WP_User' ) && $user->exists() && $user->has_cap( 'manage_options' ) ) {
  1080. $admin_email_lifespan = (int) get_option( 'admin_email_lifespan' );
  1081. /*
  1082. * If `0` (or anything "falsey" as it is cast to int) is returned, the user will not be redirected
  1083. * to the admin email confirmation screen.
  1084. */
  1085. /** This filter is documented in wp-login.php */
  1086. $admin_email_check_interval = (int) apply_filters( 'admin_email_check_interval', 6 * MONTH_IN_SECONDS );
  1087. if ( $admin_email_check_interval > 0 && time() > $admin_email_lifespan ) {
  1088. $redirect_to = add_query_arg(
  1089. array(
  1090. 'action' => 'confirm_admin_email',
  1091. 'wp_lang' => get_user_locale( $user ),
  1092. ),
  1093. wp_login_url( $redirect_to )
  1094. );
  1095. }
  1096. }
  1097. if ( ( empty( $redirect_to ) || 'wp-admin/' === $redirect_to || admin_url() === $redirect_to ) ) {
  1098. // If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile.
  1099. if ( is_multisite() && ! get_active_blog_for_user( $user->ID ) && ! is_super_admin( $user->ID ) ) {
  1100. $redirect_to = user_admin_url();
  1101. } elseif ( is_multisite() && ! $user->has_cap( 'read' ) ) {
  1102. $redirect_to = get_dashboard_url( $user->ID );
  1103. } elseif ( ! $user->has_cap( 'edit_posts' ) ) {
  1104. $redirect_to = $user->has_cap( 'read' ) ? admin_url( 'profile.php' ) : home_url();
  1105. }
  1106. wp_redirect( $redirect_to );
  1107. exit;
  1108. }
  1109. wp_safe_redirect( $redirect_to );
  1110. exit;
  1111. }
  1112. $errors = $user;
  1113. // Clear errors if loggedout is set.
  1114. if ( ! empty( $_GET['loggedout'] ) || $reauth ) {
  1115. $errors = new WP_Error();
  1116. }
  1117. if ( empty( $_POST ) && $errors->get_error_codes() === array( 'empty_username', 'empty_password' ) ) {
  1118. $errors = new WP_Error( '', '' );
  1119. }
  1120. if ( $interim_login ) {
  1121. if ( ! $errors->has_errors() ) {
  1122. $errors->add( 'expired', __( 'Your session has expired. Please log in to continue where you left off.' ), 'message' );
  1123. }
  1124. } else {
  1125. // Some parts of this script use the main login form to display a message.
  1126. if ( isset( $_GET['loggedout'] ) && $_GET['loggedout'] ) {
  1127. $errors->add( 'loggedout', __( 'You are now logged out.' ), 'message' );
  1128. } elseif ( isset( $_GET['registration'] ) && 'disabled' === $_GET['registration'] ) {
  1129. $errors->add( 'registerdisabled', __( '<strong>Error:</strong> User registration is currently not allowed.' ) );
  1130. } elseif ( strpos( $redirect_to, 'about.php?updated' ) ) {
  1131. $errors->add( 'updated', __( '<strong>You have successfully updated WordPress!</strong> Please log back in to see what&#8217;s new.' ), 'message' );
  1132. } elseif ( WP_Recovery_Mode_Link_Service::LOGIN_ACTION_ENTERED === $action ) {
  1133. $errors->add( 'enter_recovery_mode', __( 'Recovery Mode Initialized. Please log in to continue.' ), 'message' );
  1134. } elseif ( isset( $_GET['redirect_to'] ) && false !== strpos( $_GET['redirect_to'], 'wp-admin/authorize-application.php' ) ) {
  1135. $query_component = wp_parse_url( $_GET['redirect_to'], PHP_URL_QUERY );
  1136. $query = array();
  1137. if ( $query_component ) {
  1138. parse_str( $query_component, $query );
  1139. }
  1140. if ( ! empty( $query['app_name'] ) ) {
  1141. /* translators: 1: Website name, 2: Application name. */
  1142. $message = sprintf( 'Please log in to %1$s to authorize %2$s to connect to your account.', get_bloginfo( 'name', 'display' ), '<strong>' . esc_html( $query['app_name'] ) . '</strong>' );
  1143. } else {
  1144. /* translators: %s: Website name. */
  1145. $message = sprintf( 'Please log in to %s to proceed with authorization.', get_bloginfo( 'name', 'display' ) );
  1146. }
  1147. $errors->add( 'authorize_application', $message, 'message' );
  1148. }
  1149. }
  1150. /**
  1151. * Filters the login page errors.
  1152. *
  1153. * @since 3.6.0
  1154. *
  1155. * @param WP_Error $errors WP Error object.
  1156. * @param string $redirect_to Redirect destination URL.
  1157. */
  1158. $errors = apply_filters( 'wp_login_errors', $errors, $redirect_to );
  1159. // Clear any stale cookies.
  1160. if ( $reauth ) {
  1161. wp_clear_auth_cookie();
  1162. }
  1163. login_header( __( 'Log In' ), '', $errors );
  1164. if ( isset( $_POST['log'] ) ) {
  1165. $user_login = ( 'incorrect_password' === $errors->get_error_code() || 'empty_password' === $errors->get_error_code() ) ? esc_attr( wp_unslash( $_POST['log'] ) ) : '';
  1166. }
  1167. $rememberme = ! empty( $_POST['rememberme'] );
  1168. $aria_describedby = '';
  1169. $has_errors = $errors->has_errors();
  1170. if ( $has_errors ) {
  1171. $aria_describedby = ' aria-describedby="login_error"';
  1172. }
  1173. if ( $has_errors && 'message' === $errors->get_error_data() ) {
  1174. $aria_describedby = ' aria-describedby="login-message"';
  1175. }
  1176. wp_enqueue_script( 'user-profile' );
  1177. ?>
  1178. <form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">
  1179. <p>
  1180. <label for="user_login"><?php _e( 'Username or Email Address' ); ?></label>
  1181. <input type="text" name="log" id="user_login"<?php echo $aria_describedby; ?> class="input" value="<?php echo esc_attr( $user_login ); ?>" size="20" autocapitalize="off" autocomplete="username" />
  1182. </p>
  1183. <div class="user-pass-wrap">
  1184. <label for="user_pass"><?php _e( 'Password' ); ?></label>
  1185. <div class="wp-pwd">
  1186. <input type="password" name="pwd" id="user_pass"<?php echo $aria_describedby; ?> class="input password-input" value="" size="20" autocomplete="current-password" />
  1187. <button type="button" class="button button-secondary wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e( 'Show password' ); ?>">
  1188. <span class="dashicons dashicons-visibility" aria-hidden="true"></span>
  1189. </button>
  1190. </div>
  1191. </div>
  1192. <?php
  1193. /**
  1194. * Fires following the 'Password' field in the login form.
  1195. *
  1196. * @since 2.1.0
  1197. */
  1198. do_action( 'login_form' );
  1199. ?>
  1200. <p class="forgetmenot"><input name="rememberme" type="checkbox" id="rememberme" value="forever" <?php checked( $rememberme ); ?> /> <label for="rememberme"><?php esc_html_e( 'Remember Me' ); ?></label></p>
  1201. <p class="submit">
  1202. <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e( 'Log In' ); ?>" />
  1203. <?php
  1204. if ( $interim_login ) {
  1205. ?>
  1206. <input type="hidden" name="interim-login" value="1" />
  1207. <?php
  1208. } else {
  1209. ?>
  1210. <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
  1211. <?php
  1212. }
  1213. if ( $customize_login ) {
  1214. ?>
  1215. <input type="hidden" name="customize-login" value="1" />
  1216. <?php
  1217. }
  1218. ?>
  1219. <input type="hidden" name="testcookie" value="1" />
  1220. </p>
  1221. </form>
  1222. <?php
  1223. if ( ! $interim_login ) {
  1224. ?>
  1225. <p id="nav">
  1226. <?php
  1227. if ( get_option( 'users_can_register' ) ) {
  1228. $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );
  1229. /** This filter is documented in wp-includes/general-template.php */
  1230. echo apply_filters( 'register', $registration_url );
  1231. echo esc_html( $login_link_separator );
  1232. }
  1233. $html_link = sprintf( '<a href="%s">%s</a>', esc_url( wp_lostpassword_url() ), __( 'Lost your password?' ) );
  1234. /**
  1235. * Filters the link that allows the user to reset the lost password.
  1236. *
  1237. * @since 6.1.0
  1238. *
  1239. * @param string $html_link HTML link to the lost password form.
  1240. */
  1241. echo apply_filters( 'lost_password_html_link', $html_link );
  1242. ?>
  1243. </p>
  1244. <?php
  1245. }
  1246. $login_script = 'function wp_attempt_focus() {';
  1247. $login_script .= 'setTimeout( function() {';
  1248. $login_script .= 'try {';
  1249. if ( $user_login ) {
  1250. $login_script .= 'd = document.getElementById( "user_pass" ); d.value = "";';
  1251. } else {
  1252. $login_script .= 'd = document.getElementById( "user_login" );';
  1253. if ( $errors->get_error_code() === 'invalid_username' ) {
  1254. $login_script .= 'd.value = "";';
  1255. }
  1256. }
  1257. $login_script .= 'd.focus(); d.select();';
  1258. $login_script .= '} catch( er ) {}';
  1259. $login_script .= '}, 200);';
  1260. $login_script .= "}\n"; // End of wp_attempt_focus().
  1261. /**
  1262. * Filters whether to print the call to `wp_attempt_focus()` on the login screen.
  1263. *
  1264. * @since 4.8.0
  1265. *
  1266. * @param bool $print Whether to print the function call. Default true.
  1267. */
  1268. if ( apply_filters( 'enable_login_autofocus', true ) && ! $error ) {
  1269. $login_script .= "wp_attempt_focus();\n";
  1270. }
  1271. // Run `wpOnload()` if defined.
  1272. $login_script .= "if ( typeof wpOnload === 'function' ) { wpOnload() }";
  1273. ?>
  1274. <script type="text/javascript">
  1275. <?php echo $login_script; ?>
  1276. </script>
  1277. <?php
  1278. if ( $interim_login ) {
  1279. ?>
  1280. <script type="text/javascript">
  1281. ( function() {
  1282. try {
  1283. var i, links = document.getElementsByTagName( 'a' );
  1284. for ( i in links ) {
  1285. if ( links[i].href ) {
  1286. links[i].target = '_blank';
  1287. links[i].rel = 'noopener';
  1288. }
  1289. }
  1290. } catch( er ) {}
  1291. }());
  1292. </script>
  1293. <?php
  1294. }
  1295. login_footer();
  1296. break;
  1297. } // End action switch.