Welcome, visitor! Log in
 

Source View: sanitize_email

To save our bandwidth, we show only a snippet of code around each occurence of the hook. View complete file in SVN  (without highlighting).

Understanding Source Code

The best way to understand what a hook does is to look at where it occurs in the source code.

  • Action hooks look like this: do_action( "hook_name" )
  • Filter hooks look like this: apply_filters( "hook_name", "what_to_filter" ).

Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.

Source View

This hook occurs 8 times in this file

Line Code
1662       $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
1663       return $text;
1664  }
1665  
1666  /**
1667   * Strips out all characters that are not allowable in an email.
1668   *
1669   * @since 1.5.0
1670   *
1671   * @param string $email Email address to filter.
1672   * @return string Filtered email address.
1673   */
1674  function sanitize_email( $email ) {
1675       // Test for the minimum length the email can be
1676       if ( strlen( $email ) < 3 ) {
1677            return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1678       }
1679  
1680       // Test for an @ character after the first position
1681       if ( strpos( $email, '@', 1 ) === false ) {
1682            return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1683       }
1684  
1685       // Split out the local and domain parts
1686       list( $local, $domain ) = explode( '@', $email, 2 );
1687  
1688       // LOCAL PART
1689       // Test for invalid characters
1690       $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1691       if ( '' === $local ) {
1692            return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
 
Line Code
1667   * Strips out all characters that are not allowable in an email.
1668   *
1669   * @since 1.5.0
1670   *
1671   * @param string $email Email address to filter.
1672   * @return string Filtered email address.
1673   */
1674  function sanitize_email( $email ) {
1675       // Test for the minimum length the email can be
1676       if ( strlen( $email ) < 3 ) {
1677            return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1678       }
1679  
1680       // Test for an @ character after the first position
1681       if ( strpos( $email, '@', 1 ) === false ) {
1682            return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1683       }
1684  
1685       // Split out the local and domain parts
1686       list( $local, $domain ) = explode( '@', $email, 2 );
1687  
1688       // LOCAL PART
1689       // Test for invalid characters
1690       $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1691       if ( '' === $local ) {
1692            return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1693       }
1694  
1695       // DOMAIN PART
1696       // Test for sequences of periods
1697       $domain = preg_replace( '/\.{2,}/', '', $domain );
 
Line Code
1677            return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1678       }
1679  
1680       // Test for an @ character after the first position
1681       if ( strpos( $email, '@', 1 ) === false ) {
1682            return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1683       }
1684  
1685       // Split out the local and domain parts
1686       list( $local, $domain ) = explode( '@', $email, 2 );
1687  
1688       // LOCAL PART
1689       // Test for invalid characters
1690       $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1691       if ( '' === $local ) {
1692            return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1693       }
1694  
1695       // DOMAIN PART
1696       // Test for sequences of periods
1697       $domain = preg_replace( '/\.{2,}/', '', $domain );
1698       if ( '' === $domain ) {
1699            return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1700       }
1701  
1702       // Test for leading and trailing periods and whitespace
1703       $domain = trim( $domain, " \t\n\r\0\x0B." );
1704       if ( '' === $domain ) {
1705            return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1706       }
1707  
 
Line Code
1684  
1685       // Split out the local and domain parts
1686       list( $local, $domain ) = explode( '@', $email, 2 );
1687  
1688       // LOCAL PART
1689       // Test for invalid characters
1690       $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1691       if ( '' === $local ) {
1692            return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1693       }
1694  
1695       // DOMAIN PART
1696       // Test for sequences of periods
1697       $domain = preg_replace( '/\.{2,}/', '', $domain );
1698       if ( '' === $domain ) {
1699            return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1700       }
1701  
1702       // Test for leading and trailing periods and whitespace
1703       $domain = trim( $domain, " \t\n\r\0\x0B." );
1704       if ( '' === $domain ) {
1705            return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1706       }
1707  
1708       // Split the domain into subs
1709       $subs = explode( '.', $domain );
1710  
1711       // Assume the domain will have at least two subs
1712       if ( 2 > count( $subs ) ) {
1713            return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1714       }
 
Line Code
1690       $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1691       if ( '' === $local ) {
1692            return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1693       }
1694  
1695       // DOMAIN PART
1696       // Test for sequences of periods
1697       $domain = preg_replace( '/\.{2,}/', '', $domain );
1698       if ( '' === $domain ) {
1699            return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1700       }
1701  
1702       // Test for leading and trailing periods and whitespace
1703       $domain = trim( $domain, " \t\n\r\0\x0B." );
1704       if ( '' === $domain ) {
1705            return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1706       }
1707  
1708       // Split the domain into subs
1709       $subs = explode( '.', $domain );
1710  
1711       // Assume the domain will have at least two subs
1712       if ( 2 > count( $subs ) ) {
1713            return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1714       }
1715  
1716       // Create an array that will contain valid subs
1717       $new_subs = array();
1718  
1719       // Loop through each sub
1720       foreach ( $subs as $sub ) {
 
Line Code
1698       if ( '' === $domain ) {
1699            return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1700       }
1701  
1702       // Test for leading and trailing periods and whitespace
1703       $domain = trim( $domain, " \t\n\r\0\x0B." );
1704       if ( '' === $domain ) {
1705            return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1706       }
1707  
1708       // Split the domain into subs
1709       $subs = explode( '.', $domain );
1710  
1711       // Assume the domain will have at least two subs
1712       if ( 2 > count( $subs ) ) {
1713            return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1714       }
1715  
1716       // Create an array that will contain valid subs
1717       $new_subs = array();
1718  
1719       // Loop through each sub
1720       foreach ( $subs as $sub ) {
1721            // Test for leading and trailing hyphens
1722            $sub = trim( $sub, " \t\n\r\0\x0B-" );
1723  
1724            // Test for invalid characters
1725            $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
1726  
1727            // If there's anything left, add it to the valid subs
1728            if ( '' !== $sub ) {
 
Line Code
1720       foreach ( $subs as $sub ) {
1721            // Test for leading and trailing hyphens
1722            $sub = trim( $sub, " \t\n\r\0\x0B-" );
1723  
1724            // Test for invalid characters
1725            $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
1726  
1727            // If there's anything left, add it to the valid subs
1728            if ( '' !== $sub ) {
1729                 $new_subs[] = $sub;
1730            }
1731       }
1732  
1733       // If there aren't 2 or more valid subs
1734       if ( 2 > count( $new_subs ) ) {
1735            return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
1736       }
1737  
1738       // Join valid subs into the new domain
1739       $domain = join( '.', $new_subs );
1740  
1741       // Put the email back together
1742       $email = $local . '@' . $domain;
1743  
1744       // Congratulations your email made it!
1745       return apply_filters( 'sanitize_email', $email, $email, null );
1746  }
1747  
1748  /**
1749   * Determines the difference between two timestamps.
1750   *
 
Line Code
1730            }
1731       }
1732  
1733       // If there aren't 2 or more valid subs
1734       if ( 2 > count( $new_subs ) ) {
1735            return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
1736       }
1737  
1738       // Join valid subs into the new domain
1739       $domain = join( '.', $new_subs );
1740  
1741       // Put the email back together
1742       $email = $local . '@' . $domain;
1743  
1744       // Congratulations your email made it!
1745       return apply_filters( 'sanitize_email', $email, $email, null );
1746  }
1747  
1748  /**
1749   * Determines the difference between two timestamps.
1750   *
1751   * The difference is returned in a human readable format such as "1 hour",
1752   * "5 mins", "2 days".
1753   *
1754   * @since 1.5.0
1755   *
1756   * @param int $from Unix timestamp from which the difference begins.
1757   * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
1758   * @return string Human readable time difference.
1759   */
1760  function human_time_diff( $from, $to = '' ) {