Advertisement
Guest User

Untitled

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