/wp-includes/pluggable.php

  1. <?php 
  2. /** 
  3. * These functions can be replaced via plugins. If plugins do not redefine these 
  4. * functions, then these will be used instead. 
  5. * 
  6. * @package WordPress 
  7. */ 
  8.  
  9. if ( !function_exists('wp_set_current_user') ) : 
  10. /** 
  11. * Changes the current user by ID or name. 
  12. * 
  13. * Set $id to null and specify a name if you do not know a user's ID. 
  14. * 
  15. * Some WordPress functionality is based on the current user and not based on 
  16. * the signed in user. Therefore, it opens the ability to edit and perform 
  17. * actions on users who aren't signed in. 
  18. * 
  19. * @since 2.0.3 
  20. * @global WP_User $current_user The current user object which holds the user data. 
  21. * 
  22. * @param int $id User ID 
  23. * @param string $name User's username 
  24. * @return WP_User Current user User object 
  25. */ 
  26. function wp_set_current_user($id, $name = '') { 
  27. global $current_user; 
  28.  
  29. // If `$id` matches the user who's already current, there's nothing to do. 
  30. if ( isset( $current_user ) 
  31. && ( $current_user instanceof WP_User ) 
  32. && ( $id == $current_user->ID ) 
  33. && ( null !== $id ) 
  34. ) { 
  35. return $current_user; 
  36.  
  37. $current_user = new WP_User( $id, $name ); 
  38.  
  39. setup_userdata( $current_user->ID ); 
  40.  
  41. /** 
  42. * Fires after the current user is set. 
  43. * 
  44. * @since 2.0.1 
  45. */ 
  46. do_action( 'set_current_user' ); 
  47.  
  48. return $current_user; 
  49. endif; 
  50.  
  51. if ( !function_exists('wp_get_current_user') ) : 
  52. /** 
  53. * Retrieve the current user object. 
  54. * 
  55. * Will set the current user, if the current user is not set. The current user 
  56. * will be set to the logged-in person. If no user is logged-in, then it will 
  57. * set the current user to 0, which is invalid and won't have any permissions. 
  58. * 
  59. * @since 2.0.3 
  60. * 
  61. * @see _wp_get_current_user() 
  62. * @global WP_User $current_user Checks if the current user is set. 
  63. * 
  64. * @return WP_User Current WP_User instance. 
  65. */ 
  66. function wp_get_current_user() { 
  67. return _wp_get_current_user(); 
  68. endif; 
  69.  
  70. if ( !function_exists('get_userdata') ) : 
  71. /** 
  72. * Retrieve user info by user ID. 
  73. * 
  74. * @since 0.71 
  75. * 
  76. * @param int $user_id User ID 
  77. * @return WP_User|false WP_User object on success, false on failure. 
  78. */ 
  79. function get_userdata( $user_id ) { 
  80. return get_user_by( 'id', $user_id ); 
  81. endif; 
  82.  
  83. if ( !function_exists('get_user_by') ) : 
  84. /** 
  85. * Retrieve user info by a given field 
  86. * 
  87. * @since 2.8.0 
  88. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. 
  89. * 
  90. * @param string $field The field to retrieve the user with. id | ID | slug | email | login. 
  91. * @param int|string $value A value for $field. A user ID, slug, email address, or login name. 
  92. * @return WP_User|false WP_User object on success, false on failure. 
  93. */ 
  94. function get_user_by( $field, $value ) { 
  95. $userdata = WP_User::get_data_by( $field, $value ); 
  96.  
  97. if ( !$userdata ) 
  98. return false; 
  99.  
  100. $user = new WP_User; 
  101. $user->init( $userdata ); 
  102.  
  103. return $user; 
  104. endif; 
  105.  
  106. if ( !function_exists('cache_users') ) : 
  107. /** 
  108. * Retrieve info for user lists to prevent multiple queries by get_userdata() 
  109. * 
  110. * @since 3.0.0 
  111. * 
  112. * @global wpdb $wpdb WordPress database abstraction object. 
  113. * 
  114. * @param array $user_ids User ID numbers list 
  115. */ 
  116. function cache_users( $user_ids ) { 
  117. global $wpdb; 
  118.  
  119. $clean = _get_non_cached_ids( $user_ids, 'users' ); 
  120.  
  121. if ( empty( $clean ) ) 
  122. return; 
  123.  
  124. $list = implode( ', ', $clean ); 
  125.  
  126. $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" ); 
  127.  
  128. $ids = array(); 
  129. foreach ( $users as $user ) { 
  130. update_user_caches( $user ); 
  131. $ids[] = $user->ID; 
  132. update_meta_cache( 'user', $ids ); 
  133. endif; 
  134.  
  135. if ( !function_exists( 'wp_mail' ) ) : 
  136. /** 
  137. * Send mail, similar to PHP's mail 
  138. * 
  139. * A true return value does not automatically mean that the user received the 
  140. * email successfully. It just only means that the method used was able to 
  141. * process the request without any errors. 
  142. * 
  143. * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from 
  144. * creating a from address like 'Name <email@address.com>' when both are set. If 
  145. * just 'wp_mail_from' is set, then just the email address will be used with no 
  146. * name. 
  147. * 
  148. * The default content type is 'text/plain' which does not allow using HTML. 
  149. * However, you can set the content type of the email by using the 
  150. * {@see 'wp_mail_content_type'} filter. 
  151. * 
  152. * The default charset is based on the charset used on the blog. The charset can 
  153. * be set using the {@see 'wp_mail_charset'} filter. 
  154. * 
  155. * @since 1.2.1 
  156. * 
  157. * @global PHPMailer $phpmailer 
  158. * 
  159. * @param string|array $to Array or comma-separated list of email addresses to send message. 
  160. * @param string $subject Email subject 
  161. * @param string $message Message contents 
  162. * @param string|array $headers Optional. Additional headers. 
  163. * @param string|array $attachments Optional. Files to attach. 
  164. * @return bool Whether the email contents were sent successfully. 
  165. */ 
  166. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { 
  167. // Compact the input, apply the filters, and extract them back out 
  168.  
  169. /** 
  170. * Filters the wp_mail() arguments. 
  171. * 
  172. * @since 2.2.0 
  173. * 
  174. * @param array $args A compacted array of wp_mail() arguments, including the "to" email,  
  175. * subject, message, headers, and attachments values. 
  176. */ 
  177. $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ); 
  178.  
  179. if ( isset( $atts['to'] ) ) { 
  180. $to = $atts['to']; 
  181.  
  182. if ( isset( $atts['subject'] ) ) { 
  183. $subject = $atts['subject']; 
  184.  
  185. if ( isset( $atts['message'] ) ) { 
  186. $message = $atts['message']; 
  187.  
  188. if ( isset( $atts['headers'] ) ) { 
  189. $headers = $atts['headers']; 
  190.  
  191. if ( isset( $atts['attachments'] ) ) { 
  192. $attachments = $atts['attachments']; 
  193.  
  194. if ( ! is_array( $attachments ) ) { 
  195. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); 
  196. global $phpmailer; 
  197.  
  198. // (Re)create it, if it's gone missing 
  199. if ( ! ( $phpmailer instanceof PHPMailer ) ) { 
  200. require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
  201. require_once ABSPATH . WPINC . '/class-smtp.php'; 
  202. $phpmailer = new PHPMailer( true ); 
  203.  
  204. // Headers 
  205. $cc = $bcc = $reply_to = array(); 
  206.  
  207. if ( empty( $headers ) ) { 
  208. $headers = array(); 
  209. } else { 
  210. if ( !is_array( $headers ) ) { 
  211. // Explode the headers out, so this function can take both 
  212. // string headers and an array of headers. 
  213. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); 
  214. } else { 
  215. $tempheaders = $headers; 
  216. $headers = array(); 
  217.  
  218. // If it's actually got contents 
  219. if ( !empty( $tempheaders ) ) { 
  220. // Iterate through the raw headers 
  221. foreach ( (array) $tempheaders as $header ) { 
  222. if ( strpos($header, ':') === false ) { 
  223. if ( false !== stripos( $header, 'boundary=' ) ) { 
  224. $parts = preg_split('/boundary=/i', trim( $header ) ); 
  225. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); 
  226. continue; 
  227. // Explode them out 
  228. list( $name, $content ) = explode( ':', trim( $header ), 2 ); 
  229.  
  230. // Cleanup crew 
  231. $name = trim( $name ); 
  232. $content = trim( $content ); 
  233.  
  234. switch ( strtolower( $name ) ) { 
  235. // Mainly for legacy -- process a From: header if it's there 
  236. case 'from': 
  237. $bracket_pos = strpos( $content, '<' ); 
  238. if ( $bracket_pos !== false ) { 
  239. // Text before the bracketed email is the "From" name. 
  240. if ( $bracket_pos > 0 ) { 
  241. $from_name = substr( $content, 0, $bracket_pos - 1 ); 
  242. $from_name = str_replace( '"', '', $from_name ); 
  243. $from_name = trim( $from_name ); 
  244.  
  245. $from_email = substr( $content, $bracket_pos + 1 ); 
  246. $from_email = str_replace( '>', '', $from_email ); 
  247. $from_email = trim( $from_email ); 
  248.  
  249. // Avoid setting an empty $from_email. 
  250. } elseif ( '' !== trim( $content ) ) { 
  251. $from_email = trim( $content ); 
  252. break; 
  253. case 'content-type': 
  254. if ( strpos( $content, ';' ) !== false ) { 
  255. list( $type, $charset_content ) = explode( ';', $content ); 
  256. $content_type = trim( $type ); 
  257. if ( false !== stripos( $charset_content, 'charset=' ) ) { 
  258. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) ); 
  259. } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) { 
  260. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) ); 
  261. $charset = ''; 
  262.  
  263. // Avoid setting an empty $content_type. 
  264. } elseif ( '' !== trim( $content ) ) { 
  265. $content_type = trim( $content ); 
  266. break; 
  267. case 'cc': 
  268. $cc = array_merge( (array) $cc, explode( ', ', $content ) ); 
  269. break; 
  270. case 'bcc': 
  271. $bcc = array_merge( (array) $bcc, explode( ', ', $content ) ); 
  272. break; 
  273. case 'reply-to': 
  274. $reply_to = array_merge( (array) $reply_to, explode( ', ', $content ) ); 
  275. break; 
  276. default: 
  277. // Add it to our grand headers array 
  278. $headers[trim( $name )] = trim( $content ); 
  279. break; 
  280.  
  281. // Empty out the values that may be set 
  282. $phpmailer->ClearAllRecipients(); 
  283. $phpmailer->ClearAttachments(); 
  284. $phpmailer->ClearCustomHeaders(); 
  285. $phpmailer->ClearReplyTos(); 
  286.  
  287. // From email and name 
  288. // If we don't have a name from the input headers 
  289. if ( !isset( $from_name ) ) 
  290. $from_name = 'WordPress'; 
  291.  
  292. /** If we don't have an email from the input headers default to wordpress@$sitename 
  293. * Some hosts will block outgoing mail from this address if it doesn't exist but 
  294. * there's no easy alternative. Defaulting to admin_email might appear to be another 
  295. * option but some hosts may refuse to relay mail from an unknown domain. See 
  296. * https://core.trac.wordpress.org/ticket/5007. 
  297. */ 
  298.  
  299. if ( !isset( $from_email ) ) { 
  300. // Get the site domain and get rid of www. 
  301. $sitename = strtolower( $_SERVER['SERVER_NAME'] ); 
  302. if ( substr( $sitename, 0, 4 ) == 'www.' ) { 
  303. $sitename = substr( $sitename, 4 ); 
  304.  
  305. $from_email = 'wordpress@' . $sitename; 
  306.  
  307. /** 
  308. * Filters the email address to send from. 
  309. * 
  310. * @since 2.2.0 
  311. * 
  312. * @param string $from_email Email address to send from. 
  313. */ 
  314. $from_email = apply_filters( 'wp_mail_from', $from_email ); 
  315.  
  316. /** 
  317. * Filters the name to associate with the "from" email address. 
  318. * 
  319. * @since 2.3.0 
  320. * 
  321. * @param string $from_name Name associated with the "from" email address. 
  322. */ 
  323. $from_name = apply_filters( 'wp_mail_from_name', $from_name ); 
  324.  
  325. $phpmailer->setFrom( $from_email, $from_name, false ); 
  326.  
  327. // Set destination addresses 
  328. if ( !is_array( $to ) ) 
  329. $to = explode( ', ', $to ); 
  330.  
  331. // Set mail's subject and body 
  332. $phpmailer->Subject = $subject; 
  333. $phpmailer->Body = $message; 
  334.  
  335. // Use appropriate methods for handling addresses, rather than treating them as generic headers 
  336. $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); 
  337.  
  338. foreach ( $address_headers as $address_header => $addresses ) { 
  339. if ( empty( $addresses ) ) { 
  340. continue; 
  341.  
  342. foreach ( (array) $addresses as $address ) { 
  343. try { 
  344. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>" 
  345. $recipient_name = ''; 
  346.  
  347. if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { 
  348. if ( count( $matches ) == 3 ) { 
  349. $recipient_name = $matches[1]; 
  350. $address = $matches[2]; 
  351.  
  352. switch ( $address_header ) { 
  353. case 'to': 
  354. $phpmailer->addAddress( $address, $recipient_name ); 
  355. break; 
  356. case 'cc': 
  357. $phpmailer->addCc( $address, $recipient_name ); 
  358. break; 
  359. case 'bcc': 
  360. $phpmailer->addBcc( $address, $recipient_name ); 
  361. break; 
  362. case 'reply_to': 
  363. $phpmailer->addReplyTo( $address, $recipient_name ); 
  364. break; 
  365. } catch ( phpmailerException $e ) { 
  366. continue; 
  367.  
  368. // Set to use PHP's mail() 
  369. $phpmailer->IsMail(); 
  370.  
  371. // Set Content-Type and charset 
  372. // If we don't have a content-type from the input headers 
  373. if ( !isset( $content_type ) ) 
  374. $content_type = 'text/plain'; 
  375.  
  376. /** 
  377. * Filters the wp_mail() content type. 
  378. * 
  379. * @since 2.3.0 
  380. * 
  381. * @param string $content_type Default wp_mail() content type. 
  382. */ 
  383. $content_type = apply_filters( 'wp_mail_content_type', $content_type ); 
  384.  
  385. $phpmailer->ContentType = $content_type; 
  386.  
  387. // Set whether it's plaintext, depending on $content_type 
  388. if ( 'text/html' == $content_type ) 
  389. $phpmailer->IsHTML( true ); 
  390.  
  391. // If we don't have a charset from the input headers 
  392. if ( !isset( $charset ) ) 
  393. $charset = get_bloginfo( 'charset' ); 
  394.  
  395. // Set the content-type and charset 
  396.  
  397. /** 
  398. * Filters the default wp_mail() charset. 
  399. * 
  400. * @since 2.3.0 
  401. * 
  402. * @param string $charset Default email charset. 
  403. */ 
  404. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); 
  405.  
  406. // Set custom headers 
  407. if ( !empty( $headers ) ) { 
  408. foreach ( (array) $headers as $name => $content ) { 
  409. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); 
  410.  
  411. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) 
  412. $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) ); 
  413.  
  414. if ( !empty( $attachments ) ) { 
  415. foreach ( $attachments as $attachment ) { 
  416. try { 
  417. $phpmailer->AddAttachment($attachment); 
  418. } catch ( phpmailerException $e ) { 
  419. continue; 
  420.  
  421. /** 
  422. * Fires after PHPMailer is initialized. 
  423. * 
  424. * @since 2.2.0 
  425. * 
  426. * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference. 
  427. */ 
  428. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); 
  429.  
  430. // Send! 
  431. try { 
  432. return $phpmailer->Send(); 
  433. } catch ( phpmailerException $e ) { 
  434.  
  435. $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); 
  436. $mail_error_data['phpmailer_exception_code'] = $e->getCode(); 
  437.  
  438. /** 
  439. * Fires after a phpmailerException is caught. 
  440. * 
  441. * @since 4.4.0 
  442. * 
  443. * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array 
  444. * containing the mail recipient, subject, message, headers, and attachments. 
  445. */ 
  446. do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) ); 
  447.  
  448. return false; 
  449. endif; 
  450.  
  451. if ( !function_exists('wp_authenticate') ) : 
  452. /** 
  453. * Authenticate a user, confirming the login credentials are valid. 
  454. * 
  455. * @since 2.5.0 
  456. * @since 4.5.0 `$username` now accepts an email address. 
  457. * 
  458. * @param string $username User's username or email address. 
  459. * @param string $password User's password. 
  460. * @return WP_User|WP_Error WP_User object if the credentials are valid,  
  461. * otherwise WP_Error. 
  462. */ 
  463. function wp_authenticate($username, $password) { 
  464. $username = sanitize_user($username); 
  465. $password = trim($password); 
  466.  
  467. /** 
  468. * Filters whether a set of user login credentials are valid. 
  469. * 
  470. * A WP_User object is returned if the credentials authenticate a user. 
  471. * WP_Error or null otherwise. 
  472. * 
  473. * @since 2.8.0 
  474. * @since 4.5.0 `$username` now accepts an email address. 
  475. * 
  476. * @param null|WP_User|WP_Error $user WP_User if the user is authenticated. 
  477. * WP_Error or null otherwise. 
  478. * @param string $username Username or email address. 
  479. * @param string $password User password 
  480. */ 
  481. $user = apply_filters( 'authenticate', null, $username, $password ); 
  482.  
  483. if ( $user == null ) { 
  484. // TODO what should the error message be? (Or would these even happen?) 
  485. // Only needed if all authentication handlers fail to return anything. 
  486. $user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ) ); 
  487.  
  488. $ignore_codes = array('empty_username', 'empty_password'); 
  489.  
  490. if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) { 
  491. /** 
  492. * Fires after a user login has failed. 
  493. * 
  494. * @since 2.5.0 
  495. * @since 4.5.0 The value of `$username` can now be an email address. 
  496. * 
  497. * @param string $username Username or email address. 
  498. */ 
  499. do_action( 'wp_login_failed', $username ); 
  500.  
  501. return $user; 
  502. endif; 
  503.  
  504. if ( !function_exists('wp_logout') ) : 
  505. /** 
  506. * Log the current user out. 
  507. * 
  508. * @since 2.5.0 
  509. */ 
  510. function wp_logout() { 
  511. wp_destroy_current_session(); 
  512. wp_clear_auth_cookie(); 
  513.  
  514. /** 
  515. * Fires after a user is logged-out. 
  516. * 
  517. * @since 1.5.0 
  518. */ 
  519. do_action( 'wp_logout' ); 
  520. endif; 
  521.  
  522. if ( !function_exists('wp_validate_auth_cookie') ) : 
  523. /** 
  524. * Validates authentication cookie. 
  525. * 
  526. * The checks include making sure that the authentication cookie is set and 
  527. * pulling in the contents (if $cookie is not used). 
  528. * 
  529. * Makes sure the cookie is not expired. Verifies the hash in cookie is what is 
  530. * should be and compares the two. 
  531. * 
  532. * @since 2.5.0 
  533. * 
  534. * @global int $login_grace_period 
  535. * 
  536. * @param string $cookie Optional. If used, will validate contents instead of cookie's 
  537. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 
  538. * @return false|int False if invalid cookie, User ID if valid. 
  539. */ 
  540. function wp_validate_auth_cookie($cookie = '', $scheme = '') { 
  541. if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) { 
  542. /** 
  543. * Fires if an authentication cookie is malformed. 
  544. * 
  545. * @since 2.7.0 
  546. * 
  547. * @param string $cookie Malformed auth cookie. 
  548. * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',  
  549. * or 'logged_in'. 
  550. */ 
  551. do_action( 'auth_cookie_malformed', $cookie, $scheme ); 
  552. return false; 
  553.  
  554. $scheme = $cookie_elements['scheme']; 
  555. $username = $cookie_elements['username']; 
  556. $hmac = $cookie_elements['hmac']; 
  557. $token = $cookie_elements['token']; 
  558. $expired = $expiration = $cookie_elements['expiration']; 
  559.  
  560. // Allow a grace period for POST and Ajax requests 
  561. if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) { 
  562. $expired += HOUR_IN_SECONDS; 
  563.  
  564. // Quick check to see if an honest cookie has expired 
  565. if ( $expired < time() ) { 
  566. /** 
  567. * Fires once an authentication cookie has expired. 
  568. * 
  569. * @since 2.7.0 
  570. * 
  571. * @param array $cookie_elements An array of data for the authentication cookie. 
  572. */ 
  573. do_action( 'auth_cookie_expired', $cookie_elements ); 
  574. return false; 
  575.  
  576. $user = get_user_by('login', $username); 
  577. if ( ! $user ) { 
  578. /** 
  579. * Fires if a bad username is entered in the user authentication process. 
  580. * 
  581. * @since 2.7.0 
  582. * 
  583. * @param array $cookie_elements An array of data for the authentication cookie. 
  584. */ 
  585. do_action( 'auth_cookie_bad_username', $cookie_elements ); 
  586. return false; 
  587.  
  588. $pass_frag = substr($user->user_pass, 8, 4); 
  589.  
  590. $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 
  591.  
  592. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. 
  593. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; 
  594. $hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key ); 
  595.  
  596. if ( ! hash_equals( $hash, $hmac ) ) { 
  597. /** 
  598. * Fires if a bad authentication cookie hash is encountered. 
  599. * 
  600. * @since 2.7.0 
  601. * 
  602. * @param array $cookie_elements An array of data for the authentication cookie. 
  603. */ 
  604. do_action( 'auth_cookie_bad_hash', $cookie_elements ); 
  605. return false; 
  606.  
  607. $manager = WP_Session_Tokens::get_instance( $user->ID ); 
  608. if ( ! $manager->verify( $token ) ) { 
  609. do_action( 'auth_cookie_bad_session_token', $cookie_elements ); 
  610. return false; 
  611.  
  612. // Ajax/POST grace period set above 
  613. if ( $expiration < time() ) { 
  614. $GLOBALS['login_grace_period'] = 1; 
  615.  
  616. /** 
  617. * Fires once an authentication cookie has been validated. 
  618. * 
  619. * @since 2.7.0 
  620. * 
  621. * @param array $cookie_elements An array of data for the authentication cookie. 
  622. * @param WP_User $user User object. 
  623. */ 
  624. do_action( 'auth_cookie_valid', $cookie_elements, $user ); 
  625.  
  626. return $user->ID; 
  627. endif; 
  628.  
  629. if ( !function_exists('wp_generate_auth_cookie') ) : 
  630. /** 
  631. * Generate authentication cookie contents. 
  632. * 
  633. * @since 2.5.0 
  634. * 
  635. * @param int $user_id User ID 
  636. * @param int $expiration The time the cookie expires as a UNIX timestamp. 
  637. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 
  638. * @param string $token User's session token to use for this cookie 
  639. * @return string Authentication cookie contents. Empty string if user does not exist. 
  640. */ 
  641. function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) { 
  642. $user = get_userdata($user_id); 
  643. if ( ! $user ) { 
  644. return ''; 
  645.  
  646. if ( ! $token ) { 
  647. $manager = WP_Session_Tokens::get_instance( $user_id ); 
  648. $token = $manager->create( $expiration ); 
  649.  
  650. $pass_frag = substr($user->user_pass, 8, 4); 
  651.  
  652. $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); 
  653.  
  654. // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. 
  655. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; 
  656. $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key ); 
  657.  
  658. $cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash; 
  659.  
  660. /** 
  661. * Filters the authentication cookie. 
  662. * 
  663. * @since 2.5.0 
  664. * 
  665. * @param string $cookie Authentication cookie. 
  666. * @param int $user_id User ID. 
  667. * @param int $expiration The time the cookie expires as a UNIX timestamp. 
  668. * @param string $scheme Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'. 
  669. * @param string $token User's session token used. 
  670. */ 
  671. return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token ); 
  672. endif; 
  673.  
  674. if ( !function_exists('wp_parse_auth_cookie') ) : 
  675. /** 
  676. * Parse a cookie into its components 
  677. * 
  678. * @since 2.7.0 
  679. * 
  680. * @param string $cookie 
  681. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in 
  682. * @return array|false Authentication cookie components 
  683. */ 
  684. function wp_parse_auth_cookie($cookie = '', $scheme = '') { 
  685. if ( empty($cookie) ) { 
  686. switch ($scheme) { 
  687. case 'auth': 
  688. $cookie_name = AUTH_COOKIE; 
  689. break; 
  690. case 'secure_auth': 
  691. $cookie_name = SECURE_AUTH_COOKIE; 
  692. break; 
  693. case "logged_in": 
  694. $cookie_name = LOGGED_IN_COOKIE; 
  695. break; 
  696. default: 
  697. if ( is_ssl() ) { 
  698. $cookie_name = SECURE_AUTH_COOKIE; 
  699. $scheme = 'secure_auth'; 
  700. } else { 
  701. $cookie_name = AUTH_COOKIE; 
  702. $scheme = 'auth'; 
  703.  
  704. if ( empty($_COOKIE[$cookie_name]) ) 
  705. return false; 
  706. $cookie = $_COOKIE[$cookie_name]; 
  707.  
  708. $cookie_elements = explode('|', $cookie); 
  709. if ( count( $cookie_elements ) !== 4 ) { 
  710. return false; 
  711.  
  712. list( $username, $expiration, $token, $hmac ) = $cookie_elements; 
  713.  
  714. return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' ); 
  715. endif; 
  716.  
  717. if ( !function_exists('wp_set_auth_cookie') ) : 
  718. /** 
  719. * Log in a user by setting authentication cookies. 
  720. * 
  721. * The $remember parameter increases the time that the cookie will be kept. The 
  722. * default the cookie is kept without remembering is two days. When $remember is 
  723. * set, the cookies will be kept for 14 days or two weeks. 
  724. * 
  725. * @since 2.5.0 
  726. * @since 4.3.0 Added the `$token` parameter. 
  727. * 
  728. * @param int $user_id User ID 
  729. * @param bool $remember Whether to remember the user 
  730. * @param mixed $secure Whether the admin cookies should only be sent over HTTPS. 
  731. * Default is_ssl(). 
  732. * @param string $token Optional. User's session token to use for this cookie. 
  733. */ 
  734. function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) { 
  735. if ( $remember ) { 
  736. /** 
  737. * Filters the duration of the authentication cookie expiration period. 
  738. * 
  739. * @since 2.8.0 
  740. * 
  741. * @param int $length Duration of the expiration period in seconds. 
  742. * @param int $user_id User ID. 
  743. * @param bool $remember Whether to remember the user login. Default false. 
  744. */ 
  745. $expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember ); 
  746.  
  747. /** 
  748. * Ensure the browser will continue to send the cookie after the expiration time is reached. 
  749. * Needed for the login grace period in wp_validate_auth_cookie(). 
  750. */ 
  751. $expire = $expiration + ( 12 * HOUR_IN_SECONDS ); 
  752. } else { 
  753. /** This filter is documented in wp-includes/pluggable.php */ 
  754. $expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember ); 
  755. $expire = 0; 
  756.  
  757. if ( '' === $secure ) { 
  758. $secure = is_ssl(); 
  759.  
  760. // Front-end cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS. 
  761. $secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME ); 
  762.  
  763. /** 
  764. * Filters whether the connection is secure. 
  765. * 
  766. * @since 3.1.0 
  767. * 
  768. * @param bool $secure Whether the connection is secure. 
  769. * @param int $user_id User ID. 
  770. */ 
  771. $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id ); 
  772.  
  773. /** 
  774. * Filters whether to use a secure cookie when logged-in. 
  775. * 
  776. * @since 3.1.0 
  777. * 
  778. * @param bool $secure_logged_in_cookie Whether to use a secure cookie when logged-in. 
  779. * @param int $user_id User ID. 
  780. * @param bool $secure Whether the connection is secure. 
  781. */ 
  782. $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure ); 
  783.  
  784. if ( $secure ) { 
  785. $auth_cookie_name = SECURE_AUTH_COOKIE; 
  786. $scheme = 'secure_auth'; 
  787. } else { 
  788. $auth_cookie_name = AUTH_COOKIE; 
  789. $scheme = 'auth'; 
  790.  
  791. if ( '' === $token ) { 
  792. $manager = WP_Session_Tokens::get_instance( $user_id ); 
  793. $token = $manager->create( $expiration ); 
  794.  
  795. $auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token ); 
  796. $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token ); 
  797.  
  798. /** 
  799. * Fires immediately before the authentication cookie is set. 
  800. * 
  801. * @since 2.5.0 
  802. * 
  803. * @param string $auth_cookie Authentication cookie. 
  804. * @param int $expire The time the login grace period expires as a UNIX timestamp. 
  805. * Default is 12 hours past the cookie's expiration time. 
  806. * @param int $expiration The time when the authentication cookie expires as a UNIX timestamp. 
  807. * Default is 14 days from now. 
  808. * @param int $user_id User ID. 
  809. * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth', or 'logged_in'. 
  810. */ 
  811. do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme ); 
  812.  
  813. /** 
  814. * Fires immediately before the logged-in authentication cookie is set. 
  815. * 
  816. * @since 2.6.0 
  817. * 
  818. * @param string $logged_in_cookie The logged-in cookie. 
  819. * @param int $expire The time the login grace period expires as a UNIX timestamp. 
  820. * Default is 12 hours past the cookie's expiration time. 
  821. * @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp. 
  822. * Default is 14 days from now. 
  823. * @param int $user_id User ID. 
  824. * @param string $scheme Authentication scheme. Default 'logged_in'. 
  825. */ 
  826. do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in' ); 
  827.  
  828. /** 
  829. * Allows preventing auth cookies from actually being sent to the client. 
  830. * 
  831. * @since 4.7.4 
  832. * 
  833. * @param bool $send Whether to send auth cookies to the client. 
  834. */ 
  835. if ( ! apply_filters( 'send_auth_cookies', true ) ) { 
  836. return; 
  837.  
  838. setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true); 
  839. setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true); 
  840. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true); 
  841. if ( COOKIEPATH != SITECOOKIEPATH ) 
  842. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true); 
  843. endif; 
  844.  
  845. if ( !function_exists('wp_clear_auth_cookie') ) : 
  846. /** 
  847. * Removes all of the cookies associated with authentication. 
  848. * 
  849. * @since 2.5.0 
  850. */ 
  851. function wp_clear_auth_cookie() { 
  852. /** 
  853. * Fires just before the authentication cookies are cleared. 
  854. * 
  855. * @since 2.7.0 
  856. */ 
  857. do_action( 'clear_auth_cookie' ); 
  858.  
  859. /** This filter is documented in wp-includes/pluggable.php */ 
  860. if ( ! apply_filters( 'send_auth_cookies', true ) ) { 
  861. return; 
  862.  
  863. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); 
  864. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN ); 
  865. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); 
  866. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN ); 
  867. setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
  868. setcookie( LOGGED_IN_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 
  869.  
  870. // Old cookies 
  871. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
  872. setcookie( AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 
  873. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
  874. setcookie( SECURE_AUTH_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 
  875.  
  876. // Even older cookies 
  877. setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
  878. setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
  879. setcookie( USER_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 
  880. setcookie( PASS_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN ); 
  881. endif; 
  882.  
  883. if ( !function_exists('is_user_logged_in') ) : 
  884. /** 
  885. * Checks if the current visitor is a logged in user. 
  886. * 
  887. * @since 2.0.0 
  888. * 
  889. * @return bool True if user is logged in, false if not logged in. 
  890. */ 
  891. function is_user_logged_in() { 
  892. $user = wp_get_current_user(); 
  893.  
  894. return $user->exists(); 
  895. endif; 
  896.  
  897. if ( !function_exists('auth_redirect') ) : 
  898. /** 
  899. * Checks if a user is logged in, if not it redirects them to the login page. 
  900. * 
  901. * @since 1.5.0 
  902. */ 
  903. function auth_redirect() { 
  904. // Checks if a user is logged in, if not redirects them to the login page 
  905.  
  906. $secure = ( is_ssl() || force_ssl_admin() ); 
  907.  
  908. /** 
  909. * Filters whether to use a secure authentication redirect. 
  910. * 
  911. * @since 3.1.0 
  912. * 
  913. * @param bool $secure Whether to use a secure authentication redirect. Default false. 
  914. */ 
  915. $secure = apply_filters( 'secure_auth_redirect', $secure ); 
  916.  
  917. // If https is required and request is http, redirect 
  918. if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { 
  919. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { 
  920. wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); 
  921. exit(); 
  922. } else { 
  923. wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  924. exit(); 
  925.  
  926. /** 
  927. * Filters the authentication redirect scheme. 
  928. * 
  929. * @since 2.9.0 
  930. * 
  931. * @param string $scheme Authentication redirect scheme. Default empty. 
  932. */ 
  933. $scheme = apply_filters( 'auth_redirect_scheme', '' ); 
  934.  
  935. if ( $user_id = wp_validate_auth_cookie( '', $scheme) ) { 
  936. /** 
  937. * Fires before the authentication redirect. 
  938. * 
  939. * @since 2.8.0 
  940. * 
  941. * @param int $user_id User ID. 
  942. */ 
  943. do_action( 'auth_redirect', $user_id ); 
  944.  
  945. // If the user wants ssl but the session is not ssl, redirect. 
  946. if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) { 
  947. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { 
  948. wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); 
  949. exit(); 
  950. } else { 
  951. wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  952. exit(); 
  953.  
  954. return; // The cookie is good so we're done 
  955.  
  956. // The cookie is no good so force login 
  957. nocache_headers(); 
  958.  
  959. $redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  960.  
  961. $login_url = wp_login_url($redirect, true); 
  962.  
  963. wp_redirect($login_url); 
  964. exit(); 
  965. endif; 
  966.  
  967. if ( !function_exists('check_admin_referer') ) : 
  968. /** 
  969. * Makes sure that a user was referred from another admin page. 
  970. * 
  971. * To avoid security exploits. 
  972. * 
  973. * @since 1.2.0 
  974. * 
  975. * @param int|string $action Action nonce. 
  976. * @param string $query_arg Optional. Key to check for nonce in `$_REQUEST` (since 2.5). 
  977. * Default '_wpnonce'. 
  978. * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between 
  979. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 
  980. */ 
  981. function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) { 
  982. if ( -1 == $action ) 
  983. _doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2.0' ); 
  984.  
  985. $adminurl = strtolower(admin_url()); 
  986. $referer = strtolower(wp_get_referer()); 
  987. $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false; 
  988.  
  989. /** 
  990. * Fires once the admin request has been validated or not. 
  991. * 
  992. * @since 1.5.1 
  993. * 
  994. * @param string $action The nonce action. 
  995. * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between 
  996. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 
  997. */ 
  998. do_action( 'check_admin_referer', $action, $result ); 
  999.  
  1000. if ( ! $result && ! ( -1 == $action && strpos( $referer, $adminurl ) === 0 ) ) { 
  1001. wp_nonce_ays( $action ); 
  1002. die(); 
  1003.  
  1004. return $result; 
  1005. endif; 
  1006.  
  1007. if ( !function_exists('check_ajax_referer') ) : 
  1008. /** 
  1009. * Verifies the Ajax request to prevent processing requests external of the blog. 
  1010. * 
  1011. * @since 2.0.3 
  1012. * 
  1013. * @param int|string $action Action nonce. 
  1014. * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false,  
  1015. * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' 
  1016. * (in that order). Default false. 
  1017. * @param bool $die Optional. Whether to die early when the nonce cannot be verified. 
  1018. * Default true. 
  1019. * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between 
  1020. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 
  1021. */ 
  1022. function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) { 
  1023. if ( -1 == $action ) { 
  1024. _doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '4.7' ); 
  1025.  
  1026. $nonce = ''; 
  1027.  
  1028. if ( $query_arg && isset( $_REQUEST[ $query_arg ] ) ) 
  1029. $nonce = $_REQUEST[ $query_arg ]; 
  1030. elseif ( isset( $_REQUEST['_ajax_nonce'] ) ) 
  1031. $nonce = $_REQUEST['_ajax_nonce']; 
  1032. elseif ( isset( $_REQUEST['_wpnonce'] ) ) 
  1033. $nonce = $_REQUEST['_wpnonce']; 
  1034.  
  1035. $result = wp_verify_nonce( $nonce, $action ); 
  1036.  
  1037. /** 
  1038. * Fires once the Ajax request has been validated or not. 
  1039. * 
  1040. * @since 2.1.0 
  1041. * 
  1042. * @param string $action The Ajax nonce action. 
  1043. * @param false|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between 
  1044. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 
  1045. */ 
  1046. do_action( 'check_ajax_referer', $action, $result ); 
  1047.  
  1048. if ( $die && false === $result ) { 
  1049. if ( wp_doing_ajax() ) { 
  1050. wp_die( -1, 403 ); 
  1051. } else { 
  1052. die( '-1' ); 
  1053.  
  1054. return $result; 
  1055. endif; 
  1056.  
  1057. if ( !function_exists('wp_redirect') ) : 
  1058. /** 
  1059. * Redirects to another page. 
  1060. * 
  1061. * Note: wp_redirect() does not exit automatically, and should almost always be 
  1062. * followed by a call to `exit;`: 
  1063. * 
  1064. * wp_redirect( $url ); 
  1065. * exit; 
  1066. * 
  1067. * Exiting can also be selectively manipulated by using wp_redirect() as a conditional 
  1068. * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} hooks: 
  1069. * 
  1070. * if ( wp_redirect( $url ) ) { 
  1071. * exit; 
  1072. * } 
  1073. * 
  1074. * @since 1.5.1 
  1075. * 
  1076. * @global bool $is_IIS 
  1077. * 
  1078. * @param string $location The path to redirect to. 
  1079. * @param int $status Status code to use. 
  1080. * @return bool False if $location is not provided, true otherwise. 
  1081. */ 
  1082. function wp_redirect($location, $status = 302) { 
  1083. global $is_IIS; 
  1084.  
  1085. /** 
  1086. * Filters the redirect location. 
  1087. * 
  1088. * @since 2.1.0 
  1089. * 
  1090. * @param string $location The path to redirect to. 
  1091. * @param int $status Status code to use. 
  1092. */ 
  1093. $location = apply_filters( 'wp_redirect', $location, $status ); 
  1094.  
  1095. /** 
  1096. * Filters the redirect status code. 
  1097. * 
  1098. * @since 2.3.0 
  1099. * 
  1100. * @param int $status Status code to use. 
  1101. * @param string $location The path to redirect to. 
  1102. */ 
  1103. $status = apply_filters( 'wp_redirect_status', $status, $location ); 
  1104.  
  1105. if ( ! $location ) 
  1106. return false; 
  1107.  
  1108. $location = wp_sanitize_redirect($location); 
  1109.  
  1110. if ( !$is_IIS && PHP_SAPI != 'cgi-fcgi' ) 
  1111. status_header($status); // This causes problems on IIS and some FastCGI setups 
  1112.  
  1113. header("Location: $location", true, $status); 
  1114.  
  1115. return true; 
  1116. endif; 
  1117.  
  1118. if ( !function_exists('wp_sanitize_redirect') ) : 
  1119. /** 
  1120. * Sanitizes a URL for use in a redirect. 
  1121. * 
  1122. * @since 2.3.0 
  1123. * 
  1124. * @param string $location The path to redirect to. 
  1125. * @return string Redirect-sanitized URL. 
  1126. **/ 
  1127. function wp_sanitize_redirect($location) { 
  1128. $regex = '/ 
  1129. (?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx 
  1130. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 
  1131. | [\xE1-\xEC][\x80-\xBF]{2} 
  1132. | \xED[\x80-\x9F][\x80-\xBF] 
  1133. | [\xEE-\xEF][\x80-\xBF]{2} 
  1134. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 
  1135. | [\xF1-\xF3][\x80-\xBF]{3} 
  1136. | \xF4[\x80-\x8F][\x80-\xBF]{2} 
  1137. ) {1, 40} # ...one or more times 
  1138. )/x'; 
  1139. $location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location ); 
  1140. $location = preg_replace('|[^a-z0-9-~+_.?#=&;, /:%!*\[\]()@]|i', '', $location); 
  1141. $location = wp_kses_no_null($location); 
  1142.  
  1143. // remove %0d and %0a from location 
  1144. $strip = array('%0d', '%0a', '%0D', '%0A'); 
  1145. return _deep_replace( $strip, $location ); 
  1146.  
  1147. /** 
  1148. * URL encode UTF-8 characters in a URL. 
  1149. * 
  1150. * @ignore 
  1151. * @since 4.2.0 
  1152. * @access private 
  1153. * 
  1154. * @see wp_sanitize_redirect() 
  1155. * 
  1156. * @param array $matches RegEx matches against the redirect location. 
  1157. * @return string URL-encoded version of the first RegEx match. 
  1158. */ 
  1159. function _wp_sanitize_utf8_in_redirect( $matches ) { 
  1160. return urlencode( $matches[0] ); 
  1161. endif; 
  1162.  
  1163. if ( !function_exists('wp_safe_redirect') ) : 
  1164. /** 
  1165. * Performs a safe (local) redirect, using wp_redirect(). 
  1166. * 
  1167. * Checks whether the $location is using an allowed host, if it has an absolute 
  1168. * path. A plugin can therefore set or remove allowed host(s) to or from the 
  1169. * list. 
  1170. * 
  1171. * If the host is not allowed, then the redirect defaults to wp-admin on the siteurl 
  1172. * instead. This prevents malicious redirects which redirect to another host,  
  1173. * but only used in a few places. 
  1174. * 
  1175. * @since 2.3.0 
  1176. * 
  1177. * @param string $location The path to redirect to. 
  1178. * @param int $status Status code to use. 
  1179. */ 
  1180. function wp_safe_redirect($location, $status = 302) { 
  1181.  
  1182. // Need to look at the URL the way it will end up in wp_redirect() 
  1183. $location = wp_sanitize_redirect($location); 
  1184.  
  1185. /** 
  1186. * Filters the redirect fallback URL for when the provided redirect is not safe (local). 
  1187. * 
  1188. * @since 4.3.0 
  1189. * 
  1190. * @param string $fallback_url The fallback URL to use by default. 
  1191. * @param int $status The redirect status. 
  1192. */ 
  1193. $location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) ); 
  1194.  
  1195. wp_redirect($location, $status); 
  1196. endif; 
  1197.  
  1198. if ( !function_exists('wp_validate_redirect') ) : 
  1199. /** 
  1200. * Validates a URL for use in a redirect. 
  1201. * 
  1202. * Checks whether the $location is using an allowed host, if it has an absolute 
  1203. * path. A plugin can therefore set or remove allowed host(s) to or from the 
  1204. * list. 
  1205. * 
  1206. * If the host is not allowed, then the redirect is to $default supplied 
  1207. * 
  1208. * @since 2.8.1 
  1209. * 
  1210. * @param string $location The redirect to validate 
  1211. * @param string $default The value to return if $location is not allowed 
  1212. * @return string redirect-sanitized URL 
  1213. **/ 
  1214. function wp_validate_redirect($location, $default = '') { 
  1215. $location = trim( $location, " \t\n\r\0\x08\x0B" ); 
  1216. // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//' 
  1217. if ( substr($location, 0, 2) == '//' ) 
  1218. $location = 'http:' . $location; 
  1219.  
  1220. // In php 5 parse_url may fail if the URL query part contains http://, bug #38143 
  1221. $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location; 
  1222.  
  1223. // @-operator is used to prevent possible warnings in PHP < 5.3.3. 
  1224. $lp = @parse_url($test); 
  1225.  
  1226. // Give up if malformed URL 
  1227. if ( false === $lp ) 
  1228. return $default; 
  1229.  
  1230. // Allow only http and https schemes. No data:, etc. 
  1231. if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) ) 
  1232. return $default; 
  1233.  
  1234. // Reject if certain components are set but host is not. This catches urls like https:host.com for which parse_url does not set the host field. 
  1235. if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) { 
  1236. return $default; 
  1237.  
  1238. // Reject malformed components parse_url() can return on odd inputs. 
  1239. foreach ( array( 'user', 'pass', 'host' ) as $component ) { 
  1240. if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) { 
  1241. return $default; 
  1242.  
  1243. $wpp = parse_url(home_url()); 
  1244.  
  1245. /** 
  1246. * Filters the whitelist of hosts to redirect to. 
  1247. * 
  1248. * @since 2.3.0 
  1249. * 
  1250. * @param array $hosts An array of allowed hosts. 
  1251. * @param bool|string $host The parsed host; empty if not isset. 
  1252. */ 
  1253. $allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '' ); 
  1254.  
  1255. if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) ) 
  1256. $location = $default; 
  1257.  
  1258. return $location; 
  1259. endif; 
  1260.  
  1261. if ( ! function_exists('wp_notify_postauthor') ) : 
  1262. /** 
  1263. * Notify an author (and/or others) of a comment/trackback/pingback on a post. 
  1264. * 
  1265. * @since 1.0.0 
  1266. * 
  1267. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. 
  1268. * @param string $deprecated Not used 
  1269. * @return bool True on completion. False if no email addresses were specified. 
  1270. */ 
  1271. function wp_notify_postauthor( $comment_id, $deprecated = null ) { 
  1272. if ( null !== $deprecated ) { 
  1273. _deprecated_argument( __FUNCTION__, '3.8.0' ); 
  1274.  
  1275. $comment = get_comment( $comment_id ); 
  1276. if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) 
  1277. return false; 
  1278.  
  1279. $post = get_post( $comment->comment_post_ID ); 
  1280. $author = get_userdata( $post->post_author ); 
  1281.  
  1282. // Who to notify? By default, just the post author, but others can be added. 
  1283. $emails = array(); 
  1284. if ( $author ) { 
  1285. $emails[] = $author->user_email; 
  1286.  
  1287. /** 
  1288. * Filters the list of email addresses to receive a comment notification. 
  1289. * 
  1290. * By default, only post authors are notified of comments. This filter allows 
  1291. * others to be added. 
  1292. * 
  1293. * @since 3.7.0 
  1294. * 
  1295. * @param array $emails An array of email addresses to receive a comment notification. 
  1296. * @param int $comment_id The comment ID. 
  1297. */ 
  1298. $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID ); 
  1299. $emails = array_filter( $emails ); 
  1300.  
  1301. // If there are no addresses to send the comment to, bail. 
  1302. if ( ! count( $emails ) ) { 
  1303. return false; 
  1304.  
  1305. // Facilitate unsetting below without knowing the keys. 
  1306. $emails = array_flip( $emails ); 
  1307.  
  1308. /** 
  1309. * Filters whether to notify comment authors of their comments on their own posts. 
  1310. * 
  1311. * By default, comment authors aren't notified of their comments on their own 
  1312. * posts. This filter allows you to override that. 
  1313. * 
  1314. * @since 3.8.0 
  1315. * 
  1316. * @param bool $notify Whether to notify the post author of their own comment. 
  1317. * Default false. 
  1318. * @param int $comment_id The comment ID. 
  1319. */ 
  1320. $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID ); 
  1321.  
  1322. // The comment was left by the author 
  1323. if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) { 
  1324. unset( $emails[ $author->user_email ] ); 
  1325.  
  1326. // The author moderated a comment on their own post 
  1327. if ( $author && ! $notify_author && $post->post_author == get_current_user_id() ) { 
  1328. unset( $emails[ $author->user_email ] ); 
  1329.  
  1330. // The post author is no longer a member of the blog 
  1331. if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { 
  1332. unset( $emails[ $author->user_email ] ); 
  1333.  
  1334. // If there's no email to send the comment to, bail, otherwise flip array back around for use below 
  1335. if ( ! count( $emails ) ) { 
  1336. return false; 
  1337. } else { 
  1338. $emails = array_flip( $emails ); 
  1339.  
  1340. $switched_locale = switch_to_locale( get_locale() ); 
  1341.  
  1342. $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 
  1343.  
  1344. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  1345. // we want to reverse this for the plain text arena of emails. 
  1346. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  1347. $comment_content = wp_specialchars_decode( $comment->comment_content ); 
  1348.  
  1349. switch ( $comment->comment_type ) { 
  1350. case 'trackback': 
  1351. /** translators: 1: Post title */ 
  1352. $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; 
  1353. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  1354. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1355. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1356. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  1357. $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; 
  1358. /** translators: 1: blog name, 2: post title */ 
  1359. $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title ); 
  1360. break; 
  1361. case 'pingback': 
  1362. /** translators: 1: Post title */ 
  1363. $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; 
  1364. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  1365. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1366. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1367. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  1368. $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; 
  1369. /** translators: 1: blog name, 2: post title */ 
  1370. $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title ); 
  1371. break; 
  1372. default: // Comments 
  1373. $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; 
  1374. /** translators: 1: comment author, 2: author IP, 3: author domain */ 
  1375. $notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1376. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 
  1377. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1378. $notify_message .= sprintf( __('Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  1379. $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; 
  1380. /** translators: 1: blog name, 2: post title */ 
  1381. $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title ); 
  1382. break; 
  1383. $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n"; 
  1384. $notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n"; 
  1385.  
  1386. if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { 
  1387. if ( EMPTY_TRASH_DAYS ) { 
  1388. $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  1389. } else { 
  1390. $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  1391. $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  1392.  
  1393. $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); 
  1394.  
  1395. if ( '' == $comment->comment_author ) { 
  1396. $from = "From: \"$blogname\" <$wp_email>"; 
  1397. if ( '' != $comment->comment_author_email ) 
  1398. $reply_to = "Reply-To: $comment->comment_author_email"; 
  1399. } else { 
  1400. $from = "From: \"$comment->comment_author\" <$wp_email>"; 
  1401. if ( '' != $comment->comment_author_email ) 
  1402. $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; 
  1403.  
  1404. $message_headers = "$from\n" 
  1405. . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; 
  1406.  
  1407. if ( isset($reply_to) ) 
  1408. $message_headers .= $reply_to . "\n"; 
  1409.  
  1410. /** 
  1411. * Filters the comment notification email text. 
  1412. * 
  1413. * @since 1.5.2 
  1414. * 
  1415. * @param string $notify_message The comment notification email text. 
  1416. * @param int $comment_id Comment ID. 
  1417. */ 
  1418. $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); 
  1419.  
  1420. /** 
  1421. * Filters the comment notification email subject. 
  1422. * 
  1423. * @since 1.5.2 
  1424. * 
  1425. * @param string $subject The comment notification email subject. 
  1426. * @param int $comment_id Comment ID. 
  1427. */ 
  1428. $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); 
  1429.  
  1430. /** 
  1431. * Filters the comment notification email headers. 
  1432. * 
  1433. * @since 1.5.2 
  1434. * 
  1435. * @param string $message_headers Headers for the comment notification email. 
  1436. * @param int $comment_id Comment ID. 
  1437. */ 
  1438. $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); 
  1439.  
  1440. foreach ( $emails as $email ) { 
  1441. @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 
  1442.  
  1443. if ( $switched_locale ) { 
  1444. restore_previous_locale(); 
  1445.  
  1446. return true; 
  1447. endif; 
  1448.  
  1449. if ( !function_exists('wp_notify_moderator') ) : 
  1450. /** 
  1451. * Notifies the moderator of the site about a new comment that is awaiting approval. 
  1452. * 
  1453. * @since 1.0.0 
  1454. * 
  1455. * @global wpdb $wpdb WordPress database abstraction object. 
  1456. * 
  1457. * Uses the {@see 'notify_moderator'} filter to determine whether the site moderator 
  1458. * should be notified, overriding the site setting. 
  1459. * 
  1460. * @param int $comment_id Comment ID. 
  1461. * @return true Always returns true. 
  1462. */ 
  1463. function wp_notify_moderator($comment_id) { 
  1464. global $wpdb; 
  1465.  
  1466. $maybe_notify = get_option( 'moderation_notify' ); 
  1467.  
  1468. /** 
  1469. * Filters whether to send the site moderator email notifications, overriding the site setting. 
  1470. * 
  1471. * @since 4.4.0 
  1472. * 
  1473. * @param bool $maybe_notify Whether to notify blog moderator. 
  1474. * @param int $comment_ID The id of the comment for the notification. 
  1475. */ 
  1476. $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id ); 
  1477.  
  1478. if ( ! $maybe_notify ) { 
  1479. return true; 
  1480.  
  1481. $comment = get_comment($comment_id); 
  1482. $post = get_post($comment->comment_post_ID); 
  1483. $user = get_userdata( $post->post_author ); 
  1484. // Send to the administration and to the post author if the author can modify the comment. 
  1485. $emails = array( get_option( 'admin_email' ) ); 
  1486. if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { 
  1487. if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) 
  1488. $emails[] = $user->user_email; 
  1489.  
  1490. $switched_locale = switch_to_locale( get_locale() ); 
  1491.  
  1492. $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 
  1493. $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'"); 
  1494.  
  1495. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  1496. // we want to reverse this for the plain text arena of emails. 
  1497. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  1498. $comment_content = wp_specialchars_decode( $comment->comment_content ); 
  1499.  
  1500. switch ( $comment->comment_type ) { 
  1501. case 'trackback': 
  1502. /** translators: 1: Post title */ 
  1503. $notify_message = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 
  1504. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 
  1505. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  1506. $notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1507. /** translators: 1: Trackback/pingback/comment author URL */ 
  1508. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1509. $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment_content . "\r\n\r\n"; 
  1510. break; 
  1511. case 'pingback': 
  1512. /** translators: 1: Post title */ 
  1513. $notify_message = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 
  1514. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 
  1515. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  1516. $notify_message .= sprintf( __( 'Website: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1517. /** translators: 1: Trackback/pingback/comment author URL */ 
  1518. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1519. $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment_content . "\r\n\r\n"; 
  1520. break; 
  1521. default: // Comments 
  1522. /** translators: 1: Post title */ 
  1523. $notify_message = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; 
  1524. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; 
  1525. /** translators: 1: Comment author name, 2: comment author's IP, 3: comment author IP's hostname */ 
  1526. $notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  1527. /** translators: 1: Comment author URL */ 
  1528. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 
  1529. /** translators: 1: Trackback/pingback/comment author URL */ 
  1530. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  1531. /** translators: 1: Comment text */ 
  1532. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  1533. break; 
  1534.  
  1535. /** translators: Comment moderation. 1: Comment action URL */ 
  1536. $notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 
  1537.  
  1538. if ( EMPTY_TRASH_DAYS ) { 
  1539. /** translators: Comment moderation. 1: Comment action URL */ 
  1540. $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 
  1541. } else { 
  1542. /** translators: Comment moderation. 1: Comment action URL */ 
  1543. $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 
  1544.  
  1545. /** translators: Comment moderation. 1: Comment action URL */ 
  1546. $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n"; 
  1547.  
  1548. /** translators: Comment moderation. 1: Number of comments awaiting approval */ 
  1549. $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',  
  1550. 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n"; 
  1551. $notify_message .= admin_url( "edit-comments.php?comment_status=moderated#wpbody-content" ) . "\r\n"; 
  1552.  
  1553. /** translators: Comment moderation notification email subject. 1: Site name, 2: Post title */ 
  1554. $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title ); 
  1555. $message_headers = ''; 
  1556.  
  1557. /** 
  1558. * Filters the list of recipients for comment moderation emails. 
  1559. * 
  1560. * @since 3.7.0 
  1561. * 
  1562. * @param array $emails List of email addresses to notify for comment moderation. 
  1563. * @param int $comment_id Comment ID. 
  1564. */ 
  1565. $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id ); 
  1566.  
  1567. /** 
  1568. * Filters the comment moderation email text. 
  1569. * 
  1570. * @since 1.5.2 
  1571. * 
  1572. * @param string $notify_message Text of the comment moderation email. 
  1573. * @param int $comment_id Comment ID. 
  1574. */ 
  1575. $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id ); 
  1576.  
  1577. /** 
  1578. * Filters the comment moderation email subject. 
  1579. * 
  1580. * @since 1.5.2 
  1581. * 
  1582. * @param string $subject Subject of the comment moderation email. 
  1583. * @param int $comment_id Comment ID. 
  1584. */ 
  1585. $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id ); 
  1586.  
  1587. /** 
  1588. * Filters the comment moderation email headers. 
  1589. * 
  1590. * @since 2.8.0 
  1591. * 
  1592. * @param string $message_headers Headers for the comment moderation email. 
  1593. * @param int $comment_id Comment ID. 
  1594. */ 
  1595. $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id ); 
  1596.  
  1597. foreach ( $emails as $email ) { 
  1598. @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 
  1599.  
  1600. if ( $switched_locale ) { 
  1601. restore_previous_locale(); 
  1602.  
  1603. return true; 
  1604. endif; 
  1605.  
  1606. if ( !function_exists('wp_password_change_notification') ) : 
  1607. /** 
  1608. * Notify the blog admin of a user changing password, normally via email. 
  1609. * 
  1610. * @since 2.7.0 
  1611. * 
  1612. * @param WP_User $user User object. 
  1613. */ 
  1614. function wp_password_change_notification( $user ) { 
  1615. // send a copy of password change notification to the admin 
  1616. // but check to see if it's the admin whose password we're changing, and skip this 
  1617. if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) { 
  1618. /** translators: %s: user name */ 
  1619. $message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n"; 
  1620. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  1621. // we want to reverse this for the plain text arena of emails. 
  1622. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  1623. /** translators: %s: site title */ 
  1624. wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Password Changed' ), $blogname ), $message ); 
  1625. endif; 
  1626.  
  1627. if ( !function_exists('wp_new_user_notification') ) : 
  1628. /** 
  1629. * Email login credentials to a newly-registered user. 
  1630. * 
  1631. * A new user registration notification is also sent to admin email. 
  1632. * 
  1633. * @since 2.0.0 
  1634. * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. 
  1635. * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. 
  1636. * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. 
  1637. * 
  1638. * @global wpdb $wpdb WordPress database object for queries. 
  1639. * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. 
  1640. * 
  1641. * @param int $user_id User ID. 
  1642. * @param null $deprecated Not used (argument deprecated). 
  1643. * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty 
  1644. * string (admin only), 'user', or 'both' (admin and user). Default empty. 
  1645. */ 
  1646. function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) { 
  1647. if ( $deprecated !== null ) { 
  1648. _deprecated_argument( __FUNCTION__, '4.3.1' ); 
  1649.  
  1650. global $wpdb, $wp_hasher; 
  1651. $user = get_userdata( $user_id ); 
  1652.  
  1653. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  1654. // we want to reverse this for the plain text arena of emails. 
  1655. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  1656.  
  1657. if ( 'user' !== $notify ) { 
  1658. $switched_locale = switch_to_locale( get_locale() ); 
  1659. $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n"; 
  1660. $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; 
  1661. $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n"; 
  1662.  
  1663. @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message ); 
  1664.  
  1665. if ( $switched_locale ) { 
  1666. restore_previous_locale(); 
  1667.  
  1668. // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. 
  1669. if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) { 
  1670. return; 
  1671.  
  1672. // Generate something random for a password reset key. 
  1673. $key = wp_generate_password( 20, false ); 
  1674.  
  1675. /** This action is documented in wp-login.php */ 
  1676. do_action( 'retrieve_password_key', $user->user_login, $key ); 
  1677.  
  1678. // Now insert the key, hashed, into the DB. 
  1679. if ( empty( $wp_hasher ) ) { 
  1680. $wp_hasher = new PasswordHash( 8, true ); 
  1681. $hashed = time() . ':' . $wp_hasher->HashPassword( $key ); 
  1682. $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) ); 
  1683.  
  1684. $switched_locale = switch_to_locale( get_user_locale( $user ) ); 
  1685.  
  1686. $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; 
  1687. $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; 
  1688. $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n"; 
  1689.  
  1690. $message .= wp_login_url() . "\r\n"; 
  1691.  
  1692. wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message); 
  1693.  
  1694. if ( $switched_locale ) { 
  1695. restore_previous_locale(); 
  1696. endif; 
  1697.  
  1698. if ( !function_exists('wp_nonce_tick') ) : 
  1699. /** 
  1700. * Get the time-dependent variable for nonce creation. 
  1701. * 
  1702. * A nonce has a lifespan of two ticks. Nonces in their second tick may be 
  1703. * updated, e.g. by autosave. 
  1704. * 
  1705. * @since 2.5.0 
  1706. * 
  1707. * @return float Float value rounded up to the next highest integer. 
  1708. */ 
  1709. function wp_nonce_tick() { 
  1710. /** 
  1711. * Filters the lifespan of nonces in seconds. 
  1712. * 
  1713. * @since 2.5.0 
  1714. * 
  1715. * @param int $lifespan Lifespan of nonces in seconds. Default 86, 400 seconds, or one day. 
  1716. */ 
  1717. $nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS ); 
  1718.  
  1719. return ceil(time() / ( $nonce_life / 2 )); 
  1720. endif; 
  1721.  
  1722. if ( !function_exists('wp_verify_nonce') ) : 
  1723. /** 
  1724. * Verify that correct nonce was used with time limit. 
  1725. * 
  1726. * The user is given an amount of time to use the token, so therefore, since the 
  1727. * UID and $action remain the same, the independent variable is the time. 
  1728. * 
  1729. * @since 2.0.3 
  1730. * 
  1731. * @param string $nonce Nonce that was used in the form to verify 
  1732. * @param string|int $action Should give context to what is taking place and be the same when nonce was created. 
  1733. * @return false|int False if the nonce is invalid, 1 if the nonce is valid and generated between 
  1734. * 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago. 
  1735. */ 
  1736. function wp_verify_nonce( $nonce, $action = -1 ) { 
  1737. $nonce = (string) $nonce; 
  1738. $user = wp_get_current_user(); 
  1739. $uid = (int) $user->ID; 
  1740. if ( ! $uid ) { 
  1741. /** 
  1742. * Filters whether the user who generated the nonce is logged out. 
  1743. * 
  1744. * @since 3.5.0 
  1745. * 
  1746. * @param int $uid ID of the nonce-owning user. 
  1747. * @param string $action The nonce action. 
  1748. */ 
  1749. $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); 
  1750.  
  1751. if ( empty( $nonce ) ) { 
  1752. return false; 
  1753.  
  1754. $token = wp_get_session_token(); 
  1755. $i = wp_nonce_tick(); 
  1756.  
  1757. // Nonce generated 0-12 hours ago 
  1758. $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 ); 
  1759. if ( hash_equals( $expected, $nonce ) ) { 
  1760. return 1; 
  1761.  
  1762. // Nonce generated 12-24 hours ago 
  1763. $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); 
  1764. if ( hash_equals( $expected, $nonce ) ) { 
  1765. return 2; 
  1766.  
  1767. /** 
  1768. * Fires when nonce verification fails. 
  1769. * 
  1770. * @since 4.4.0 
  1771. * 
  1772. * @param string $nonce The invalid nonce. 
  1773. * @param string|int $action The nonce action. 
  1774. * @param WP_User $user The current user object. 
  1775. * @param string $token The user's session token. 
  1776. */ 
  1777. do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token ); 
  1778.  
  1779. // Invalid nonce 
  1780. return false; 
  1781. endif; 
  1782.  
  1783. if ( !function_exists('wp_create_nonce') ) : 
  1784. /** 
  1785. * Creates a cryptographic token tied to a specific action, user, user session,  
  1786. * and window of time. 
  1787. * 
  1788. * @since 2.0.3 
  1789. * @since 4.0.0 Session tokens were integrated with nonce creation 
  1790. * 
  1791. * @param string|int $action Scalar value to add context to the nonce. 
  1792. * @return string The token. 
  1793. */ 
  1794. function wp_create_nonce($action = -1) { 
  1795. $user = wp_get_current_user(); 
  1796. $uid = (int) $user->ID; 
  1797. if ( ! $uid ) { 
  1798. /** This filter is documented in wp-includes/pluggable.php */ 
  1799. $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); 
  1800.  
  1801. $token = wp_get_session_token(); 
  1802. $i = wp_nonce_tick(); 
  1803.  
  1804. return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); 
  1805. endif; 
  1806.  
  1807. if ( !function_exists('wp_salt') ) : 
  1808. /** 
  1809. * Get salt to add to hashes. 
  1810. * 
  1811. * Salts are created using secret keys. Secret keys are located in two places: 
  1812. * in the database and in the wp-config.php file. The secret key in the database 
  1813. * is randomly generated and will be appended to the secret keys in wp-config.php. 
  1814. * 
  1815. * The secret keys in wp-config.php should be updated to strong, random keys to maximize 
  1816. * security. Below is an example of how the secret key constants are defined. 
  1817. * Do not paste this example directly into wp-config.php. Instead, have a 
  1818. * {@link https://api.wordpress.org/secret-key/1.1/salt/ secret key created} just 
  1819. * for you. 
  1820. * 
  1821. * define('AUTH_KEY', ' Xakm<o xQy rw4EMsLKM-?!T+, PFF})H4lzcW57AF0U@N@< >M%G4Yt>f`z]MON'); 
  1822. * define('SECURE_AUTH_KEY', 'LzJ}op]mr|6+![P}Ak:uNdJCJZd>(Hx.-Mh#Tz)pCIU#uGEnfFz|f ;;eU%/U^O~'); 
  1823. * define('LOGGED_IN_KEY', '|i|Ux`9<p-h$aFf(qnT:sDO:D1P^wZ$$/Ra@miTJi9G;ddp_<q}6H1)o|a +&JCM'); 
  1824. * define('NONCE_KEY', '%:R{[P|, s.KuMltH5}cI;/k<Gx~j!f0I)m_sIyu+&NJZ)-iO>z7X>QYR0Z_XnZ@|'); 
  1825. * define('AUTH_SALT', 'eZyT)-Naw]F8CwA*VaW#q*|.)g@o}||wf~@C-YSt}(dh_r6EbI#A, y|nU2{B#JBW'); 
  1826. * define('SECURE_AUTH_SALT', '!=oLUTXh, QW=H `}`L|9/^4-3 STz}, T(w}W<I`.JjPi)<Bmf1v, HpGe}T1:Xt7n'); 
  1827. * define('LOGGED_IN_SALT', '+XSqHc;@Q*K_b|Z?NC[3H!!EONbh.n<+=uKR:>*c(u`g~EJBf#8u#R{mUEZrozmm'); 
  1828. * define('NONCE_SALT', 'h`GXHhD>SLWVfg1(1(N{;.V!MoE(SfbA_ksP@&`+AycHcAV$+?@3q+rxV{%^VyKT'); 
  1829. * 
  1830. * Salting passwords helps against tools which has stored hashed values of 
  1831. * common dictionary strings. The added values makes it harder to crack. 
  1832. * 
  1833. * @since 2.5.0 
  1834. * 
  1835. * @link https://api.wordpress.org/secret-key/1.1/salt/ Create secrets for wp-config.php 
  1836. * 
  1837. * @staticvar array $cached_salts 
  1838. * @staticvar array $duplicated_keys 
  1839. * 
  1840. * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce) 
  1841. * @return string Salt value 
  1842. */ 
  1843. function wp_salt( $scheme = 'auth' ) { 
  1844. static $cached_salts = array(); 
  1845. if ( isset( $cached_salts[ $scheme ] ) ) { 
  1846. /** 
  1847. * Filters the WordPress salt. 
  1848. * 
  1849. * @since 2.5.0 
  1850. * 
  1851. * @param string $cached_salt Cached salt for the given scheme. 
  1852. * @param string $scheme Authentication scheme. Values include 'auth',  
  1853. * 'secure_auth', 'logged_in', and 'nonce'. 
  1854. */ 
  1855. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); 
  1856.  
  1857. static $duplicated_keys; 
  1858. if ( null === $duplicated_keys ) { 
  1859. $duplicated_keys = array( 'put your unique phrase here' => true ); 
  1860. foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) { 
  1861. foreach ( array( 'KEY', 'SALT' ) as $second ) { 
  1862. if ( ! defined( "{$first}_{$second}" ) ) { 
  1863. continue; 
  1864. $value = constant( "{$first}_{$second}" ); 
  1865. $duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] ); 
  1866.  
  1867. $values = array( 
  1868. 'key' => '',  
  1869. 'salt' => '' 
  1870. ); 
  1871. if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) { 
  1872. $values['key'] = SECRET_KEY; 
  1873. if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) { 
  1874. $values['salt'] = SECRET_SALT; 
  1875.  
  1876. if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) { 
  1877. foreach ( array( 'key', 'salt' ) as $type ) { 
  1878. $const = strtoupper( "{$scheme}_{$type}" ); 
  1879. if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) { 
  1880. $values[ $type ] = constant( $const ); 
  1881. } elseif ( ! $values[ $type ] ) { 
  1882. $values[ $type ] = get_site_option( "{$scheme}_{$type}" ); 
  1883. if ( ! $values[ $type ] ) { 
  1884. $values[ $type ] = wp_generate_password( 64, true, true ); 
  1885. update_site_option( "{$scheme}_{$type}", $values[ $type ] ); 
  1886. } else { 
  1887. if ( ! $values['key'] ) { 
  1888. $values['key'] = get_site_option( 'secret_key' ); 
  1889. if ( ! $values['key'] ) { 
  1890. $values['key'] = wp_generate_password( 64, true, true ); 
  1891. update_site_option( 'secret_key', $values['key'] ); 
  1892. $values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] ); 
  1893.  
  1894. $cached_salts[ $scheme ] = $values['key'] . $values['salt']; 
  1895.  
  1896. /** This filter is documented in wp-includes/pluggable.php */ 
  1897. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); 
  1898. endif; 
  1899.  
  1900. if ( !function_exists('wp_hash') ) : 
  1901. /** 
  1902. * Get hash of given string. 
  1903. * 
  1904. * @since 2.0.3 
  1905. * 
  1906. * @param string $data Plain text to hash 
  1907. * @param string $scheme Authentication scheme (auth, secure_auth, logged_in, nonce) 
  1908. * @return string Hash of $data 
  1909. */ 
  1910. function wp_hash($data, $scheme = 'auth') { 
  1911. $salt = wp_salt($scheme); 
  1912.  
  1913. return hash_hmac('md5', $data, $salt); 
  1914. endif; 
  1915.  
  1916. if ( !function_exists('wp_hash_password') ) : 
  1917. /** 
  1918. * Create a hash (encrypt) of a plain text password. 
  1919. * 
  1920. * For integration with other applications, this function can be overwritten to 
  1921. * instead use the other package password checking algorithm. 
  1922. * 
  1923. * @since 2.5.0 
  1924. * 
  1925. * @global PasswordHash $wp_hasher PHPass object 
  1926. * 
  1927. * @param string $password Plain text user password to hash 
  1928. * @return string The hash string of the password 
  1929. */ 
  1930. function wp_hash_password($password) { 
  1931. global $wp_hasher; 
  1932.  
  1933. if ( empty($wp_hasher) ) { 
  1934. // By default, use the portable hash from phpass 
  1935. $wp_hasher = new PasswordHash(8, true); 
  1936.  
  1937. return $wp_hasher->HashPassword( trim( $password ) ); 
  1938. endif; 
  1939.  
  1940. if ( !function_exists('wp_check_password') ) : 
  1941. /** 
  1942. * Checks the plaintext password against the encrypted Password. 
  1943. * 
  1944. * Maintains compatibility between old version and the new cookie authentication 
  1945. * protocol using PHPass library. The $hash parameter is the encrypted password 
  1946. * and the function compares the plain text password when encrypted similarly 
  1947. * against the already encrypted password to see if they match. 
  1948. * 
  1949. * For integration with other applications, this function can be overwritten to 
  1950. * instead use the other package password checking algorithm. 
  1951. * 
  1952. * @since 2.5.0 
  1953. * 
  1954. * @global PasswordHash $wp_hasher PHPass object used for checking the password 
  1955. * against the $hash + $password 
  1956. * @uses PasswordHash::CheckPassword 
  1957. * 
  1958. * @param string $password Plaintext user's password 
  1959. * @param string $hash Hash of the user's password to check against. 
  1960. * @param string|int $user_id Optional. User ID. 
  1961. * @return bool False, if the $password does not match the hashed password 
  1962. */ 
  1963. function wp_check_password($password, $hash, $user_id = '') { 
  1964. global $wp_hasher; 
  1965.  
  1966. // If the hash is still md5... 
  1967. if ( strlen($hash) <= 32 ) { 
  1968. $check = hash_equals( $hash, md5( $password ) ); 
  1969. if ( $check && $user_id ) { 
  1970. // Rehash using new hash. 
  1971. wp_set_password($password, $user_id); 
  1972. $hash = wp_hash_password($password); 
  1973.  
  1974. /** 
  1975. * Filters whether the plaintext password matches the encrypted password. 
  1976. * 
  1977. * @since 2.5.0 
  1978. * 
  1979. * @param bool $check Whether the passwords match. 
  1980. * @param string $password The plaintext password. 
  1981. * @param string $hash The hashed password. 
  1982. * @param string|int $user_id User ID. Can be empty. 
  1983. */ 
  1984. return apply_filters( 'check_password', $check, $password, $hash, $user_id ); 
  1985.  
  1986. // If the stored hash is longer than an MD5, presume the 
  1987. // new style phpass portable hash. 
  1988. if ( empty($wp_hasher) ) { 
  1989. // By default, use the portable hash from phpass 
  1990. $wp_hasher = new PasswordHash(8, true); 
  1991.  
  1992. $check = $wp_hasher->CheckPassword($password, $hash); 
  1993.  
  1994. /** This filter is documented in wp-includes/pluggable.php */ 
  1995. return apply_filters( 'check_password', $check, $password, $hash, $user_id ); 
  1996. endif; 
  1997.  
  1998. if ( !function_exists('wp_generate_password') ) : 
  1999. /** 
  2000. * Generates a random password drawn from the defined set of characters. 
  2001. * 
  2002. * @since 2.5.0 
  2003. * 
  2004. * @param int $length Optional. The length of password to generate. Default 12. 
  2005. * @param bool $special_chars Optional. Whether to include standard special characters. 
  2006. * Default true. 
  2007. * @param bool $extra_special_chars Optional. Whether to include other special characters. 
  2008. * Used when generating secret keys and salts. Default false. 
  2009. * @return string The random password. 
  2010. */ 
  2011. function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { 
  2012. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
  2013. if ( $special_chars ) 
  2014. $chars .= '!@#$%^&*()'; 
  2015. if ( $extra_special_chars ) 
  2016. $chars .= '-_ []{}<>~`+=, .;:/?|'; 
  2017.  
  2018. $password = ''; 
  2019. for ( $i = 0; $i < $length; $i++ ) { 
  2020. $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1); 
  2021.  
  2022. /** 
  2023. * Filters the randomly-generated password. 
  2024. * 
  2025. * @since 3.0.0 
  2026. * 
  2027. * @param string $password The generated password. 
  2028. */ 
  2029. return apply_filters( 'random_password', $password ); 
  2030. endif; 
  2031.  
  2032. if ( !function_exists('wp_rand') ) : 
  2033. /** 
  2034. * Generates a random number 
  2035. * 
  2036. * @since 2.6.2 
  2037. * @since 4.4.0 Uses PHP7 random_int() or the random_compat library if available. 
  2038. * 
  2039. * @global string $rnd_value 
  2040. * @staticvar string $seed 
  2041. * @staticvar bool $external_rand_source_available 
  2042. * 
  2043. * @param int $min Lower limit for the generated number 
  2044. * @param int $max Upper limit for the generated number 
  2045. * @return int A random number between min and max 
  2046. */ 
  2047. function wp_rand( $min = 0, $max = 0 ) { 
  2048. global $rnd_value; 
  2049.  
  2050. // Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. 
  2051. $max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff 
  2052.  
  2053. // We only handle Ints, floats are truncated to their integer value. 
  2054. $min = (int) $min; 
  2055. $max = (int) $max; 
  2056.  
  2057. // Use PHP's CSPRNG, or a compatible method 
  2058. static $use_random_int_functionality = true; 
  2059. if ( $use_random_int_functionality ) { 
  2060. try { 
  2061. $_max = ( 0 != $max ) ? $max : $max_random_number; 
  2062. // wp_rand() can accept arguments in either order, PHP cannot. 
  2063. $_max = max( $min, $_max ); 
  2064. $_min = min( $min, $_max ); 
  2065. $val = random_int( $_min, $_max ); 
  2066. if ( false !== $val ) { 
  2067. return absint( $val ); 
  2068. } else { 
  2069. $use_random_int_functionality = false; 
  2070. } catch ( Error $e ) { 
  2071. $use_random_int_functionality = false; 
  2072. } catch ( Exception $e ) { 
  2073. $use_random_int_functionality = false; 
  2074.  
  2075. // Reset $rnd_value after 14 uses 
  2076. // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value 
  2077. if ( strlen($rnd_value) < 8 ) { 
  2078. if ( defined( 'WP_SETUP_CONFIG' ) ) 
  2079. static $seed = ''; 
  2080. else 
  2081. $seed = get_transient('random_seed'); 
  2082. $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed ); 
  2083. $rnd_value .= sha1($rnd_value); 
  2084. $rnd_value .= sha1($rnd_value . $seed); 
  2085. $seed = md5($seed . $rnd_value); 
  2086. if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { 
  2087. set_transient( 'random_seed', $seed ); 
  2088.  
  2089. // Take the first 8 digits for our value 
  2090. $value = substr($rnd_value, 0, 8); 
  2091.  
  2092. // Strip the first eight, leaving the remainder for the next call to wp_rand(). 
  2093. $rnd_value = substr($rnd_value, 8); 
  2094.  
  2095. $value = abs(hexdec($value)); 
  2096.  
  2097. // Reduce the value to be within the min - max range 
  2098. if ( $max != 0 ) 
  2099. $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); 
  2100.  
  2101. return abs(intval($value)); 
  2102. endif; 
  2103.  
  2104. if ( !function_exists('wp_set_password') ) : 
  2105. /** 
  2106. * Updates the user's password with a new encrypted one. 
  2107. * 
  2108. * For integration with other applications, this function can be overwritten to 
  2109. * instead use the other package password checking algorithm. 
  2110. * 
  2111. * Please note: This function should be used sparingly and is really only meant for single-time 
  2112. * application. Leveraging this improperly in a plugin or theme could result in an endless loop 
  2113. * of password resets if precautions are not taken to ensure it does not execute on every page load. 
  2114. * 
  2115. * @since 2.5.0 
  2116. * 
  2117. * @global wpdb $wpdb WordPress database abstraction object. 
  2118. * 
  2119. * @param string $password The plaintext new user password 
  2120. * @param int $user_id User ID 
  2121. */ 
  2122. function wp_set_password( $password, $user_id ) { 
  2123. global $wpdb; 
  2124.  
  2125. $hash = wp_hash_password( $password ); 
  2126. $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) ); 
  2127.  
  2128. wp_cache_delete($user_id, 'users'); 
  2129. endif; 
  2130.  
  2131. if ( !function_exists( 'get_avatar' ) ) : 
  2132. /** 
  2133. * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post. 
  2134. * 
  2135. * @since 2.5.0 
  2136. * @since 4.2.0 Optional `$args` parameter added. 
  2137. * 
  2138. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  2139. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  2140. * @param int $size Optional. Height and width of the avatar image file in pixels. Default 96. 
  2141. * @param string $default Optional. URL for the default image or a default type. Accepts '404' 
  2142. * (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' 
  2143. * (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),  
  2144. * 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF),  
  2145. * or 'gravatar_default' (the Gravatar logo). Default is the value of the 
  2146. * 'avatar_default' option, with a fallback of 'mystery'. 
  2147. * @param string $alt Optional. Alternative text to use in <img> tag. Default empty. 
  2148. * @param array $args { 
  2149. * Optional. Extra arguments to retrieve the avatar. 
  2150. * 
  2151. * @type int $height Display height of the avatar in pixels. Defaults to $size. 
  2152. * @type int $width Display width of the avatar in pixels. Defaults to $size. 
  2153. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 
  2154. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 
  2155. * judged in that order. Default is the value of the 'avatar_rating' option. 
  2156. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 
  2157. * Default null. 
  2158. * @type array|string $class Array or string of additional classes to add to the <img> element. 
  2159. * Default null. 
  2160. * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. 
  2161. * Default false. 
  2162. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. 
  2163. * } 
  2164. * @return false|string `<img>` tag for the user's avatar. False on failure. 
  2165. */ 
  2166. function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { 
  2167. $defaults = array( 
  2168. // get_avatar_data() args. 
  2169. 'size' => 96,  
  2170. 'height' => null,  
  2171. 'width' => null,  
  2172. 'default' => get_option( 'avatar_default', 'mystery' ),  
  2173. 'force_default' => false,  
  2174. 'rating' => get_option( 'avatar_rating' ),  
  2175. 'scheme' => null,  
  2176. 'alt' => '',  
  2177. 'class' => null,  
  2178. 'force_display' => false,  
  2179. 'extra_attr' => '',  
  2180. ); 
  2181.  
  2182. if ( empty( $args ) ) { 
  2183. $args = array(); 
  2184.  
  2185. $args['size'] = (int) $size; 
  2186. $args['default'] = $default; 
  2187. $args['alt'] = $alt; 
  2188.  
  2189. $args = wp_parse_args( $args, $defaults ); 
  2190.  
  2191. if ( empty( $args['height'] ) ) { 
  2192. $args['height'] = $args['size']; 
  2193. if ( empty( $args['width'] ) ) { 
  2194. $args['width'] = $args['size']; 
  2195.  
  2196. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 
  2197. $id_or_email = get_comment( $id_or_email ); 
  2198.  
  2199. /** 
  2200. * Filters whether to retrieve the avatar URL early. 
  2201. * 
  2202. * Passing a non-null value will effectively short-circuit get_avatar(), passing 
  2203. * the value through the {@see 'get_avatar'} filter and returning early. 
  2204. * 
  2205. * @since 4.2.0 
  2206. * 
  2207. * @param string $avatar HTML for the user's avatar. Default null. 
  2208. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  2209. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  2210. * @param array $args Arguments passed to get_avatar_url(), after processing. 
  2211. */ 
  2212. $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args ); 
  2213.  
  2214. if ( ! is_null( $avatar ) ) { 
  2215. /** This filter is documented in wp-includes/pluggable.php */ 
  2216. return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); 
  2217.  
  2218. if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) { 
  2219. return false; 
  2220.  
  2221. $url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) ); 
  2222.  
  2223. $args = get_avatar_data( $id_or_email, $args ); 
  2224.  
  2225. $url = $args['url']; 
  2226.  
  2227. if ( ! $url || is_wp_error( $url ) ) { 
  2228. return false; 
  2229.  
  2230. $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' ); 
  2231.  
  2232. if ( ! $args['found_avatar'] || $args['force_default'] ) { 
  2233. $class[] = 'avatar-default'; 
  2234.  
  2235. if ( $args['class'] ) { 
  2236. if ( is_array( $args['class'] ) ) { 
  2237. $class = array_merge( $class, $args['class'] ); 
  2238. } else { 
  2239. $class[] = $args['class']; 
  2240.  
  2241. $avatar = sprintf( 
  2242. "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",  
  2243. esc_attr( $args['alt'] ),  
  2244. esc_url( $url ),  
  2245. esc_attr( "$url2x 2x" ),  
  2246. esc_attr( join( ' ', $class ) ),  
  2247. (int) $args['height'],  
  2248. (int) $args['width'],  
  2249. $args['extra_attr'] 
  2250. ); 
  2251.  
  2252. /** 
  2253. * Filters the avatar to retrieve. 
  2254. * 
  2255. * @since 2.5.0 
  2256. * @since 4.2.0 The `$args` parameter was added. 
  2257. * 
  2258. * @param string $avatar <img> tag for the user's avatar. 
  2259. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  2260. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  2261. * @param int $size Square avatar width and height in pixels to retrieve. 
  2262. * @param string $alt Alternative text to use in the avatar image tag. 
  2263. * Default empty. 
  2264. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  2265. */ 
  2266. return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); 
  2267. endif; 
  2268.  
  2269. if ( !function_exists( 'wp_text_diff' ) ) : 
  2270. /** 
  2271. * Displays a human readable HTML representation of the difference between two strings. 
  2272. * 
  2273. * The Diff is available for getting the changes between versions. The output is 
  2274. * HTML, so the primary use is for displaying the changes. If the two strings 
  2275. * are equivalent, then an empty string will be returned. 
  2276. * 
  2277. * The arguments supported and can be changed are listed below. 
  2278. * 
  2279. * 'title' : Default is an empty string. Titles the diff in a manner compatible 
  2280. * with the output. 
  2281. * 'title_left' : Default is an empty string. Change the HTML to the left of the 
  2282. * title. 
  2283. * 'title_right' : Default is an empty string. Change the HTML to the right of 
  2284. * the title. 
  2285. * 
  2286. * @since 2.6.0 
  2287. * 
  2288. * @see wp_parse_args() Used to change defaults to user defined settings. 
  2289. * @uses Text_Diff 
  2290. * @uses WP_Text_Diff_Renderer_Table 
  2291. * 
  2292. * @param string $left_string "old" (left) version of string 
  2293. * @param string $right_string "new" (right) version of string 
  2294. * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults. 
  2295. * @return string Empty string if strings are equivalent or HTML with differences. 
  2296. */ 
  2297. function wp_text_diff( $left_string, $right_string, $args = null ) { 
  2298. $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' ); 
  2299. $args = wp_parse_args( $args, $defaults ); 
  2300.  
  2301. if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) 
  2302. require( ABSPATH . WPINC . '/wp-diff.php' ); 
  2303.  
  2304. $left_string = normalize_whitespace($left_string); 
  2305. $right_string = normalize_whitespace($right_string); 
  2306.  
  2307. $left_lines = explode("\n", $left_string); 
  2308. $right_lines = explode("\n", $right_string); 
  2309. $text_diff = new Text_Diff($left_lines, $right_lines); 
  2310. $renderer = new WP_Text_Diff_Renderer_Table( $args ); 
  2311. $diff = $renderer->render($text_diff); 
  2312.  
  2313. if ( !$diff ) 
  2314. return ''; 
  2315.  
  2316. $r = "<table class='diff'>\n"; 
  2317.  
  2318. if ( ! empty( $args[ 'show_split_view' ] ) ) { 
  2319. $r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />"; 
  2320. } else { 
  2321. $r .= "<col class='content' />"; 
  2322.  
  2323. if ( $args['title'] || $args['title_left'] || $args['title_right'] ) 
  2324. $r .= "<thead>"; 
  2325. if ( $args['title'] ) 
  2326. $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n"; 
  2327. if ( $args['title_left'] || $args['title_right'] ) { 
  2328. $r .= "<tr class='diff-sub-title'>\n"; 
  2329. $r .= "\t<td></td><th>$args[title_left]</th>\n"; 
  2330. $r .= "\t<td></td><th>$args[title_right]</th>\n"; 
  2331. $r .= "</tr>\n"; 
  2332. if ( $args['title'] || $args['title_left'] || $args['title_right'] ) 
  2333. $r .= "</thead>\n"; 
  2334.  
  2335. $r .= "<tbody>\n$diff\n</tbody>\n"; 
  2336. $r .= "</table>"; 
  2337.  
  2338. return $r; 
  2339. endif; 
.