(.+?)<\/category>/','',$content);
+ if ( strpos( $content, '&' ) !== false ) {
+ $content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content );
+ }
- // Converts lone & characters into & (a.k.a. &)
- $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content);
+ return $content;
+}
- // Fix Word pasting
- $content = strtr($content, $wp_htmltranswinuni);
+/**
+ * Converts invalid Unicode references range to valid range.
+ *
+ * @since 4.3.0
+ *
+ * @param string $content String with entities that need converting.
+ * @return string Converted string.
+ */
+function convert_invalid_entities( $content ) {
+ $wp_htmltranswinuni = array(
+ '' => '€', // the Euro sign
+ '' => '',
+ '' => '‚', // these are Windows CP1252 specific characters
+ '' => 'ƒ', // they would look weird on non-Windows browsers
+ '' => '„',
+ '
' => '…',
+ '' => '†',
+ '' => '‡',
+ '' => 'ˆ',
+ '' => '‰',
+ '' => 'Š',
+ '' => '‹',
+ '' => 'Œ',
+ '' => '',
+ '' => 'Ž',
+ '' => '',
+ '' => '',
+ '' => '‘',
+ '' => '’',
+ '' => '“',
+ '' => '”',
+ '' => '•',
+ '' => '–',
+ '' => '—',
+ '' => '˜',
+ '' => '™',
+ '' => 'š',
+ '' => '›',
+ '' => 'œ',
+ '' => '',
+ '' => 'ž',
+ '' => 'Ÿ'
+ );
- // Just a little XHTML help
- $content = str_replace('
', '
', $content);
- $content = str_replace('
', '
', $content);
+ if ( strpos( $content, '' ) !== false ) {
+ $content = strtr( $content, $wp_htmltranswinuni );
+ }
return $content;
}
@@ -1493,8 +2135,8 @@ function convert_chars($content, $deprecated = '') {
*
* @since 0.71
*
- * @param string $text Text to be balanced
- * @param bool $force If true, forces balancing, ignoring the value of the option. Default false.
+ * @param string $text Text to be balanced
+ * @param bool $force If true, forces balancing, ignoring the value of the option. Default false.
* @return string Balanced text
*/
function balanceTags( $text, $force = false ) {
@@ -1550,7 +2192,7 @@ function force_balance_tags( $text ) {
if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
$tag = strtolower(substr($regex[1],1));
// if too many closing tags
- if( $stacksize <= 0 ) {
+ if ( $stacksize <= 0 ) {
$tag = '';
// or close to be safe $tag = '/' . $tag;
}
@@ -1605,7 +2247,7 @@ function force_balance_tags( $text ) {
// Attributes
$attributes = $regex[2];
- if( ! empty( $attributes ) && $attributes[0] != '>' )
+ if ( ! empty( $attributes ) && $attributes[0] != '>' )
$attributes = ' ' . $attributes;
$tag = '<' . $tag . $attributes . '>';
@@ -1640,25 +2282,28 @@ function force_balance_tags( $text ) {
* Acts on text which is about to be edited.
*
* The $content is run through esc_textarea(), which uses htmlspecialchars()
- * to convert special characters to HTML entities. If $richedit is set to true,
- * it is simply a holder for the 'format_to_edit' filter.
+ * to convert special characters to HTML entities. If `$richedit` is set to true,
+ * it is simply a holder for the {@see 'format_to_edit'} filter.
*
* @since 0.71
+ * @since 4.4.0 The `$richedit` parameter was renamed to `$rich_text` for clarity.
*
- * @param string $content The text about to be edited.
- * @param bool $richedit Whether the $content should not pass through htmlspecialchars(). Default false (meaning it will be passed).
+ * @param string $content The text about to be edited.
+ * @param bool $rich_text Optional. Whether `$content` should be considered rich text,
+ * in which case it would not be passed through esc_textarea().
+ * Default false.
* @return string The text after the filter (and possibly htmlspecialchars()) has been run.
*/
-function format_to_edit( $content, $richedit = false ) {
+function format_to_edit( $content, $rich_text = false ) {
/**
- * Filter the text to be formatted for editing.
+ * Filters the text to be formatted for editing.
*
* @since 1.2.0
*
* @param string $content The text, prior to formatting for editing.
*/
$content = apply_filters( 'format_to_edit', $content );
- if ( ! $richedit )
+ if ( ! $rich_text )
$content = esc_textarea( $content );
return $content;
}
@@ -1676,12 +2321,12 @@ function format_to_edit( $content, $richedit = false ) {
*
* @since 0.71
*
- * @param mixed $number Number to append zeros to if not greater than threshold.
- * @param int $threshold Digit places number needs to be to not have zeros added.
+ * @param int $number Number to append zeros to if not greater than threshold.
+ * @param int $threshold Digit places number needs to be to not have zeros added.
* @return string Adds leading zeros to number if needed.
*/
-function zeroise($number, $threshold) {
- return sprintf('%0'.$threshold.'s', $number);
+function zeroise( $number, $threshold ) {
+ return sprintf( '%0' . $threshold . 's', $number );
}
/**
@@ -1692,7 +2337,7 @@ function zeroise($number, $threshold) {
* @param string $string Value to which backslashes will be added.
* @return string String with backslashes inserted.
*/
-function backslashit($string) {
+function backslashit( $string ) {
if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' )
$string = '\\\\' . $string;
return addcslashes( $string, 'A..Za..z' );
@@ -1735,7 +2380,7 @@ function untrailingslashit( $string ) {
* Adds slashes to escape strings.
*
* Slashes will first be removed if magic_quotes_gpc is set, see {@link
- * http://www.php.net/magic_quotes} for more details.
+ * https://secure.php.net/magic_quotes} for more details.
*
* @since 0.71
*
@@ -1750,55 +2395,63 @@ function addslashes_gpc($gpc) {
}
/**
- * Navigates through an array and removes slashes from the values.
- *
- * If an array is passed, the array_map() function causes a callback to pass the
- * value back to the function. The slashes from this value will removed.
+ * Navigates through an array, object, or scalar, and removes slashes from the values.
*
* @since 2.0.0
*
* @param mixed $value The value to be stripped.
* @return mixed Stripped value.
*/
-function stripslashes_deep($value) {
- if ( is_array($value) ) {
- $value = array_map('stripslashes_deep', $value);
- } elseif ( is_object($value) ) {
- $vars = get_object_vars( $value );
- foreach ($vars as $key=>$data) {
- $value->{$key} = stripslashes_deep( $data );
- }
- } elseif ( is_string( $value ) ) {
- $value = stripslashes($value);
- }
-
- return $value;
+function stripslashes_deep( $value ) {
+ return map_deep( $value, 'stripslashes_from_strings_only' );
}
/**
- * Navigates through an array and encodes the values to be used in a URL.
+ * Callback function for `stripslashes_deep()` which strips slashes from strings.
*
+ * @since 4.4.0
+ *
+ * @param mixed $value The array or string to be stripped.
+ * @return mixed $value The stripped value.
+ */
+function stripslashes_from_strings_only( $value ) {
+ return is_string( $value ) ? stripslashes( $value ) : $value;
+}
+
+/**
+ * Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
*
* @since 2.2.0
*
- * @param array|string $value The array or string to be encoded.
- * @return array|string $value The encoded array (or string from the callback).
+ * @param mixed $value The array or string to be encoded.
+ * @return mixed $value The encoded value.
*/
-function urlencode_deep($value) {
- $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
- return $value;
+function urlencode_deep( $value ) {
+ return map_deep( $value, 'urlencode' );
}
/**
- * Navigates through an array and raw encodes the values to be used in a URL.
+ * Navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL.
*
* @since 3.4.0
*
- * @param array|string $value The array or string to be encoded.
- * @return array|string $value The encoded array (or string from the callback).
+ * @param mixed $value The array or string to be encoded.
+ * @return mixed $value The encoded value.
*/
function rawurlencode_deep( $value ) {
- return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
+ return map_deep( $value, 'rawurlencode' );
+}
+
+/**
+ * Navigates through an array, object, or scalar, and decodes URL-encoded values
+ *
+ * @since 4.4.0
+ *
+ * @param mixed $value The array or string to be decoded.
+ * @return mixed $value The decoded value.
+ */
+function urldecode_deep( $value ) {
+ return map_deep( $value, 'urldecode' );
}
/**
@@ -1807,7 +2460,7 @@ function rawurlencode_deep( $value ) {
* @since 0.71
*
* @param string $email_address Email address.
- * @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
+ * @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
* @return string Converted email address.
*/
function antispambot( $email_address, $hex_encoding = 0 ) {
@@ -1823,16 +2476,13 @@ function antispambot( $email_address, $hex_encoding = 0 ) {
}
}
- $email_no_spam_address = str_replace( '@', '@', $email_no_spam_address );
-
- return $email_no_spam_address;
+ return str_replace( '@', '@', $email_no_spam_address );
}
/**
* Callback to convert URI match to HTML A element.
*
- * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
- * make_clickable()}.
+ * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
*
* @since 2.3.2
* @access private
@@ -1840,7 +2490,7 @@ function antispambot( $email_address, $hex_encoding = 0 ) {
* @param array $matches Single Regex Match.
* @return string HTML A element with URI address.
*/
-function _make_url_clickable_cb($matches) {
+function _make_url_clickable_cb( $matches ) {
$url = $matches[2];
if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
@@ -1868,8 +2518,7 @@ function _make_url_clickable_cb($matches) {
/**
* Callback to convert URL match to HTML A element.
*
- * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
- * make_clickable()}.
+ * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
*
* @since 2.3.2
* @access private
@@ -1877,27 +2526,28 @@ function _make_url_clickable_cb($matches) {
* @param array $matches Single Regex Match.
* @return string HTML A element with URL address.
*/
-function _make_web_ftp_clickable_cb($matches) {
+function _make_web_ftp_clickable_cb( $matches ) {
$ret = '';
$dest = $matches[2];
$dest = 'http://' . $dest;
- $dest = esc_url($dest);
- if ( empty($dest) )
- return $matches[0];
// removed trailing [.,;:)] from URL
if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
$ret = substr($dest, -1);
$dest = substr($dest, 0, strlen($dest)-1);
}
+
+ $dest = esc_url($dest);
+ if ( empty($dest) )
+ return $matches[0];
+
return $matches[1] . "$dest$ret";
}
/**
* Callback to convert email address match to HTML A element.
*
- * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
- * make_clickable()}.
+ * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
*
* @since 2.3.2
* @access private
@@ -1905,7 +2555,7 @@ function _make_web_ftp_clickable_cb($matches) {
* @param array $matches Single Regex Match.
* @return string HTML A element with email address.
*/
-function _make_email_clickable_cb($matches) {
+function _make_email_clickable_cb( $matches ) {
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "$email";
}
@@ -1927,9 +2577,9 @@ function make_clickable( $text ) {
$nested_code_pre = 0; // Keep track of how many levels link is nested inside or
foreach ( $textarr as $piece ) {
- if ( preg_match( '|^]|i', $piece ) || preg_match( '|^]|i', $piece ) )
+ if ( preg_match( '|^]|i', $piece ) || preg_match( '|^]|i', $piece ) || preg_match( '|^' === strtolower( $piece ) || '' === strtolower( $piece ) ) )
$nested_code_pre--;
if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
@@ -1976,8 +2626,7 @@ function make_clickable( $text ) {
}
// Cleanup of accidental links within links
- $r = preg_replace( '#(]+?>|>))]+?>([^>]+?)#i', "$1$3", $r );
- return $r;
+ return preg_replace( '#(]+?>|>))]+?>([^>]+?)#i', "$1$3", $r );
}
/**
@@ -2006,7 +2655,7 @@ function make_clickable( $text ) {
* @access private
*
* @param string $string The string to split.
- * @param int $goal The desired chunk length.
+ * @param int $goal The desired chunk length.
* @return array Numeric array of chunks.
*/
function _split_str_by_whitespace( $string, $goal ) {
@@ -2048,8 +2697,7 @@ function wp_rel_nofollow( $text ) {
// This is a pre save filter, so text is already escaped.
$text = stripslashes($text);
$text = preg_replace_callback('||i', 'wp_rel_nofollow_callback', $text);
- $text = wp_slash($text);
- return $text;
+ return wp_slash( $text );
}
/**
@@ -2065,20 +2713,44 @@ function wp_rel_nofollow( $text ) {
*/
function wp_rel_nofollow_callback( $matches ) {
$text = $matches[1];
- $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
- return "";
+ $atts = shortcode_parse_atts( $matches[1] );
+ $rel = 'nofollow';
+
+ if ( preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'http' ) ) . ')%i', $text ) ||
+ preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'https' ) ) . ')%i', $text )
+ ) {
+ return "";
+ }
+
+ if ( ! empty( $atts['rel'] ) ) {
+ $parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
+ if ( false === array_search( 'nofollow', $parts ) ) {
+ $parts[] = 'nofollow';
+ }
+ $rel = implode( ' ', $parts );
+ unset( $atts['rel'] );
+
+ $html = '';
+ foreach ( $atts as $name => $value ) {
+ $html .= "{$name}=\"$value\" ";
+ }
+ $text = trim( $html );
+ }
+ return "";
}
/**
* Convert one smiley code to the icon graphic file equivalent.
*
- * Callback handler for {@link convert_smilies()}.
+ * Callback handler for convert_smilies().
+ *
* Looks up one smiley code in the $wpsmiliestrans global array and returns an
* `` string for that smiley.
*
- * @global array $wpsmiliestrans
* @since 2.8.0
*
+ * @global array $wpsmiliestrans
+ *
* @param array $matches Single match. Smiley code to convert to image.
* @return string Image string for smiley.
*/
@@ -2101,7 +2773,7 @@ function translate_smiley( $matches ) {
}
/**
- * Filter the Smiley image URL before it's used in the image element.
+ * Filters the Smiley image URL before it's used in the image element.
*
* @since 2.9.0
*
@@ -2121,7 +2793,8 @@ function translate_smiley( $matches ) {
* used in the function isn't empty.
*
* @since 0.71
- * @uses $wp_smiliessearch
+ *
+ * @global string|array $wp_smiliessearch
*
* @param string $text Content to convert smilies from text.
* @return string Converted content with text smilies replaced with images.
@@ -2172,18 +2845,18 @@ function convert_smilies( $text ) {
*
* @since 0.71
*
- * @param string $email Email address to verify.
- * @param boolean $deprecated Deprecated.
+ * @param string $email Email address to verify.
+ * @param bool $deprecated Deprecated.
* @return string|bool Either false or the valid email address.
*/
function is_email( $email, $deprecated = false ) {
if ( ! empty( $deprecated ) )
- _deprecated_argument( __FUNCTION__, '3.0' );
+ _deprecated_argument( __FUNCTION__, '3.0.0' );
// Test for the minimum length the email can be
if ( strlen( $email ) < 3 ) {
/**
- * Filter whether an email address is valid.
+ * Filters whether an email address is valid.
*
* This filter is evaluated under several different contexts, such as 'email_too_short',
* 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
@@ -2193,7 +2866,6 @@ function is_email( $email, $deprecated = false ) {
*
* @param bool $is_email Whether the email address has passed the is_email() checks. Default false.
* @param string $email The email address being checked.
- * @param string $message An explanatory message to the user.
* @param string $context Context under which the email was tested.
*/
return apply_filters( 'is_email', false, $email, 'email_too_short' );
@@ -2265,14 +2937,13 @@ function is_email( $email, $deprecated = false ) {
* @param string $string Subject line
* @return string Converted string to ASCII
*/
-function wp_iso_descrambler($string) {
+function wp_iso_descrambler( $string ) {
/* this may only work with iso-8859-1, I'm afraid */
if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
return $string;
} else {
$subject = str_replace('_', ' ', $matches[2]);
- $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject);
- return $subject;
+ return preg_replace_callback( '#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject );
}
}
@@ -2307,13 +2978,19 @@ 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 )
+ 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 );
+ 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 ) ) {
+ $datetime = strtotime( $string );
+ if ( false === $datetime ) {
+ return gmdate( $format, 0 );
+ }
+ return gmdate( $format, $datetime );
+ }
$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 );
}
@@ -2359,7 +3036,7 @@ function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
* @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
* @return int|float The offset in seconds.
*/
-function iso8601_timezone_to_offset($timezone) {
+function iso8601_timezone_to_offset( $timezone ) {
// $timezone is either 'Z' or '[+|-]hhmm'
if ($timezone == 'Z') {
$offset = 0;
@@ -2377,11 +3054,11 @@ function iso8601_timezone_to_offset($timezone) {
*
* @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'.
+ * @param string $date_string Date and time in ISO 8601 format {@link https://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') {
+function iso8601_to_datetime( $date_string, $timezone = 'user' ) {
$timezone = strtolower($timezone);
if ($timezone == 'gmt') {
@@ -2404,23 +3081,6 @@ function iso8601_to_datetime($date_string, $timezone = 'user') {
}
}
-/**
- * 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('//i', "", $text);
- return $text;
-}
-
/**
* Strips out all characters that are not allowable in an email.
*
@@ -2433,7 +3093,7 @@ function sanitize_email( $email ) {
// Test for the minimum length the email can be
if ( strlen( $email ) < 3 ) {
/**
- * Filter a sanitized email address.
+ * Filters 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',
@@ -2532,7 +3192,7 @@ function sanitize_email( $email ) {
* @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.
+ * @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 = '' ) {
@@ -2558,13 +3218,13 @@ function human_time_diff( $from, $to = '' ) {
if ( $days <= 1 )
$days = 1;
$since = sprintf( _n( '%s day', '%s days', $days ), $days );
- } elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
+ } elseif ( $diff < MONTH_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 ) );
+ } elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
+ $months = round( $diff / MONTH_IN_SECONDS );
if ( $months <= 1 )
$months = 1;
$since = sprintf( _n( '%s month', '%s months', $months ), $months );
@@ -2576,7 +3236,7 @@ function human_time_diff( $from, $to = '' ) {
}
/**
- * Filter the human readable difference between two timestamps.
+ * Filters the human readable difference between two timestamps.
*
* @since 4.0.0
*
@@ -2595,15 +3255,15 @@ function human_time_diff( $from, $to = '' ) {
* 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
+ * The 55 word limit can be modified by plugins/themes using the {@see 'excerpt_length'} filter
+ * The ' […]' string can be modified by plugins/themes using the {@see '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 = '') {
+function wp_trim_excerpt( $text = '' ) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
@@ -2615,7 +3275,7 @@ function wp_trim_excerpt($text = '') {
$text = str_replace(']]>', ']]>', $text);
/**
- * Filter the number of words in an excerpt.
+ * Filters the number of words in an excerpt.
*
* @since 2.7.0
*
@@ -2623,7 +3283,7 @@ function wp_trim_excerpt($text = '') {
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
- * Filter the string in the "more" link displayed after a trimmed excerpt.
+ * Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
@@ -2633,7 +3293,7 @@ function wp_trim_excerpt($text = '') {
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/**
- * Filter the trimmed excerpt string.
+ * Filters the trimmed excerpt string.
*
* @since 2.8.0
*
@@ -2652,19 +3312,25 @@ function wp_trim_excerpt($text = '') {
*
* @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 '…'.
+ * @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 )
+ 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' ) ) ) {
+
+ /*
+ * translators: If your word count is based on single characters (e.g. East Asian characters),
+ * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
+ * Do not translate into your own language.
+ */
+ if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && 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 );
@@ -2673,6 +3339,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
$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 );
@@ -2680,8 +3347,9 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
} else {
$text = implode( $sep, $words_array );
}
+
/**
- * Filter the text content after words have been trimmed.
+ * Filters the text content after words have been trimmed.
*
* @since 3.3.0
*
@@ -2701,10 +3369,10 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
* @param string $text The text within which entities will be converted.
* @return string Text with converted entities.
*/
-function ent2ncr($text) {
+function ent2ncr( $text ) {
/**
- * Filter text before named entities are converted into numbered entities.
+ * Filters text before named entities are converted into numbered entities.
*
* A non-null string must be returned for the filter to be evaluated.
*
@@ -2714,7 +3382,7 @@ function ent2ncr($text) {
* @param string $text The text prior to entity conversion.
*/
$filtered = apply_filters( 'pre_ent2ncr', null, $text );
- if( null !== $filtered )
+ if ( null !== $filtered )
return $filtered;
$to_ncr = array(
@@ -2980,66 +3648,38 @@ function ent2ncr($text) {
}
/**
- * Formats text for the rich text editor.
- *
- * The filter 'richedit_pre' is applied here. If $text is empty the filter will
- * be applied to an empty string.
+ * Formats text for the editor.
*
- * @since 2.0.0
+ * Generally the browsers treat everything inside a textarea as text, but
+ * it is still a good idea to HTML entity encode `<`, `>` and `&` in the content.
*
- * @param string $text The text to be formatted.
- * @return string The formatted text after filter is applied.
- */
-function wp_richedit_pre($text) {
- if ( empty( $text ) ) {
- /**
- * Filter text returned for the rich text editor.
- *
- * This filter is first evaluated, and the value returned, if an empty string
- * is passed to wp_richedit_pre(). If an empty string is passed, it results
- * in a break tag and line feed.
- *
- * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre()
- * return after being formatted.
- *
- * @since 2.0.0
- *
- * @param string $output Text for the rich text editor.
- */
- return apply_filters( 'richedit_pre', '' );
- }
-
- $output = convert_chars($text);
- $output = wpautop($output);
- $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) );
-
- /** This filter is documented in wp-includes/formatting.php */
- return apply_filters( 'richedit_pre', $output );
-}
-
-/**
- * Formats text for the HTML editor.
+ * The filter {@see 'format_for_editor'} is applied here. If `$text` is empty the
+ * filter will be applied to an empty string.
*
- * Unless $output is empty it will pass through htmlspecialchars before the
- * 'htmledit_pre' filter is applied.
+ * @since 4.3.0
*
- * @since 2.5.0
+ * @see _WP_Editors::editor()
*
- * @param string $output The text to be formatted.
- * @return string Formatted text after filter applied.
+ * @param string $text The text to be formatted.
+ * @param string $default_editor The default editor for the current user.
+ * It is usually either 'html' or 'tinymce'.
+ * @return string The formatted text after filter is applied.
*/
-function wp_htmledit_pre($output) {
- if ( !empty($output) )
- $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > &
+function format_for_editor( $text, $default_editor = null ) {
+ if ( $text ) {
+ $text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) );
+ }
/**
- * Filter the text before it is formatted for the HTML editor.
+ * Filters the text after it is formatted for the editor.
*
- * @since 2.5.0
+ * @since 4.3.0
*
- * @param string $output The HTML-formatted text.
+ * @param string $text The formatted text.
+ * @param string $default_editor The default editor for the current user.
+ * It is usually either 'html' or 'tinymce'.
*/
- return apply_filters( 'htmledit_pre', $output );
+ return apply_filters( 'format_for_editor', $text, $default_editor );
}
/**
@@ -3052,8 +3692,9 @@ function wp_htmledit_pre($output) {
* @since 2.8.1
* @access private
*
- * @param string|array $search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
- * @param string $subject The string being searched and replaced on, otherwise known as the haystack.
+ * @param string|array $search The value being searched for, otherwise known as the needle.
+ * An array may be used to designate multiple needles.
+ * @param string $subject The string being searched and replaced on, otherwise known as the haystack.
* @return string The string with the replaced svalues.
*/
function _deep_replace( $search, $subject ) {
@@ -3075,6 +3716,9 @@ function _deep_replace( $search, $subject ) {
* is preparing an array for use in an IN clause.
*
* @since 2.8.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
* @param string|array $data Unescaped data
* @return string|array Escaped data
*/
@@ -3087,28 +3731,38 @@ function esc_sql( $data ) {
* Checks and cleans a URL.
*
* A number of characters are removed from the URL. If the URL is for displaying
- * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
+ * (the default behaviour) ampersands are also replaced. The {@see 'clean_url'} filter
* is applied to the returned cleaned URL.
*
* @since 2.8.0
*
- * @param string $url The URL to be cleaned.
- * @param array $protocols Optional. An array of acceptable protocols.
- * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set.
- * @param string $_context Private. Use esc_url_raw() for database usage.
- * @return string The cleaned $url after the 'clean_url' filter is applied.
+ * @param string $url The URL to be cleaned.
+ * @param array $protocols Optional. An array of acceptable protocols.
+ * Defaults to return value of wp_allowed_protocols()
+ * @param string $_context Private. Use esc_url_raw() for database usage.
+ * @return string The cleaned $url after the {@see 'clean_url'} filter is applied.
*/
function esc_url( $url, $protocols = null, $_context = 'display' ) {
$original_url = $url;
if ( '' == $url )
return $url;
- $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
- $strip = array('%0d', '%0a', '%0D', '%0A');
- $url = _deep_replace($strip, $url);
+
+ $url = str_replace( ' ', '%20', $url );
+ $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url);
+
+ if ( '' === $url ) {
+ return $url;
+ }
+
+ if ( 0 !== stripos( $url, 'mailto:' ) ) {
+ $strip = array('%0d', '%0a', '%0D', '%0A');
+ $url = _deep_replace($strip, $url);
+ }
+
$url = str_replace(';//', '://', $url);
/* If the URL doesn't appear to contain a scheme, we
- * presume it needs http:// appended (unless a relative
+ * presume it needs http:// prepended (unless a relative
* link starting with /, # or ? or a php file).
*/
if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
@@ -3122,6 +3776,43 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) {
$url = str_replace( "'", ''', $url );
}
+ if ( ( false !== strpos( $url, '[' ) ) || ( false !== strpos( $url, ']' ) ) ) {
+
+ $parsed = wp_parse_url( $url );
+ $front = '';
+
+ if ( isset( $parsed['scheme'] ) ) {
+ $front .= $parsed['scheme'] . '://';
+ } elseif ( '/' === $url[0] ) {
+ $front .= '//';
+ }
+
+ if ( isset( $parsed['user'] ) ) {
+ $front .= $parsed['user'];
+ }
+
+ if ( isset( $parsed['pass'] ) ) {
+ $front .= ':' . $parsed['pass'];
+ }
+
+ if ( isset( $parsed['user'] ) || isset( $parsed['pass'] ) ) {
+ $front .= '@';
+ }
+
+ if ( isset( $parsed['host'] ) ) {
+ $front .= $parsed['host'];
+ }
+
+ if ( isset( $parsed['port'] ) ) {
+ $front .= ':' . $parsed['port'];
+ }
+
+ $end_dirty = str_replace( $front, '', $url );
+ $end_clean = str_replace( array( '[', ']' ), array( '%5B', '%5D' ), $end_dirty );
+ $url = str_replace( $end_dirty, $end_clean, $url );
+
+ }
+
if ( '/' === $url[0] ) {
$good_protocol_url = $url;
} else {
@@ -3133,7 +3824,7 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) {
}
/**
- * Filter a string cleaned and escaped for output as a URL.
+ * Filters a string cleaned and escaped for output as a URL.
*
* @since 2.3.0
*
@@ -3149,8 +3840,8 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) {
*
* @since 2.8.0
*
- * @param string $url The URL to be cleaned.
- * @param array $protocols An array of acceptable protocols.
+ * @param string $url The URL to be cleaned.
+ * @param array $protocols An array of acceptable protocols.
* @return string The cleaned URL.
*/
function esc_url_raw( $url, $protocols = null ) {
@@ -3160,14 +3851,14 @@ function esc_url_raw( $url, $protocols = null ) {
/**
* Convert entities, while preserving already-encoded entities.
*
- * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
+ * @link https://secure.php.net/htmlentities Borrowed from the PHP Manual user notes.
*
* @since 1.2.2
*
* @param string $myHTML The text to be converted.
* @return string Converted text.
*/
-function htmlentities2($myHTML) {
+function htmlentities2( $myHTML ) {
$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
$translation_table[chr(38)] = '&';
return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&", strtr($myHTML, $translation_table) );
@@ -3178,7 +3869,7 @@ function htmlentities2($myHTML) {
*
* Escapes text strings for echoing in JS. It is intended to be used for inline JS
* (in a tag attribute, for example onclick="..."). Note that the strings have to
- * be in single quotes. The filter 'js_escape' is also applied here.
+ * be in single quotes. The {@see 'js_escape'} filter is also applied here.
*
* @since 2.8.0
*
@@ -3192,7 +3883,7 @@ function esc_js( $text ) {
$safe_text = str_replace( "\r", '', $safe_text );
$safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
/**
- * Filter a string cleaned and escaped for output in JavaScript.
+ * Filters a string cleaned and escaped for output in JavaScript.
*
* Text passed to esc_js() is stripped of invalid or special characters,
* and properly slashed for output.
@@ -3217,7 +3908,7 @@ function esc_html( $text ) {
$safe_text = wp_check_invalid_utf8( $text );
$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
/**
- * Filter a string cleaned and escaped for output in HTML.
+ * Filters a string cleaned and escaped for output in HTML.
*
* Text passed to esc_html() is stripped of invalid or special characters
* before output.
@@ -3242,7 +3933,7 @@ function esc_attr( $text ) {
$safe_text = wp_check_invalid_utf8( $text );
$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
/**
- * Filter a string cleaned and escaped for output in an HTML attribute.
+ * Filters a string cleaned and escaped for output in an HTML attribute.
*
* Text passed to esc_attr() is stripped of invalid or special characters
* before output.
@@ -3266,7 +3957,7 @@ function esc_attr( $text ) {
function esc_textarea( $text ) {
$safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
/**
- * Filter a string cleaned and escaped for output in a textarea element.
+ * Filters a string cleaned and escaped for output in a textarea element.
*
* @since 3.1.0
*
@@ -3284,10 +3975,10 @@ function esc_textarea( $text ) {
* @param string $tag_name
* @return string
*/
-function tag_escape($tag_name) {
+function tag_escape( $tag_name ) {
$safe_tag = strtolower( preg_replace('/[^a-zA-Z0-9_:]/', '', $tag_name) );
/**
- * Filter a string cleaned and escaped for output as an HTML tag.
+ * Filters a string cleaned and escaped for output as an HTML tag.
*
* @since 2.8.0
*
@@ -3310,7 +4001,7 @@ function tag_escape($tag_name) {
* @return string Absolute path.
*/
function wp_make_link_relative( $link ) {
- return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link );
+ return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link );
}
/**
@@ -3321,22 +4012,29 @@ function wp_make_link_relative( $link ) {
*
* @since 2.0.5
*
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
* @param string $option The name of the option.
- * @param string $value The unsanitised value.
+ * @param string $value The unsanitised value.
* @return string Sanitized value.
*/
-function sanitize_option($option, $value) {
+function sanitize_option( $option, $value ) {
global $wpdb;
+ $original_value = $value;
+ $error = '';
+
switch ( $option ) {
case 'admin_email' :
case 'new_admin_email' :
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- $value = sanitize_email( $value );
- if ( ! is_email( $value ) ) {
- $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
- if ( function_exists( 'add_settings_error' ) )
- add_settings_error( $option, 'invalid_admin_email', __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' ) );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ $value = sanitize_email( $value );
+ if ( ! is_email( $value ) ) {
+ $error = __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' );
+ }
}
break;
@@ -3344,6 +4042,8 @@ function sanitize_option($option, $value) {
case 'thumbnail_size_h':
case 'medium_size_w':
case 'medium_size_h':
+ case 'medium_large_size_w':
+ case 'medium_large_size_h':
case 'large_size_w':
case 'large_size_h':
case 'mailserver_port':
@@ -3359,6 +4059,7 @@ function sanitize_option($option, $value) {
case 'thread_comments_depth':
case 'users_can_register':
case 'start_of_week':
+ case 'site_icon':
$value = absint( $value );
break;
@@ -3381,8 +4082,15 @@ function sanitize_option($option, $value) {
case 'blogdescription':
case 'blogname':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- $value = wp_kses_post( $value );
- $value = esc_html( $value );
+ if ( $value !== $original_value ) {
+ $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', wp_encode_emoji( $original_value ) );
+ }
+
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ $value = esc_html( $value );
+ }
break;
case 'blog_charset':
@@ -3404,8 +4112,12 @@ function sanitize_option($option, $value) {
case 'mailserver_pass':
case 'upload_path':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- $value = strip_tags( $value );
- $value = wp_kses_data( $value );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ $value = strip_tags( $value );
+ $value = wp_kses_data( $value );
+ }
break;
case 'ping_sites':
@@ -3421,23 +4133,27 @@ function sanitize_option($option, $value) {
case 'siteurl':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
- $value = esc_url_raw($value);
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
} else {
- $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
- if ( function_exists('add_settings_error') )
- add_settings_error('siteurl', 'invalid_siteurl', __('The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.'));
+ if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
+ $value = esc_url_raw( $value );
+ } else {
+ $error = __( 'The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.' );
+ }
}
break;
case 'home':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
- $value = esc_url_raw($value);
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
} else {
- $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
- if ( function_exists('add_settings_error') )
- add_settings_error('home', 'invalid_home', __('The Site address you entered did not appear to be a valid URL. Please enter a valid URL.'));
+ if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
+ $value = esc_url_raw( $value );
+ } else {
+ $error = __( 'The Site address you entered did not appear to be a valid URL. Please enter a valid URL.' );
+ }
}
break;
@@ -3453,38 +4169,45 @@ function sanitize_option($option, $value) {
case 'illegal_names':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- if ( ! is_array( $value ) )
- $value = explode( ' ', $value );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ if ( ! is_array( $value ) )
+ $value = explode( ' ', $value );
- $value = array_values( array_filter( array_map( 'trim', $value ) ) );
+ $value = array_values( array_filter( array_map( 'trim', $value ) ) );
- if ( ! $value )
- $value = '';
+ if ( ! $value )
+ $value = '';
+ }
break;
case 'limited_email_domains':
case 'banned_email_domains':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- if ( ! is_array( $value ) )
- $value = explode( "\n", $value );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ if ( ! is_array( $value ) )
+ $value = explode( "\n", $value );
- $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
- $value = array();
+ $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
+ $value = array();
- foreach ( $domains as $domain ) {
- if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) )
- $value[] = $domain;
+ foreach ( $domains as $domain ) {
+ if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) ) {
+ $value[] = $domain;
+ }
+ }
+ if ( ! $value )
+ $value = '';
}
- if ( ! $value )
- $value = '';
break;
case 'timezone_string':
$allowed_zones = timezone_identifiers_list();
if ( ! in_array( $value, $allowed_zones ) && ! empty( $value ) ) {
- $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
- if ( function_exists('add_settings_error') )
- add_settings_error('timezone_string', 'invalid_timezone_string', __('The timezone you have entered is not valid. Please select a valid timezone.') );
+ $error = __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
}
break;
@@ -3492,8 +4215,20 @@ function sanitize_option($option, $value) {
case 'category_base':
case 'tag_base':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- $value = esc_url_raw( $value );
- $value = str_replace( 'http://', '', $value );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ $value = esc_url_raw( $value );
+ $value = str_replace( 'http://', '', $value );
+ }
+
+ if ( 'permalink_structure' === $option && '' !== $value && ! preg_match( '/%[^\/%]+%/', $value ) ) {
+ $error = sprintf(
+ /* translators: %s: Codex URL */
+ __( 'A structure tag is required when using custom permalinks. Learn more' ),
+ __( 'https://codex.wordpress.org/Using_Permalinks#Choosing_your_permalink_structure' )
+ );
+ }
break;
case 'default_role' :
@@ -3504,22 +4239,61 @@ function sanitize_option($option, $value) {
case 'moderation_keys':
case 'blacklist_keys':
$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
- $value = explode( "\n", $value );
- $value = array_filter( array_map( 'trim', $value ) );
- $value = array_unique( $value );
- $value = implode( "\n", $value );
+ if ( is_wp_error( $value ) ) {
+ $error = $value->get_error_message();
+ } else {
+ $value = explode( "\n", $value );
+ $value = array_filter( array_map( 'trim', $value ) );
+ $value = array_unique( $value );
+ $value = implode( "\n", $value );
+ }
break;
}
+ if ( ! empty( $error ) ) {
+ $value = get_option( $option );
+ if ( function_exists( 'add_settings_error' ) ) {
+ add_settings_error( $option, "invalid_{$option}", $error );
+ }
+ }
+
/**
- * Filter an option value following sanitization.
+ * Filters an option value following sanitization.
*
* @since 2.3.0
+ * @since 4.3.0 Added the `$original_value` parameter.
*
- * @param string $value The sanitized option value.
- * @param string $option The option name.
+ * @param string $value The sanitized option value.
+ * @param string $option The option name.
+ * @param string $original_value The original value passed to the function.
*/
- $value = apply_filters( "sanitize_option_{$option}", $value, $option );
+ return apply_filters( "sanitize_option_{$option}", $value, $option, $original_value );
+}
+
+/**
+ * Maps a function to all non-iterable elements of an array or an object.
+ *
+ * This is similar to `array_walk_recursive()` but acts upon objects too.
+ *
+ * @since 4.4.0
+ *
+ * @param mixed $value The array, object, or scalar.
+ * @param callable $callback The function to map onto $value.
+ * @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
+ */
+function map_deep( $value, $callback ) {
+ if ( is_array( $value ) ) {
+ foreach ( $value as $index => $item ) {
+ $value[ $index ] = map_deep( $item, $callback );
+ }
+ } elseif ( is_object( $value ) ) {
+ $object_vars = get_object_vars( $value );
+ foreach ( $object_vars as $property_name => $property_value ) {
+ $value->$property_name = map_deep( $property_value, $callback );
+ }
+ } else {
+ $value = call_user_func( $callback, $value );
+ }
return $value;
}
@@ -3527,20 +4301,20 @@ function sanitize_option($option, $value) {
/**
* Parses a string into variables to be stored in an array.
*
- * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
- * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
+ * Uses {@link https://secure.php.net/parse_str parse_str()} and stripslashes if
+ * {@link https://secure.php.net/magic_quotes magic_quotes_gpc} is on.
*
* @since 2.2.1
*
* @param string $string The string to be parsed.
- * @param array $array Variables will be stored in this array.
+ * @param array $array Variables will be stored in this array.
*/
function wp_parse_str( $string, &$array ) {
parse_str( $string, $array );
if ( get_magic_quotes_gpc() )
$array = stripslashes_deep( $array );
/**
- * Filter the array of variables derived from a parsed string.
+ * Filters the array of variables derived from a parsed string.
*
* @since 2.3.0
*
@@ -3581,9 +4355,9 @@ function wp_pre_kses_less_than_callback( $matches ) {
* WordPress implementation of PHP sprintf() with filters.
*
* @since 2.5.0
- * @link http://www.php.net/sprintf
+ * @link https://secure.php.net/sprintf
*
- * @param string $pattern The string which formatted args are inserted.
+ * @param string $pattern The string which formatted args are inserted.
* @param mixed $args ,... Arguments to be formatted into the $pattern string.
* @return string The formatted string.
*/
@@ -3625,7 +4399,7 @@ function wp_sprintf( $pattern ) {
}
/**
- * Filter a fragment from the pattern passed to wp_sprintf().
+ * Filters a fragment from the pattern passed to wp_sprintf().
*
* If the fragment is unchanged, then sprintf() will be run on the fragment.
*
@@ -3658,10 +4432,10 @@ function wp_sprintf( $pattern ) {
* @since 2.5.0
*
* @param string $pattern Content containing '%l' at the beginning.
- * @param array $args List items to prepend to the content and replace '%l'.
+ * @param array $args List items to prepend to the content and replace '%l'.
* @return string Localized list items and rest of the content.
*/
-function wp_sprintf_l($pattern, $args) {
+function wp_sprintf_l( $pattern, $args ) {
// Not a match
if ( substr($pattern, 0, 2) != '%l' )
return $pattern;
@@ -3671,7 +4445,7 @@ function wp_sprintf_l($pattern, $args) {
return '';
/**
- * Filter the translated delimiters used by wp_sprintf_l().
+ * Filters the translated delimiters used by wp_sprintf_l().
* Placeholders (%s) are included to assist translators and then
* removed before the array of strings reaches the filter.
*
@@ -3716,9 +4490,9 @@ function wp_sprintf_l($pattern, $args) {
*
* @since 2.5.0
*
- * @param string $str String to get the excerpt from.
- * @param integer $count Maximum number of characters to take.
- * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
+ * @param string $str String to get the excerpt from.
+ * @param int $count Maximum number of characters to take.
+ * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
* @return string The excerpt.
*/
function wp_html_excerpt( $str, $count, $more = null ) {
@@ -3741,9 +4515,11 @@ function wp_html_excerpt( $str, $count, $more = null ) {
*
* @since 2.7.0
*
+ * @global string $_links_add_base
+ *
* @param string $content String to search for links in.
- * @param string $base The base URL to prefix to links.
- * @param array $attrs The attributes which should be processed.
+ * @param string $base The base URL to prefix to links.
+ * @param array $attrs The attributes which should be processed.
* @return string The processed content.
*/
function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
@@ -3759,16 +4535,18 @@ function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
* @since 2.7.0
* @access private
*
+ * @global string $_links_add_base
+ *
* @param string $m The matched link.
* @return string The processed link.
*/
-function _links_add_base($m) {
+function _links_add_base( $m ) {
global $_links_add_base;
//1 = attribute name 2 = quotation mark 3 = URL
return $m[1] . '=' . $m[2] .
( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ?
$m[3] :
- WP_HTTP::make_absolute_url( $m[3], $_links_add_base )
+ WP_Http::make_absolute_url( $m[3], $_links_add_base )
)
. $m[2];
}
@@ -3783,9 +4561,11 @@ function _links_add_base($m) {
*
* @since 2.7.0
*
+ * @global string $_links_add_target
+ *
* @param string $content String to search for links in.
- * @param string $target The Target to add to the links.
- * @param array $tags An array of tags to apply to.
+ * @param string $target The Target to add to the links.
+ * @param array $tags An array of tags to apply to.
* @return string The processed content.
*/
function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
@@ -3801,6 +4581,8 @@ function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
* @since 2.7.0
* @access private
*
+ * @global string $_links_add_target
+ *
* @param string $m The matched link.
* @return string The processed link.
*/
@@ -3835,8 +4617,8 @@ function normalize_whitespace( $str ) {
*
* @since 2.9.0
*
- * @param string $string String containing HTML tags
- * @param bool $remove_breaks optional Whether to remove left over line breaks and white space chars
+ * @param string $string String containing HTML tags
+ * @param bool $remove_breaks Optional. Whether to remove left over line breaks and white space chars
* @return string The processed string.
*/
function wp_strip_all_tags($string, $remove_breaks = false) {
@@ -3850,20 +4632,23 @@ function wp_strip_all_tags($string, $remove_breaks = false) {
}
/**
- * Sanitize a string from user input or from the db
+ * Sanitizes a string from user input or from the database.
*
- * check for invalid UTF-8,
- * Convert single < characters to entity,
- * strip all tags,
- * remove line breaks, tabs and extra white space,
- * strip octets.
+ * - Checks for invalid UTF-8,
+ * - Converts single `<` characters to entities
+ * - Strips all tags
+ * - Removes line breaks, tabs, and extra whitespace
+ * - Strips octets
*
* @since 2.9.0
*
- * @param string $str
- * @return string
+ * @see wp_check_invalid_utf8()
+ * @see wp_strip_all_tags()
+ *
+ * @param string $str String to sanitize.
+ * @return string Sanitized string.
*/
-function sanitize_text_field($str) {
+function sanitize_text_field( $str ) {
$filtered = wp_check_invalid_utf8( $str );
if ( strpos($filtered, '<') !== false ) {
@@ -3886,7 +4671,7 @@ function sanitize_text_field($str) {
}
/**
- * Filter a sanitized text field string.
+ * Filters a sanitized text field string.
*
* @since 2.9.0
*
@@ -3901,7 +4686,7 @@ function sanitize_text_field($str) {
*
* @since 3.1.0
*
- * @param string $path A path.
+ * @param string $path A path.
* @param string $suffix If the filename ends in suffix this will also be cut off.
* @return string
*/
@@ -3915,6 +4700,11 @@ function wp_basename( $path, $suffix = '' ) {
* Violating our coding standards for a good function name.
*
* @since 3.0.0
+ *
+ * @staticvar string|false $dblq
+ *
+ * @param string $text The text to be modified.
+ * @return string The modified text.
*/
function capital_P_dangit( $text ) {
// Simple replacement for titles
@@ -3923,13 +4713,13 @@ function capital_P_dangit( $text ) {
return str_replace( 'Wordpress', 'WordPress', $text );
// Still here? Use the more judicious replacement
static $dblq = false;
- if ( false === $dblq )
+ if ( false === $dblq ) {
$dblq = _x( '“', 'opening curly double quote' );
+ }
return str_replace(
array( ' Wordpress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ),
array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
$text );
-
}
/**
@@ -3943,7 +4733,7 @@ function capital_P_dangit( $text ) {
function sanitize_mime_type( $mime_type ) {
$sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
/**
- * Filter a mime type following sanitization.
+ * Filters a mime type following sanitization.
*
* @since 3.1.3
*
@@ -3970,7 +4760,7 @@ function sanitize_trackback_urls( $to_ping ) {
$urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping );
$urls_to_ping = implode( "\n", $urls_to_ping );
/**
- * Filter a list of trackback URLs following sanitization.
+ * Filters a list of trackback URLs following sanitization.
*
* The string returned here consists of a space or carriage return-delimited list
* of trackback URLs.
@@ -4031,7 +4821,7 @@ function wp_unslash( $value ) {
* @since 3.6.0
*
* @param string $content A string which might contain a URL.
- * @return string The found URL.
+ * @return string|false The found URL.
*/
function get_url_in_content( $content ) {
if ( empty( $content ) ) {
@@ -4054,14 +4844,16 @@ function get_url_in_content( $content ) {
*
* @since 4.0.0
*
+ * @staticvar string $spaces
+ *
* @return string The spaces regexp.
*/
function wp_spaces_regexp() {
- static $spaces;
+ static $spaces = '';
if ( empty( $spaces ) ) {
/**
- * Filter the regexp for common whitespace characters.
+ * Filters the regexp for common whitespace characters.
*
* This string is substituted for the \s sequence as needed in regular
* expressions. For websites not written in English, different characters
@@ -4082,6 +4874,8 @@ function wp_spaces_regexp() {
* Print the important emoji-related styles.
*
* @since 4.2.0
+ *
+ * @staticvar bool $printed
*/
function print_emoji_styles() {
static $printed = false;
@@ -4109,8 +4903,13 @@ img.emoji {
apply_filters( 'emoji_url', set_url_scheme( '//s.w.org/images/core/emoji/72x72/' ) ),
+ 'baseUrl' => apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/2/72x72/' ),
/**
- * Filter the extension of the emoji files.
+ * Filters the extension of the emoji png files.
*
* @since 4.2.0
*
- * @param string The emoji extension. Default .png.
+ * @param string The emoji extension for png files. Default .png.
*/
'ext' => apply_filters( 'emoji_ext', '.png' ),
+
+ /**
+ * Filters the URL where emoji SVG images are hosted.
+ *
+ * @since 4.6.0
+ *
+ * @param string The emoji base URL for svg images.
+ */
+ 'svgUrl' => apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ),
+
+ /**
+ * Filters the extension of the emoji SVG files.
+ *
+ * @since 4.6.0
+ *
+ * @param string The emoji extension for svg files. Default .svg.
+ */
+ 'svgExt' => apply_filters( 'emoji_svg_ext', '.svg' ),
);
$version = 'ver=' . $wp_version;
@@ -4174,7 +5006,7 @@ function print_emoji_detection_script() {
?>
$length ) {
+ $short_url = substr( $short_url, 0, $length - 3 ) . '…';
+ }
+ return $short_url;
+}
+
+/**
+ * Sanitizes a hex color.
+ *
+ * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
+ * For sanitizing values without a #, see sanitize_hex_color_no_hash().
+ *
+ * @since 3.4.0
+ *
+ * @param string $color
+ * @return string|void
+ */
+function sanitize_hex_color( $color ) {
+ if ( '' === $color ) {
+ return '';
+ }
+
+ // 3 or 6 hex digits, or the empty string.
+ if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
+ return $color;
+ }
+}
+
+/**
+ * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
+ *
+ * Saving hex colors without a hash puts the burden of adding the hash on the
+ * UI, which makes it difficult to use or upgrade to other color types such as
+ * rgba, hsl, rgb, and html color names.
+ *
+ * Returns either '', a 3 or 6 digit hex color (without a #), or null.
+ *
+ * @since 3.4.0
+ *
+ * @param string $color
+ * @return string|null
+ */
+function sanitize_hex_color_no_hash( $color ) {
+ $color = ltrim( $color, '#' );
+
+ if ( '' === $color ) {
+ return '';
+ }
+
+ return sanitize_hex_color( '#' . $color ) ? $color : null;
+}
+
+/**
+ * Ensures that any hex color is properly hashed.
+ * Otherwise, returns value untouched.
+ *
+ * This method should only be necessary if using sanitize_hex_color_no_hash().
+ *
+ * @since 3.4.0
+ *
+ * @param string $color
+ * @return string
+ */
+function maybe_hash_hex_color( $color ) {
+ if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) {
+ return '#' . $unhashed;
+ }
+
+ return $color;
+}