Advertisement
Guest User

Untitled

a guest
Jun 6th, 2012
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 59.91 KB | None | 0 0
  1. <?php /**
  2. * These functions can be replaced via plugins. If plugins do not redefine these
  3. * functions, then these will be used instead.
  4. *
  5. * @package WordPress
  6. */
  7.  
  8. if ( !function_exists('wp_set_current_user') ) :
  9. /**
  10. * Changes the current user by ID or name.
  11. *
  12. * Set $id to null and specify a name if you do not know a user's ID.
  13. *
  14. * Some WordPress functionality is based on the current user and not based on
  15. * the signed in user. Therefore, it opens the ability to edit and perform
  16. * actions on users who aren't signed in.
  17. *
  18. * @since 2.0.3
  19. * @global object $current_user The current user object which holds the user data.
  20. * @uses do_action() Calls 'set_current_user' hook after setting the current user.
  21. *
  22. * @param int $id User ID
  23. * @param string $name User's username
  24. * @return WP_User Current user User object
  25. */
  26. function wp_set_current_user($id, $name = '') {
  27. global $current_user;
  28.  
  29. if ( isset($current_user) && ($id == $current_user->ID) )
  30. return $current_user;
  31.  
  32. $current_user = new WP_User($id, $name);
  33.  
  34. setup_userdata($current_user->ID);
  35.  
  36. do_action('set_current_user');
  37.  
  38. return $current_user;
  39. }
  40. endif;
  41.  
  42. if ( !function_exists('wp_get_current_user') ) :
  43. /**
  44. * Retrieve the current user object.
  45. *
  46. * @since 2.0.3
  47. *
  48. * @return WP_User Current user WP_User object
  49. */
  50. function wp_get_current_user() {
  51. global $current_user;
  52.  
  53. get_currentuserinfo();
  54.  
  55. return $current_user;
  56. }
  57. endif;
  58.  
  59. if ( !function_exists('get_currentuserinfo') ) :
  60. /**
  61. * Populate global variables with information about the currently logged in user.
  62. *
  63. * Will set the current user, if the current user is not set. The current user
  64. * will be set to the logged in person. If no user is logged in, then it will
  65. * set the current user to 0, which is invalid and won't have any permissions.
  66. *
  67. * @since 0.71
  68. * @uses $current_user Checks if the current user is set
  69. * @uses wp_validate_auth_cookie() Retrieves current logged in user.
  70. *
  71. * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
  72. */
  73. function get_currentuserinfo() {
  74. global $current_user;
  75.  
  76. if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
  77. return false;
  78.  
  79. if ( ! empty($current_user) )
  80. return;
  81.  
  82. if ( ! $user = wp_validate_auth_cookie() ) {
  83. if ( is_blog_admin() || is_network_admin() || empty($_COOKIE[LOGGED_IN_COOKIE]) || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') ) {
  84. wp_set_current_user(0);
  85. return false;
  86. }
  87. }
  88.  
  89. wp_set_current_user($user);
  90. }
  91. endif;
  92.  
  93. if ( !function_exists('get_userdata') ) :
  94. /**
  95. * Retrieve user info by user ID.
  96. *
  97. * @since 0.71
  98. *
  99. * @param int $user_id User ID
  100. * @return bool|object False on failure, WP_User object on success
  101. */
  102. function get_userdata( $user_id ) {
  103. return get_user_by( 'id', $user_id );
  104. }
  105. endif;
  106.  
  107. if ( !function_exists('get_user_by') ) :
  108. /**
  109. * Retrieve user info by a given field
  110. *
  111. * @since 2.8.0
  112. *
  113. * @param string $field The field to retrieve the user with. id | slug | email | login
  114. * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
  115. * @return bool|object False on failure, WP_User object on success
  116. */
  117. function get_user_by( $field, $value ) {
  118. $userdata = WP_User::get_data_by( $field, $value );
  119.  
  120. if ( !$userdata )
  121. return false;
  122.  
  123. $user = new WP_User;
  124. $user->init( $userdata );
  125.  
  126. return $user;
  127. }
  128. endif;
  129.  
  130. if ( !function_exists('cache_users') ) :
  131. /**
  132. * Retrieve info for user lists to prevent multiple queries by get_userdata()
  133. *
  134. * @since 3.0.0
  135. *
  136. * @param array $user_ids User ID numbers list
  137. */
  138. function cache_users( $user_ids ) {
  139. global $wpdb;
  140.  
  141. $clean = array();
  142. foreach ( $user_ids as $id ) {
  143. $id = (int) $id;
  144. if ( !wp_cache_get( $id, 'users' ) ) {
  145. $clean[] = $id;
  146. }
  147. }
  148.  
  149. if ( empty( $clean ) )
  150. return;
  151.  
  152. $list = implode( ',', $clean );
  153.  
  154. $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
  155.  
  156. $ids = array();
  157. foreach ( $users as $user ) {
  158. update_user_caches( $user );
  159. $ids[] = $user->ID;
  160. }
  161. update_meta_cache( 'user', $ids );
  162. }
  163. endif;
  164.  
  165. if ( !function_exists( 'wp_mail' ) ) :
  166. /**
  167. * Send mail, similar to PHP's mail
  168. *
  169. * A true return value does not automatically mean that the user received the
  170. * email successfully. It just only means that the method used was able to
  171. * process the request without any errors.
  172. *
  173. * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
  174. * creating a from address like 'Name <email@address.com>' when both are set. If
  175. * just 'wp_mail_from' is set, then just the email address will be used with no
  176. * name.
  177. *
  178. * The default content type is 'text/plain' which does not allow using HTML.
  179. * However, you can set the content type of the email by using the
  180. * 'wp_mail_content_type' filter.
  181. *
  182. * The default charset is based on the charset used on the blog. The charset can
  183. * be set using the 'wp_mail_charset' filter.
  184. *
  185. * @since 1.2.1
  186. * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
  187. * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
  188. * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
  189. * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
  190. * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
  191. * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
  192. * phpmailer object.
  193. * @uses PHPMailer
  194. *
  195. * @param string|array $to Array or comma-separated list of email addresses to send message.
  196. * @param string $subject Email subject
  197. * @param string $message Message contents
  198. * @param string|array $headers Optional. Additional headers.
  199. * @param string|array $attachments Optional. Files to attach.
  200. * @return bool Whether the email contents were sent successfully.
  201. */
  202. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  203. // Compact the input, apply the filters, and extract them back out
  204. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
  205.  
  206. if ( !is_array($attachments) )
  207. $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
  208.  
  209. global $phpmailer;
  210.  
  211. // (Re)create it, if it's gone missing
  212. if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
  213. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  214. require_once ABSPATH . WPINC . '/class-smtp.php';
  215. $phpmailer = new PHPMailer( true );
  216. }
  217.  
  218. // Headers
  219. if ( empty( $headers ) ) {
  220. $headers = array();
  221. } else {
  222. if ( !is_array( $headers ) ) {
  223. // Explode the headers out, so this function can take both
  224. // string headers and an array of headers.
  225. $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
  226. } else {
  227. $tempheaders = $headers;
  228. }
  229. $headers = array();
  230. $cc = array();
  231. $bcc = array();
  232.  
  233. // If it's actually got contents
  234. if ( !empty( $tempheaders ) ) {
  235. // Iterate through the raw headers
  236. foreach ( (array) $tempheaders as $header ) {
  237. if ( strpos($header, ':') === false ) {
  238. if ( false !== stripos( $header, 'boundary=' ) ) {
  239. $parts = preg_split('/boundary=/i', trim( $header ) );
  240. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  241. }
  242. continue;
  243. }
  244. // Explode them out
  245. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  246.  
  247. // Cleanup crew
  248. $name = trim( $name );
  249. $content = trim( $content );
  250.  
  251. switch ( strtolower( $name ) ) {
  252. // Mainly for legacy -- process a From: header if it's there
  253. case 'from':
  254. if ( strpos($content, '<' ) !== false ) {
  255. // So... making my life hard again?
  256. $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
  257. $from_name = str_replace( '"', '', $from_name );
  258. $from_name = trim( $from_name );
  259.  
  260. $from_email = substr( $content, strpos( $content, '<' ) + 1 );
  261. $from_email = str_replace( '>', '', $from_email );
  262. $from_email = trim( $from_email );
  263. } else {
  264. $from_email = trim( $content );
  265. }
  266. break;
  267. case 'content-type':
  268. if ( strpos( $content, ';' ) !== false ) {
  269. list( $type, $charset ) = explode( ';', $content );
  270. $content_type = trim( $type );
  271. if ( false !== stripos( $charset, 'charset=' ) ) {
  272. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
  273. } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
  274. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
  275. $charset = '';
  276. }
  277. } else {
  278. $content_type = trim( $content );
  279. }
  280. break;
  281. case 'cc':
  282. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  283. break;
  284. case 'bcc':
  285. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  286. break;
  287. default:
  288. // Add it to our grand headers array
  289. $headers[trim( $name )] = trim( $content );
  290. break;
  291. }
  292. }
  293. }
  294. }
  295.  
  296. // Empty out the values that may be set
  297. $phpmailer->ClearAddresses();
  298. $phpmailer->ClearAllRecipients();
  299. $phpmailer->ClearAttachments();
  300. $phpmailer->ClearBCCs();
  301. $phpmailer->ClearCCs();
  302. $phpmailer->ClearCustomHeaders();
  303. $phpmailer->ClearReplyTos();
  304.  
  305. // From email and name
  306. // If we don't have a name from the input headers
  307. if ( !isset( $from_name ) )
  308. $from_name = 'WordPress';
  309.  
  310. /* If we don't have an email from the input headers default to wordpress@$sitename
  311. * Some hosts will block outgoing mail from this address if it doesn't exist but
  312. * there's no easy alternative. Defaulting to admin_email might appear to be another
  313. * option but some hosts may refuse to relay mail from an unknown domain. See
  314. * http://trac.wordpress.org/ticket/5007.
  315. */
  316.  
  317. if ( !isset( $from_email ) ) {
  318. // Get the site domain and get rid of www.
  319. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  320. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  321. $sitename = substr( $sitename, 4 );
  322. }
  323.  
  324. $from_email = 'wordpress@' . $sitename;
  325. }
  326.  
  327. // Plugin authors can override the potentially troublesome default
  328. $phpmailer->From = apply_filters( 'wp_mail_from' , $from_email );
  329. $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
  330.  
  331. // Set destination addresses
  332. if ( !is_array( $to ) )
  333. $to = explode( ',', $to );
  334.  
  335. foreach ( (array) $to as $recipient ) {
  336. try {
  337. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  338. $recipient_name = '';
  339. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  340. if ( count( $matches ) == 3 ) {
  341. $recipient_name = $matches[1];
  342. $recipient = $matches[2];
  343. }
  344. }
  345. $phpmailer->AddAddress( $recipient, $recipient_name);
  346. } catch ( phpmailerException $e ) {
  347. continue;
  348. }
  349. }
  350.  
  351. // Set mail's subject and body
  352. $phpmailer->Subject = $subject;
  353. $phpmailer->Body = $message;
  354.  
  355. // Add any CC and BCC recipients
  356. if ( !empty( $cc ) ) {
  357. foreach ( (array) $cc as $recipient ) {
  358. try {
  359. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  360. $recipient_name = '';
  361. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  362. if ( count( $matches ) == 3 ) {
  363. $recipient_name = $matches[1];
  364. $recipient = $matches[2];
  365. }
  366. }
  367. $phpmailer->AddCc( $recipient, $recipient_name );
  368. } catch ( phpmailerException $e ) {
  369. continue;
  370. }
  371. }
  372. }
  373.  
  374. if ( !empty( $bcc ) ) {
  375. foreach ( (array) $bcc as $recipient) {
  376. try {
  377. // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
  378. $recipient_name = '';
  379. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  380. if ( count( $matches ) == 3 ) {
  381. $recipient_name = $matches[1];
  382. $recipient = $matches[2];
  383. }
  384. }
  385. $phpmailer->AddBcc( $recipient, $recipient_name );
  386. } catch ( phpmailerException $e ) {
  387. continue;
  388. }
  389. }
  390. }
  391.  
  392. // Set to use PHP's mail()
  393. $phpmailer->IsMail();
  394.  
  395. // Set Content-Type and charset
  396. // If we don't have a content-type from the input headers
  397. if ( !isset( $content_type ) )
  398. $content_type = 'text/plain';
  399.  
  400. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  401.  
  402. $phpmailer->ContentType = $content_type;
  403.  
  404. // Set whether it's plaintext, depending on $content_type
  405. if ( 'text/html' == $content_type )
  406. $phpmailer->IsHTML( true );
  407.  
  408. // If we don't have a charset from the input headers
  409. if ( !isset( $charset ) )
  410. $charset = get_bloginfo( 'charset' );
  411.  
  412. // Set the content-type and charset
  413. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  414.  
  415. // Set custom headers
  416. if ( !empty( $headers ) ) {
  417. foreach( (array) $headers as $name => $content ) {
  418. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  419. }
  420.  
  421. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
  422. $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
  423. }
  424.  
  425. if ( !empty( $attachments ) ) {
  426. foreach ( $attachments as $attachment ) {
  427. try {
  428. $phpmailer->AddAttachment($attachment);
  429. } catch ( phpmailerException $e ) {
  430. continue;
  431. }
  432. }
  433. }
  434.  
  435. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  436.  
  437. // Send!
  438. try {
  439. $phpmailer->Send();
  440. } catch ( phpmailerException $e ) {
  441. return false;
  442. }
  443.  
  444. return true;
  445. }
  446. endif;
  447.  
  448. if ( !function_exists('wp_authenticate') ) :
  449. /**
  450. * Checks a user's login information and logs them in if it checks out.
  451. *
  452. * @since 2.5.0
  453. *
  454. * @param string $username User's username
  455. * @param string $password User's password
  456. * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
  457. */
  458. function wp_authenticate($username, $password) {
  459. $username = sanitize_user($username);
  460. $password = trim($password);
  461.  
  462. $user = apply_filters('authenticate', null, $username, $password);
  463.  
  464. if ( $user == null ) {
  465. // TODO what should the error message be? (Or would these even happen?)
  466. // Only needed if all authentication handlers fail to return anything.
  467. $user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
  468. }
  469.  
  470. $ignore_codes = array('empty_username', 'empty_password');
  471.  
  472. if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
  473. do_action('wp_login_failed', $username);
  474. }
  475.  
  476. return $user;
  477. }
  478. endif;
  479.  
  480. if ( !function_exists('wp_logout') ) :
  481. /**
  482. * Log the current user out.
  483. *
  484. * @since 2.5.0
  485. */
  486. function wp_logout() {
  487. wp_clear_auth_cookie();
  488. do_action('wp_logout');
  489. }
  490. endif;
  491.  
  492. if ( !function_exists('wp_validate_auth_cookie') ) :
  493. /**
  494. * Validates authentication cookie.
  495. *
  496. * The checks include making sure that the authentication cookie is set and
  497. * pulling in the contents (if $cookie is not used).
  498. *
  499. * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
  500. * should be and compares the two.
  501. *
  502. * @since 2.5
  503. *
  504. * @param string $cookie Optional. If used, will validate contents instead of cookie's
  505. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  506. * @return bool|int False if invalid cookie, User ID if valid.
  507. */
  508. function wp_validate_auth_cookie($cookie = '', $scheme = '') {
  509. if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
  510. do_action('auth_cookie_malformed', $cookie, $scheme);
  511. return false;
  512. }
  513.  
  514. extract($cookie_elements, EXTR_OVERWRITE);
  515.  
  516. $expired = $expiration;
  517.  
  518. // Allow a grace period for POST and AJAX requests
  519. if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
  520. $expired += 3600;
  521.  
  522. // Quick check to see if an honest cookie has expired
  523. if ( $expired < time() ) {
  524. do_action('auth_cookie_expired', $cookie_elements);
  525. return false;
  526. }
  527.  
  528. $user = get_user_by('login', $username);
  529. if ( ! $user ) {
  530. do_action('auth_cookie_bad_username', $cookie_elements);
  531. return false;
  532. }
  533.  
  534. $pass_frag = substr($user->user_pass, 8, 4);
  535.  
  536. $key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
  537. $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
  538.  
  539. if ( $hmac != $hash ) {
  540. do_action('auth_cookie_bad_hash', $cookie_elements);
  541. return false;
  542. }
  543.  
  544. if ( $expiration < time() ) // AJAX/POST grace period set above
  545. $GLOBALS['login_grace_period'] = 1;
  546.  
  547. do_action('auth_cookie_valid', $cookie_elements, $user);
  548.  
  549. return $user->ID;
  550. }
  551. endif;
  552.  
  553. if ( !function_exists('wp_generate_auth_cookie') ) :
  554. /**
  555. * Generate authentication cookie contents.
  556. *
  557. * @since 2.5
  558. * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
  559. * and expiration of cookie.
  560. *
  561. * @param int $user_id User ID
  562. * @param int $expiration Cookie expiration in seconds
  563. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  564. * @return string Authentication cookie contents
  565. */
  566. function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
  567. $user = get_userdata($user_id);
  568.  
  569. $pass_frag = substr($user->user_pass, 8, 4);
  570.  
  571. $key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
  572. $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
  573.  
  574. $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
  575.  
  576. return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
  577. }
  578. endif;
  579.  
  580. if ( !function_exists('wp_parse_auth_cookie') ) :
  581. /**
  582. * Parse a cookie into its components
  583. *
  584. * @since 2.7
  585. *
  586. * @param string $cookie
  587. * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
  588. * @return array Authentication cookie components
  589. */
  590. function wp_parse_auth_cookie($cookie = '', $scheme = '') {
  591. if ( empty($cookie) ) {
  592. switch ($scheme){
  593. case 'auth':
  594. $cookie_name = AUTH_COOKIE;
  595. break;
  596. case 'secure_auth':
  597. $cookie_name = SECURE_AUTH_COOKIE;
  598. break;
  599. case "logged_in":
  600. $cookie_name = LOGGED_IN_COOKIE;
  601. break;
  602. default:
  603. if ( is_ssl() ) {
  604. $cookie_name = SECURE_AUTH_COOKIE;
  605. $scheme = 'secure_auth';
  606. } else {
  607. $cookie_name = AUTH_COOKIE;
  608. $scheme = 'auth';
  609. }
  610. }
  611.  
  612. if ( empty($_COOKIE[$cookie_name]) )
  613. return false;
  614. $cookie = $_COOKIE[$cookie_name];
  615. }
  616.  
  617. $cookie_elements = explode('|', $cookie);
  618. if ( count($cookie_elements) != 3 )
  619. return false;
  620.  
  621. list($username, $expiration, $hmac) = $cookie_elements;
  622.  
  623. return compact('username', 'expiration', 'hmac', 'scheme');
  624. }
  625. endif;
  626.  
  627. if ( !function_exists('wp_set_auth_cookie') ) :
  628. /**
  629. * Sets the authentication cookies based User ID.
  630. *
  631. * The $remember parameter increases the time that the cookie will be kept. The
  632. * default the cookie is kept without remembering is two days. When $remember is
  633. * set, the cookies will be kept for 14 days or two weeks.
  634. *
  635. * @since 2.5
  636. *
  637. * @param int $user_id User ID
  638. * @param bool $remember Whether to remember the user
  639. */
  640. function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
  641. if ( $remember ) {
  642. $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
  643. } else {
  644. $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
  645. $expire = 0;
  646. }
  647.  
  648. if ( '' === $secure )
  649. $secure = is_ssl();
  650.  
  651. $secure = apply_filters('secure_auth_cookie', $secure, $user_id);
  652. $secure_logged_in_cookie = apply_filters('secure_logged_in_cookie', false, $user_id, $secure);
  653.  
  654. if ( $secure ) {
  655. $auth_cookie_name = SECURE_AUTH_COOKIE;
  656. $scheme = 'secure_auth';
  657. } else {
  658. $auth_cookie_name = AUTH_COOKIE;
  659. $scheme = 'auth';
  660. }
  661.  
  662. $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
  663. $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
  664.  
  665. do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
  666. do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
  667.  
  668. setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
  669. setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
  670. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
  671. if ( COOKIEPATH != SITECOOKIEPATH )
  672. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
  673. }
  674. endif;
  675.  
  676. if ( !function_exists('wp_clear_auth_cookie') ) :
  677. /**
  678. * Removes all of the cookies associated with authentication.
  679. *
  680. * @since 2.5
  681. */
  682. function wp_clear_auth_cookie() {
  683. do_action('clear_auth_cookie');
  684.  
  685. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  686. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  687. setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
  688. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
  689. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  690. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  691.  
  692. // Old cookies
  693. setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  694. setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  695. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  696. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  697.  
  698. // Even older cookies
  699. setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  700. setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  701. setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  702. setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  703. }
  704. endif;
  705.  
  706. if ( !function_exists('is_user_logged_in') ) :
  707. /**
  708. * Checks if the current visitor is a logged in user.
  709. *
  710. * @since 2.0.0
  711. *
  712. * @return bool True if user is logged in, false if not logged in.
  713. */
  714. function is_user_logged_in() {
  715. $user = wp_get_current_user();
  716.  
  717. if ( empty( $user->ID ) )
  718. return false;
  719.  
  720. return true;
  721. }
  722. endif;
  723.  
  724. if ( !function_exists('auth_redirect') ) :
  725. /**
  726. * Checks if a user is logged in, if not it redirects them to the login page.
  727. *
  728. * @since 1.5
  729. */
  730. function auth_redirect() {
  731. // Checks if a user is logged in, if not redirects them to the login page
  732.  
  733. $secure = ( is_ssl() || force_ssl_admin() );
  734.  
  735. $secure = apply_filters('secure_auth_redirect', $secure);
  736.  
  737. // If https is required and request is http, redirect
  738. if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
  739. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  740. wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
  741. exit();
  742. } else {
  743. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  744. exit();
  745. }
  746. }
  747.  
  748. if ( is_user_admin() )
  749. $scheme = 'logged_in';
  750. else
  751. $scheme = apply_filters( 'auth_redirect_scheme', '' );
  752.  
  753. if ( $user_id = wp_validate_auth_cookie( '', $scheme) ) {
  754. do_action('auth_redirect', $user_id);
  755.  
  756. // If the user wants ssl but the session is not ssl, redirect.
  757. if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
  758. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  759. wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
  760. exit();
  761. } else {
  762. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  763. exit();
  764. }
  765. }
  766.  
  767. return; // The cookie is good so we're done
  768. }
  769.  
  770. // The cookie is no good so force login
  771. nocache_headers();
  772.  
  773. if ( is_ssl() )
  774. $proto = 'https://';
  775. else
  776. $proto = 'http://';
  777.  
  778. $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  779.  
  780. $login_url = wp_login_url($redirect, true);
  781.  
  782. wp_redirect($login_url);
  783. exit();
  784. }
  785. endif;
  786.  
  787. if ( !function_exists('check_admin_referer') ) :
  788. /**
  789. * Makes sure that a user was referred from another admin page.
  790. *
  791. * To avoid security exploits.
  792. *
  793. * @since 1.2.0
  794. * @uses do_action() Calls 'check_admin_referer' on $action.
  795. *
  796. * @param string $action Action nonce
  797. * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
  798. */
  799. function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
  800. if ( -1 == $action )
  801. _doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
  802.  
  803. $adminurl = strtolower(admin_url());
  804. $referer = strtolower(wp_get_referer());
  805. $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
  806. if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
  807. wp_nonce_ays($action);
  808. die();
  809. }
  810. do_action('check_admin_referer', $action, $result);
  811. return $result;
  812. }endif;
  813.  
  814. if ( !function_exists('check_ajax_referer') ) :
  815. /**
  816. * Verifies the AJAX request to prevent processing requests external of the blog.
  817. *
  818. * @since 2.0.3
  819. *
  820. * @param string $action Action nonce
  821. * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
  822. */
  823. function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
  824. if ( $query_arg )
  825. $nonce = $_REQUEST[$query_arg];
  826. else
  827. $nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
  828.  
  829. $result = wp_verify_nonce( $nonce, $action );
  830.  
  831. if ( $die && false == $result )
  832. die('-1');
  833.  
  834. do_action('check_ajax_referer', $action, $result);
  835.  
  836. return $result;
  837. }
  838. endif;
  839.  
  840. if ( !function_exists('wp_redirect') ) :
  841. /**
  842. * Redirects to another page.
  843. *
  844. * @since 1.5.1
  845. * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
  846. *
  847. * @param string $location The path to redirect to
  848. * @param int $status Status code to use
  849. * @return bool False if $location is not set
  850. */
  851. function wp_redirect($location, $status = 302) {
  852. global $is_IIS;
  853.  
  854. $location = apply_filters('wp_redirect', $location, $status);
  855. $status = apply_filters('wp_redirect_status', $status, $location);
  856.  
  857. if ( !$location ) // allows the wp_redirect filter to cancel a redirect
  858. return false;
  859.  
  860. $location = wp_sanitize_redirect($location);
  861.  
  862. if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
  863. status_header($status); // This causes problems on IIS and some FastCGI setups
  864.  
  865. header("Location: $location", true, $status);
  866. }
  867. endif;
  868.  
  869. if ( !function_exists('wp_sanitize_redirect') ) :
  870. /**
  871. * Sanitizes a URL for use in a redirect.
  872. *
  873. * @since 2.3
  874. *
  875. * @return string redirect-sanitized URL
  876. **/
  877. function wp_sanitize_redirect($location) {
  878. $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
  879. $location = wp_kses_no_null($location);
  880.  
  881. // remove %0d and %0a from location
  882. $strip = array('%0d', '%0a', '%0D', '%0A');
  883. $location = _deep_replace($strip, $location);
  884. return $location;
  885. }
  886. endif;
  887.  
  888. if ( !function_exists('wp_safe_redirect') ) :
  889. /**
  890. * Performs a safe (local) redirect, using wp_redirect().
  891. *
  892. * Checks whether the $location is using an allowed host, if it has an absolute
  893. * path. A plugin can therefore set or remove allowed host(s) to or from the
  894. * list.
  895. *
  896. * If the host is not allowed, then the redirect is to wp-admin on the siteurl
  897. * instead. This prevents malicious redirects which redirect to another host,
  898. * but only used in a few places.
  899. *
  900. * @since 2.3
  901. * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
  902. *
  903. * @return void Does not return anything
  904. **/
  905. function wp_safe_redirect($location, $status = 302) {
  906.  
  907. // Need to look at the URL the way it will end up in wp_redirect()
  908. $location = wp_sanitize_redirect($location);
  909.  
  910. $location = wp_validate_redirect($location, admin_url());
  911.  
  912. wp_redirect($location, $status);
  913. }
  914. endif;
  915.  
  916. if ( !function_exists('wp_validate_redirect') ) :
  917. /**
  918. * Validates a URL for use in a redirect.
  919. *
  920. * Checks whether the $location is using an allowed host, if it has an absolute
  921. * path. A plugin can therefore set or remove allowed host(s) to or from the
  922. * list.
  923. *
  924. * If the host is not allowed, then the redirect is to $default supplied
  925. *
  926. * @since 2.8.1
  927. * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
  928. * WordPress host string and $location host string.
  929. *
  930. * @param string $location The redirect to validate
  931. * @param string $default The value to return is $location is not allowed
  932. * @return string redirect-sanitized URL
  933. **/
  934. function wp_validate_redirect($location, $default = '') {
  935. // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
  936. if ( substr($location, 0, 2) == '//' )
  937. $location = 'http:' . $location;
  938.  
  939. // In php 5 parse_url may fail if the URL query part contains http://, bug #38143
  940. $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
  941.  
  942. $lp = parse_url($test);
  943.  
  944. // Give up if malformed URL
  945. if ( false === $lp )
  946. return $default;
  947.  
  948. // Allow only http and https schemes. No data:, etc.
  949. if ( isset($lp['scheme']) && !('http' == $lp['scheme'] || 'https' == $lp['scheme']) )
  950. return $default;
  951.  
  952. // Reject if scheme is set but host is not. This catches urls like https:host.com for which parse_url does not set the host field.
  953. if ( isset($lp['scheme']) && !isset($lp['host']) )
  954. return $default;
  955.  
  956. $wpp = parse_url(home_url());
  957.  
  958. $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
  959.  
  960. if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
  961. $location = $default;
  962.  
  963. return $location;
  964. }
  965. endif;
  966.  
  967. if ( ! function_exists('wp_notify_postauthor') ) :
  968. /**
  969. * Notify an author of a comment/trackback/pingback to one of their posts.
  970. *
  971. * @since 1.0.0
  972. *
  973. * @param int $comment_id Comment ID
  974. * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
  975. * @return bool False if user email does not exist. True on completion.
  976. */
  977. function wp_notify_postauthor( $comment_id, $comment_type = '' ) {
  978. $comment = get_comment( $comment_id );
  979. $post = get_post( $comment->comment_post_ID );
  980. $author = get_userdata( $post->post_author );
  981.  
  982. // The comment was left by the author
  983. if ( $comment->user_id == $post->post_author )
  984. return false;
  985.  
  986. // The author moderated a comment on his own post
  987. if ( $post->post_author == get_current_user_id() )
  988. return false;
  989.  
  990. // If there's no email to send the comment to
  991. if ( '' == $author->user_email )
  992. return false;
  993.  
  994. $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  995.  
  996. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  997. // we want to reverse this for the plain text arena of emails.
  998. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  999.  
  1000. if ( empty( $comment_type ) ) $comment_type = 'comment';
  1001.  
  1002. if ('comment' == $comment_type) {
  1003. $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  1004. /* translators: 1: comment author, 2: author IP, 3: author domain */
  1005. $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1006. $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
  1007. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1008. $notify_message .= sprintf( __('Whois : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
  1009. $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1010. $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
  1011. /* translators: 1: blog name, 2: post title */
  1012. $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
  1013. } elseif ('trackback' == $comment_type) {
  1014. $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  1015. /* translators: 1: website name, 2: author IP, 3: author domain */
  1016. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1017. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1018. $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1019. $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
  1020. /* translators: 1: blog name, 2: post title */
  1021. $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
  1022. } elseif ('pingback' == $comment_type) {
  1023. $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  1024. /* translators: 1: comment author, 2: author IP, 3: author domain */
  1025. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1026. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1027. $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
  1028. $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
  1029. /* translators: 1: blog name, 2: post title */
  1030. $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
  1031. }
  1032. $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
  1033. $notify_message .= sprintf( __('Permalink: %s'), get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment_id ) . "\r\n";
  1034. if ( EMPTY_TRASH_DAYS )
  1035. $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1036. else
  1037. $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
  1038. $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
  1039.  
  1040. $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  1041.  
  1042. if ( '' == $comment->comment_author ) {
  1043. $from = "From: \"$blogname\" <$wp_email>";
  1044. if ( '' != $comment->comment_author_email )
  1045. $reply_to = "Reply-To: $comment->comment_author_email";
  1046. } else {
  1047. $from = "From: \"$comment->comment_author\" <$wp_email>";
  1048. if ( '' != $comment->comment_author_email )
  1049. $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
  1050. }
  1051.  
  1052. $message_headers = "$from\n"
  1053. . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1054.  
  1055. if ( isset($reply_to) )
  1056. $message_headers .= $reply_to . "\n";
  1057.  
  1058. $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
  1059. $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
  1060. $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
  1061.  
  1062. @wp_mail( $author->user_email, $subject, $notify_message, $message_headers );
  1063.  
  1064. return true;
  1065. }
  1066. endif;
  1067.  
  1068. if ( !function_exists('wp_notify_moderator') ) :
  1069. /**
  1070. * Notifies the moderator of the blog about a new comment that is awaiting approval.
  1071. *
  1072. * @since 1.0
  1073. * @uses $wpdb
  1074. *
  1075. * @param int $comment_id Comment ID
  1076. * @return bool Always returns true
  1077. */
  1078. function wp_notify_moderator($comment_id) {
  1079. global $wpdb;
  1080.  
  1081. if ( 0 == get_option( 'moderation_notify' ) )
  1082. return true;
  1083.  
  1084. $comment = get_comment($comment_id);
  1085. $post = get_post($comment->comment_post_ID);
  1086. $user = get_userdata( $post->post_author );
  1087. // Send to the administration and to the post author if the author can modify the comment.
  1088. $email_to = array( get_option('admin_email') );
  1089. if ( user_can($user->ID, 'edit_comment', $comment_id) && !empty($user->user_email) && ( get_option('admin_email') != $user->user_email) )
  1090. $email_to[] = $user->user_email;
  1091.  
  1092. $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
  1093. $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
  1094.  
  1095. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1096. // we want to reverse this for the plain text arena of emails.
  1097. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1098.  
  1099. switch ($comment->comment_type)
  1100. {
  1101. case 'trackback':
  1102. $notify_message = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1103. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1104. $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1105. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1106. $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1107. break;
  1108. case 'pingback':
  1109. $notify_message = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1110. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1111. $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1112. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1113. $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1114. break;
  1115. default: //Comments
  1116. $notify_message = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
  1117. $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
  1118. $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  1119. $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
  1120. $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
  1121. $notify_message .= sprintf( __('Whois : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n";
  1122. $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
  1123. break;
  1124. }
  1125.  
  1126. $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n";
  1127. if ( EMPTY_TRASH_DAYS )
  1128. $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
  1129. else
  1130. $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
  1131. $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
  1132.  
  1133. $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
  1134. 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
  1135. $notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
  1136.  
  1137. $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
  1138. $message_headers = '';
  1139.  
  1140. $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
  1141. $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
  1142. $message_headers = apply_filters('comment_moderation_headers', $message_headers);
  1143.  
  1144. foreach ( $email_to as $email )
  1145. @wp_mail($email, $subject, $notify_message, $message_headers);
  1146.  
  1147. return true;
  1148. }
  1149. endif;
  1150.  
  1151. if ( !function_exists('wp_password_change_notification') ) :
  1152. /**
  1153. * Notify the blog admin of a user changing password, normally via email.
  1154. *
  1155. * @since 2.7
  1156. *
  1157. * @param object $user User Object
  1158. */
  1159. function wp_password_change_notification(&$user) {
  1160. // send a copy of password change notification to the admin
  1161. // but check to see if it's the admin whose password we're changing, and skip this
  1162. if ( $user->user_email != get_option('admin_email') ) {
  1163. $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
  1164. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1165. // we want to reverse this for the plain text arena of emails.
  1166. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1167. wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), $blogname), $message);
  1168. }
  1169. }
  1170. endif;
  1171.  
  1172. if ( !function_exists('wp_new_user_notification') ) :
  1173. /**
  1174. * Notify the blog admin of a new user, normally via email.
  1175. *
  1176. * @since 2.0
  1177. *
  1178. * @param int $user_id User ID
  1179. * @param string $plaintext_pass Optional. The user's plaintext password
  1180. */
  1181. function wp_new_user_notification($user_id, $plaintext_pass = '') {
  1182. $user = new WP_User($user_id);
  1183.  
  1184. $user_login = stripslashes($user->user_login);
  1185. $user_email = stripslashes($user->user_email);
  1186.  
  1187. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  1188. // we want to reverse this for the plain text arena of emails.
  1189. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  1190.  
  1191. $message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
  1192. $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
  1193. $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
  1194.  
  1195. @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
  1196.  
  1197. if ( empty($plaintext_pass) )
  1198. return;
  1199.  
  1200. $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
  1201. $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
  1202. $message .= wp_login_url() . "\r\n";
  1203.  
  1204. wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
  1205.  
  1206. }
  1207. endif;
  1208.  
  1209. if ( !function_exists('wp_nonce_tick') ) :
  1210. /**
  1211. * Get the time-dependent variable for nonce creation.
  1212. *
  1213. * A nonce has a lifespan of two ticks. Nonces in their second tick may be
  1214. * updated, e.g. by autosave.
  1215. *
  1216. * @since 2.5
  1217. *
  1218. * @return int
  1219. */
  1220. function wp_nonce_tick() {
  1221. $nonce_life = apply_filters('nonce_life', 86400);
  1222.  
  1223. return ceil(time() / ( $nonce_life / 2 ));
  1224. }
  1225. endif;
  1226.  
  1227. if ( !function_exists('wp_verify_nonce') ) :
  1228. /**
  1229. * Verify that correct nonce was used with time limit.
  1230. *
  1231. * The user is given an amount of time to use the token, so therefore, since the
  1232. * UID and $action remain the same, the independent variable is the time.
  1233. *
  1234. * @since 2.0.3
  1235. *
  1236. * @param string $nonce Nonce that was used in the form to verify
  1237. * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
  1238. * @return bool Whether the nonce check passed or failed.
  1239. */
  1240. function wp_verify_nonce($nonce, $action = -1) {
  1241. $user = wp_get_current_user();
  1242. $uid = (int) $user->ID;
  1243.  
  1244. $i = wp_nonce_tick();
  1245.  
  1246. // Nonce generated 0-12 hours ago
  1247. if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
  1248. return 1;
  1249. // Nonce generated 12-24 hours ago
  1250. if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
  1251. return 2;
  1252. // Invalid nonce
  1253. return false;
  1254. }
  1255. endif;
  1256.  
  1257. if ( !function_exists('wp_create_nonce') ) :
  1258. /**
  1259. * Creates a random, one time use token.
  1260. *
  1261. * @since 2.0.3
  1262. *
  1263. * @param string|int $action Scalar value to add context to the nonce.
  1264. * @return string The one use form token
  1265. */
  1266. function wp_create_nonce($action = -1) {
  1267. $user = wp_get_current_user();
  1268. $uid = (int) $user->ID;
  1269.  
  1270. $i = wp_nonce_tick();
  1271.  
  1272. return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
  1273. }
  1274. endif;
  1275.  
  1276. if ( !function_exists('wp_salt') ) :
  1277. /**
  1278. * Get salt to add to hashes to help prevent attacks.
  1279. *
  1280. * The secret key is located in two places: the database in case the secret key
  1281. * isn't defined in the second place, which is in the wp-config.php file. If you
  1282. * are going to set the secret key, then you must do so in the wp-config.php
  1283. * file.
  1284. *
  1285. * The secret key in the database is randomly generated and will be appended to
  1286. * the secret key that is in wp-config.php file in some instances. It is
  1287. * important to have the secret key defined or changed in wp-config.php.
  1288. *
  1289. * If you have installed WordPress 2.5 or later, then you will have the
  1290. * SECRET_KEY defined in the wp-config.php already. You will want to change the
  1291. * value in it because hackers will know what it is. If you have upgraded to
  1292. * WordPress 2.5 or later version from a version before WordPress 2.5, then you
  1293. * should add the constant to your wp-config.php file.
  1294. *
  1295. * Below is an example of how the SECRET_KEY constant is defined with a value.
  1296. * You must not copy the below example and paste into your wp-config.php. If you
  1297. * need an example, then you can have a
  1298. * {@link https://api.wordpress.org/secret-key/1.1/ secret key created} for you.
  1299. *
  1300. * <code>
  1301. * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
  1302. * </code>
  1303. *
  1304. * Salting passwords helps against tools which has stored hashed values of
  1305. * common dictionary strings. The added values makes it harder to crack if given
  1306. * salt string is not weak.
  1307. *
  1308. * @since 2.5
  1309. * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php
  1310. *
  1311. * @param string $scheme Authentication scheme
  1312. * @return string Salt value
  1313. */
  1314. function wp_salt($scheme = 'auth') {
  1315. global $wp_default_secret_key;
  1316. $secret_key = '';
  1317. if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
  1318. $secret_key = SECRET_KEY;
  1319.  
  1320. if ( 'auth' == $scheme ) {
  1321. if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) )
  1322. $secret_key = AUTH_KEY;
  1323.  
  1324. if ( defined('AUTH_SALT') && ('' != AUTH_SALT) && ( $wp_default_secret_key != AUTH_SALT) ) {
  1325. $salt = AUTH_SALT;
  1326. } elseif ( defined('SECRET_SALT') && ('' != SECRET_SALT) && ( $wp_default_secret_key != SECRET_SALT) ) {
  1327. $salt = SECRET_SALT;
  1328. } else {
  1329. $salt = get_site_option('auth_salt');
  1330. if ( empty($salt) ) {
  1331. $salt = wp_generate_password( 64, true, true );
  1332. update_site_option('auth_salt', $salt);
  1333. }
  1334. }
  1335. } elseif ( 'secure_auth' == $scheme ) {
  1336. if ( defined('SECURE_AUTH_KEY') && ('' != SECURE_AUTH_KEY) && ( $wp_default_secret_key != SECURE_AUTH_KEY) )
  1337. $secret_key = SECURE_AUTH_KEY;
  1338.  
  1339. if ( defined('SECURE_AUTH_SALT') && ('' != SECURE_AUTH_SALT) && ( $wp_default_secret_key != SECURE_AUTH_SALT) ) {
  1340. $salt = SECURE_AUTH_SALT;
  1341. } else {
  1342. $salt = get_site_option('secure_auth_salt');
  1343. if ( empty($salt) ) {
  1344. $salt = wp_generate_password( 64, true, true );
  1345. update_site_option('secure_auth_salt', $salt);
  1346. }
  1347. }
  1348. } elseif ( 'logged_in' == $scheme ) {
  1349. if ( defined('LOGGED_IN_KEY') && ('' != LOGGED_IN_KEY) && ( $wp_default_secret_key != LOGGED_IN_KEY) )
  1350. $secret_key = LOGGED_IN_KEY;
  1351.  
  1352. if ( defined('LOGGED_IN_SALT') && ('' != LOGGED_IN_SALT) && ( $wp_default_secret_key != LOGGED_IN_SALT) ) {
  1353. $salt = LOGGED_IN_SALT;
  1354. } else {
  1355. $salt = get_site_option('logged_in_salt');
  1356. if ( empty($salt) ) {
  1357. $salt = wp_generate_password( 64, true, true );
  1358. update_site_option('logged_in_salt', $salt);
  1359. }
  1360. }
  1361. } elseif ( 'nonce' == $scheme ) {
  1362. if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) )
  1363. $secret_key = NONCE_KEY;
  1364.  
  1365. if ( defined('NONCE_SALT') && ('' != NONCE_SALT) && ( $wp_default_secret_key != NONCE_SALT) ) {
  1366. $salt = NONCE_SALT;
  1367. } else {
  1368. $salt = get_site_option('nonce_salt');
  1369. if ( empty($salt) ) {
  1370. $salt = wp_generate_password( 64, true, true );
  1371. update_site_option('nonce_salt', $salt);
  1372. }
  1373. }
  1374. } else {
  1375. // ensure each auth scheme has its own unique salt
  1376. $salt = hash_hmac('md5', $scheme, $secret_key);
  1377. }
  1378.  
  1379. return apply_filters('salt', $secret_key . $salt, $scheme);
  1380. }
  1381. endif;
  1382.  
  1383. if ( !function_exists('wp_hash') ) :
  1384. /**
  1385. * Get hash of given string.
  1386. *
  1387. * @since 2.0.3
  1388. * @uses wp_salt() Get WordPress salt
  1389. *
  1390. * @param string $data Plain text to hash
  1391. * @return string Hash of $data
  1392. */
  1393. function wp_hash($data, $scheme = 'auth') {
  1394. $salt = wp_salt($scheme);
  1395.  
  1396. return hash_hmac('md5', $data, $salt);
  1397. }
  1398. endif;
  1399.  
  1400. if ( !function_exists('wp_hash_password') ) :
  1401. /**
  1402. * Create a hash (encrypt) of a plain text password.
  1403. *
  1404. * For integration with other applications, this function can be overwritten to
  1405. * instead use the other package password checking algorithm.
  1406. *
  1407. * @since 2.5
  1408. * @global object $wp_hasher PHPass object
  1409. * @uses PasswordHash::HashPassword
  1410. *
  1411. * @param string $password Plain text user password to hash
  1412. * @return string The hash string of the password
  1413. */
  1414. function wp_hash_password($password) {
  1415. global $wp_hasher;
  1416.  
  1417. if ( empty($wp_hasher) ) {
  1418. require_once( ABSPATH . 'wp-includes/class-phpass.php');
  1419. // By default, use the portable hash from phpass
  1420. $wp_hasher = new PasswordHash(8, TRUE);
  1421. }
  1422.  
  1423. return $wp_hasher->HashPassword($password);
  1424. }
  1425. endif;
  1426.  
  1427. if ( !function_exists('wp_check_password') ) :
  1428. /**
  1429. * Checks the plaintext password against the encrypted Password.
  1430. *
  1431. * Maintains compatibility between old version and the new cookie authentication
  1432. * protocol using PHPass library. The $hash parameter is the encrypted password
  1433. * and the function compares the plain text password when encrypted similarly
  1434. * against the already encrypted password to see if they match.
  1435. *
  1436. * For integration with other applications, this function can be overwritten to
  1437. * instead use the other package password checking algorithm.
  1438. *
  1439. * @since 2.5
  1440. * @global object $wp_hasher PHPass object used for checking the password
  1441. * against the $hash + $password
  1442. * @uses PasswordHash::CheckPassword
  1443. *
  1444. * @param string $password Plaintext user's password
  1445. * @param string $hash Hash of the user's password to check against.
  1446. * @return bool False, if the $password does not match the hashed password
  1447. */
  1448. function wp_check_password($password, $hash, $user_id = '') {
  1449. global $wp_hasher;
  1450.  
  1451. // If the hash is still md5...
  1452. if ( strlen($hash) <= 32 ) {
  1453. $check = ( $hash == md5($password) );
  1454. if ( $check && $user_id ) {
  1455. // Rehash using new hash.
  1456. wp_set_password($password, $user_id);
  1457. $hash = wp_hash_password($password);
  1458. }
  1459.  
  1460. return apply_filters('check_password', $check, $password, $hash, $user_id);
  1461. }
  1462.  
  1463. // If the stored hash is longer than an MD5, presume the
  1464. // new style phpass portable hash.
  1465. if ( empty($wp_hasher) ) {
  1466. require_once( ABSPATH . 'wp-includes/class-phpass.php');
  1467. // By default, use the portable hash from phpass
  1468. $wp_hasher = new PasswordHash(8, TRUE);
  1469. }
  1470.  
  1471. $check = $wp_hasher->CheckPassword($password, $hash);
  1472.  
  1473. return apply_filters('check_password', $check, $password, $hash, $user_id);
  1474. }
  1475. endif;
  1476.  
  1477. if ( !function_exists('wp_generate_password') ) :
  1478. /**
  1479. * Generates a random password drawn from the defined set of characters.
  1480. *
  1481. * @since 2.5
  1482. *
  1483. * @param int $length The length of password to generate
  1484. * @param bool $special_chars Whether to include standard special characters. Default true.
  1485. * @param bool $extra_special_chars Whether to include other special characters. Used when
  1486. * generating secret keys and salts. Default false.
  1487. * @return string The random password
  1488. **/
  1489. function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
  1490. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  1491. if ( $special_chars )
  1492. $chars .= '!@#$%^&*()';
  1493. if ( $extra_special_chars )
  1494. $chars .= '-_ []{}<>~`+=,.;:/?|';
  1495.  
  1496. $password = '';
  1497. for ( $i = 0; $i < $length; $i++ ) {
  1498. $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
  1499. }
  1500.  
  1501. // random_password filter was previously in random_password function which was deprecated
  1502. return apply_filters('random_password', $password);
  1503. }
  1504. endif;
  1505.  
  1506. if ( !function_exists('wp_rand') ) :
  1507. /**
  1508. * Generates a random number
  1509. *
  1510. * @since 2.6.2
  1511. *
  1512. * @param int $min Lower limit for the generated number (optional, default is 0)
  1513. * @param int $max Upper limit for the generated number (optional, default is 4294967295)
  1514. * @return int A random number between min and max
  1515. */
  1516. function wp_rand( $min = 0, $max = 0 ) {
  1517. global $rnd_value;
  1518.  
  1519. // Reset $rnd_value after 14 uses
  1520. // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
  1521. if ( strlen($rnd_value) < 8 ) {
  1522. if ( defined( 'WP_SETUP_CONFIG' ) )
  1523. static $seed = '';
  1524. else
  1525. $seed = get_transient('random_seed');
  1526. $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
  1527. $rnd_value .= sha1($rnd_value);
  1528. $rnd_value .= sha1($rnd_value . $seed);
  1529. $seed = md5($seed . $rnd_value);
  1530. if ( ! defined( 'WP_SETUP_CONFIG' ) )
  1531. set_transient('random_seed', $seed);
  1532. }
  1533.  
  1534. // Take the first 8 digits for our value
  1535. $value = substr($rnd_value, 0, 8);
  1536.  
  1537. // Strip the first eight, leaving the remainder for the next call to wp_rand().
  1538. $rnd_value = substr($rnd_value, 8);
  1539.  
  1540. $value = abs(hexdec($value));
  1541.  
  1542. // Reduce the value to be within the min - max range
  1543. // 4294967295 = 0xffffffff = max random number
  1544. if ( $max != 0 )
  1545. $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
  1546.  
  1547. return abs(intval($value));
  1548. }
  1549. endif;
  1550.  
  1551. if ( !function_exists('wp_set_password') ) :
  1552. /**
  1553. * Updates the user's password with a new encrypted one.
  1554. *
  1555. * For integration with other applications, this function can be overwritten to
  1556. * instead use the other package password checking algorithm.
  1557. *
  1558. * @since 2.5
  1559. * @uses $wpdb WordPress database object for queries
  1560. * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
  1561. *
  1562. * @param string $password The plaintext new user password
  1563. * @param int $user_id User ID
  1564. */
  1565. function wp_set_password( $password, $user_id ) {
  1566. global $wpdb;
  1567.  
  1568. $hash = wp_hash_password($password);
  1569. $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );
  1570.  
  1571. wp_cache_delete($user_id, 'users');
  1572. }
  1573. endif;
  1574.  
  1575. if ( !function_exists( 'get_avatar' ) ) :
  1576. /**
  1577. * Retrieve the avatar for a user who provided a user ID or email address.
  1578. *
  1579. * @since 2.5
  1580. * @param int|string|object $id_or_email A user ID, email address, or comment object
  1581. * @param int $size Size of the avatar image
  1582. * @param string $default URL to a default image to use if no avatar is available
  1583. * @param string $alt Alternate text to use in image tag. Defaults to blank
  1584. * @return string <img> tag for the user's avatar
  1585. */
  1586. function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
  1587. if ( ! get_option('show_avatars') )
  1588. return false;
  1589.  
  1590. if ( false === $alt)
  1591. $safe_alt = '';
  1592. else
  1593. $safe_alt = esc_attr( $alt );
  1594.  
  1595. if ( !is_numeric($size) )
  1596. $size = '96';
  1597.  
  1598. $email = '';
  1599. if ( is_numeric($id_or_email) ) {
  1600. $id = (int) $id_or_email;
  1601. $user = get_userdata($id);
  1602. if ( $user )
  1603. $email = $user->user_email;
  1604. } elseif ( is_object($id_or_email) ) {
  1605. // No avatar for pingbacks or trackbacks
  1606. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  1607. if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
  1608. return false;
  1609.  
  1610. if ( !empty($id_or_email->user_id) ) {
  1611. $id = (int) $id_or_email->user_id;
  1612. $user = get_userdata($id);
  1613. if ( $user)
  1614. $email = $user->user_email;
  1615. } elseif ( !empty($id_or_email->comment_author_email) ) {
  1616. $email = $id_or_email->comment_author_email;
  1617. }
  1618. } else {
  1619. $email = $id_or_email;
  1620. }
  1621.  
  1622. if ( empty($default) ) {
  1623. $avatar_default = get_option('avatar_default');
  1624. if ( empty($avatar_default) )
  1625. $default = 'mystery';
  1626. else
  1627. $default = $avatar_default;
  1628. }
  1629.  
  1630. if ( !empty($email) )
  1631. $email_hash = md5( strtolower( $email ) );
  1632.  
  1633. if ( is_ssl() ) {
  1634. $host = 'https://secure.gravatar.com';
  1635. } else {
  1636. if ( !empty($email) )
  1637. $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
  1638. else
  1639. $host = 'http://0.gravatar.com';
  1640. }
  1641.  
  1642. if ( 'mystery' == $default )
  1643. $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  1644. elseif ( 'blank' == $default )
  1645. $default = includes_url('images/blank.gif');
  1646. elseif ( !empty($email) && 'gravatar_default' == $default )
  1647. $default = '';
  1648. elseif ( 'gravatar_default' == $default )
  1649. $default = "$host/avatar/s={$size}";
  1650. elseif ( empty($email) )
  1651. $default = "$host/avatar/?d=$default&amp;s={$size}";
  1652. elseif ( strpos($default, 'http://') === 0 )
  1653. $default = add_query_arg( 's', $size, $default );
  1654.  
  1655. if ( !empty($email) ) {
  1656. $out = "$host/avatar/";
  1657. $out .= $email_hash;
  1658. $out .= '?s='.$size;
  1659. $out .= '&amp;d=' . urlencode( $default );
  1660.  
  1661. $rating = get_option('avatar_rating');
  1662. if ( !empty( $rating ) )
  1663. $out .= "&amp;r={$rating}";
  1664.  
  1665. $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
  1666. } else {
  1667. $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
  1668. }
  1669.  
  1670. return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
  1671. }
  1672. endif;
  1673.  
  1674. if ( !function_exists( 'wp_text_diff' ) ) :
  1675. /**
  1676. * Displays a human readable HTML representation of the difference between two strings.
  1677. *
  1678. * The Diff is available for getting the changes between versions. The output is
  1679. * HTML, so the primary use is for displaying the changes. If the two strings
  1680. * are equivalent, then an empty string will be returned.
  1681. *
  1682. * The arguments supported and can be changed are listed below.
  1683. *
  1684. * 'title' : Default is an empty string. Titles the diff in a manner compatible
  1685. * with the output.
  1686. * 'title_left' : Default is an empty string. Change the HTML to the left of the
  1687. * title.
  1688. * 'title_right' : Default is an empty string. Change the HTML to the right of
  1689. * the title.
  1690. *
  1691. * @since 2.6
  1692. * @see wp_parse_args() Used to change defaults to user defined settings.
  1693. * @uses Text_Diff
  1694. * @uses WP_Text_Diff_Renderer_Table
  1695. *
  1696. * @param string $left_string "old" (left) version of string
  1697. * @param string $right_string "new" (right) version of string
  1698. * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
  1699. * @return string Empty string if strings are equivalent or HTML with differences.
  1700. */
  1701. function wp_text_diff( $left_string, $right_string, $args = null ) {
  1702. $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
  1703. $args = wp_parse_args( $args, $defaults );
  1704.  
  1705. if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
  1706. require( ABSPATH . WPINC . '/wp-diff.php' );
  1707.  
  1708. $left_string = normalize_whitespace($left_string);
  1709. $right_string = normalize_whitespace($right_string);
  1710.  
  1711. $left_lines = split("\n", $left_string);
  1712. $right_lines = split("\n", $right_string);
  1713.  
  1714. $text_diff = new Text_Diff($left_lines, $right_lines);
  1715. $renderer = new WP_Text_Diff_Renderer_Table();
  1716. $diff = $renderer->render($text_diff);
  1717.  
  1718. if ( !$diff )
  1719. return '';
  1720.  
  1721. $r = "<table class='diff'>\n";
  1722. $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
  1723.  
  1724. if ( $args['title'] || $args['title_left'] || $args['title_right'] )
  1725. $r .= "<thead>";
  1726. if ( $args['title'] )
  1727. $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
  1728. if ( $args['title_left'] || $args['title_right'] ) {
  1729. $r .= "<tr class='diff-sub-title'>\n";
  1730. $r .= "\t<td></td><th>$args[title_left]</th>\n";
  1731. $r .= "\t<td></td><th>$args[title_right]</th>\n";
  1732. $r .= "</tr>\n";
  1733. }
  1734. if ( $args['title'] || $args['title_left'] || $args['title_right'] )
  1735. $r .= "</thead>\n";
  1736.  
  1737. $r .= "<tbody>\n$diff\n</tbody>\n";
  1738. $r .= "</table>";
  1739.  
  1740. return $r;
  1741. }
  1742. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement