+/**
+ * Helper function to convert hex encoded chars to ASCII
+ *
+ * @since 3.1.0
+ * @access private
+ *
+ * @param array $match The preg_replace_callback matches array
+ * @return string Converted chars
+ */
+function _wp_iso_convert( $match ) {
+ return chr( hexdec( strtolower( $match[1] ) ) );
+}
+
+/**
+ * Returns a date in the GMT equivalent.
+ *
+ * Requires and returns a date in the Y-m-d H:i:s format. If there is a
+ * timezone_string available, the date is assumed to be in that timezone,
+ * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
+ * format can be overridden using the $format parameter.
+ *
+ * @since 1.2.0
+ *
+ * @param string $string The date to be converted.
+ * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
+ * @return string GMT version of the date provided.
+ */
+function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
+ $tz = get_option( 'timezone_string' );
+ if ( $tz ) {
+ $datetime = date_create( $string, new DateTimeZone( $tz ) );
+ if ( ! $datetime )
+ return gmdate( $format, 0 );
+ $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
+ $string_gmt = $datetime->format( $format );
+ } else {
+ if ( ! preg_match( '#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches ) )
+ return gmdate( $format, 0 );
+ $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
+ $string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
+ }
+ return $string_gmt;
+}
+
+/**
+ * Converts a GMT date into the correct format for the blog.
+ *
+ * Requires and returns a date in the Y-m-d H:i:s format. If there is a
+ * timezone_string available, the returned date is in that timezone, otherwise
+ * it simply adds the value of gmt_offset. Return format can be overridden
+ * using the $format parameter
+ *
+ * @since 1.2.0
+ *
+ * @param string $string The date to be converted.
+ * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
+ * @return string Formatted date relative to the timezone / GMT offset.
+ */
+function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
+ $tz = get_option( 'timezone_string' );
+ if ( $tz ) {
+ $datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
+ if ( ! $datetime )
+ return date( $format, 0 );
+ $datetime->setTimezone( new DateTimeZone( $tz ) );
+ $string_localtime = $datetime->format( $format );
+ } else {
+ if ( ! preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches) )
+ return date( $format, 0 );
+ $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
+ $string_localtime = gmdate( $format, $string_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
+ }
+ return $string_localtime;
+}
+
+/**
+ * Computes an offset in seconds from an iso8601 timezone.
+ *
+ * @since 1.5.0
+ *
+ * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
+ * @return int|float The offset in seconds.
+ */
+function iso8601_timezone_to_offset($timezone) {
+ // $timezone is either 'Z' or '[+|-]hhmm'
+ if ($timezone == 'Z') {
+ $offset = 0;
+ } else {
+ $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
+ $hours = intval(substr($timezone, 1, 2));
+ $minutes = intval(substr($timezone, 3, 4)) / 60;
+ $offset = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
+ }
+ return $offset;
+}
+
+/**
+ * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
+ *
+ * @since 1.5.0
+ *
+ * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
+ * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
+ * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
+ */
+function iso8601_to_datetime($date_string, $timezone = 'user') {
+ $timezone = strtolower($timezone);
+
+ if ($timezone == 'gmt') {
+
+ preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
+
+ if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
+ $offset = iso8601_timezone_to_offset($date_bits[7]);
+ } else { // we don't have a timezone, so we assume user local timezone (not server's!)
+ $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
+ }
+
+ $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
+ $timestamp -= $offset;
+
+ return gmdate('Y-m-d H:i:s', $timestamp);
+
+ } else if ($timezone == 'user') {
+ return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
+ }
+}
+
+/**
+ * Adds a element attributes to open links in new windows.
+ *
+ * Comment text in popup windows should be filtered through this. Right now it's
+ * a moderately dumb function, ideally it would detect whether a target or rel
+ * attribute was already there and adjust its actions accordingly.
+ *
+ * @since 0.71
+ *
+ * @param string $text Content to replace links to open in a new window.
+ * @return string Content that has filtered links.
+ */
+function popuplinks($text) {
+ $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
+ return $text;
+}
+
+/**
+ * Strips out all characters that are not allowable in an email.
+ *
+ * @since 1.5.0
+ *
+ * @param string $email Email address to filter.
+ * @return string Filtered email address.
+ */
+function sanitize_email( $email ) {
+ // Test for the minimum length the email can be
+ if ( strlen( $email ) < 3 ) {
+ /**
+ * Filter a sanitized email address.
+ *
+ * This filter is evaluated under several contexts, including 'email_too_short',
+ * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
+ * 'domain_no_periods', 'domain_no_valid_subs', or no context.
+ *
+ * @since 2.8.0
+ *
+ * @param string $email The sanitized email address.
+ * @param string $email The email address, as provided to sanitize_email().
+ * @param string $message A message to pass to the user.
+ */
+ return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
+ }
+
+ // Test for an @ character after the first position
+ if ( strpos( $email, '@', 1 ) === false ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
+ }
+
+ // Split out the local and domain parts
+ list( $local, $domain ) = explode( '@', $email, 2 );
+
+ // LOCAL PART
+ // Test for invalid characters
+ $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
+ if ( '' === $local ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
+ }
+
+ // DOMAIN PART
+ // Test for sequences of periods
+ $domain = preg_replace( '/\.{2,}/', '', $domain );
+ if ( '' === $domain ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
+ }
+
+ // Test for leading and trailing periods and whitespace
+ $domain = trim( $domain, " \t\n\r\0\x0B." );
+ if ( '' === $domain ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
+ }
+
+ // Split the domain into subs
+ $subs = explode( '.', $domain );
+
+ // Assume the domain will have at least two subs
+ if ( 2 > count( $subs ) ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
+ }
+
+ // Create an array that will contain valid subs
+ $new_subs = array();
+
+ // Loop through each sub
+ foreach ( $subs as $sub ) {
+ // Test for leading and trailing hyphens
+ $sub = trim( $sub, " \t\n\r\0\x0B-" );
+
+ // Test for invalid characters
+ $sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );
+
+ // If there's anything left, add it to the valid subs
+ if ( '' !== $sub ) {
+ $new_subs[] = $sub;
+ }
+ }
+
+ // If there aren't 2 or more valid subs
+ if ( 2 > count( $new_subs ) ) {
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
+ }
+
+ // Join valid subs into the new domain
+ $domain = join( '.', $new_subs );
+
+ // Put the email back together
+ $email = $local . '@' . $domain;
+
+ // Congratulations your email made it!
+ /** This filter is documented in wp-includes/formatting.php */
+ return apply_filters( 'sanitize_email', $email, $email, null );
+}
+
+/**
+ * Determines the difference between two timestamps.
+ *
+ * The difference is returned in a human readable format such as "1 hour",
+ * "5 mins", "2 days".
+ *
+ * @since 1.5.0
+ *
+ * @param int $from Unix timestamp from which the difference begins.
+ * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
+ * @return string Human readable time difference.
+ */
+function human_time_diff( $from, $to = '' ) {
+ if ( empty( $to ) ) {
+ $to = time();
+ }
+
+ $diff = (int) abs( $to - $from );
+
+ if ( $diff < HOUR_IN_SECONDS ) {
+ $mins = round( $diff / MINUTE_IN_SECONDS );
+ if ( $mins <= 1 )
+ $mins = 1;
+ /* translators: min=minute */
+ $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
+ } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
+ $hours = round( $diff / HOUR_IN_SECONDS );
+ if ( $hours <= 1 )
+ $hours = 1;
+ $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
+ } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
+ $days = round( $diff / DAY_IN_SECONDS );
+ if ( $days <= 1 )
+ $days = 1;
+ $since = sprintf( _n( '%s day', '%s days', $days ), $days );
+ } elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
+ $weeks = round( $diff / WEEK_IN_SECONDS );
+ if ( $weeks <= 1 )
+ $weeks = 1;
+ $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
+ } elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
+ $months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
+ if ( $months <= 1 )
+ $months = 1;
+ $since = sprintf( _n( '%s month', '%s months', $months ), $months );
+ } elseif ( $diff >= YEAR_IN_SECONDS ) {
+ $years = round( $diff / YEAR_IN_SECONDS );
+ if ( $years <= 1 )
+ $years = 1;
+ $since = sprintf( _n( '%s year', '%s years', $years ), $years );
+ }
+
+ /**
+ * Filter the human readable difference between two timestamps.
+ *
+ * @since 4.0.0
+ *
+ * @param string $since The difference in human readable text.
+ * @param int $diff The difference in seconds.
+ * @param int $from Unix timestamp from which the difference begins.
+ * @param int $to Unix timestamp to end the time difference.
+ */
+ return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
+}
+
+/**
+ * Generates an excerpt from the content, if needed.
+ *
+ * The excerpt word amount will be 55 words and if the amount is greater than
+ * that, then the string ' […]' will be appended to the excerpt. If the string
+ * is less than 55 words, then the content will be returned as is.
+ *
+ * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
+ * The ' […]' string can be modified by plugins/themes using the excerpt_more filter
+ *
+ * @since 1.5.0
+ *
+ * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
+ * @return string The excerpt.
+ */
+function wp_trim_excerpt($text = '') {
+ $raw_excerpt = $text;
+ if ( '' == $text ) {
+ $text = get_the_content('');
+
+ $text = strip_shortcodes( $text );
+
+ /** This filter is documented in wp-includes/post-template.php */
+ $text = apply_filters( 'the_content', $text );
+ $text = str_replace(']]>', ']]>', $text);
+
+ /**
+ * Filter the number of words in an excerpt.
+ *
+ * @since 2.7.0
+ *
+ * @param int $number The number of words. Default 55.
+ */
+ $excerpt_length = apply_filters( 'excerpt_length', 55 );
+ /**
+ * Filter the string in the "more" link displayed after a trimmed excerpt.
+ *
+ * @since 2.9.0
+ *
+ * @param string $more_string The string shown within the more link.
+ */
+ $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
+ $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
+ }
+ /**
+ * Filter the trimmed excerpt string.
+ *
+ * @since 2.8.0
+ *
+ * @param string $text The trimmed text.
+ * @param string $raw_excerpt The text prior to trimming.
+ */
+ return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
+}
+
+/**
+ * Trims text to a certain number of words.
+ *
+ * This function is localized. For languages that count 'words' by the individual
+ * character (such as East Asian languages), the $num_words argument will apply
+ * to the number of individual characters.
+ *
+ * @since 3.3.0
+ *
+ * @param string $text Text to trim.
+ * @param int $num_words Number of words. Default 55.
+ * @param string $more Optional. What to append if $text needs to be trimmed. Default '…'.
+ * @return string Trimmed text.
+ */
+function wp_trim_words( $text, $num_words = 55, $more = null ) {
+ if ( null === $more )
+ $more = __( '…' );
+ $original_text = $text;
+ $text = wp_strip_all_tags( $text );
+ /* translators: If your word count is based on single characters (East Asian characters),
+ enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
+ if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
+ $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
+ preg_match_all( '/./u', $text, $words_array );
+ $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
+ $sep = '';
+ } else {
+ $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
+ $sep = ' ';
+ }
+ if ( count( $words_array ) > $num_words ) {
+ array_pop( $words_array );
+ $text = implode( $sep, $words_array );
+ $text = $text . $more;
+ } else {
+ $text = implode( $sep, $words_array );
+ }
+ /**
+ * Filter the text content after words have been trimmed.
+ *
+ * @since 3.3.0
+ *
+ * @param string $text The trimmed text.
+ * @param int $num_words The number of words to trim the text to. Default 5.
+ * @param string $more An optional string to append to the end of the trimmed text, e.g. ….
+ * @param string $original_text The text before it was trimmed.
+ */
+ return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
+}
+
+/**
+ * Converts named entities into numbered entities.
+ *
+ * @since 1.5.1
+ *
+ * @param string $text The text within which entities will be converted.
+ * @return string Text with converted entities.
+ */
+function ent2ncr($text) {
+
+ /**
+ * Filter text before named entities are converted into numbered entities.
+ *
+ * A non-null string must be returned for the filter to be evaluated.
+ *
+ * @since 3.3.0
+ *
+ * @param null $converted_text The text to be converted. Default null.
+ * @param string $text The text prior to entity conversion.
+ */
+ $filtered = apply_filters( 'pre_ent2ncr', null, $text );
+ if( null !== $filtered )
+ return $filtered;
+