[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-includes/ -> pluggable.php (source)

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


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting