Advertisement
Guest User

Untitled

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