wp-login.php 46 KB

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