formatting.php ➔ wpautop()   F
last analyzed

Complexity

Conditions 13
Paths 257

Size

Total Lines 149
Code Lines 63

Duplication

Lines 3
Ratio 2.01 %

Importance

Changes 0
Metric Value
cc 13
eloc 63
nc 257
nop 2
dl 3
loc 149
rs 3.7737
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Main WordPress Formatting API.
4
 *
5
 * Handles many functions for formatting output.
6
 *
7
 * @package WordPress
8
 */
9
10
/**
11
 * Replaces common plain text characters into formatted entities
12
 *
13
 * As an example,
14
 *
15
 *     'cause today's effort makes it worth tomorrow's "holiday" ...
16
 *
17
 * Becomes:
18
 *
19
 *     &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221; &#8230;
20
 *
21
 * Code within certain html blocks are skipped.
22
 *
23
 * Do not use this function before the {@see 'init'} action hook; everything will break.
24
 *
25
 * @since 0.71
26
 *
27
 * @global array $wp_cockneyreplace Array of formatted entities for certain common phrases
28
 * @global array $shortcode_tags
29
 * @staticvar array $static_characters
30
 * @staticvar array $static_replacements
31
 * @staticvar array $dynamic_characters
32
 * @staticvar array $dynamic_replacements
33
 * @staticvar array $default_no_texturize_tags
34
 * @staticvar array $default_no_texturize_shortcodes
35
 * @staticvar bool  $run_texturize
36
 *
37
 * @param string $text The text to be formatted
38
 * @param bool   $reset Set to true for unit testing. Translated patterns will reset.
39
 * @return string The string replaced with html entities
40
 */
41
function wptexturize( $text, $reset = false ) {
42
	global $wp_cockneyreplace, $shortcode_tags;
43
	static $static_characters = null,
44
		$static_replacements = null,
45
		$dynamic_characters = null,
46
		$dynamic_replacements = null,
47
		$default_no_texturize_tags = null,
48
		$default_no_texturize_shortcodes = null,
49
		$run_texturize = true,
50
		$apos = null,
51
		$prime = null,
52
		$double_prime = null,
53
		$opening_quote = null,
54
		$closing_quote = null,
55
		$opening_single_quote = null,
56
		$closing_single_quote = null,
57
		$open_q_flag = '<!--oq-->',
58
		$open_sq_flag = '<!--osq-->',
59
		$apos_flag = '<!--apos-->';
60
61
	// If there's nothing to do, just stop.
62
	if ( empty( $text ) || false === $run_texturize ) {
63
		return $text;
64
	}
65
66
	// Set up static variables. Run once only.
67
	if ( $reset || ! isset( $static_characters ) ) {
68
		/**
69
		 * Filters whether to skip running wptexturize().
70
		 *
71
		 * Passing false to the filter will effectively short-circuit wptexturize().
72
		 * returning the original text passed to the function instead.
73
		 *
74
		 * The filter runs only once, the first time wptexturize() is called.
75
		 *
76
		 * @since 4.0.0
77
		 *
78
		 * @see wptexturize()
79
		 *
80
		 * @param bool $run_texturize Whether to short-circuit wptexturize().
81
		 */
82
		$run_texturize = apply_filters( 'run_wptexturize', $run_texturize );
83
		if ( false === $run_texturize ) {
84
			return $text;
85
		}
86
87
		/* translators: opening curly double quote */
88
		$opening_quote = _x( '&#8220;', 'opening curly double quote' );
89
		/* translators: closing curly double quote */
90
		$closing_quote = _x( '&#8221;', 'closing curly double quote' );
91
92
		/* translators: apostrophe, for example in 'cause or can't */
93
		$apos = _x( '&#8217;', 'apostrophe' );
94
95
		/* translators: prime, for example in 9' (nine feet) */
96
		$prime = _x( '&#8242;', 'prime' );
97
		/* translators: double prime, for example in 9" (nine inches) */
98
		$double_prime = _x( '&#8243;', 'double prime' );
99
100
		/* translators: opening curly single quote */
101
		$opening_single_quote = _x( '&#8216;', 'opening curly single quote' );
102
		/* translators: closing curly single quote */
103
		$closing_single_quote = _x( '&#8217;', 'closing curly single quote' );
104
105
		/* translators: en dash */
106
		$en_dash = _x( '&#8211;', 'en dash' );
107
		/* translators: em dash */
108
		$em_dash = _x( '&#8212;', 'em dash' );
109
110
		$default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');
111
		$default_no_texturize_shortcodes = array('code');
112
113
		// if a plugin has provided an autocorrect array, use it
114 View Code Duplication
		if ( isset($wp_cockneyreplace) ) {
115
			$cockney = array_keys( $wp_cockneyreplace );
116
			$cockneyreplace = array_values( $wp_cockneyreplace );
117
		} else {
118
			/* translators: This is a comma-separated list of words that defy the syntax of quotations in normal use,
119
			 * for example...  'We do not have enough words yet' ... is a typical quoted phrase.  But when we write
120
			 * lines of code 'til we have enough of 'em, then we need to insert apostrophes instead of quotes.
121
			 */
122
			$cockney = explode( ',', _x( "'tain't,'twere,'twas,'tis,'twill,'til,'bout,'nuff,'round,'cause,'em",
123
				'Comma-separated list of words to texturize in your language' ) );
124
125
			$cockneyreplace = explode( ',', _x( '&#8217;tain&#8217;t,&#8217;twere,&#8217;twas,&#8217;tis,&#8217;twill,&#8217;til,&#8217;bout,&#8217;nuff,&#8217;round,&#8217;cause,&#8217;em',
126
				'Comma-separated list of replacement words in your language' ) );
127
		}
128
129
		$static_characters = array_merge( array( '...', '``', '\'\'', ' (tm)' ), $cockney );
130
		$static_replacements = array_merge( array( '&#8230;', $opening_quote, $closing_quote, ' &#8482;' ), $cockneyreplace );
131
132
133
		// Pattern-based replacements of characters.
134
		// Sort the remaining patterns into several arrays for performance tuning.
135
		$dynamic_characters = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
136
		$dynamic_replacements = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
137
		$dynamic = array();
138
		$spaces = wp_spaces_regexp();
139
140
		// '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation.
141
		if ( "'" !== $apos || "'" !== $closing_single_quote ) {
142
			$dynamic[ '/\'(\d\d)\'(?=\Z|[.,:;!?)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos_flag . '$1' . $closing_single_quote;
143
		}
144 View Code Duplication
		if ( "'" !== $apos || '"' !== $closing_quote ) {
145
			$dynamic[ '/\'(\d\d)"(?=\Z|[.,:;!?)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos_flag . '$1' . $closing_quote;
146
		}
147
148
		// '99 '99s '99's (apostrophe)  But never '9 or '99% or '999 or '99.0.
149
		if ( "'" !== $apos ) {
150
			$dynamic[ '/\'(?=\d\d(?:\Z|(?![%\d]|[.,]\d)))/' ] = $apos_flag;
151
		}
152
153
		// Quoted Numbers like '0.42'
154
		if ( "'" !== $opening_single_quote && "'" !== $closing_single_quote ) {
155
			$dynamic[ '/(?<=\A|' . $spaces . ')\'(\d[.,\d]*)\'/' ] = $open_sq_flag . '$1' . $closing_single_quote;
156
		}
157
158
		// Single quote at start, or preceded by (, {, <, [, ", -, or spaces.
159
		if ( "'" !== $opening_single_quote ) {
160
			$dynamic[ '/(?<=\A|[([{"\-]|&lt;|' . $spaces . ')\'/' ] = $open_sq_flag;
161
		}
162
163
		// Apostrophe in a word.  No spaces, double apostrophes, or other punctuation.
164
		if ( "'" !== $apos ) {
165
			$dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;!?"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos_flag;
166
		}
167
168
		$dynamic_characters['apos'] = array_keys( $dynamic );
169
		$dynamic_replacements['apos'] = array_values( $dynamic );
170
		$dynamic = array();
171
172
		// Quoted Numbers like "42"
173 View Code Duplication
		if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
174
			$dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $open_q_flag . '$1' . $closing_quote;
175
		}
176
177
		// Double quote at start, or preceded by (, {, <, [, -, or spaces, and not followed by spaces.
178 View Code Duplication
		if ( '"' !== $opening_quote ) {
179
			$dynamic[ '/(?<=\A|[([{\-]|&lt;|' . $spaces . ')"(?!' . $spaces . ')/' ] = $open_q_flag;
180
		}
181
182
		$dynamic_characters['quote'] = array_keys( $dynamic );
183
		$dynamic_replacements['quote'] = array_values( $dynamic );
184
		$dynamic = array();
185
186
		// Dashes and spaces
187
		$dynamic[ '/---/' ] = $em_dash;
188
		$dynamic[ '/(?<=^|' . $spaces . ')--(?=$|' . $spaces . ')/' ] = $em_dash;
189
		$dynamic[ '/(?<!xn)--/' ] = $en_dash;
190
		$dynamic[ '/(?<=^|' . $spaces . ')-(?=$|' . $spaces . ')/' ] = $en_dash;
191
192
		$dynamic_characters['dash'] = array_keys( $dynamic );
193
		$dynamic_replacements['dash'] = array_values( $dynamic );
194
	}
195
196
	// Must do this every time in case plugins use these filters in a context sensitive manner
197
	/**
198
	 * Filters the list of HTML elements not to texturize.
199
	 *
200
	 * @since 2.8.0
201
	 *
202
	 * @param array $default_no_texturize_tags An array of HTML element names.
203
	 */
204
	$no_texturize_tags = apply_filters( 'no_texturize_tags', $default_no_texturize_tags );
205
	/**
206
	 * Filters the list of shortcodes not to texturize.
207
	 *
208
	 * @since 2.8.0
209
	 *
210
	 * @param array $default_no_texturize_shortcodes An array of shortcode names.
211
	 */
212
	$no_texturize_shortcodes = apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes );
213
214
	$no_texturize_tags_stack = array();
215
	$no_texturize_shortcodes_stack = array();
216
217
	// Look for shortcodes and HTML elements.
218
219
	preg_match_all( '@\[/?([^<>&/\[\]\x00-\x20=]++)@', $text, $matches );
220
	$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
221
	$found_shortcodes = ! empty( $tagnames );
222
	$shortcode_regex = $found_shortcodes ? _get_wptexturize_shortcode_regex( $tagnames ) : '';
223
	$regex = _get_wptexturize_split_regex( $shortcode_regex );
224
225
	$textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
226
227
	foreach ( $textarr as &$curl ) {
228
		// Only call _wptexturize_pushpop_element if $curl is a delimiter.
229
		$first = $curl[0];
230
		if ( '<' === $first ) {
231
			if ( '<!--' === substr( $curl, 0, 4 ) ) {
232
				// This is an HTML comment delimiter.
233
				continue;
234
			} else {
235
				// This is an HTML element delimiter.
236
237
				// Replace each & with &#038; unless it already looks like an entity.
238
				$curl = preg_replace( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $curl );
239
240
				_wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
241
			}
242
243
		} elseif ( '' === trim( $curl ) ) {
244
			// This is a newline between delimiters.  Performance improves when we check this.
245
			continue;
246
247
		} elseif ( '[' === $first && $found_shortcodes && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
248
			// This is a shortcode delimiter.
249
250
			if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
251
				// Looks like a normal shortcode.
252
				_wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
253
			} else {
254
				// Looks like an escaped shortcode.
255
				continue;
256
			}
257
258
		} elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
259
			// This is neither a delimiter, nor is this content inside of no_texturize pairs.  Do texturize.
260
261
			$curl = str_replace( $static_characters, $static_replacements, $curl );
262
263
			if ( false !== strpos( $curl, "'" ) ) {
264
				$curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
265
				$curl = wptexturize_primes( $curl, "'", $prime, $open_sq_flag, $closing_single_quote );
266
				$curl = str_replace( $apos_flag, $apos, $curl );
267
				$curl = str_replace( $open_sq_flag, $opening_single_quote, $curl );
268
			}
269
			if ( false !== strpos( $curl, '"' ) ) {
270
				$curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
271
				$curl = wptexturize_primes( $curl, '"', $double_prime, $open_q_flag, $closing_quote );
272
				$curl = str_replace( $open_q_flag, $opening_quote, $curl );
273
			}
274
			if ( false !== strpos( $curl, '-' ) ) {
275
				$curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
276
			}
277
278
			// 9x9 (times), but never 0x9999
279
			if ( 1 === preg_match( '/(?<=\d)x\d/', $curl ) ) {
280
				// Searching for a digit is 10 times more expensive than for the x, so we avoid doing this one!
281
				$curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
282
			}
283
284
			// Replace each & with &#038; unless it already looks like an entity.
285
			$curl = preg_replace( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $curl );
286
		}
287
	}
288
289
	return implode( '', $textarr );
290
}
291
292
/**
293
 * Implements a logic tree to determine whether or not "7'." represents seven feet,
294
 * then converts the special char into either a prime char or a closing quote char.
295
 *
296
 * @since 4.3.0
297
 *
298
 * @param string $haystack    The plain text to be searched.
299
 * @param string $needle      The character to search for such as ' or ".
300
 * @param string $prime       The prime char to use for replacement.
301
 * @param string $open_quote  The opening quote char. Opening quote replacement must be
302
 *                            accomplished already.
303
 * @param string $close_quote The closing quote char to use for replacement.
304
 * @return string The $haystack value after primes and quotes replacements.
305
 */
306
function wptexturize_primes( $haystack, $needle, $prime, $open_quote, $close_quote ) {
307
	$spaces = wp_spaces_regexp();
308
	$flag = '<!--wp-prime-or-quote-->';
309
	$quote_pattern = "/$needle(?=\\Z|[.,:;!?)}\\-\\]]|&gt;|" . $spaces . ")/";
310
	$prime_pattern    = "/(?<=\\d)$needle/";
311
	$flag_after_digit = "/(?<=\\d)$flag/";
312
	$flag_no_digit    = "/(?<!\\d)$flag/";
313
314
	$sentences = explode( $open_quote, $haystack );
315
316
	foreach ( $sentences as $key => &$sentence ) {
317
		if ( false === strpos( $sentence, $needle ) ) {
318
			continue;
319
		} elseif ( 0 !== $key && 0 === substr_count( $sentence, $close_quote ) ) {
320
			$sentence = preg_replace( $quote_pattern, $flag, $sentence, -1, $count );
321
			if ( $count > 1 ) {
322
				// This sentence appears to have multiple closing quotes.  Attempt Vulcan logic.
323
				$sentence = preg_replace( $flag_no_digit, $close_quote, $sentence, -1, $count2 );
324
				if ( 0 === $count2 ) {
325
					// Try looking for a quote followed by a period.
326
					$count2 = substr_count( $sentence, "$flag." );
327
					if ( $count2 > 0 ) {
328
						// Assume the rightmost quote-period match is the end of quotation.
329
						$pos = strrpos( $sentence, "$flag." );
330
					} else {
331
						// When all else fails, make the rightmost candidate a closing quote.
332
						// This is most likely to be problematic in the context of bug #18549.
333
						$pos = strrpos( $sentence, $flag );
334
					}
335
					$sentence = substr_replace( $sentence, $close_quote, $pos, strlen( $flag ) );
336
				}
337
				// Use conventional replacement on any remaining primes and quotes.
338
				$sentence = preg_replace( $prime_pattern, $prime, $sentence );
339
				$sentence = preg_replace( $flag_after_digit, $prime, $sentence );
340
				$sentence = str_replace( $flag, $close_quote, $sentence );
341
			} elseif ( 1 == $count ) {
342
				// Found only one closing quote candidate, so give it priority over primes.
343
				$sentence = str_replace( $flag, $close_quote, $sentence );
344
				$sentence = preg_replace( $prime_pattern, $prime, $sentence );
345
			} else {
346
				// No closing quotes found.  Just run primes pattern.
347
				$sentence = preg_replace( $prime_pattern, $prime, $sentence );
348
			}
349
		} else {
350
			$sentence = preg_replace( $prime_pattern, $prime, $sentence );
351
			$sentence = preg_replace( $quote_pattern, $close_quote, $sentence );
352
		}
353
		if ( '"' == $needle && false !== strpos( $sentence, '"' ) ) {
354
			$sentence = str_replace( '"', $close_quote, $sentence );
355
		}
356
	}
357
358
	return implode( $open_quote, $sentences );
359
}
360
361
/**
362
 * Search for disabled element tags. Push element to stack on tag open and pop
363
 * on tag close.
364
 *
365
 * Assumes first char of $text is tag opening and last char is tag closing.
366
 * Assumes second char of $text is optionally '/' to indicate closing as in </html>.
367
 *
368
 * @since 2.9.0
369
 * @access private
370
 *
371
 * @param string $text Text to check. Must be a tag like `<html>` or `[shortcode]`.
372
 * @param array  $stack List of open tag elements.
373
 * @param array  $disabled_elements The tag names to match against. Spaces are not allowed in tag names.
374
 */
375
function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
376
	// Is it an opening tag or closing tag?
377
	if ( isset( $text[1] ) && '/' !== $text[1] ) {
378
		$opening_tag = true;
379
		$name_offset = 1;
380
	} elseif ( 0 == count( $stack ) ) {
381
		// Stack is empty. Just stop.
382
		return;
383
	} else {
384
		$opening_tag = false;
385
		$name_offset = 2;
386
	}
387
388
	// Parse out the tag name.
389
	$space = strpos( $text, ' ' );
390
	if ( false === $space ) {
391
		$space = -1;
392
	} else {
393
		$space -= $name_offset;
394
	}
395
	$tag = substr( $text, $name_offset, $space );
396
397
	// Handle disabled tags.
398
	if ( in_array( $tag, $disabled_elements ) ) {
399
		if ( $opening_tag ) {
400
			/*
401
			 * This disables texturize until we find a closing tag of our type
402
			 * (e.g. <pre>) even if there was invalid nesting before that
403
			 *
404
			 * Example: in the case <pre>sadsadasd</code>"baba"</pre>
405
			 *          "baba" won't be texturize
406
			 */
407
408
			array_push( $stack, $tag );
409
		} elseif ( end( $stack ) == $tag ) {
410
			array_pop( $stack );
411
		}
412
	}
413
}
414
415
/**
416
 * Replaces double line-breaks with paragraph elements.
417
 *
418
 * A group of regex replaces used to identify text formatted with newlines and
419
 * replace double line-breaks with HTML paragraph tags. The remaining line-breaks
420
 * after conversion become <<br />> tags, unless $br is set to '0' or 'false'.
421
 *
422
 * @since 0.71
423
 *
424
 * @param string $pee The text which has to be formatted.
425
 * @param bool   $br  Optional. If set, this will convert all remaining line-breaks
426
 *                    after paragraphing. Default true.
427
 * @return string Text which has been converted into correct paragraph tags.
428
 */
429
function wpautop( $pee, $br = true ) {
430
	$pre_tags = array();
431
432
	if ( trim($pee) === '' )
433
		return '';
434
435
	// Just to make things a little easier, pad the end.
436
	$pee = $pee . "\n";
437
438
	/*
439
	 * Pre tags shouldn't be touched by autop.
440
	 * Replace pre tags with placeholders and bring them back after autop.
441
	 */
442
	if ( strpos($pee, '<pre') !== false ) {
443
		$pee_parts = explode( '</pre>', $pee );
444
		$last_pee = array_pop($pee_parts);
445
		$pee = '';
446
		$i = 0;
447
448
		foreach ( $pee_parts as $pee_part ) {
449
			$start = strpos($pee_part, '<pre');
450
451
			// Malformed html?
452
			if ( $start === false ) {
453
				$pee .= $pee_part;
454
				continue;
455
			}
456
457
			$name = "<pre wp-pre-tag-$i></pre>";
458
			$pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
459
460
			$pee .= substr( $pee_part, 0, $start ) . $name;
461
			$i++;
462
		}
463
464
		$pee .= $last_pee;
465
	}
466
	// Change multiple <br>s into two line breaks, which will turn into paragraphs.
467
	$pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
468
469
	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
470
471
	// Add a single line break above block-level opening tags.
472
	$pee = preg_replace('!(<' . $allblocks . '[\s/>])!', "\n$1", $pee);
473
474
	// Add a double line break below block-level closing tags.
475
	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
476
477
	// Standardize newline characters to "\n".
478
	$pee = str_replace(array("\r\n", "\r"), "\n", $pee);
479
480
	// Find newlines in all elements and add placeholders.
481
	$pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
482
483
	// Collapse line breaks before and after <option> elements so they don't get autop'd.
484
	if ( strpos( $pee, '<option' ) !== false ) {
485
		$pee = preg_replace( '|\s*<option|', '<option', $pee );
486
		$pee = preg_replace( '|</option>\s*|', '</option>', $pee );
487
	}
488
489
	/*
490
	 * Collapse line breaks inside <object> elements, before <param> and <embed> elements
491
	 * so they don't get autop'd.
492
	 */
493
	if ( strpos( $pee, '</object>' ) !== false ) {
494
		$pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
495
		$pee = preg_replace( '|\s*</object>|', '</object>', $pee );
496
		$pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
497
	}
498
499
	/*
500
	 * Collapse line breaks inside <audio> and <video> elements,
501
	 * before and after <source> and <track> elements.
502
	 */
503
	if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
504
		$pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
505
		$pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
506
		$pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
507
	}
508
509
	// Remove more than two contiguous line breaks.
510
	$pee = preg_replace("/\n\n+/", "\n\n", $pee);
511
512
	// Split up the contents into an array of strings, separated by double line breaks.
513
	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
514
515
	// Reset $pee prior to rebuilding.
516
	$pee = '';
517
518
	// Rebuild the content as a string, wrapping every bit with a <p>.
519
	foreach ( $pees as $tinkle ) {
520
		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
521
	}
522
523
	// Under certain strange conditions it could create a P of entirely whitespace.
524
	$pee = preg_replace('|<p>\s*</p>|', '', $pee);
525
526
	// Add a closing <p> inside <div>, <address>, or <form> tag if missing.
527
	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
528
529
	// If an opening or closing block element tag is wrapped in a <p>, unwrap it.
530
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
531
532
	// In some cases <li> may get wrapped in <p>, fix them.
533
	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee);
534
535
	// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
536
	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
537
	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
538
539
	// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
540
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
541
542
	// If an opening or closing block element tag is followed by a closing <p> tag, remove it.
543
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
544
545
	// Optionally insert line breaks.
546
	if ( $br ) {
547
		// Replace newlines that shouldn't be touched with a placeholder.
548
		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
549
550
		// Normalize <br>
551
		$pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee );
552
553
		// Replace any new line characters that aren't preceded by a <br /> with a <br />.
554
		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee);
555
556
		// Replace newline placeholders with newlines.
557
		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
558
	}
559
560
	// If a <br /> tag is after an opening or closing block tag, remove it.
561
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
562
563
	// If a <br /> tag is before a subset of opening or closing block tags, remove it.
564
	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
565
	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
566
567
	// Replace placeholder <pre> tags with their original content.
568
	if ( !empty($pre_tags) )
569
		$pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
570
571
	// Restore newlines in all elements.
572 View Code Duplication
	if ( false !== strpos( $pee, '<!-- wpnl -->' ) ) {
573
		$pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee );
574
	}
575
576
	return $pee;
577
}
578
579
/**
580
 * Separate HTML elements and comments from the text.
581
 *
582
 * @since 4.2.4
583
 *
584
 * @param string $input The text which has to be formatted.
585
 * @return array The formatted text.
586
 */
587
function wp_html_split( $input ) {
588
	return preg_split( get_html_split_regex(), $input, -1, PREG_SPLIT_DELIM_CAPTURE );
589
}
590
591
/**
592
 * Retrieve the regular expression for an HTML element.
593
 *
594
 * @since 4.4.0
595
 *
596
 * @return string The regular expression
597
 */
598
function get_html_split_regex() {
599
	static $regex;
600
601
	if ( ! isset( $regex ) ) {
602
		$comments =
603
			  '!'           // Start of comment, after the <.
604
			. '(?:'         // Unroll the loop: Consume everything until --> is found.
605
			.     '-(?!->)' // Dash not followed by end of comment.
606
			.     '[^\-]*+' // Consume non-dashes.
607
			. ')*+'         // Loop possessively.
608
			. '(?:-->)?';   // End of comment. If not found, match all input.
609
610
		$cdata =
611
			  '!\[CDATA\['  // Start of comment, after the <.
612
			. '[^\]]*+'     // Consume non-].
613
			. '(?:'         // Unroll the loop: Consume everything until ]]> is found.
614
			.     '](?!]>)' // One ] not followed by end of comment.
615
			.     '[^\]]*+' // Consume non-].
616
			. ')*+'         // Loop possessively.
617
			. '(?:]]>)?';   // End of comment. If not found, match all input.
618
619
		$escaped =
620
			  '(?='           // Is the element escaped?
621
			.    '!--'
622
			. '|'
623
			.    '!\[CDATA\['
624
			. ')'
625
			. '(?(?=!-)'      // If yes, which type?
626
			.     $comments
627
			. '|'
628
			.     $cdata
629
			. ')';
630
631
		$regex =
632
			  '/('              // Capture the entire match.
633
			.     '<'           // Find start of element.
634
			.     '(?'          // Conditional expression follows.
635
			.         $escaped  // Find end of escaped element.
636
			.     '|'           // ... else ...
637
			.         '[^>]*>?' // Find end of normal element.
638
			.     ')'
639
			. ')/';
640
	}
641
642
	return $regex;
643
}
644
645
/**
646
 * Retrieve the combined regular expression for HTML and shortcodes.
647
 *
648
 * @access private
649
 * @ignore
650
 * @internal This function will be removed in 4.5.0 per Shortcode API Roadmap.
651
 * @since 4.4.0
652
 *
653
 * @param string $shortcode_regex The result from _get_wptexturize_shortcode_regex().  Optional.
654
 * @return string The regular expression
655
 */
656
function _get_wptexturize_split_regex( $shortcode_regex = '' ) {
657
	static $html_regex;
658
659
	if ( ! isset( $html_regex ) ) {
660
		$comment_regex =
661
			  '!'           // Start of comment, after the <.
662
			. '(?:'         // Unroll the loop: Consume everything until --> is found.
663
			.     '-(?!->)' // Dash not followed by end of comment.
664
			.     '[^\-]*+' // Consume non-dashes.
665
			. ')*+'         // Loop possessively.
666
			. '(?:-->)?';   // End of comment. If not found, match all input.
667
668
		$html_regex =			 // Needs replaced with wp_html_split() per Shortcode API Roadmap.
669
			  '<'                // Find start of element.
670
			. '(?(?=!--)'        // Is this a comment?
671
			.     $comment_regex // Find end of comment.
672
			. '|'
673
			.     '[^>]*>?'      // Find end of element. If not found, match all input.
674
			. ')';
675
	}
676
677
	if ( empty( $shortcode_regex ) ) {
678
		$regex = '/(' . $html_regex . ')/';
679
	} else {
680
		$regex = '/(' . $html_regex . '|' . $shortcode_regex . ')/';
681
	}
682
683
	return $regex;
684
}
685
686
/**
687
 * Retrieve the regular expression for shortcodes.
688
 *
689
 * @access private
690
 * @ignore
691
 * @internal This function will be removed in 4.5.0 per Shortcode API Roadmap.
692
 * @since 4.4.0
693
 *
694
 * @param array $tagnames List of shortcodes to find.
695
 * @return string The regular expression
696
 */
697
function _get_wptexturize_shortcode_regex( $tagnames ) {
698
	$tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
699
	$tagregexp = "(?:$tagregexp)(?=[\\s\\]\\/])"; // Excerpt of get_shortcode_regex().
700
	$regex =
701
		  '\['              // Find start of shortcode.
702
		. '[\/\[]?'         // Shortcodes may begin with [/ or [[
703
		. $tagregexp        // Only match registered shortcodes, because performance.
704
		. '(?:'
705
		.     '[^\[\]<>]+'  // Shortcodes do not contain other shortcodes. Quantifier critical.
706
		. '|'
707
		.     '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >.
708
		. ')*+'             // Possessive critical.
709
		. '\]'              // Find end of shortcode.
710
		. '\]?';            // Shortcodes may end with ]]
711
712
	return $regex;
713
}
714
715
/**
716
 * Replace characters or phrases within HTML elements only.
717
 *
718
 * @since 4.2.3
719
 *
720
 * @param string $haystack The text which has to be formatted.
721
 * @param array $replace_pairs In the form array('from' => 'to', ...).
722
 * @return string The formatted text.
723
 */
724
function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
725
	// Find all elements.
726
	$textarr = wp_html_split( $haystack );
727
	$changed = false;
728
729
	// Optimize when searching for one item.
730
	if ( 1 === count( $replace_pairs ) ) {
731
		// Extract $needle and $replace.
732
		foreach ( $replace_pairs as $needle => $replace );
733
734
		// Loop through delimiters (elements) only.
735 View Code Duplication
		for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
736
			if ( false !== strpos( $textarr[$i], $needle ) ) {
737
				$textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
0 ignored issues
show
Bug introduced by
The variable $needle seems to be defined by a foreach iteration on line 732. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
Bug introduced by
The variable $replace seems to be defined by a foreach iteration on line 732. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
738
				$changed = true;
739
			}
740
		}
741
	} else {
742
		// Extract all $needles.
743
		$needles = array_keys( $replace_pairs );
744
745
		// Loop through delimiters (elements) only.
746 View Code Duplication
		for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
747
			foreach ( $needles as $needle ) {
748
				if ( false !== strpos( $textarr[$i], $needle ) ) {
749
					$textarr[$i] = strtr( $textarr[$i], $replace_pairs );
750
					$changed = true;
751
					// After one strtr() break out of the foreach loop and look at next element.
752
					break;
753
				}
754
			}
755
		}
756
	}
757
758
	if ( $changed ) {
759
		$haystack = implode( $textarr );
760
	}
761
762
	return $haystack;
763
}
764
765
/**
766
 * Newline preservation help function for wpautop
767
 *
768
 * @since 3.1.0
769
 * @access private
770
 *
771
 * @param array $matches preg_replace_callback matches array
772
 * @return string
773
 */
774
function _autop_newline_preservation_helper( $matches ) {
775
	return str_replace( "\n", "<WPPreserveNewline />", $matches[0] );
776
}
777
778
/**
779
 * Don't auto-p wrap shortcodes that stand alone
780
 *
781
 * Ensures that shortcodes are not wrapped in `<p>...</p>`.
782
 *
783
 * @since 2.9.0
784
 *
785
 * @global array $shortcode_tags
786
 *
787
 * @param string $pee The content.
788
 * @return string The filtered content.
789
 */
790
function shortcode_unautop( $pee ) {
791
	global $shortcode_tags;
792
793
	if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
794
		return $pee;
795
	}
796
797
	$tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
798
	$spaces = wp_spaces_regexp();
799
800
	$pattern =
801
		  '/'
802
		. '<p>'                              // Opening paragraph
803
		. '(?:' . $spaces . ')*+'            // Optional leading whitespace
804
		. '('                                // 1: The shortcode
805
		.     '\\['                          // Opening bracket
806
		.     "($tagregexp)"                 // 2: Shortcode name
807
		.     '(?![\\w-])'                   // Not followed by word character or hyphen
808
		                                     // Unroll the loop: Inside the opening shortcode tag
809
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
810
		.     '(?:'
811
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
812
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
813
		.     ')*?'
814
		.     '(?:'
815
		.         '\\/\\]'                   // Self closing tag and closing bracket
816
		.     '|'
817
		.         '\\]'                      // Closing bracket
818
		.         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
819
		.             '[^\\[]*+'             // Not an opening bracket
820
		.             '(?:'
821
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
822
		.                 '[^\\[]*+'         // Not an opening bracket
823
		.             ')*+'
824
		.             '\\[\\/\\2\\]'         // Closing shortcode tag
825
		.         ')?'
826
		.     ')'
827
		. ')'
828
		. '(?:' . $spaces . ')*+'            // optional trailing whitespace
829
		. '<\\/p>'                           // closing paragraph
830
		. '/';
831
832
	return preg_replace( $pattern, '$1', $pee );
833
}
834
835
/**
836
 * Checks to see if a string is utf8 encoded.
837
 *
838
 * NOTE: This function checks for 5-Byte sequences, UTF8
839
 *       has Bytes Sequences with a maximum length of 4.
840
 *
841
 * @author bmorel at ssi dot fr (modified)
842
 * @since 1.2.1
843
 *
844
 * @param string $str The string to be checked
845
 * @return bool True if $str fits a UTF-8 model, false otherwise.
846
 */
847
function seems_utf8( $str ) {
848
	mbstring_binary_safe_encoding();
849
	$length = strlen($str);
850
	reset_mbstring_encoding();
851
	for ($i=0; $i < $length; $i++) {
852
		$c = ord($str[$i]);
853
		if ($c < 0x80) $n = 0; // 0bbbbbbb
854
		elseif (($c & 0xE0) == 0xC0) $n=1; // 110bbbbb
855
		elseif (($c & 0xF0) == 0xE0) $n=2; // 1110bbbb
856
		elseif (($c & 0xF8) == 0xF0) $n=3; // 11110bbb
857
		elseif (($c & 0xFC) == 0xF8) $n=4; // 111110bb
858
		elseif (($c & 0xFE) == 0xFC) $n=5; // 1111110b
859
		else return false; // Does not match any model
860
		for ($j=0; $j<$n; $j++) { // n bytes matching 10bbbbbb follow ?
861
			if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
862
				return false;
863
		}
864
	}
865
	return true;
866
}
867
868
/**
869
 * Converts a number of special characters into their HTML entities.
870
 *
871
 * Specifically deals with: &, <, >, ", and '.
872
 *
873
 * $quote_style can be set to ENT_COMPAT to encode " to
874
 * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
875
 *
876
 * @since 1.2.2
877
 * @access private
878
 *
879
 * @staticvar string $_charset
880
 *
881
 * @param string     $string         The text which is to be encoded.
882
 * @param int|string $quote_style    Optional. Converts double quotes if set to ENT_COMPAT,
883
 *                                   both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES.
884
 *                                   Also compatible with old values; converting single quotes if set to 'single',
885
 *                                   double if set to 'double' or both if otherwise set.
886
 *                                   Default is ENT_NOQUOTES.
887
 * @param string     $charset        Optional. The character encoding of the string. Default is false.
888
 * @param bool       $double_encode  Optional. Whether to encode existing html entities. Default is false.
889
 * @return string The encoded text with HTML entities.
890
 */
891
function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
892
	$string = (string) $string;
893
894
	if ( 0 === strlen( $string ) )
895
		return '';
896
897
	// Don't bother if there are no specialchars - saves some processing
898
	if ( ! preg_match( '/[&<>"\']/', $string ) )
899
		return $string;
900
901
	// Account for the previous behaviour of the function when the $quote_style is not an accepted value
902 View Code Duplication
	if ( empty( $quote_style ) )
903
		$quote_style = ENT_NOQUOTES;
904
	elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
905
		$quote_style = ENT_QUOTES;
906
907
	// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
908
	if ( ! $charset ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $charset of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
909
		static $_charset = null;
910
		if ( ! isset( $_charset ) ) {
911
			$alloptions = wp_load_alloptions();
912
			$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
913
		}
914
		$charset = $_charset;
915
	}
916
917
	if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
918
		$charset = 'UTF-8';
919
920
	$_quote_style = $quote_style;
921
922
	if ( $quote_style === 'double' ) {
923
		$quote_style = ENT_COMPAT;
924
		$_quote_style = ENT_COMPAT;
925
	} elseif ( $quote_style === 'single' ) {
926
		$quote_style = ENT_NOQUOTES;
927
	}
928
929
	if ( ! $double_encode ) {
930
		// Guarantee every &entity; is valid, convert &garbage; into &amp;garbage;
931
		// This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable.
932
		$string = wp_kses_normalize_entities( $string );
933
	}
934
935
	$string = @htmlspecialchars( $string, $quote_style, $charset, $double_encode );
936
937
	// Back-compat.
938
	if ( 'single' === $_quote_style )
939
		$string = str_replace( "'", '&#039;', $string );
940
941
	return $string;
942
}
943
944
/**
945
 * Converts a number of HTML entities into their special characters.
946
 *
947
 * Specifically deals with: &, <, >, ", and '.
948
 *
949
 * $quote_style can be set to ENT_COMPAT to decode " entities,
950
 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
951
 *
952
 * @since 2.8.0
953
 *
954
 * @param string     $string The text which is to be decoded.
955
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
956
 *                                both single and double if set to ENT_QUOTES or
957
 *                                none if set to ENT_NOQUOTES.
958
 *                                Also compatible with old _wp_specialchars() values;
959
 *                                converting single quotes if set to 'single',
960
 *                                double if set to 'double' or both if otherwise set.
961
 *                                Default is ENT_NOQUOTES.
962
 * @return string The decoded text without HTML entities.
963
 */
964
function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
965
	$string = (string) $string;
966
967
	if ( 0 === strlen( $string ) ) {
968
		return '';
969
	}
970
971
	// Don't bother if there are no entities - saves a lot of processing
972
	if ( strpos( $string, '&' ) === false ) {
973
		return $string;
974
	}
975
976
	// Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
977 View Code Duplication
	if ( empty( $quote_style ) ) {
978
		$quote_style = ENT_NOQUOTES;
979
	} elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
980
		$quote_style = ENT_QUOTES;
981
	}
982
983
	// More complete than get_html_translation_table( HTML_SPECIALCHARS )
984
	$single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
985
	$single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
986
	$double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
987
	$double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
988
	$others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
989
	$others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
990
991
	if ( $quote_style === ENT_QUOTES ) {
992
		$translation = array_merge( $single, $double, $others );
993
		$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
994
	} elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
995
		$translation = array_merge( $double, $others );
996
		$translation_preg = array_merge( $double_preg, $others_preg );
997
	} elseif ( $quote_style === 'single' ) {
998
		$translation = array_merge( $single, $others );
999
		$translation_preg = array_merge( $single_preg, $others_preg );
1000
	} elseif ( $quote_style === ENT_NOQUOTES ) {
1001
		$translation = $others;
1002
		$translation_preg = $others_preg;
1003
	}
1004
1005
	// Remove zero padding on numeric entities
1006
	$string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
0 ignored issues
show
Bug introduced by
The variable $translation_preg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1007
1008
	// Replace characters according to translation table
1009
	return strtr( $string, $translation );
0 ignored issues
show
Bug introduced by
The variable $translation does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1010
}
1011
1012
/**
1013
 * Checks for invalid UTF8 in a string.
1014
 *
1015
 * @since 2.8.0
1016
 *
1017
 * @staticvar bool $is_utf8
1018
 * @staticvar bool $utf8_pcre
1019
 *
1020
 * @param string  $string The text which is to be checked.
1021
 * @param bool    $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
1022
 * @return string The checked text.
1023
 */
1024
function wp_check_invalid_utf8( $string, $strip = false ) {
1025
	$string = (string) $string;
1026
1027
	if ( 0 === strlen( $string ) ) {
1028
		return '';
1029
	}
1030
1031
	// Store the site charset as a static to avoid multiple calls to get_option()
1032
	static $is_utf8 = null;
1033
	if ( ! isset( $is_utf8 ) ) {
1034
		$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
1035
	}
1036
	if ( ! $is_utf8 ) {
1037
		return $string;
1038
	}
1039
1040
	// Check for support for utf8 in the installed PCRE library once and store the result in a static
1041
	static $utf8_pcre = null;
1042
	if ( ! isset( $utf8_pcre ) ) {
1043
		$utf8_pcre = @preg_match( '/^./u', 'a' );
1044
	}
1045
	// We can't demand utf8 in the PCRE installation, so just return the string in those cases
1046
	if ( !$utf8_pcre ) {
1047
		return $string;
1048
	}
1049
1050
	// preg_match fails when it encounters invalid UTF8 in $string
1051
	if ( 1 === @preg_match( '/^./us', $string ) ) {
1052
		return $string;
1053
	}
1054
1055
	// Attempt to strip the bad chars if requested (not recommended)
1056
	if ( $strip && function_exists( 'iconv' ) ) {
1057
		return iconv( 'utf-8', 'utf-8', $string );
1058
	}
1059
1060
	return '';
1061
}
1062
1063
/**
1064
 * Encode the Unicode values to be used in the URI.
1065
 *
1066
 * @since 1.5.0
1067
 *
1068
 * @param string $utf8_string
1069
 * @param int    $length Max  length of the string
1070
 * @return string String with Unicode encoded for URI.
1071
 */
1072
function utf8_uri_encode( $utf8_string, $length = 0 ) {
1073
	$unicode = '';
1074
	$values = array();
1075
	$num_octets = 1;
1076
	$unicode_length = 0;
1077
1078
	mbstring_binary_safe_encoding();
1079
	$string_length = strlen( $utf8_string );
1080
	reset_mbstring_encoding();
1081
1082
	for ($i = 0; $i < $string_length; $i++ ) {
1083
1084
		$value = ord( $utf8_string[ $i ] );
1085
1086
		if ( $value < 128 ) {
1087
			if ( $length && ( $unicode_length >= $length ) )
1088
				break;
1089
			$unicode .= chr($value);
1090
			$unicode_length++;
1091
		} else {
1092
			if ( count( $values ) == 0 ) {
1093
				if ( $value < 224 ) {
1094
					$num_octets = 2;
1095
				} elseif ( $value < 240 ) {
1096
					$num_octets = 3;
1097
				} else {
1098
					$num_octets = 4;
1099
				}
1100
			}
1101
1102
			$values[] = $value;
1103
1104
			if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
1105
				break;
1106
			if ( count( $values ) == $num_octets ) {
1107
				for ( $j = 0; $j < $num_octets; $j++ ) {
1108
					$unicode .= '%' . dechex( $values[ $j ] );
1109
				}
1110
1111
				$unicode_length += $num_octets * 3;
1112
1113
				$values = array();
1114
				$num_octets = 1;
1115
			}
1116
		}
1117
	}
1118
1119
	return $unicode;
1120
}
1121
1122
/**
1123
 * Converts all accent characters to ASCII characters.
1124
 *
1125
 * If there are no accent characters, then the string given is just returned.
1126
 *
1127
 * **Accent characters converted:**
1128
 *
1129
 * Currency signs:
1130
 *
1131
 * |   Code   | Glyph | Replacement |     Description     |
1132
 * | -------- | ----- | ----------- | ------------------- |
1133
 * | U+00A3   | £     | (empty)     | British Pound sign  |
1134
 * | U+20AC   | €     | E           | Euro sign           |
1135
 *
1136
 * Decompositions for Latin-1 Supplement:
1137
 *
1138
 * |  Code   | Glyph | Replacement |               Description              |
1139
 * | ------- | ----- | ----------- | -------------------------------------- |
1140
 * | U+00AA  | ª     | a           | Feminine ordinal indicator             |
1141
 * | U+00BA  | º     | o           | Masculine ordinal indicator            |
1142
 * | U+00C0  | À     | A           | Latin capital letter A with grave      |
1143
 * | U+00C1  | Á     | A           | Latin capital letter A with acute      |
1144
 * | U+00C2  | Â     | A           | Latin capital letter A with circumflex |
1145
 * | U+00C3  | Ã     | A           | Latin capital letter A with tilde      |
1146
 * | U+00C4  | Ä     | A           | Latin capital letter A with diaeresis  |
1147
 * | U+00C5  | Å     | A           | Latin capital letter A with ring above |
1148
 * | U+00C6  | Æ     | AE          | Latin capital letter AE                |
1149
 * | U+00C7  | Ç     | C           | Latin capital letter C with cedilla    |
1150
 * | U+00C8  | È     | E           | Latin capital letter E with grave      |
1151
 * | U+00C9  | É     | E           | Latin capital letter E with acute      |
1152
 * | U+00CA  | Ê     | E           | Latin capital letter E with circumflex |
1153
 * | U+00CB  | Ë     | E           | Latin capital letter E with diaeresis  |
1154
 * | U+00CC  | Ì     | I           | Latin capital letter I with grave      |
1155
 * | U+00CD  | Í     | I           | Latin capital letter I with acute      |
1156
 * | U+00CE  | Î     | I           | Latin capital letter I with circumflex |
1157
 * | U+00CF  | Ï     | I           | Latin capital letter I with diaeresis  |
1158
 * | U+00D0  | Ð     | D           | Latin capital letter Eth               |
1159
 * | U+00D1  | Ñ     | N           | Latin capital letter N with tilde      |
1160
 * | U+00D2  | Ò     | O           | Latin capital letter O with grave      |
1161
 * | U+00D3  | Ó     | O           | Latin capital letter O with acute      |
1162
 * | U+00D4  | Ô     | O           | Latin capital letter O with circumflex |
1163
 * | U+00D5  | Õ     | O           | Latin capital letter O with tilde      |
1164
 * | U+00D6  | Ö     | O           | Latin capital letter O with diaeresis  |
1165
 * | U+00D8  | Ø     | O           | Latin capital letter O with stroke     |
1166
 * | U+00D9  | Ù     | U           | Latin capital letter U with grave      |
1167
 * | U+00DA  | Ú     | U           | Latin capital letter U with acute      |
1168
 * | U+00DB  | Û     | U           | Latin capital letter U with circumflex |
1169
 * | U+00DC  | Ü     | U           | Latin capital letter U with diaeresis  |
1170
 * | U+00DD  | Ý     | Y           | Latin capital letter Y with acute      |
1171
 * | U+00DE  | Þ     | TH          | Latin capital letter Thorn             |
1172
 * | U+00DF  | ß     | s           | Latin small letter sharp s             |
1173
 * | U+00E0  | à     | a           | Latin small letter a with grave        |
1174
 * | U+00E1  | á     | a           | Latin small letter a with acute        |
1175
 * | U+00E2  | â     | a           | Latin small letter a with circumflex   |
1176
 * | U+00E3  | ã     | a           | Latin small letter a with tilde        |
1177
 * | U+00E4  | ä     | a           | Latin small letter a with diaeresis    |
1178
 * | U+00E5  | å     | a           | Latin small letter a with ring above   |
1179
 * | U+00E6  | æ     | ae          | Latin small letter ae                  |
1180
 * | U+00E7  | ç     | c           | Latin small letter c with cedilla      |
1181
 * | U+00E8  | è     | e           | Latin small letter e with grave        |
1182
 * | U+00E9  | é     | e           | Latin small letter e with acute        |
1183
 * | U+00EA  | ê     | e           | Latin small letter e with circumflex   |
1184
 * | U+00EB  | ë     | e           | Latin small letter e with diaeresis    |
1185
 * | U+00EC  | ì     | i           | Latin small letter i with grave        |
1186
 * | U+00ED  | í     | i           | Latin small letter i with acute        |
1187
 * | U+00EE  | î     | i           | Latin small letter i with circumflex   |
1188
 * | U+00EF  | ï     | i           | Latin small letter i with diaeresis    |
1189
 * | U+00F0  | ð     | d           | Latin small letter Eth                 |
1190
 * | U+00F1  | ñ     | n           | Latin small letter n with tilde        |
1191
 * | U+00F2  | ò     | o           | Latin small letter o with grave        |
1192
 * | U+00F3  | ó     | o           | Latin small letter o with acute        |
1193
 * | U+00F4  | ô     | o           | Latin small letter o with circumflex   |
1194
 * | U+00F5  | õ     | o           | Latin small letter o with tilde        |
1195
 * | U+00F6  | ö     | o           | Latin small letter o with diaeresis    |
1196
 * | U+00F8  | ø     | o           | Latin small letter o with stroke       |
1197
 * | U+00F9  | ù     | u           | Latin small letter u with grave        |
1198
 * | U+00FA  | ú     | u           | Latin small letter u with acute        |
1199
 * | U+00FB  | û     | u           | Latin small letter u with circumflex   |
1200
 * | U+00FC  | ü     | u           | Latin small letter u with diaeresis    |
1201
 * | U+00FD  | ý     | y           | Latin small letter y with acute        |
1202
 * | U+00FE  | þ     | th          | Latin small letter Thorn               |
1203
 * | U+00FF  | ÿ     | y           | Latin small letter y with diaeresis    |
1204
 *
1205
 * Decompositions for Latin Extended-A:
1206
 *
1207
 * |  Code   | Glyph | Replacement |                    Description                    |
1208
 * | ------- | ----- | ----------- | ------------------------------------------------- |
1209
 * | U+0100  | Ā     | A           | Latin capital letter A with macron                |
1210
 * | U+0101  | ā     | a           | Latin small letter a with macron                  |
1211
 * | U+0102  | Ă     | A           | Latin capital letter A with breve                 |
1212
 * | U+0103  | ă     | a           | Latin small letter a with breve                   |
1213
 * | U+0104  | Ą     | A           | Latin capital letter A with ogonek                |
1214
 * | U+0105  | ą     | a           | Latin small letter a with ogonek                  |
1215
 * | U+01006 | Ć     | C           | Latin capital letter C with acute                 |
1216
 * | U+0107  | ć     | c           | Latin small letter c with acute                   |
1217
 * | U+0108  | Ĉ     | C           | Latin capital letter C with circumflex            |
1218
 * | U+0109  | ĉ     | c           | Latin small letter c with circumflex              |
1219
 * | U+010A  | Ċ     | C           | Latin capital letter C with dot above             |
1220
 * | U+010B  | ċ     | c           | Latin small letter c with dot above               |
1221
 * | U+010C  | Č     | C           | Latin capital letter C with caron                 |
1222
 * | U+010D  | č     | c           | Latin small letter c with caron                   |
1223
 * | U+010E  | Ď     | D           | Latin capital letter D with caron                 |
1224
 * | U+010F  | ď     | d           | Latin small letter d with caron                   |
1225
 * | U+0110  | Đ     | D           | Latin capital letter D with stroke                |
1226
 * | U+0111  | đ     | d           | Latin small letter d with stroke                  |
1227
 * | U+0112  | Ē     | E           | Latin capital letter E with macron                |
1228
 * | U+0113  | ē     | e           | Latin small letter e with macron                  |
1229
 * | U+0114  | Ĕ     | E           | Latin capital letter E with breve                 |
1230
 * | U+0115  | ĕ     | e           | Latin small letter e with breve                   |
1231
 * | U+0116  | Ė     | E           | Latin capital letter E with dot above             |
1232
 * | U+0117  | ė     | e           | Latin small letter e with dot above               |
1233
 * | U+0118  | Ę     | E           | Latin capital letter E with ogonek                |
1234
 * | U+0119  | ę     | e           | Latin small letter e with ogonek                  |
1235
 * | U+011A  | Ě     | E           | Latin capital letter E with caron                 |
1236
 * | U+011B  | ě     | e           | Latin small letter e with caron                   |
1237
 * | U+011C  | Ĝ     | G           | Latin capital letter G with circumflex            |
1238
 * | U+011D  | ĝ     | g           | Latin small letter g with circumflex              |
1239
 * | U+011E  | Ğ     | G           | Latin capital letter G with breve                 |
1240
 * | U+011F  | ğ     | g           | Latin small letter g with breve                   |
1241
 * | U+0120  | Ġ     | G           | Latin capital letter G with dot above             |
1242
 * | U+0121  | ġ     | g           | Latin small letter g with dot above               |
1243
 * | U+0122  | Ģ     | G           | Latin capital letter G with cedilla               |
1244
 * | U+0123  | ģ     | g           | Latin small letter g with cedilla                 |
1245
 * | U+0124  | Ĥ     | H           | Latin capital letter H with circumflex            |
1246
 * | U+0125  | ĥ     | h           | Latin small letter h with circumflex              |
1247
 * | U+0126  | Ħ     | H           | Latin capital letter H with stroke                |
1248
 * | U+0127  | ħ     | h           | Latin small letter h with stroke                  |
1249
 * | U+0128  | Ĩ     | I           | Latin capital letter I with tilde                 |
1250
 * | U+0129  | ĩ     | i           | Latin small letter i with tilde                   |
1251
 * | U+012A  | Ī     | I           | Latin capital letter I with macron                |
1252
 * | U+012B  | ī     | i           | Latin small letter i with macron                  |
1253
 * | U+012C  | Ĭ     | I           | Latin capital letter I with breve                 |
1254
 * | U+012D  | ĭ     | i           | Latin small letter i with breve                   |
1255
 * | U+012E  | Į     | I           | Latin capital letter I with ogonek                |
1256
 * | U+012F  | į     | i           | Latin small letter i with ogonek                  |
1257
 * | U+0130  | İ     | I           | Latin capital letter I with dot above             |
1258
 * | U+0131  | ı     | i           | Latin small letter dotless i                      |
1259
 * | U+0132  | IJ     | IJ          | Latin capital ligature IJ                         |
1260
 * | U+0133  | ij     | ij          | Latin small ligature ij                           |
1261
 * | U+0134  | Ĵ     | J           | Latin capital letter J with circumflex            |
1262
 * | U+0135  | ĵ     | j           | Latin small letter j with circumflex              |
1263
 * | U+0136  | Ķ     | K           | Latin capital letter K with cedilla               |
1264
 * | U+0137  | ķ     | k           | Latin small letter k with cedilla                 |
1265
 * | U+0138  | ĸ     | k           | Latin small letter Kra                            |
1266
 * | U+0139  | Ĺ     | L           | Latin capital letter L with acute                 |
1267
 * | U+013A  | ĺ     | l           | Latin small letter l with acute                   |
1268
 * | U+013B  | Ļ     | L           | Latin capital letter L with cedilla               |
1269
 * | U+013C  | ļ     | l           | Latin small letter l with cedilla                 |
1270
 * | U+013D  | Ľ     | L           | Latin capital letter L with caron                 |
1271
 * | U+013E  | ľ     | l           | Latin small letter l with caron                   |
1272
 * | U+013F  | Ŀ     | L           | Latin capital letter L with middle dot            |
1273
 * | U+0140  | ŀ     | l           | Latin small letter l with middle dot              |
1274
 * | U+0141  | Ł     | L           | Latin capital letter L with stroke                |
1275
 * | U+0142  | ł     | l           | Latin small letter l with stroke                  |
1276
 * | U+0143  | Ń     | N           | Latin capital letter N with acute                 |
1277
 * | U+0144  | ń     | n           | Latin small letter N with acute                   |
1278
 * | U+0145  | Ņ     | N           | Latin capital letter N with cedilla               |
1279
 * | U+0146  | ņ     | n           | Latin small letter n with cedilla                 |
1280
 * | U+0147  | Ň     | N           | Latin capital letter N with caron                 |
1281
 * | U+0148  | ň     | n           | Latin small letter n with caron                   |
1282
 * | U+0149  | ʼn     | n           | Latin small letter n preceded by apostrophe       |
1283
 * | U+014A  | Ŋ     | N           | Latin capital letter Eng                          |
1284
 * | U+014B  | ŋ     | n           | Latin small letter Eng                            |
1285
 * | U+014C  | Ō     | O           | Latin capital letter O with macron                |
1286
 * | U+014D  | ō     | o           | Latin small letter o with macron                  |
1287
 * | U+014E  | Ŏ     | O           | Latin capital letter O with breve                 |
1288
 * | U+014F  | ŏ     | o           | Latin small letter o with breve                   |
1289
 * | U+0150  | Ő     | O           | Latin capital letter O with double acute          |
1290
 * | U+0151  | ő     | o           | Latin small letter o with double acute            |
1291
 * | U+0152  | Œ     | OE          | Latin capital ligature OE                         |
1292
 * | U+0153  | œ     | oe          | Latin small ligature oe                           |
1293
 * | U+0154  | Ŕ     | R           | Latin capital letter R with acute                 |
1294
 * | U+0155  | ŕ     | r           | Latin small letter r with acute                   |
1295
 * | U+0156  | Ŗ     | R           | Latin capital letter R with cedilla               |
1296
 * | U+0157  | ŗ     | r           | Latin small letter r with cedilla                 |
1297
 * | U+0158  | Ř     | R           | Latin capital letter R with caron                 |
1298
 * | U+0159  | ř     | r           | Latin small letter r with caron                   |
1299
 * | U+015A  | Ś     | S           | Latin capital letter S with acute                 |
1300
 * | U+015B  | ś     | s           | Latin small letter s with acute                   |
1301
 * | U+015C  | Ŝ     | S           | Latin capital letter S with circumflex            |
1302
 * | U+015D  | ŝ     | s           | Latin small letter s with circumflex              |
1303
 * | U+015E  | Ş     | S           | Latin capital letter S with cedilla               |
1304
 * | U+015F  | ş     | s           | Latin small letter s with cedilla                 |
1305
 * | U+0160  | Š     | S           | Latin capital letter S with caron                 |
1306
 * | U+0161  | š     | s           | Latin small letter s with caron                   |
1307
 * | U+0162  | Ţ     | T           | Latin capital letter T with cedilla               |
1308
 * | U+0163  | ţ     | t           | Latin small letter t with cedilla                 |
1309
 * | U+0164  | Ť     | T           | Latin capital letter T with caron                 |
1310
 * | U+0165  | ť     | t           | Latin small letter t with caron                   |
1311
 * | U+0166  | Ŧ     | T           | Latin capital letter T with stroke                |
1312
 * | U+0167  | ŧ     | t           | Latin small letter t with stroke                  |
1313
 * | U+0168  | Ũ     | U           | Latin capital letter U with tilde                 |
1314
 * | U+0169  | ũ     | u           | Latin small letter u with tilde                   |
1315
 * | U+016A  | Ū     | U           | Latin capital letter U with macron                |
1316
 * | U+016B  | ū     | u           | Latin small letter u with macron                  |
1317
 * | U+016C  | Ŭ     | U           | Latin capital letter U with breve                 |
1318
 * | U+016D  | ŭ     | u           | Latin small letter u with breve                   |
1319
 * | U+016E  | Ů     | U           | Latin capital letter U with ring above            |
1320
 * | U+016F  | ů     | u           | Latin small letter u with ring above              |
1321
 * | U+0170  | Ű     | U           | Latin capital letter U with double acute          |
1322
 * | U+0171  | ű     | u           | Latin small letter u with double acute            |
1323
 * | U+0172  | Ų     | U           | Latin capital letter U with ogonek                |
1324
 * | U+0173  | ų     | u           | Latin small letter u with ogonek                  |
1325
 * | U+0174  | Ŵ     | W           | Latin capital letter W with circumflex            |
1326
 * | U+0175  | ŵ     | w           | Latin small letter w with circumflex              |
1327
 * | U+0176  | Ŷ     | Y           | Latin capital letter Y with circumflex            |
1328
 * | U+0177  | ŷ     | y           | Latin small letter y with circumflex              |
1329
 * | U+0178  | Ÿ     | Y           | Latin capital letter Y with diaeresis             |
1330
 * | U+0179  | Ź     | Z           | Latin capital letter Z with acute                 |
1331
 * | U+017A  | ź     | z           | Latin small letter z with acute                   |
1332
 * | U+017B  | Ż     | Z           | Latin capital letter Z with dot above             |
1333
 * | U+017C  | ż     | z           | Latin small letter z with dot above               |
1334
 * | U+017D  | Ž     | Z           | Latin capital letter Z with caron                 |
1335
 * | U+017E  | ž     | z           | Latin small letter z with caron                   |
1336
 * | U+017F  | ſ     | s           | Latin small letter long s                         |
1337
 * | U+01A0  | Ơ     | O           | Latin capital letter O with horn                  |
1338
 * | U+01A1  | ơ     | o           | Latin small letter o with horn                    |
1339
 * | U+01AF  | Ư     | U           | Latin capital letter U with horn                  |
1340
 * | U+01B0  | ư     | u           | Latin small letter u with horn                    |
1341
 * | U+01CD  | Ǎ     | A           | Latin capital letter A with caron                 |
1342
 * | U+01CE  | ǎ     | a           | Latin small letter a with caron                   |
1343
 * | U+01CF  | Ǐ     | I           | Latin capital letter I with caron                 |
1344
 * | U+01D0  | ǐ     | i           | Latin small letter i with caron                   |
1345
 * | U+01D1  | Ǒ     | O           | Latin capital letter O with caron                 |
1346
 * | U+01D2  | ǒ     | o           | Latin small letter o with caron                   |
1347
 * | U+01D3  | Ǔ     | U           | Latin capital letter U with caron                 |
1348
 * | U+01D4  | ǔ     | u           | Latin small letter u with caron                   |
1349
 * | U+01D5  | Ǖ     | U           | Latin capital letter U with diaeresis and macron  |
1350
 * | U+01D6  | ǖ     | u           | Latin small letter u with diaeresis and macron    |
1351
 * | U+01D7  | Ǘ     | U           | Latin capital letter U with diaeresis and acute   |
1352
 * | U+01D8  | ǘ     | u           | Latin small letter u with diaeresis and acute     |
1353
 * | U+01D9  | Ǚ     | U           | Latin capital letter U with diaeresis and caron   |
1354
 * | U+01DA  | ǚ     | u           | Latin small letter u with diaeresis and caron     |
1355
 * | U+01DB  | Ǜ     | U           | Latin capital letter U with diaeresis and grave   |
1356
 * | U+01DC  | ǜ     | u           | Latin small letter u with diaeresis and grave     |
1357
 *
1358
 * Decompositions for Latin Extended-B:
1359
 *
1360
 * |   Code   | Glyph | Replacement |                Description                |
1361
 * | -------- | ----- | ----------- | ----------------------------------------- |
1362
 * | U+0218   | Ș     | S           | Latin capital letter S with comma below   |
1363
 * | U+0219   | ș     | s           | Latin small letter s with comma below     |
1364
 * | U+021A   | Ț     | T           | Latin capital letter T with comma below   |
1365
 * | U+021B   | ț     | t           | Latin small letter t with comma below     |
1366
 *
1367
 * Vowels with diacritic (Chinese, Hanyu Pinyin):
1368
 *
1369
 * |   Code   | Glyph | Replacement |                      Description                      |
1370
 * | -------- | ----- | ----------- | ----------------------------------------------------- |
1371
 * | U+0251   | ɑ     | a           | Latin small letter alpha                              |
1372
 * | U+1EA0   | Ạ     | A           | Latin capital letter A with dot below                 |
1373
 * | U+1EA1   | ạ     | a           | Latin small letter a with dot below                   |
1374
 * | U+1EA2   | Ả     | A           | Latin capital letter A with hook above                |
1375
 * | U+1EA3   | ả     | a           | Latin small letter a with hook above                  |
1376
 * | U+1EA4   | Ấ     | A           | Latin capital letter A with circumflex and acute      |
1377
 * | U+1EA5   | ấ     | a           | Latin small letter a with circumflex and acute        |
1378
 * | U+1EA6   | Ầ     | A           | Latin capital letter A with circumflex and grave      |
1379
 * | U+1EA7   | ầ     | a           | Latin small letter a with circumflex and grave        |
1380
 * | U+1EA8   | Ẩ     | A           | Latin capital letter A with circumflex and hook above |
1381
 * | U+1EA9   | ẩ     | a           | Latin small letter a with circumflex and hook above   |
1382
 * | U+1EAA   | Ẫ     | A           | Latin capital letter A with circumflex and tilde      |
1383
 * | U+1EAB   | ẫ     | a           | Latin small letter a with circumflex and tilde        |
1384
 * | U+1EA6   | Ậ     | A           | Latin capital letter A with circumflex and dot below  |
1385
 * | U+1EAD   | ậ     | a           | Latin small letter a with circumflex and dot below    |
1386
 * | U+1EAE   | Ắ     | A           | Latin capital letter A with breve and acute           |
1387
 * | U+1EAF   | ắ     | a           | Latin small letter a with breve and acute             |
1388
 * | U+1EB0   | Ằ     | A           | Latin capital letter A with breve and grave           |
1389
 * | U+1EB1   | ằ     | a           | Latin small letter a with breve and grave             |
1390
 * | U+1EB2   | Ẳ     | A           | Latin capital letter A with breve and hook above      |
1391
 * | U+1EB3   | ẳ     | a           | Latin small letter a with breve and hook above        |
1392
 * | U+1EB4   | Ẵ     | A           | Latin capital letter A with breve and tilde           |
1393
 * | U+1EB5   | ẵ     | a           | Latin small letter a with breve and tilde             |
1394
 * | U+1EB6   | Ặ     | A           | Latin capital letter A with breve and dot below       |
1395
 * | U+1EB7   | ặ     | a           | Latin small letter a with breve and dot below         |
1396
 * | U+1EB8   | Ẹ     | E           | Latin capital letter E with dot below                 |
1397
 * | U+1EB9   | ẹ     | e           | Latin small letter e with dot below                   |
1398
 * | U+1EBA   | Ẻ     | E           | Latin capital letter E with hook above                |
1399
 * | U+1EBB   | ẻ     | e           | Latin small letter e with hook above                  |
1400
 * | U+1EBC   | Ẽ     | E           | Latin capital letter E with tilde                     |
1401
 * | U+1EBD   | ẽ     | e           | Latin small letter e with tilde                       |
1402
 * | U+1EBE   | Ế     | E           | Latin capital letter E with circumflex and acute      |
1403
 * | U+1EBF   | ế     | e           | Latin small letter e with circumflex and acute        |
1404
 * | U+1EC0   | Ề     | E           | Latin capital letter E with circumflex and grave      |
1405
 * | U+1EC1   | ề     | e           | Latin small letter e with circumflex and grave        |
1406
 * | U+1EC2   | Ể     | E           | Latin capital letter E with circumflex and hook above |
1407
 * | U+1EC3   | ể     | e           | Latin small letter e with circumflex and hook above   |
1408
 * | U+1EC4   | Ễ     | E           | Latin capital letter E with circumflex and tilde      |
1409
 * | U+1EC5   | ễ     | e           | Latin small letter e with circumflex and tilde        |
1410
 * | U+1EC6   | Ệ     | E           | Latin capital letter E with circumflex and dot below  |
1411
 * | U+1EC7   | ệ     | e           | Latin small letter e with circumflex and dot below    |
1412
 * | U+1EC8   | Ỉ     | I           | Latin capital letter I with hook above                |
1413
 * | U+1EC9   | ỉ     | i           | Latin small letter i with hook above                  |
1414
 * | U+1ECA   | Ị     | I           | Latin capital letter I with dot below                 |
1415
 * | U+1ECB   | ị     | i           | Latin small letter i with dot below                   |
1416
 * | U+1ECC   | Ọ     | O           | Latin capital letter O with dot below                 |
1417
 * | U+1ECD   | ọ     | o           | Latin small letter o with dot below                   |
1418
 * | U+1ECE   | Ỏ     | O           | Latin capital letter O with hook above                |
1419
 * | U+1ECF   | ỏ     | o           | Latin small letter o with hook above                  |
1420
 * | U+1ED0   | Ố     | O           | Latin capital letter O with circumflex and acute      |
1421
 * | U+1ED1   | ố     | o           | Latin small letter o with circumflex and acute        |
1422
 * | U+1ED2   | Ồ     | O           | Latin capital letter O with circumflex and grave      |
1423
 * | U+1ED3   | ồ     | o           | Latin small letter o with circumflex and grave        |
1424
 * | U+1ED4   | Ổ     | O           | Latin capital letter O with circumflex and hook above |
1425
 * | U+1ED5   | ổ     | o           | Latin small letter o with circumflex and hook above   |
1426
 * | U+1ED6   | Ỗ     | O           | Latin capital letter O with circumflex and tilde      |
1427
 * | U+1ED7   | ỗ     | o           | Latin small letter o with circumflex and tilde        |
1428
 * | U+1ED8   | Ộ     | O           | Latin capital letter O with circumflex and dot below  |
1429
 * | U+1ED9   | ộ     | o           | Latin small letter o with circumflex and dot below    |
1430
 * | U+1EDA   | Ớ     | O           | Latin capital letter O with horn and acute            |
1431
 * | U+1EDB   | ớ     | o           | Latin small letter o with horn and acute              |
1432
 * | U+1EDC   | Ờ     | O           | Latin capital letter O with horn and grave            |
1433
 * | U+1EDD   | ờ     | o           | Latin small letter o with horn and grave              |
1434
 * | U+1EDE   | Ở     | O           | Latin capital letter O with horn and hook above       |
1435
 * | U+1EDF   | ở     | o           | Latin small letter o with horn and hook above         |
1436
 * | U+1EE0   | Ỡ     | O           | Latin capital letter O with horn and tilde            |
1437
 * | U+1EE1   | ỡ     | o           | Latin small letter o with horn and tilde              |
1438
 * | U+1EE2   | Ợ     | O           | Latin capital letter O with horn and dot below        |
1439
 * | U+1EE3   | ợ     | o           | Latin small letter o with horn and dot below          |
1440
 * | U+1EE4   | Ụ     | U           | Latin capital letter U with dot below                 |
1441
 * | U+1EE5   | ụ     | u           | Latin small letter u with dot below                   |
1442
 * | U+1EE6   | Ủ     | U           | Latin capital letter U with hook above                |
1443
 * | U+1EE7   | ủ     | u           | Latin small letter u with hook above                  |
1444
 * | U+1EE8   | Ứ     | U           | Latin capital letter U with horn and acute            |
1445
 * | U+1EE9   | ứ     | u           | Latin small letter u with horn and acute              |
1446
 * | U+1EEA   | Ừ     | U           | Latin capital letter U with horn and grave            |
1447
 * | U+1EEB   | ừ     | u           | Latin small letter u with horn and grave              |
1448
 * | U+1EEC   | Ử     | U           | Latin capital letter U with horn and hook above       |
1449
 * | U+1EED   | ử     | u           | Latin small letter u with horn and hook above         |
1450
 * | U+1EEE   | Ữ     | U           | Latin capital letter U with horn and tilde            |
1451
 * | U+1EEF   | ữ     | u           | Latin small letter u with horn and tilde              |
1452
 * | U+1EF0   | Ự     | U           | Latin capital letter U with horn and dot below        |
1453
 * | U+1EF1   | ự     | u           | Latin small letter u with horn and dot below          |
1454
 * | U+1EF2   | Ỳ     | Y           | Latin capital letter Y with grave                     |
1455
 * | U+1EF3   | ỳ     | y           | Latin small letter y with grave                       |
1456
 * | U+1EF4   | Ỵ     | Y           | Latin capital letter Y with dot below                 |
1457
 * | U+1EF5   | ỵ     | y           | Latin small letter y with dot below                   |
1458
 * | U+1EF6   | Ỷ     | Y           | Latin capital letter Y with hook above                |
1459
 * | U+1EF7   | ỷ     | y           | Latin small letter y with hook above                  |
1460
 * | U+1EF8   | Ỹ     | Y           | Latin capital letter Y with tilde                     |
1461
 * | U+1EF9   | ỹ     | y           | Latin small letter y with tilde                       |
1462
 *
1463
 * German (`de_DE`), German formal (`de_DE_formal`), German (Switzerland) formal (`de_CH`),
1464
 * and German (Switzerland) informal (`de_CH_informal`) locales:
1465
 *
1466
 * |   Code   | Glyph | Replacement |               Description               |
1467
 * | -------- | ----- | ----------- | --------------------------------------- |
1468
 * | U+00C4   | Ä     | Ae          | Latin capital letter A with diaeresis   |
1469
 * | U+00E4   | ä     | ae          | Latin small letter a with diaeresis     |
1470
 * | U+00D6   | Ö     | Oe          | Latin capital letter O with diaeresis   |
1471
 * | U+00F6   | ö     | oe          | Latin small letter o with diaeresis     |
1472
 * | U+00DC   | Ü     | Ue          | Latin capital letter U with diaeresis   |
1473
 * | U+00FC   | ü     | ue          | Latin small letter u with diaeresis     |
1474
 * | U+00DF   | ß     | ss          | Latin small letter sharp s              |
1475
 *
1476
 * Danish (`da_DK`) locale:
1477
 *
1478
 * |   Code   | Glyph | Replacement |               Description               |
1479
 * | -------- | ----- | ----------- | --------------------------------------- |
1480
 * | U+00C6   | Æ     | Ae          | Latin capital letter AE                 |
1481
 * | U+00E6   | æ     | ae          | Latin small letter ae                   |
1482
 * | U+00D8   | Ø     | Oe          | Latin capital letter O with stroke      |
1483
 * | U+00F8   | ø     | oe          | Latin small letter o with stroke        |
1484
 * | U+00C5   | Å     | Aa          | Latin capital letter A with ring above  |
1485
 * | U+00E5   | å     | aa          | Latin small letter a with ring above    |
1486
 *
1487
 * Catalan (`ca`) locale:
1488
 *
1489
 * |   Code   | Glyph | Replacement |               Description               |
1490
 * | -------- | ----- | ----------- | --------------------------------------- |
1491
 * | U+00B7   | l·l   | ll          | Flown dot (between two Ls)              |
1492
 *
1493
 * @since 1.2.1
1494
 * @since 4.6.0 Added locale support for `de_CH`, `de_CH_informal`, and `ca`.
1495
 *
1496
 * @param string $string Text that might have accent characters
1497
 * @return string Filtered string with replaced "nice" characters.
1498
 */
1499
function remove_accents( $string ) {
1500
	if ( !preg_match('/[\x80-\xff]/', $string) )
1501
		return $string;
1502
1503
	if (seems_utf8($string)) {
1504
		$chars = array(
1505
		// Decompositions for Latin-1 Supplement
1506
		chr(194).chr(170) => 'a', chr(194).chr(186) => 'o',
1507
		chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
1508
		chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
1509
		chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
1510
		chr(195).chr(134) => 'AE',chr(195).chr(135) => 'C',
1511
		chr(195).chr(136) => 'E', chr(195).chr(137) => 'E',
1512
		chr(195).chr(138) => 'E', chr(195).chr(139) => 'E',
1513
		chr(195).chr(140) => 'I', chr(195).chr(141) => 'I',
1514
		chr(195).chr(142) => 'I', chr(195).chr(143) => 'I',
1515
		chr(195).chr(144) => 'D', chr(195).chr(145) => 'N',
1516
		chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
1517
		chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
1518
		chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
1519
		chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
1520
		chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
1521
		chr(195).chr(158) => 'TH',chr(195).chr(159) => 's',
1522
		chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
1523
		chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
1524
		chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
1525
		chr(195).chr(166) => 'ae',chr(195).chr(167) => 'c',
1526
		chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
1527
		chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
1528
		chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
1529
		chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
1530
		chr(195).chr(176) => 'd', chr(195).chr(177) => 'n',
1531
		chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
1532
		chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
1533
		chr(195).chr(182) => 'o', chr(195).chr(184) => 'o',
1534
		chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
1535
		chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
1536
		chr(195).chr(189) => 'y', chr(195).chr(190) => 'th',
1537
		chr(195).chr(191) => 'y', chr(195).chr(152) => 'O',
1538
		// Decompositions for Latin Extended-A
1539
		chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
1540
		chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
1541
		chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
1542
		chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
1543
		chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
1544
		chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
1545
		chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
1546
		chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
1547
		chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
1548
		chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
1549
		chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
1550
		chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
1551
		chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
1552
		chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
1553
		chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
1554
		chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
1555
		chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
1556
		chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
1557
		chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
1558
		chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
1559
		chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
1560
		chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
1561
		chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
1562
		chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
1563
		chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
1564
		chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
1565
		chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
1566
		chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
1567
		chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
1568
		chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
1569
		chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
1570
		chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
1571
		chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
1572
		chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
1573
		chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
1574
		chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
1575
		chr(197).chr(136) => 'n', chr(197).chr(137) => 'n',
1576
		chr(197).chr(138) => 'N', chr(197).chr(139) => 'n',
1577
		chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
1578
		chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
1579
		chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
1580
		chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
1581
		chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
1582
		chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
1583
		chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
1584
		chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
1585
		chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
1586
		chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
1587
		chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
1588
		chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
1589
		chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
1590
		chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
1591
		chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
1592
		chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
1593
		chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
1594
		chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
1595
		chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
1596
		chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
1597
		chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
1598
		chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
1599
		chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
1600
		chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
1601
		chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
1602
		chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
1603
		// Decompositions for Latin Extended-B
1604
		chr(200).chr(152) => 'S', chr(200).chr(153) => 's',
1605
		chr(200).chr(154) => 'T', chr(200).chr(155) => 't',
1606
		// Euro Sign
1607
		chr(226).chr(130).chr(172) => 'E',
1608
		// GBP (Pound) Sign
1609
		chr(194).chr(163) => '',
1610
		// Vowels with diacritic (Vietnamese)
1611
		// unmarked
1612
		chr(198).chr(160) => 'O', chr(198).chr(161) => 'o',
1613
		chr(198).chr(175) => 'U', chr(198).chr(176) => 'u',
1614
		// grave accent
1615
		chr(225).chr(186).chr(166) => 'A', chr(225).chr(186).chr(167) => 'a',
1616
		chr(225).chr(186).chr(176) => 'A', chr(225).chr(186).chr(177) => 'a',
1617
		chr(225).chr(187).chr(128) => 'E', chr(225).chr(187).chr(129) => 'e',
1618
		chr(225).chr(187).chr(146) => 'O', chr(225).chr(187).chr(147) => 'o',
1619
		chr(225).chr(187).chr(156) => 'O', chr(225).chr(187).chr(157) => 'o',
1620
		chr(225).chr(187).chr(170) => 'U', chr(225).chr(187).chr(171) => 'u',
1621
		chr(225).chr(187).chr(178) => 'Y', chr(225).chr(187).chr(179) => 'y',
1622
		// hook
1623
		chr(225).chr(186).chr(162) => 'A', chr(225).chr(186).chr(163) => 'a',
1624
		chr(225).chr(186).chr(168) => 'A', chr(225).chr(186).chr(169) => 'a',
1625
		chr(225).chr(186).chr(178) => 'A', chr(225).chr(186).chr(179) => 'a',
1626
		chr(225).chr(186).chr(186) => 'E', chr(225).chr(186).chr(187) => 'e',
1627
		chr(225).chr(187).chr(130) => 'E', chr(225).chr(187).chr(131) => 'e',
1628
		chr(225).chr(187).chr(136) => 'I', chr(225).chr(187).chr(137) => 'i',
1629
		chr(225).chr(187).chr(142) => 'O', chr(225).chr(187).chr(143) => 'o',
1630
		chr(225).chr(187).chr(148) => 'O', chr(225).chr(187).chr(149) => 'o',
1631
		chr(225).chr(187).chr(158) => 'O', chr(225).chr(187).chr(159) => 'o',
1632
		chr(225).chr(187).chr(166) => 'U', chr(225).chr(187).chr(167) => 'u',
1633
		chr(225).chr(187).chr(172) => 'U', chr(225).chr(187).chr(173) => 'u',
1634
		chr(225).chr(187).chr(182) => 'Y', chr(225).chr(187).chr(183) => 'y',
1635
		// tilde
1636
		chr(225).chr(186).chr(170) => 'A', chr(225).chr(186).chr(171) => 'a',
1637
		chr(225).chr(186).chr(180) => 'A', chr(225).chr(186).chr(181) => 'a',
1638
		chr(225).chr(186).chr(188) => 'E', chr(225).chr(186).chr(189) => 'e',
1639
		chr(225).chr(187).chr(132) => 'E', chr(225).chr(187).chr(133) => 'e',
1640
		chr(225).chr(187).chr(150) => 'O', chr(225).chr(187).chr(151) => 'o',
1641
		chr(225).chr(187).chr(160) => 'O', chr(225).chr(187).chr(161) => 'o',
1642
		chr(225).chr(187).chr(174) => 'U', chr(225).chr(187).chr(175) => 'u',
1643
		chr(225).chr(187).chr(184) => 'Y', chr(225).chr(187).chr(185) => 'y',
1644
		// acute accent
1645
		chr(225).chr(186).chr(164) => 'A', chr(225).chr(186).chr(165) => 'a',
1646
		chr(225).chr(186).chr(174) => 'A', chr(225).chr(186).chr(175) => 'a',
1647
		chr(225).chr(186).chr(190) => 'E', chr(225).chr(186).chr(191) => 'e',
1648
		chr(225).chr(187).chr(144) => 'O', chr(225).chr(187).chr(145) => 'o',
1649
		chr(225).chr(187).chr(154) => 'O', chr(225).chr(187).chr(155) => 'o',
1650
		chr(225).chr(187).chr(168) => 'U', chr(225).chr(187).chr(169) => 'u',
1651
		// dot below
1652
		chr(225).chr(186).chr(160) => 'A', chr(225).chr(186).chr(161) => 'a',
1653
		chr(225).chr(186).chr(172) => 'A', chr(225).chr(186).chr(173) => 'a',
1654
		chr(225).chr(186).chr(182) => 'A', chr(225).chr(186).chr(183) => 'a',
1655
		chr(225).chr(186).chr(184) => 'E', chr(225).chr(186).chr(185) => 'e',
1656
		chr(225).chr(187).chr(134) => 'E', chr(225).chr(187).chr(135) => 'e',
1657
		chr(225).chr(187).chr(138) => 'I', chr(225).chr(187).chr(139) => 'i',
1658
		chr(225).chr(187).chr(140) => 'O', chr(225).chr(187).chr(141) => 'o',
1659
		chr(225).chr(187).chr(152) => 'O', chr(225).chr(187).chr(153) => 'o',
1660
		chr(225).chr(187).chr(162) => 'O', chr(225).chr(187).chr(163) => 'o',
1661
		chr(225).chr(187).chr(164) => 'U', chr(225).chr(187).chr(165) => 'u',
1662
		chr(225).chr(187).chr(176) => 'U', chr(225).chr(187).chr(177) => 'u',
1663
		chr(225).chr(187).chr(180) => 'Y', chr(225).chr(187).chr(181) => 'y',
1664
		// Vowels with diacritic (Chinese, Hanyu Pinyin)
1665
		chr(201).chr(145) => 'a',
1666
		// macron
1667
		chr(199).chr(149) => 'U', chr(199).chr(150) => 'u',
1668
		// acute accent
1669
		chr(199).chr(151) => 'U', chr(199).chr(152) => 'u',
1670
		// caron
1671
		chr(199).chr(141) => 'A', chr(199).chr(142) => 'a',
1672
		chr(199).chr(143) => 'I', chr(199).chr(144) => 'i',
1673
		chr(199).chr(145) => 'O', chr(199).chr(146) => 'o',
1674
		chr(199).chr(147) => 'U', chr(199).chr(148) => 'u',
1675
		chr(199).chr(153) => 'U', chr(199).chr(154) => 'u',
1676
		// grave accent
1677
		chr(199).chr(155) => 'U', chr(199).chr(156) => 'u',
1678
		);
1679
1680
		// Used for locale-specific rules
1681
		$locale = get_locale();
1682
1683
		if ( 'de_DE' == $locale || 'de_DE_formal' == $locale || 'de_CH' == $locale || 'de_CH_informal' == $locale ) {
1684
			$chars[ chr(195).chr(132) ] = 'Ae';
1685
			$chars[ chr(195).chr(164) ] = 'ae';
1686
			$chars[ chr(195).chr(150) ] = 'Oe';
1687
			$chars[ chr(195).chr(182) ] = 'oe';
1688
			$chars[ chr(195).chr(156) ] = 'Ue';
1689
			$chars[ chr(195).chr(188) ] = 'ue';
1690
			$chars[ chr(195).chr(159) ] = 'ss';
1691
		} elseif ( 'da_DK' === $locale ) {
1692
			$chars[ chr(195).chr(134) ] = 'Ae';
1693
 			$chars[ chr(195).chr(166) ] = 'ae';
1694
			$chars[ chr(195).chr(152) ] = 'Oe';
1695
			$chars[ chr(195).chr(184) ] = 'oe';
1696
			$chars[ chr(195).chr(133) ] = 'Aa';
1697
			$chars[ chr(195).chr(165) ] = 'aa';
1698
		} elseif ( 'ca' === $locale ) {
1699
			$chars[ chr(108).chr(194).chr(183).chr(108) ] = 'll';
1700
		}
1701
1702
		$string = strtr($string, $chars);
1703
	} else {
1704
		$chars = array();
1705
		// Assume ISO-8859-1 if not UTF-8
1706
		$chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
1707
			.chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
1708
			.chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
1709
			.chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
1710
			.chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
1711
			.chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
1712
			.chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
1713
			.chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
1714
			.chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
1715
			.chr(252).chr(253).chr(255);
1716
1717
		$chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
1718
1719
		$string = strtr($string, $chars['in'], $chars['out']);
1720
		$double_chars = array();
1721
		$double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
1722
		$double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
1723
		$string = str_replace($double_chars['in'], $double_chars['out'], $string);
1724
	}
1725
1726
	return $string;
1727
}
1728
1729
/**
1730
 * Sanitizes a filename, replacing whitespace with dashes.
1731
 *
1732
 * Removes special characters that are illegal in filenames on certain
1733
 * operating systems and special characters requiring special escaping
1734
 * to manipulate at the command line. Replaces spaces and consecutive
1735
 * dashes with a single dash. Trims period, dash and underscore from beginning
1736
 * and end of filename. It is not guaranteed that this function will return a
1737
 * filename that is allowed to be uploaded.
1738
 *
1739
 * @since 2.1.0
1740
 *
1741
 * @param string $filename The filename to be sanitized
1742
 * @return string The sanitized filename
1743
 */
1744
function sanitize_file_name( $filename ) {
1745
	$filename_raw = $filename;
1746
	$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
1747
	/**
1748
	 * Filters the list of characters to remove from a filename.
1749
	 *
1750
	 * @since 2.8.0
1751
	 *
1752
	 * @param array  $special_chars Characters to remove.
1753
	 * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
1754
	 */
1755
	$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
1756
	$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
1757
	$filename = str_replace( $special_chars, '', $filename );
1758
	$filename = str_replace( array( '%20', '+' ), '-', $filename );
1759
	$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
1760
	$filename = trim( $filename, '.-_' );
1761
1762
	if ( false === strpos( $filename, '.' ) ) {
1763
		$mime_types = wp_get_mime_types();
1764
		$filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
1765
		if ( $filetype['ext'] === $filename ) {
1766
			$filename = 'unnamed-file.' . $filetype['ext'];
1767
		}
1768
	}
1769
1770
	// Split the filename into a base and extension[s]
1771
	$parts = explode('.', $filename);
1772
1773
	// Return if only one extension
1774
	if ( count( $parts ) <= 2 ) {
1775
		/**
1776
		 * Filters a sanitized filename string.
1777
		 *
1778
		 * @since 2.8.0
1779
		 *
1780
		 * @param string $filename     Sanitized filename.
1781
		 * @param string $filename_raw The filename prior to sanitization.
1782
		 */
1783
		return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
1784
	}
1785
1786
	// Process multiple extensions
1787
	$filename = array_shift($parts);
1788
	$extension = array_pop($parts);
1789
	$mimes = get_allowed_mime_types();
1790
1791
	/*
1792
	 * Loop over any intermediate extensions. Postfix them with a trailing underscore
1793
	 * if they are a 2 - 5 character long alpha string not in the extension whitelist.
1794
	 */
1795
	foreach ( (array) $parts as $part) {
1796
		$filename .= '.' . $part;
1797
1798
		if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
1799
			$allowed = false;
1800 View Code Duplication
			foreach ( $mimes as $ext_preg => $mime_match ) {
1801
				$ext_preg = '!^(' . $ext_preg . ')$!i';
1802
				if ( preg_match( $ext_preg, $part ) ) {
1803
					$allowed = true;
1804
					break;
1805
				}
1806
			}
1807
			if ( !$allowed )
1808
				$filename .= '_';
1809
		}
1810
	}
1811
	$filename .= '.' . $extension;
1812
	/** This filter is documented in wp-includes/formatting.php */
1813
	return apply_filters('sanitize_file_name', $filename, $filename_raw);
1814
}
1815
1816
/**
1817
 * Sanitizes a username, stripping out unsafe characters.
1818
 *
1819
 * Removes tags, octets, entities, and if strict is enabled, will only keep
1820
 * alphanumeric, _, space, ., -, @. After sanitizing, it passes the username,
1821
 * raw username (the username in the parameter), and the value of $strict as
1822
 * parameters for the {@see 'sanitize_user'} filter.
1823
 *
1824
 * @since 2.0.0
1825
 *
1826
 * @param string $username The username to be sanitized.
1827
 * @param bool   $strict   If set limits $username to specific characters. Default false.
1828
 * @return string The sanitized username, after passing through filters.
1829
 */
1830
function sanitize_user( $username, $strict = false ) {
1831
	$raw_username = $username;
1832
	$username = wp_strip_all_tags( $username );
1833
	$username = remove_accents( $username );
1834
	// Kill octets
1835
	$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
1836
	$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
1837
1838
	// If strict, reduce to ASCII for max portability.
1839
	if ( $strict )
1840
		$username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
1841
1842
	$username = trim( $username );
1843
	// Consolidate contiguous whitespace
1844
	$username = preg_replace( '|\s+|', ' ', $username );
1845
1846
	/**
1847
	 * Filters a sanitized username string.
1848
	 *
1849
	 * @since 2.0.1
1850
	 *
1851
	 * @param string $username     Sanitized username.
1852
	 * @param string $raw_username The username prior to sanitization.
1853
	 * @param bool   $strict       Whether to limit the sanitization to specific characters. Default false.
1854
	 */
1855
	return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
1856
}
1857
1858
/**
1859
 * Sanitizes a string key.
1860
 *
1861
 * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
1862
 *
1863
 * @since 3.0.0
1864
 *
1865
 * @param string $key String key
1866
 * @return string Sanitized key
1867
 */
1868
function sanitize_key( $key ) {
1869
	$raw_key = $key;
1870
	$key = strtolower( $key );
1871
	$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
1872
1873
	/**
1874
	 * Filters a sanitized key string.
1875
	 *
1876
	 * @since 3.0.0
1877
	 *
1878
	 * @param string $key     Sanitized key.
1879
	 * @param string $raw_key The key prior to sanitization.
1880
	 */
1881
	return apply_filters( 'sanitize_key', $key, $raw_key );
1882
}
1883
1884
/**
1885
 * Sanitizes a title, or returns a fallback title.
1886
 *
1887
 * Specifically, HTML and PHP tags are stripped. Further actions can be added
1888
 * via the plugin API. If $title is empty and $fallback_title is set, the latter
1889
 * will be used.
1890
 *
1891
 * @since 1.0.0
1892
 *
1893
 * @param string $title          The string to be sanitized.
1894
 * @param string $fallback_title Optional. A title to use if $title is empty.
1895
 * @param string $context        Optional. The operation for which the string is sanitized
1896
 * @return string The sanitized string.
1897
 */
1898
function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
1899
	$raw_title = $title;
1900
1901
	if ( 'save' == $context )
1902
		$title = remove_accents($title);
1903
1904
	/**
1905
	 * Filters a sanitized title string.
1906
	 *
1907
	 * @since 1.2.0
1908
	 *
1909
	 * @param string $title     Sanitized title.
1910
	 * @param string $raw_title The title prior to sanitization.
1911
	 * @param string $context   The context for which the title is being sanitized.
1912
	 */
1913
	$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );
1914
1915
	if ( '' === $title || false === $title )
1916
		$title = $fallback_title;
1917
1918
	return $title;
1919
}
1920
1921
/**
1922
 * Sanitizes a title with the 'query' context.
1923
 *
1924
 * Used for querying the database for a value from URL.
1925
 *
1926
 * @since 3.1.0
1927
 *
1928
 * @param string $title The string to be sanitized.
1929
 * @return string The sanitized string.
1930
 */
1931
function sanitize_title_for_query( $title ) {
1932
	return sanitize_title( $title, '', 'query' );
1933
}
1934
1935
/**
1936
 * Sanitizes a title, replacing whitespace and a few other characters with dashes.
1937
 *
1938
 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
1939
 * Whitespace becomes a dash.
1940
 *
1941
 * @since 1.2.0
1942
 *
1943
 * @param string $title     The title to be sanitized.
1944
 * @param string $raw_title Optional. Not used.
1945
 * @param string $context   Optional. The operation for which the string is sanitized.
1946
 * @return string The sanitized title.
1947
 */
1948
function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {
1949
	$title = strip_tags($title);
1950
	// Preserve escaped octets.
1951
	$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
1952
	// Remove percent signs that are not part of an octet.
1953
	$title = str_replace('%', '', $title);
1954
	// Restore octets.
1955
	$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
1956
1957
	if (seems_utf8($title)) {
1958
		if (function_exists('mb_strtolower')) {
1959
			$title = mb_strtolower($title, 'UTF-8');
1960
		}
1961
		$title = utf8_uri_encode($title, 200);
1962
	}
1963
1964
	$title = strtolower($title);
1965
1966
	if ( 'save' == $context ) {
1967
		// Convert nbsp, ndash and mdash to hyphens
1968
		$title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
1969
		// Convert nbsp, ndash and mdash HTML entities to hyphens
1970
		$title = str_replace( array( '&nbsp;', '&#160;', '&ndash;', '&#8211;', '&mdash;', '&#8212;' ), '-', $title );
1971
1972
		// Strip these characters entirely
1973
		$title = str_replace( array(
1974
			// iexcl and iquest
1975
			'%c2%a1', '%c2%bf',
1976
			// angle quotes
1977
			'%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
1978
			// curly quotes
1979
			'%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
1980
			'%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
1981
			// copy, reg, deg, hellip and trade
1982
			'%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
1983
			// acute accents
1984
			'%c2%b4', '%cb%8a', '%cc%81', '%cd%81',
1985
			// grave accent, macron, caron
1986
			'%cc%80', '%cc%84', '%cc%8c',
1987
		), '', $title );
1988
1989
		// Convert times to x
1990
		$title = str_replace( '%c3%97', 'x', $title );
1991
	}
1992
1993
	$title = preg_replace('/&.+?;/', '', $title); // kill entities
1994
	$title = str_replace('.', '-', $title);
1995
1996
	$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
1997
	$title = preg_replace('/\s+/', '-', $title);
1998
	$title = preg_replace('|-+|', '-', $title);
1999
	$title = trim($title, '-');
2000
2001
	return $title;
2002
}
2003
2004
/**
2005
 * Ensures a string is a valid SQL 'order by' clause.
2006
 *
2007
 * Accepts one or more columns, with or without a sort order (ASC / DESC).
2008
 * e.g. 'column_1', 'column_1, column_2', 'column_1 ASC, column_2 DESC' etc.
2009
 *
2010
 * Also accepts 'RAND()'.
2011
 *
2012
 * @since 2.5.1
2013
 *
2014
 * @param string $orderby Order by clause to be validated.
2015
 * @return string|false Returns $orderby if valid, false otherwise.
2016
 */
2017
function sanitize_sql_orderby( $orderby ) {
2018
	if ( preg_match( '/^\s*(([a-z0-9_]+|`[a-z0-9_]+`)(\s+(ASC|DESC))?\s*(,\s*(?=[a-z0-9_`])|$))+$/i', $orderby ) || preg_match( '/^\s*RAND\(\s*\)\s*$/i', $orderby ) ) {
2019
		return $orderby;
2020
	}
2021
	return false;
2022
}
2023
2024
/**
2025
 * Sanitizes an HTML classname to ensure it only contains valid characters.
2026
 *
2027
 * Strips the string down to A-Z,a-z,0-9,_,-. If this results in an empty
2028
 * string then it will return the alternative value supplied.
2029
 *
2030
 * @todo Expand to support the full range of CDATA that a class attribute can contain.
2031
 *
2032
 * @since 2.8.0
2033
 *
2034
 * @param string $class    The classname to be sanitized
2035
 * @param string $fallback Optional. The value to return if the sanitization ends up as an empty string.
2036
 * 	Defaults to an empty string.
2037
 * @return string The sanitized value
2038
 */
2039
function sanitize_html_class( $class, $fallback = '' ) {
2040
	//Strip out any % encoded octets
2041
	$sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );
2042
2043
	//Limit to A-Z,a-z,0-9,_,-
2044
	$sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );
2045
2046
	if ( '' == $sanitized && $fallback ) {
2047
		return sanitize_html_class( $fallback );
2048
	}
2049
	/**
2050
	 * Filters a sanitized HTML class string.
2051
	 *
2052
	 * @since 2.8.0
2053
	 *
2054
	 * @param string $sanitized The sanitized HTML class.
2055
	 * @param string $class     HTML class before sanitization.
2056
	 * @param string $fallback  The fallback string.
2057
	 */
2058
	return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );
2059
}
2060
2061
/**
2062
 * Converts lone & characters into `&#038;` (a.k.a. `&amp;`)
2063
 *
2064
 * @since 0.71
2065
 *
2066
 * @param string $content    String of characters to be converted.
2067
 * @param string $deprecated Not used.
2068
 * @return string Converted string.
2069
 */
2070
function convert_chars( $content, $deprecated = '' ) {
2071
	if ( ! empty( $deprecated ) ) {
2072
		_deprecated_argument( __FUNCTION__, '0.71' );
2073
	}
2074
2075
	if ( strpos( $content, '&' ) !== false ) {
2076
		$content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content );
2077
	}
2078
2079
	return $content;
2080
}
2081
2082
/**
2083
 * Converts invalid Unicode references range to valid range.
2084
 *
2085
 * @since 4.3.0
2086
 *
2087
 * @param string $content String with entities that need converting.
2088
 * @return string Converted string.
2089
 */
2090
function convert_invalid_entities( $content ) {
2091
	$wp_htmltranswinuni = array(
2092
		'&#128;' => '&#8364;', // the Euro sign
2093
		'&#129;' => '',
2094
		'&#130;' => '&#8218;', // these are Windows CP1252 specific characters
2095
		'&#131;' => '&#402;',  // they would look weird on non-Windows browsers
2096
		'&#132;' => '&#8222;',
2097
		'&#133;' => '&#8230;',
2098
		'&#134;' => '&#8224;',
2099
		'&#135;' => '&#8225;',
2100
		'&#136;' => '&#710;',
2101
		'&#137;' => '&#8240;',
2102
		'&#138;' => '&#352;',
2103
		'&#139;' => '&#8249;',
2104
		'&#140;' => '&#338;',
2105
		'&#141;' => '',
2106
		'&#142;' => '&#381;',
2107
		'&#143;' => '',
2108
		'&#144;' => '',
2109
		'&#145;' => '&#8216;',
2110
		'&#146;' => '&#8217;',
2111
		'&#147;' => '&#8220;',
2112
		'&#148;' => '&#8221;',
2113
		'&#149;' => '&#8226;',
2114
		'&#150;' => '&#8211;',
2115
		'&#151;' => '&#8212;',
2116
		'&#152;' => '&#732;',
2117
		'&#153;' => '&#8482;',
2118
		'&#154;' => '&#353;',
2119
		'&#155;' => '&#8250;',
2120
		'&#156;' => '&#339;',
2121
		'&#157;' => '',
2122
		'&#158;' => '&#382;',
2123
		'&#159;' => '&#376;'
2124
	);
2125
2126
	if ( strpos( $content, '&#1' ) !== false ) {
2127
		$content = strtr( $content, $wp_htmltranswinuni );
2128
	}
2129
2130
	return $content;
2131
}
2132
2133
/**
2134
 * Balances tags if forced to, or if the 'use_balanceTags' option is set to true.
2135
 *
2136
 * @since 0.71
2137
 *
2138
 * @param string $text  Text to be balanced
2139
 * @param bool   $force If true, forces balancing, ignoring the value of the option. Default false.
2140
 * @return string Balanced text
2141
 */
2142
function balanceTags( $text, $force = false ) {
2143
	if ( $force || get_option('use_balanceTags') == 1 ) {
2144
		return force_balance_tags( $text );
2145
	} else {
2146
		return $text;
2147
	}
2148
}
2149
2150
/**
2151
 * Balances tags of string using a modified stack.
2152
 *
2153
 * @since 2.0.4
2154
 *
2155
 * @author Leonard Lin <[email protected]>
2156
 * @license GPL
2157
 * @copyright November 4, 2001
2158
 * @version 1.1
2159
 * @todo Make better - change loop condition to $text in 1.2
2160
 * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
2161
 *		1.1  Fixed handling of append/stack pop order of end text
2162
 *			 Added Cleaning Hooks
2163
 *		1.0  First Version
2164
 *
2165
 * @param string $text Text to be balanced.
2166
 * @return string Balanced text.
2167
 */
2168
function force_balance_tags( $text ) {
2169
	$tagstack = array();
2170
	$stacksize = 0;
2171
	$tagqueue = '';
2172
	$newtext = '';
2173
	// Known single-entity/self-closing tags
2174
	$single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
2175
	// Tags that can be immediately nested within themselves
2176
	$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
2177
2178
	// WP bug fix for comments - in case you REALLY meant to type '< !--'
2179
	$text = str_replace('< !--', '<    !--', $text);
2180
	// WP bug fix for LOVE <3 (and other situations with '<' before a number)
2181
	$text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
2182
2183
	while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
2184
		$newtext .= $tagqueue;
2185
2186
		$i = strpos($text, $regex[0]);
2187
		$l = strlen($regex[0]);
2188
2189
		// clear the shifter
2190
		$tagqueue = '';
2191
		// Pop or Push
2192
		if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
2193
			$tag = strtolower(substr($regex[1],1));
2194
			// if too many closing tags
2195
			if ( $stacksize <= 0 ) {
2196
				$tag = '';
2197
				// or close to be safe $tag = '/' . $tag;
2198
			}
2199
			// if stacktop value = tag close value then pop
2200
			elseif ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
2201
				$tag = '</' . $tag . '>'; // Close Tag
2202
				// Pop
2203
				array_pop( $tagstack );
2204
				$stacksize--;
2205
			} else { // closing tag not at top, search for it
2206
				for ( $j = $stacksize-1; $j >= 0; $j-- ) {
2207
					if ( $tagstack[$j] == $tag ) {
2208
					// add tag to tagqueue
2209
						for ( $k = $stacksize-1; $k >= $j; $k--) {
2210
							$tagqueue .= '</' . array_pop( $tagstack ) . '>';
2211
							$stacksize--;
2212
						}
2213
						break;
2214
					}
2215
				}
2216
				$tag = '';
2217
			}
2218
		} else { // Begin Tag
2219
			$tag = strtolower($regex[1]);
2220
2221
			// Tag Cleaning
2222
2223
			// If it's an empty tag "< >", do nothing
2224
			if ( '' == $tag ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
2225
				// do nothing
2226
			}
2227
			// ElseIf it presents itself as a self-closing tag...
2228
			elseif ( substr( $regex[2], -1 ) == '/' ) {
2229
				// ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
2230
				// immediately close it with a closing tag (the tag will encapsulate no text as a result)
2231
				if ( ! in_array( $tag, $single_tags ) )
2232
					$regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
2233
			}
2234
			// ElseIf it's a known single-entity tag but it doesn't close itself, do so
2235
			elseif ( in_array($tag, $single_tags) ) {
2236
				$regex[2] .= '/';
2237
			}
2238
			// Else it's not a single-entity tag
2239
			else {
2240
				// If the top of the stack is the same as the tag we want to push, close previous tag
2241
				if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
2242
					$tagqueue = '</' . array_pop( $tagstack ) . '>';
2243
					$stacksize--;
2244
				}
2245
				$stacksize = array_push( $tagstack, $tag );
2246
			}
2247
2248
			// Attributes
2249
			$attributes = $regex[2];
2250
			if ( ! empty( $attributes ) && $attributes[0] != '>' )
2251
				$attributes = ' ' . $attributes;
2252
2253
			$tag = '<' . $tag . $attributes . '>';
2254
			//If already queuing a close tag, then put this tag on, too
2255
			if ( !empty($tagqueue) ) {
2256
				$tagqueue .= $tag;
2257
				$tag = '';
2258
			}
2259
		}
2260
		$newtext .= substr($text, 0, $i) . $tag;
2261
		$text = substr($text, $i + $l);
2262
	}
2263
2264
	// Clear Tag Queue
2265
	$newtext .= $tagqueue;
2266
2267
	// Add Remaining text
2268
	$newtext .= $text;
2269
2270
	// Empty Stack
2271
	while( $x = array_pop($tagstack) )
2272
		$newtext .= '</' . $x . '>'; // Add remaining tags to close
2273
2274
	// WP fix for the bug with HTML comments
2275
	$newtext = str_replace("< !--","<!--",$newtext);
2276
	$newtext = str_replace("<    !--","< !--",$newtext);
2277
2278
	return $newtext;
2279
}
2280
2281
/**
2282
 * Acts on text which is about to be edited.
2283
 *
2284
 * The $content is run through esc_textarea(), which uses htmlspecialchars()
2285
 * to convert special characters to HTML entities. If `$richedit` is set to true,
2286
 * it is simply a holder for the {@see 'format_to_edit'} filter.
2287
 *
2288
 * @since 0.71
2289
 * @since 4.4.0 The `$richedit` parameter was renamed to `$rich_text` for clarity.
2290
 *
2291
 * @param string $content   The text about to be edited.
2292
 * @param bool   $rich_text Optional. Whether `$content` should be considered rich text,
2293
 *                          in which case it would not be passed through esc_textarea().
2294
 *                          Default false.
2295
 * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
2296
 */
2297
function format_to_edit( $content, $rich_text = false ) {
2298
	/**
2299
	 * Filters the text to be formatted for editing.
2300
	 *
2301
	 * @since 1.2.0
2302
	 *
2303
	 * @param string $content The text, prior to formatting for editing.
2304
	 */
2305
	$content = apply_filters( 'format_to_edit', $content );
2306
	if ( ! $rich_text )
2307
		$content = esc_textarea( $content );
2308
	return $content;
2309
}
2310
2311
/**
2312
 * Add leading zeros when necessary.
2313
 *
2314
 * If you set the threshold to '4' and the number is '10', then you will get
2315
 * back '0010'. If you set the threshold to '4' and the number is '5000', then you
2316
 * will get back '5000'.
2317
 *
2318
 * Uses sprintf to append the amount of zeros based on the $threshold parameter
2319
 * and the size of the number. If the number is large enough, then no zeros will
2320
 * be appended.
2321
 *
2322
 * @since 0.71
2323
 *
2324
 * @param int $number     Number to append zeros to if not greater than threshold.
2325
 * @param int $threshold  Digit places number needs to be to not have zeros added.
2326
 * @return string Adds leading zeros to number if needed.
2327
 */
2328
function zeroise( $number, $threshold ) {
2329
	return sprintf( '%0' . $threshold . 's', $number );
2330
}
2331
2332
/**
2333
 * Adds backslashes before letters and before a number at the start of a string.
2334
 *
2335
 * @since 0.71
2336
 *
2337
 * @param string $string Value to which backslashes will be added.
2338
 * @return string String with backslashes inserted.
2339
 */
2340
function backslashit( $string ) {
2341
	if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' )
2342
		$string = '\\\\' . $string;
2343
	return addcslashes( $string, 'A..Za..z' );
2344
}
2345
2346
/**
2347
 * Appends a trailing slash.
2348
 *
2349
 * Will remove trailing forward and backslashes if it exists already before adding
2350
 * a trailing forward slash. This prevents double slashing a string or path.
2351
 *
2352
 * The primary use of this is for paths and thus should be used for paths. It is
2353
 * not restricted to paths and offers no specific path support.
2354
 *
2355
 * @since 1.2.0
2356
 *
2357
 * @param string $string What to add the trailing slash to.
2358
 * @return string String with trailing slash added.
2359
 */
2360
function trailingslashit( $string ) {
2361
	return untrailingslashit( $string ) . '/';
2362
}
2363
2364
/**
2365
 * Removes trailing forward slashes and backslashes if they exist.
2366
 *
2367
 * The primary use of this is for paths and thus should be used for paths. It is
2368
 * not restricted to paths and offers no specific path support.
2369
 *
2370
 * @since 2.2.0
2371
 *
2372
 * @param string $string What to remove the trailing slashes from.
2373
 * @return string String without the trailing slashes.
2374
 */
2375
function untrailingslashit( $string ) {
2376
	return rtrim( $string, '/\\' );
2377
}
2378
2379
/**
2380
 * Adds slashes to escape strings.
2381
 *
2382
 * Slashes will first be removed if magic_quotes_gpc is set, see {@link
2383
 * https://secure.php.net/magic_quotes} for more details.
2384
 *
2385
 * @since 0.71
2386
 *
2387
 * @param string $gpc The string returned from HTTP request data.
2388
 * @return string Returns a string escaped with slashes.
2389
 */
2390
function addslashes_gpc($gpc) {
2391
	if ( get_magic_quotes_gpc() )
2392
		$gpc = stripslashes($gpc);
2393
2394
	return wp_slash($gpc);
2395
}
2396
2397
/**
2398
 * Navigates through an array, object, or scalar, and removes slashes from the values.
2399
 *
2400
 * @since 2.0.0
2401
 *
2402
 * @param mixed $value The value to be stripped.
2403
 * @return mixed Stripped value.
2404
 */
2405
function stripslashes_deep( $value ) {
2406
	return map_deep( $value, 'stripslashes_from_strings_only' );
2407
}
2408
2409
/**
2410
 * Callback function for `stripslashes_deep()` which strips slashes from strings.
2411
 *
2412
 * @since 4.4.0
2413
 *
2414
 * @param mixed $value The array or string to be stripped.
2415
 * @return mixed $value The stripped value.
2416
 */
2417
function stripslashes_from_strings_only( $value ) {
2418
	return is_string( $value ) ? stripslashes( $value ) : $value;
2419
}
2420
2421
/**
2422
 * Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
2423
 *
2424
 * @since 2.2.0
2425
 *
2426
 * @param mixed $value The array or string to be encoded.
2427
 * @return mixed $value The encoded value.
2428
 */
2429
function urlencode_deep( $value ) {
2430
	return map_deep( $value, 'urlencode' );
2431
}
2432
2433
/**
2434
 * Navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL.
2435
 *
2436
 * @since 3.4.0
2437
 *
2438
 * @param mixed $value The array or string to be encoded.
2439
 * @return mixed $value The encoded value.
2440
 */
2441
function rawurlencode_deep( $value ) {
2442
	return map_deep( $value, 'rawurlencode' );
2443
}
2444
2445
/**
2446
 * Navigates through an array, object, or scalar, and decodes URL-encoded values
2447
 *
2448
 * @since 4.4.0
2449
 *
2450
 * @param mixed $value The array or string to be decoded.
2451
 * @return mixed $value The decoded value.
2452
 */
2453
function urldecode_deep( $value ) {
2454
	return map_deep( $value, 'urldecode' );
2455
}
2456
2457
/**
2458
 * Converts email addresses characters to HTML entities to block spam bots.
2459
 *
2460
 * @since 0.71
2461
 *
2462
 * @param string $email_address Email address.
2463
 * @param int    $hex_encoding  Optional. Set to 1 to enable hex encoding.
2464
 * @return string Converted email address.
2465
 */
2466
function antispambot( $email_address, $hex_encoding = 0 ) {
2467
	$email_no_spam_address = '';
2468
	for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
2469
		$j = rand( 0, 1 + $hex_encoding );
2470
		if ( $j == 0 ) {
2471
			$email_no_spam_address .= '&#' . ord( $email_address[$i] ) . ';';
2472
		} elseif ( $j == 1 ) {
2473
			$email_no_spam_address .= $email_address[$i];
2474
		} elseif ( $j == 2 ) {
2475
			$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[$i] ) ), 2 );
2476
		}
2477
	}
2478
2479
	return str_replace( '@', '&#64;', $email_no_spam_address );
2480
}
2481
2482
/**
2483
 * Callback to convert URI match to HTML A element.
2484
 *
2485
 * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
2486
 *
2487
 * @since 2.3.2
2488
 * @access private
2489
 *
2490
 * @param array $matches Single Regex Match.
2491
 * @return string HTML A element with URI address.
2492
 */
2493
function _make_url_clickable_cb( $matches ) {
2494
	$url = $matches[2];
2495
2496
	if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
2497
		// If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
2498
		// Then we can let the parenthesis balancer do its thing below.
2499
		$url .= $matches[3];
2500
		$suffix = '';
2501
	} else {
2502
		$suffix = $matches[3];
2503
	}
2504
2505
	// Include parentheses in the URL only if paired
2506
	while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
2507
		$suffix = strrchr( $url, ')' ) . $suffix;
2508
		$url = substr( $url, 0, strrpos( $url, ')' ) );
2509
	}
2510
2511
	$url = esc_url($url);
2512
	if ( empty($url) )
2513
		return $matches[0];
2514
2515
	return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
2516
}
2517
2518
/**
2519
 * Callback to convert URL match to HTML A element.
2520
 *
2521
 * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
2522
 *
2523
 * @since 2.3.2
2524
 * @access private
2525
 *
2526
 * @param array $matches Single Regex Match.
2527
 * @return string HTML A element with URL address.
2528
 */
2529
function _make_web_ftp_clickable_cb( $matches ) {
2530
	$ret = '';
2531
	$dest = $matches[2];
2532
	$dest = 'http://' . $dest;
2533
2534
	// removed trailing [.,;:)] from URL
2535
	if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
2536
		$ret = substr($dest, -1);
2537
		$dest = substr($dest, 0, strlen($dest)-1);
2538
	}
2539
2540
	$dest = esc_url($dest);
2541
	if ( empty($dest) )
2542
		return $matches[0];
2543
2544
	return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
2545
}
2546
2547
/**
2548
 * Callback to convert email address match to HTML A element.
2549
 *
2550
 * This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
2551
 *
2552
 * @since 2.3.2
2553
 * @access private
2554
 *
2555
 * @param array $matches Single Regex Match.
2556
 * @return string HTML A element with email address.
2557
 */
2558
function _make_email_clickable_cb( $matches ) {
2559
	$email = $matches[2] . '@' . $matches[3];
2560
	return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
2561
}
2562
2563
/**
2564
 * Convert plaintext URI to HTML links.
2565
 *
2566
 * Converts URI, www and ftp, and email addresses. Finishes by fixing links
2567
 * within links.
2568
 *
2569
 * @since 0.71
2570
 *
2571
 * @param string $text Content to convert URIs.
2572
 * @return string Content with converted URIs.
2573
 */
2574
function make_clickable( $text ) {
2575
	$r = '';
2576
	$textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
2577
	$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
2578
	foreach ( $textarr as $piece ) {
2579
2580
		if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) || preg_match( '|^<script[\s>]|i', $piece ) || preg_match( '|^<style[\s>]|i', $piece ) )
2581
			$nested_code_pre++;
2582
		elseif ( $nested_code_pre && ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) || '</script>' === strtolower( $piece ) || '</style>' === strtolower( $piece ) ) )
2583
			$nested_code_pre--;
2584
2585
		if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
2586
			$r .= $piece;
2587
			continue;
2588
		}
2589
2590
		// Long strings might contain expensive edge cases ...
2591
		if ( 10000 < strlen( $piece ) ) {
2592
			// ... break it up
2593
			foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
2594
				if ( 2101 < strlen( $chunk ) ) {
2595
					$r .= $chunk; // Too big, no whitespace: bail.
2596
				} else {
2597
					$r .= make_clickable( $chunk );
2598
				}
2599
			}
2600
		} else {
2601
			$ret = " $piece "; // Pad with whitespace to simplify the regexes
2602
2603
			$url_clickable = '~
2604
				([\\s(<.,;:!?])                                        # 1: Leading whitespace, or punctuation
2605
				(                                                      # 2: URL
2606
					[\\w]{1,20}+://                                # Scheme and hier-part prefix
2607
					(?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long
2608
					[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character
2609
					(?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
2610
						[\'.,;:!?)]                            # Punctuation URL character
2611
						[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
2612
					)*
2613
				)
2614
				(\)?)                                                  # 3: Trailing closing parenthesis (for parethesis balancing post processing)
2615
			~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
2616
			      // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
2617
2618
			$ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
2619
2620
			$ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
2621
			$ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
2622
2623
			$ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
2624
			$r .= $ret;
2625
		}
2626
	}
2627
2628
	// Cleanup of accidental links within links
2629
	return preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
2630
}
2631
2632
/**
2633
 * Breaks a string into chunks by splitting at whitespace characters.
2634
 * The length of each returned chunk is as close to the specified length goal as possible,
2635
 * with the caveat that each chunk includes its trailing delimiter.
2636
 * Chunks longer than the goal are guaranteed to not have any inner whitespace.
2637
 *
2638
 * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
2639
 *
2640
 * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
2641
 *
2642
 *     _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234   890 123456789 1234567890a    45678   1 3 5 7 90 ", 10 ) ==
2643
 *     array (
2644
 *         0 => '1234 67890 ',  // 11 characters: Perfect split
2645
 *         1 => '1234 ',        //  5 characters: '1234 67890a' was too long
2646
 *         2 => '67890a cd ',   // 10 characters: '67890a cd 1234' was too long
2647
 *         3 => '1234   890 ',  // 11 characters: Perfect split
2648
 *         4 => '123456789 ',   // 10 characters: '123456789 1234567890a' was too long
2649
 *         5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
2650
 *         6 => '   45678   ',  // 11 characters: Perfect split
2651
 *         7 => '1 3 5 7 90 ',  // 11 characters: End of $string
2652
 *     );
2653
 *
2654
 * @since 3.4.0
2655
 * @access private
2656
 *
2657
 * @param string $string The string to split.
2658
 * @param int    $goal   The desired chunk length.
2659
 * @return array Numeric array of chunks.
2660
 */
2661
function _split_str_by_whitespace( $string, $goal ) {
2662
	$chunks = array();
2663
2664
	$string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
2665
2666
	while ( $goal < strlen( $string_nullspace ) ) {
2667
		$pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
2668
2669
		if ( false === $pos ) {
2670
			$pos = strpos( $string_nullspace, "\000", $goal + 1 );
2671
			if ( false === $pos ) {
2672
				break;
2673
			}
2674
		}
2675
2676
		$chunks[] = substr( $string, 0, $pos + 1 );
2677
		$string = substr( $string, $pos + 1 );
2678
		$string_nullspace = substr( $string_nullspace, $pos + 1 );
2679
	}
2680
2681
	if ( $string ) {
2682
		$chunks[] = $string;
2683
	}
2684
2685
	return $chunks;
2686
}
2687
2688
/**
2689
 * Adds rel nofollow string to all HTML A elements in content.
2690
 *
2691
 * @since 1.5.0
2692
 *
2693
 * @param string $text Content that may contain HTML A elements.
2694
 * @return string Converted content.
2695
 */
2696
function wp_rel_nofollow( $text ) {
2697
	// This is a pre save filter, so text is already escaped.
2698
	$text = stripslashes($text);
2699
	$text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
2700
	return wp_slash( $text );
2701
}
2702
2703
/**
2704
 * Callback to add rel=nofollow string to HTML A element.
2705
 *
2706
 * Will remove already existing rel="nofollow" and rel='nofollow' from the
2707
 * string to prevent from invalidating (X)HTML.
2708
 *
2709
 * @since 2.3.0
2710
 *
2711
 * @param array $matches Single Match
2712
 * @return string HTML A Element with rel nofollow.
2713
 */
2714
function wp_rel_nofollow_callback( $matches ) {
2715
	$text = $matches[1];
2716
	$atts = shortcode_parse_atts( $matches[1] );
2717
	$rel  = 'nofollow';
2718
2719
	if ( preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'http' ) ) . ')%i', $text ) ||
2720
	     preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'https' ) ) . ')%i', $text )
2721
	) {
2722
		return "<a $text>";
2723
	}
2724
2725
	if ( ! empty( $atts['rel'] ) ) {
2726
		$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
2727
		if ( false === array_search( 'nofollow', $parts ) ) {
2728
			$parts[] = 'nofollow';
2729
		}
2730
		$rel = implode( ' ', $parts );
2731
		unset( $atts['rel'] );
2732
2733
		$html = '';
2734
		foreach ( $atts as $name => $value ) {
0 ignored issues
show
Bug introduced by
The expression $atts of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
2735
			$html .= "{$name}=\"$value\" ";
2736
		}
2737
		$text = trim( $html );
2738
	}
2739
	return "<a $text rel=\"$rel\">";
2740
}
2741
2742
/**
2743
 * Convert one smiley code to the icon graphic file equivalent.
2744
 *
2745
 * Callback handler for convert_smilies().
2746
 *
2747
 * Looks up one smiley code in the $wpsmiliestrans global array and returns an
2748
 * `<img>` string for that smiley.
2749
 *
2750
 * @since 2.8.0
2751
 *
2752
 * @global array $wpsmiliestrans
2753
 *
2754
 * @param array $matches Single match. Smiley code to convert to image.
2755
 * @return string Image string for smiley.
2756
 */
2757
function translate_smiley( $matches ) {
2758
	global $wpsmiliestrans;
2759
2760
	if ( count( $matches ) == 0 )
2761
		return '';
2762
2763
	$smiley = trim( reset( $matches ) );
2764
	$img = $wpsmiliestrans[ $smiley ];
2765
2766
	$matches = array();
2767
	$ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
2768
	$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
2769
2770
	// Don't convert smilies that aren't images - they're probably emoji.
2771
	if ( ! in_array( $ext, $image_exts ) ) {
2772
		return $img;
2773
	}
2774
2775
	/**
2776
	 * Filters the Smiley image URL before it's used in the image element.
2777
	 *
2778
	 * @since 2.9.0
2779
	 *
2780
	 * @param string $smiley_url URL for the smiley image.
2781
	 * @param string $img        Filename for the smiley image.
2782
	 * @param string $site_url   Site URL, as returned by site_url().
2783
	 */
2784
	$src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
2785
2786
	return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ) );
2787
}
2788
2789
/**
2790
 * Convert text equivalent of smilies to images.
2791
 *
2792
 * Will only convert smilies if the option 'use_smilies' is true and the global
2793
 * used in the function isn't empty.
2794
 *
2795
 * @since 0.71
2796
 *
2797
 * @global string|array $wp_smiliessearch
2798
 *
2799
 * @param string $text Content to convert smilies from text.
2800
 * @return string Converted content with text smilies replaced with images.
2801
 */
2802
function convert_smilies( $text ) {
2803
	global $wp_smiliessearch;
2804
	$output = '';
2805
	if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
2806
		// HTML loop taken from texturize function, could possible be consolidated
2807
		$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
2808
		$stop = count( $textarr );// loop stuff
2809
2810
		// Ignore proessing of specific tags
2811
		$tags_to_ignore = 'code|pre|style|script|textarea';
2812
		$ignore_block_element = '';
2813
2814
		for ( $i = 0; $i < $stop; $i++ ) {
2815
			$content = $textarr[$i];
2816
2817
			// If we're in an ignore block, wait until we find its closing tag
2818 View Code Duplication
			if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
2819
				$ignore_block_element = $matches[1];
2820
			}
2821
2822
			// If it's not a tag and not in ignore block
2823
			if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
2824
				$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
2825
			}
2826
2827
			// did we exit ignore block
2828
			if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
2829
				$ignore_block_element = '';
2830
			}
2831
2832
			$output .= $content;
2833
		}
2834
	} else {
2835
		// return default text.
2836
		$output = $text;
2837
	}
2838
	return $output;
2839
}
2840
2841
/**
2842
 * Verifies that an email is valid.
2843
 *
2844
 * Does not grok i18n domains. Not RFC compliant.
2845
 *
2846
 * @since 0.71
2847
 *
2848
 * @param string $email      Email address to verify.
2849
 * @param bool   $deprecated Deprecated.
2850
 * @return string|bool Either false or the valid email address.
2851
 */
2852
function is_email( $email, $deprecated = false ) {
2853
	if ( ! empty( $deprecated ) )
2854
		_deprecated_argument( __FUNCTION__, '3.0.0' );
2855
2856
	// Test for the minimum length the email can be
2857
	if ( strlen( $email ) < 3 ) {
2858
		/**
2859
		 * Filters whether an email address is valid.
2860
		 *
2861
		 * This filter is evaluated under several different contexts, such as 'email_too_short',
2862
		 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
2863
		 * 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context.
2864
		 *
2865
		 * @since 2.8.0
2866
		 *
2867
		 * @param bool   $is_email Whether the email address has passed the is_email() checks. Default false.
2868
		 * @param string $email    The email address being checked.
2869
		 * @param string $context  Context under which the email was tested.
2870
		 */
2871
		return apply_filters( 'is_email', false, $email, 'email_too_short' );
2872
	}
2873
2874
	// Test for an @ character after the first position
2875 View Code Duplication
	if ( strpos( $email, '@', 1 ) === false ) {
2876
		/** This filter is documented in wp-includes/formatting.php */
2877
		return apply_filters( 'is_email', false, $email, 'email_no_at' );
2878
	}
2879
2880
	// Split out the local and domain parts
2881
	list( $local, $domain ) = explode( '@', $email, 2 );
2882
2883
	// LOCAL PART
2884
	// Test for invalid characters
2885
	if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
2886
		/** This filter is documented in wp-includes/formatting.php */
2887
		return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
2888
	}
2889
2890
	// DOMAIN PART
2891
	// Test for sequences of periods
2892
	if ( preg_match( '/\.{2,}/', $domain ) ) {
2893
		/** This filter is documented in wp-includes/formatting.php */
2894
		return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
2895
	}
2896
2897
	// Test for leading and trailing periods and whitespace
2898
	if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
2899
		/** This filter is documented in wp-includes/formatting.php */
2900
		return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
2901
	}
2902
2903
	// Split the domain into subs
2904
	$subs = explode( '.', $domain );
2905
2906
	// Assume the domain will have at least two subs
2907
	if ( 2 > count( $subs ) ) {
2908
		/** This filter is documented in wp-includes/formatting.php */
2909
		return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
2910
	}
2911
2912
	// Loop through each sub
2913
	foreach ( $subs as $sub ) {
2914
		// Test for leading and trailing hyphens and whitespace
2915
		if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
2916
			/** This filter is documented in wp-includes/formatting.php */
2917
			return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
2918
		}
2919
2920
		// Test for invalid characters
2921
		if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
2922
			/** This filter is documented in wp-includes/formatting.php */
2923
			return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
2924
		}
2925
	}
2926
2927
	// Congratulations your email made it!
2928
	/** This filter is documented in wp-includes/formatting.php */
2929
	return apply_filters( 'is_email', $email, $email, null );
2930
}
2931
2932
/**
2933
 * Convert to ASCII from email subjects.
2934
 *
2935
 * @since 1.2.0
2936
 *
2937
 * @param string $string Subject line
2938
 * @return string Converted string to ASCII
2939
 */
2940
function wp_iso_descrambler( $string ) {
2941
	/* this may only work with iso-8859-1, I'm afraid */
2942
	if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
2943
		return $string;
2944
	} else {
2945
		$subject = str_replace('_', ' ', $matches[2]);
2946
		return preg_replace_callback( '#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject );
2947
	}
2948
}
2949
2950
/**
2951
 * Helper function to convert hex encoded chars to ASCII
2952
 *
2953
 * @since 3.1.0
2954
 * @access private
2955
 *
2956
 * @param array $match The preg_replace_callback matches array
2957
 * @return string Converted chars
2958
 */
2959
function _wp_iso_convert( $match ) {
2960
	return chr( hexdec( strtolower( $match[1] ) ) );
2961
}
2962
2963
/**
2964
 * Returns a date in the GMT equivalent.
2965
 *
2966
 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
2967
 * timezone_string available, the date is assumed to be in that timezone,
2968
 * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
2969
 * format can be overridden using the $format parameter.
2970
 *
2971
 * @since 1.2.0
2972
 *
2973
 * @param string $string The date to be converted.
2974
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
2975
 * @return string GMT version of the date provided.
2976
 */
2977
function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
2978
	$tz = get_option( 'timezone_string' );
2979
	if ( $tz ) {
2980
		$datetime = date_create( $string, new DateTimeZone( $tz ) );
2981
		if ( ! $datetime ) {
2982
			return gmdate( $format, 0 );
2983
		}
2984
		$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
2985
		$string_gmt = $datetime->format( $format );
2986
	} else {
2987
		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 ) ) {
2988
			$datetime = strtotime( $string );
2989
			if ( false === $datetime ) {
2990
				return gmdate( $format, 0 );
2991
			}
2992
			return gmdate( $format, $datetime );
2993
		}
2994
		$string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
2995
		$string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
2996
	}
2997
	return $string_gmt;
2998
}
2999
3000
/**
3001
 * Converts a GMT date into the correct format for the blog.
3002
 *
3003
 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
3004
 * timezone_string available, the returned date is in that timezone, otherwise
3005
 * it simply adds the value of gmt_offset. Return format can be overridden
3006
 * using the $format parameter
3007
 *
3008
 * @since 1.2.0
3009
 *
3010
 * @param string $string The date to be converted.
3011
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
3012
 * @return string Formatted date relative to the timezone / GMT offset.
3013
 */
3014
function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
3015
	$tz = get_option( 'timezone_string' );
3016
	if ( $tz ) {
3017
		$datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
3018
		if ( ! $datetime )
3019
			return date( $format, 0 );
3020
		$datetime->setTimezone( new DateTimeZone( $tz ) );
3021
		$string_localtime = $datetime->format( $format );
3022
	} else {
3023
		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) )
3024
			return date( $format, 0 );
3025
		$string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
3026
		$string_localtime = gmdate( $format, $string_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
3027
	}
3028
	return $string_localtime;
3029
}
3030
3031
/**
3032
 * Computes an offset in seconds from an iso8601 timezone.
3033
 *
3034
 * @since 1.5.0
3035
 *
3036
 * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
3037
 * @return int|float The offset in seconds.
3038
 */
3039
function iso8601_timezone_to_offset( $timezone ) {
3040
	// $timezone is either 'Z' or '[+|-]hhmm'
3041
	if ($timezone == 'Z') {
3042
		$offset = 0;
3043
	} else {
3044
		$sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
3045
		$hours   = intval(substr($timezone, 1, 2));
3046
		$minutes = intval(substr($timezone, 3, 4)) / 60;
3047
		$offset  = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
3048
	}
3049
	return $offset;
3050
}
3051
3052
/**
3053
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
3054
 *
3055
 * @since 1.5.0
3056
 *
3057
 * @param string $date_string Date and time in ISO 8601 format {@link https://en.wikipedia.org/wiki/ISO_8601}.
3058
 * @param string $timezone    Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
3059
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
3060
 */
3061
function iso8601_to_datetime( $date_string, $timezone = 'user' ) {
3062
	$timezone = strtolower($timezone);
3063
3064
	if ($timezone == 'gmt') {
3065
3066
		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);
3067
3068
		if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
3069
			$offset = iso8601_timezone_to_offset($date_bits[7]);
3070
		} else { // we don't have a timezone, so we assume user local timezone (not server's!)
3071
			$offset = HOUR_IN_SECONDS * get_option('gmt_offset');
3072
		}
3073
3074
		$timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
3075
		$timestamp -= $offset;
3076
3077
		return gmdate('Y-m-d H:i:s', $timestamp);
3078
3079
	} elseif ($timezone == 'user') {
3080
		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);
3081
	}
3082
}
3083
3084
/**
3085
 * Strips out all characters that are not allowable in an email.
3086
 *
3087
 * @since 1.5.0
3088
 *
3089
 * @param string $email Email address to filter.
3090
 * @return string Filtered email address.
3091
 */
3092
function sanitize_email( $email ) {
3093
	// Test for the minimum length the email can be
3094
	if ( strlen( $email ) < 3 ) {
3095
		/**
3096
		 * Filters a sanitized email address.
3097
		 *
3098
		 * This filter is evaluated under several contexts, including 'email_too_short',
3099
		 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
3100
		 * 'domain_no_periods', 'domain_no_valid_subs', or no context.
3101
		 *
3102
		 * @since 2.8.0
3103
		 *
3104
		 * @param string $email   The sanitized email address.
3105
		 * @param string $email   The email address, as provided to sanitize_email().
3106
		 * @param string $message A message to pass to the user.
3107
		 */
3108
		return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
3109
	}
3110
3111
	// Test for an @ character after the first position
3112 View Code Duplication
	if ( strpos( $email, '@', 1 ) === false ) {
3113
		/** This filter is documented in wp-includes/formatting.php */
3114
		return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
3115
	}
3116
3117
	// Split out the local and domain parts
3118
	list( $local, $domain ) = explode( '@', $email, 2 );
3119
3120
	// LOCAL PART
3121
	// Test for invalid characters
3122
	$local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
3123
	if ( '' === $local ) {
3124
		/** This filter is documented in wp-includes/formatting.php */
3125
		return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
3126
	}
3127
3128
	// DOMAIN PART
3129
	// Test for sequences of periods
3130
	$domain = preg_replace( '/\.{2,}/', '', $domain );
3131
	if ( '' === $domain ) {
3132
		/** This filter is documented in wp-includes/formatting.php */
3133
		return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
3134
	}
3135
3136
	// Test for leading and trailing periods and whitespace
3137
	$domain = trim( $domain, " \t\n\r\0\x0B." );
3138
	if ( '' === $domain ) {
3139
		/** This filter is documented in wp-includes/formatting.php */
3140
		return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
3141
	}
3142
3143
	// Split the domain into subs
3144
	$subs = explode( '.', $domain );
3145
3146
	// Assume the domain will have at least two subs
3147
	if ( 2 > count( $subs ) ) {
3148
		/** This filter is documented in wp-includes/formatting.php */
3149
		return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
3150
	}
3151
3152
	// Create an array that will contain valid subs
3153
	$new_subs = array();
3154
3155
	// Loop through each sub
3156
	foreach ( $subs as $sub ) {
3157
		// Test for leading and trailing hyphens
3158
		$sub = trim( $sub, " \t\n\r\0\x0B-" );
3159
3160
		// Test for invalid characters
3161
		$sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );
3162
3163
		// If there's anything left, add it to the valid subs
3164
		if ( '' !== $sub ) {
3165
			$new_subs[] = $sub;
3166
		}
3167
	}
3168
3169
	// If there aren't 2 or more valid subs
3170
	if ( 2 > count( $new_subs ) ) {
3171
		/** This filter is documented in wp-includes/formatting.php */
3172
		return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
3173
	}
3174
3175
	// Join valid subs into the new domain
3176
	$domain = join( '.', $new_subs );
3177
3178
	// Put the email back together
3179
	$email = $local . '@' . $domain;
3180
3181
	// Congratulations your email made it!
3182
	/** This filter is documented in wp-includes/formatting.php */
3183
	return apply_filters( 'sanitize_email', $email, $email, null );
3184
}
3185
3186
/**
3187
 * Determines the difference between two timestamps.
3188
 *
3189
 * The difference is returned in a human readable format such as "1 hour",
3190
 * "5 mins", "2 days".
3191
 *
3192
 * @since 1.5.0
3193
 *
3194
 * @param int $from Unix timestamp from which the difference begins.
3195
 * @param int $to   Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
3196
 * @return string Human readable time difference.
3197
 */
3198
function human_time_diff( $from, $to = '' ) {
3199
	if ( empty( $to ) ) {
3200
		$to = time();
3201
	}
3202
3203
	$diff = (int) abs( $to - $from );
3204
3205
	if ( $diff < HOUR_IN_SECONDS ) {
3206
		$mins = round( $diff / MINUTE_IN_SECONDS );
3207
		if ( $mins <= 1 )
3208
			$mins = 1;
3209
		/* translators: min=minute */
3210
		$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
3211
	} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
3212
		$hours = round( $diff / HOUR_IN_SECONDS );
3213
		if ( $hours <= 1 )
3214
			$hours = 1;
3215
		$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
3216
	} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
3217
		$days = round( $diff / DAY_IN_SECONDS );
3218
		if ( $days <= 1 )
3219
			$days = 1;
3220
		$since = sprintf( _n( '%s day', '%s days', $days ), $days );
3221
	} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
3222
		$weeks = round( $diff / WEEK_IN_SECONDS );
3223
		if ( $weeks <= 1 )
3224
			$weeks = 1;
3225
		$since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
3226
	} elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
3227
		$months = round( $diff / MONTH_IN_SECONDS );
3228
		if ( $months <= 1 )
3229
			$months = 1;
3230
		$since = sprintf( _n( '%s month', '%s months', $months ), $months );
3231
	} elseif ( $diff >= YEAR_IN_SECONDS ) {
3232
		$years = round( $diff / YEAR_IN_SECONDS );
3233
		if ( $years <= 1 )
3234
			$years = 1;
3235
		$since = sprintf( _n( '%s year', '%s years', $years ), $years );
3236
	}
3237
3238
	/**
3239
	 * Filters the human readable difference between two timestamps.
3240
	 *
3241
	 * @since 4.0.0
3242
	 *
3243
	 * @param string $since The difference in human readable text.
3244
	 * @param int    $diff  The difference in seconds.
3245
	 * @param int    $from  Unix timestamp from which the difference begins.
3246
	 * @param int    $to    Unix timestamp to end the time difference.
3247
	 */
3248
	return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
0 ignored issues
show
Bug introduced by
The variable $since does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
3249
}
3250
3251
/**
3252
 * Generates an excerpt from the content, if needed.
3253
 *
3254
 * The excerpt word amount will be 55 words and if the amount is greater than
3255
 * that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
3256
 * is less than 55 words, then the content will be returned as is.
3257
 *
3258
 * The 55 word limit can be modified by plugins/themes using the {@see 'excerpt_length'} filter
3259
 * The ' [&hellip;]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter
3260
 *
3261
 * @since 1.5.0
3262
 *
3263
 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
3264
 * @return string The excerpt.
3265
 */
3266
function wp_trim_excerpt( $text = '' ) {
3267
	$raw_excerpt = $text;
3268
	if ( '' == $text ) {
3269
		$text = get_the_content('');
3270
3271
		$text = strip_shortcodes( $text );
3272
3273
		/** This filter is documented in wp-includes/post-template.php */
3274
		$text = apply_filters( 'the_content', $text );
3275
		$text = str_replace(']]>', ']]&gt;', $text);
3276
3277
		/**
3278
		 * Filters the number of words in an excerpt.
3279
		 *
3280
		 * @since 2.7.0
3281
		 *
3282
		 * @param int $number The number of words. Default 55.
3283
		 */
3284
		$excerpt_length = apply_filters( 'excerpt_length', 55 );
3285
		/**
3286
		 * Filters the string in the "more" link displayed after a trimmed excerpt.
3287
		 *
3288
		 * @since 2.9.0
3289
		 *
3290
		 * @param string $more_string The string shown within the more link.
3291
		 */
3292
		$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
3293
		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
3294
	}
3295
	/**
3296
	 * Filters the trimmed excerpt string.
3297
	 *
3298
	 * @since 2.8.0
3299
	 *
3300
	 * @param string $text        The trimmed text.
3301
	 * @param string $raw_excerpt The text prior to trimming.
3302
	 */
3303
	return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
3304
}
3305
3306
/**
3307
 * Trims text to a certain number of words.
3308
 *
3309
 * This function is localized. For languages that count 'words' by the individual
3310
 * character (such as East Asian languages), the $num_words argument will apply
3311
 * to the number of individual characters.
3312
 *
3313
 * @since 3.3.0
3314
 *
3315
 * @param string $text      Text to trim.
3316
 * @param int    $num_words Number of words. Default 55.
3317
 * @param string $more      Optional. What to append if $text needs to be trimmed. Default '&hellip;'.
3318
 * @return string Trimmed text.
3319
 */
3320
function wp_trim_words( $text, $num_words = 55, $more = null ) {
3321
	if ( null === $more ) {
3322
		$more = __( '&hellip;' );
3323
	}
3324
3325
	$original_text = $text;
3326
	$text = wp_strip_all_tags( $text );
3327
3328
	/*
3329
	 * translators: If your word count is based on single characters (e.g. East Asian characters),
3330
	 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
3331
	 * Do not translate into your own language.
3332
	 */
3333
	if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
3334
		$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
3335
		preg_match_all( '/./u', $text, $words_array );
3336
		$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
3337
		$sep = '';
3338
	} else {
3339
		$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
3340
		$sep = ' ';
3341
	}
3342
3343
	if ( count( $words_array ) > $num_words ) {
3344
		array_pop( $words_array );
3345
		$text = implode( $sep, $words_array );
3346
		$text = $text . $more;
3347
	} else {
3348
		$text = implode( $sep, $words_array );
3349
	}
3350
3351
	/**
3352
	 * Filters the text content after words have been trimmed.
3353
	 *
3354
	 * @since 3.3.0
3355
	 *
3356
	 * @param string $text          The trimmed text.
3357
	 * @param int    $num_words     The number of words to trim the text to. Default 5.
3358
	 * @param string $more          An optional string to append to the end of the trimmed text, e.g. &hellip;.
3359
	 * @param string $original_text The text before it was trimmed.
3360
	 */
3361
	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
3362
}
3363
3364
/**
3365
 * Converts named entities into numbered entities.
3366
 *
3367
 * @since 1.5.1
3368
 *
3369
 * @param string $text The text within which entities will be converted.
3370
 * @return string Text with converted entities.
3371
 */
3372
function ent2ncr( $text ) {
3373
3374
	/**
3375
	 * Filters text before named entities are converted into numbered entities.
3376
	 *
3377
	 * A non-null string must be returned for the filter to be evaluated.
3378
	 *
3379
	 * @since 3.3.0
3380
	 *
3381
	 * @param null   $converted_text The text to be converted. Default null.
3382
	 * @param string $text           The text prior to entity conversion.
3383
	 */
3384
	$filtered = apply_filters( 'pre_ent2ncr', null, $text );
3385
	if ( null !== $filtered )
3386
		return $filtered;
3387
3388
	$to_ncr = array(
3389
		'&quot;' => '&#34;',
3390
		'&amp;' => '&#38;',
3391
		'&lt;' => '&#60;',
3392
		'&gt;' => '&#62;',
3393
		'|' => '&#124;',
3394
		'&nbsp;' => '&#160;',
3395
		'&iexcl;' => '&#161;',
3396
		'&cent;' => '&#162;',
3397
		'&pound;' => '&#163;',
3398
		'&curren;' => '&#164;',
3399
		'&yen;' => '&#165;',
3400
		'&brvbar;' => '&#166;',
3401
		'&brkbar;' => '&#166;',
3402
		'&sect;' => '&#167;',
3403
		'&uml;' => '&#168;',
3404
		'&die;' => '&#168;',
3405
		'&copy;' => '&#169;',
3406
		'&ordf;' => '&#170;',
3407
		'&laquo;' => '&#171;',
3408
		'&not;' => '&#172;',
3409
		'&shy;' => '&#173;',
3410
		'&reg;' => '&#174;',
3411
		'&macr;' => '&#175;',
3412
		'&hibar;' => '&#175;',
3413
		'&deg;' => '&#176;',
3414
		'&plusmn;' => '&#177;',
3415
		'&sup2;' => '&#178;',
3416
		'&sup3;' => '&#179;',
3417
		'&acute;' => '&#180;',
3418
		'&micro;' => '&#181;',
3419
		'&para;' => '&#182;',
3420
		'&middot;' => '&#183;',
3421
		'&cedil;' => '&#184;',
3422
		'&sup1;' => '&#185;',
3423
		'&ordm;' => '&#186;',
3424
		'&raquo;' => '&#187;',
3425
		'&frac14;' => '&#188;',
3426
		'&frac12;' => '&#189;',
3427
		'&frac34;' => '&#190;',
3428
		'&iquest;' => '&#191;',
3429
		'&Agrave;' => '&#192;',
3430
		'&Aacute;' => '&#193;',
3431
		'&Acirc;' => '&#194;',
3432
		'&Atilde;' => '&#195;',
3433
		'&Auml;' => '&#196;',
3434
		'&Aring;' => '&#197;',
3435
		'&AElig;' => '&#198;',
3436
		'&Ccedil;' => '&#199;',
3437
		'&Egrave;' => '&#200;',
3438
		'&Eacute;' => '&#201;',
3439
		'&Ecirc;' => '&#202;',
3440
		'&Euml;' => '&#203;',
3441
		'&Igrave;' => '&#204;',
3442
		'&Iacute;' => '&#205;',
3443
		'&Icirc;' => '&#206;',
3444
		'&Iuml;' => '&#207;',
3445
		'&ETH;' => '&#208;',
3446
		'&Ntilde;' => '&#209;',
3447
		'&Ograve;' => '&#210;',
3448
		'&Oacute;' => '&#211;',
3449
		'&Ocirc;' => '&#212;',
3450
		'&Otilde;' => '&#213;',
3451
		'&Ouml;' => '&#214;',
3452
		'&times;' => '&#215;',
3453
		'&Oslash;' => '&#216;',
3454
		'&Ugrave;' => '&#217;',
3455
		'&Uacute;' => '&#218;',
3456
		'&Ucirc;' => '&#219;',
3457
		'&Uuml;' => '&#220;',
3458
		'&Yacute;' => '&#221;',
3459
		'&THORN;' => '&#222;',
3460
		'&szlig;' => '&#223;',
3461
		'&agrave;' => '&#224;',
3462
		'&aacute;' => '&#225;',
3463
		'&acirc;' => '&#226;',
3464
		'&atilde;' => '&#227;',
3465
		'&auml;' => '&#228;',
3466
		'&aring;' => '&#229;',
3467
		'&aelig;' => '&#230;',
3468
		'&ccedil;' => '&#231;',
3469
		'&egrave;' => '&#232;',
3470
		'&eacute;' => '&#233;',
3471
		'&ecirc;' => '&#234;',
3472
		'&euml;' => '&#235;',
3473
		'&igrave;' => '&#236;',
3474
		'&iacute;' => '&#237;',
3475
		'&icirc;' => '&#238;',
3476
		'&iuml;' => '&#239;',
3477
		'&eth;' => '&#240;',
3478
		'&ntilde;' => '&#241;',
3479
		'&ograve;' => '&#242;',
3480
		'&oacute;' => '&#243;',
3481
		'&ocirc;' => '&#244;',
3482
		'&otilde;' => '&#245;',
3483
		'&ouml;' => '&#246;',
3484
		'&divide;' => '&#247;',
3485
		'&oslash;' => '&#248;',
3486
		'&ugrave;' => '&#249;',
3487
		'&uacute;' => '&#250;',
3488
		'&ucirc;' => '&#251;',
3489
		'&uuml;' => '&#252;',
3490
		'&yacute;' => '&#253;',
3491
		'&thorn;' => '&#254;',
3492
		'&yuml;' => '&#255;',
3493
		'&OElig;' => '&#338;',
3494
		'&oelig;' => '&#339;',
3495
		'&Scaron;' => '&#352;',
3496
		'&scaron;' => '&#353;',
3497
		'&Yuml;' => '&#376;',
3498
		'&fnof;' => '&#402;',
3499
		'&circ;' => '&#710;',
3500
		'&tilde;' => '&#732;',
3501
		'&Alpha;' => '&#913;',
3502
		'&Beta;' => '&#914;',
3503
		'&Gamma;' => '&#915;',
3504
		'&Delta;' => '&#916;',
3505
		'&Epsilon;' => '&#917;',
3506
		'&Zeta;' => '&#918;',
3507
		'&Eta;' => '&#919;',
3508
		'&Theta;' => '&#920;',
3509
		'&Iota;' => '&#921;',
3510
		'&Kappa;' => '&#922;',
3511
		'&Lambda;' => '&#923;',
3512
		'&Mu;' => '&#924;',
3513
		'&Nu;' => '&#925;',
3514
		'&Xi;' => '&#926;',
3515
		'&Omicron;' => '&#927;',
3516
		'&Pi;' => '&#928;',
3517
		'&Rho;' => '&#929;',
3518
		'&Sigma;' => '&#931;',
3519
		'&Tau;' => '&#932;',
3520
		'&Upsilon;' => '&#933;',
3521
		'&Phi;' => '&#934;',
3522
		'&Chi;' => '&#935;',
3523
		'&Psi;' => '&#936;',
3524
		'&Omega;' => '&#937;',
3525
		'&alpha;' => '&#945;',
3526
		'&beta;' => '&#946;',
3527
		'&gamma;' => '&#947;',
3528
		'&delta;' => '&#948;',
3529
		'&epsilon;' => '&#949;',
3530
		'&zeta;' => '&#950;',
3531
		'&eta;' => '&#951;',
3532
		'&theta;' => '&#952;',
3533
		'&iota;' => '&#953;',
3534
		'&kappa;' => '&#954;',
3535
		'&lambda;' => '&#955;',
3536
		'&mu;' => '&#956;',
3537
		'&nu;' => '&#957;',
3538
		'&xi;' => '&#958;',
3539
		'&omicron;' => '&#959;',
3540
		'&pi;' => '&#960;',
3541
		'&rho;' => '&#961;',
3542
		'&sigmaf;' => '&#962;',
3543
		'&sigma;' => '&#963;',
3544
		'&tau;' => '&#964;',
3545
		'&upsilon;' => '&#965;',
3546
		'&phi;' => '&#966;',
3547
		'&chi;' => '&#967;',
3548
		'&psi;' => '&#968;',
3549
		'&omega;' => '&#969;',
3550
		'&thetasym;' => '&#977;',
3551
		'&upsih;' => '&#978;',
3552
		'&piv;' => '&#982;',
3553
		'&ensp;' => '&#8194;',
3554
		'&emsp;' => '&#8195;',
3555
		'&thinsp;' => '&#8201;',
3556
		'&zwnj;' => '&#8204;',
3557
		'&zwj;' => '&#8205;',
3558
		'&lrm;' => '&#8206;',
3559
		'&rlm;' => '&#8207;',
3560
		'&ndash;' => '&#8211;',
3561
		'&mdash;' => '&#8212;',
3562
		'&lsquo;' => '&#8216;',
3563
		'&rsquo;' => '&#8217;',
3564
		'&sbquo;' => '&#8218;',
3565
		'&ldquo;' => '&#8220;',
3566
		'&rdquo;' => '&#8221;',
3567
		'&bdquo;' => '&#8222;',
3568
		'&dagger;' => '&#8224;',
3569
		'&Dagger;' => '&#8225;',
3570
		'&bull;' => '&#8226;',
3571
		'&hellip;' => '&#8230;',
3572
		'&permil;' => '&#8240;',
3573
		'&prime;' => '&#8242;',
3574
		'&Prime;' => '&#8243;',
3575
		'&lsaquo;' => '&#8249;',
3576
		'&rsaquo;' => '&#8250;',
3577
		'&oline;' => '&#8254;',
3578
		'&frasl;' => '&#8260;',
3579
		'&euro;' => '&#8364;',
3580
		'&image;' => '&#8465;',
3581
		'&weierp;' => '&#8472;',
3582
		'&real;' => '&#8476;',
3583
		'&trade;' => '&#8482;',
3584
		'&alefsym;' => '&#8501;',
3585
		'&crarr;' => '&#8629;',
3586
		'&lArr;' => '&#8656;',
3587
		'&uArr;' => '&#8657;',
3588
		'&rArr;' => '&#8658;',
3589
		'&dArr;' => '&#8659;',
3590
		'&hArr;' => '&#8660;',
3591
		'&forall;' => '&#8704;',
3592
		'&part;' => '&#8706;',
3593
		'&exist;' => '&#8707;',
3594
		'&empty;' => '&#8709;',
3595
		'&nabla;' => '&#8711;',
3596
		'&isin;' => '&#8712;',
3597
		'&notin;' => '&#8713;',
3598
		'&ni;' => '&#8715;',
3599
		'&prod;' => '&#8719;',
3600
		'&sum;' => '&#8721;',
3601
		'&minus;' => '&#8722;',
3602
		'&lowast;' => '&#8727;',
3603
		'&radic;' => '&#8730;',
3604
		'&prop;' => '&#8733;',
3605
		'&infin;' => '&#8734;',
3606
		'&ang;' => '&#8736;',
3607
		'&and;' => '&#8743;',
3608
		'&or;' => '&#8744;',
3609
		'&cap;' => '&#8745;',
3610
		'&cup;' => '&#8746;',
3611
		'&int;' => '&#8747;',
3612
		'&there4;' => '&#8756;',
3613
		'&sim;' => '&#8764;',
3614
		'&cong;' => '&#8773;',
3615
		'&asymp;' => '&#8776;',
3616
		'&ne;' => '&#8800;',
3617
		'&equiv;' => '&#8801;',
3618
		'&le;' => '&#8804;',
3619
		'&ge;' => '&#8805;',
3620
		'&sub;' => '&#8834;',
3621
		'&sup;' => '&#8835;',
3622
		'&nsub;' => '&#8836;',
3623
		'&sube;' => '&#8838;',
3624
		'&supe;' => '&#8839;',
3625
		'&oplus;' => '&#8853;',
3626
		'&otimes;' => '&#8855;',
3627
		'&perp;' => '&#8869;',
3628
		'&sdot;' => '&#8901;',
3629
		'&lceil;' => '&#8968;',
3630
		'&rceil;' => '&#8969;',
3631
		'&lfloor;' => '&#8970;',
3632
		'&rfloor;' => '&#8971;',
3633
		'&lang;' => '&#9001;',
3634
		'&rang;' => '&#9002;',
3635
		'&larr;' => '&#8592;',
3636
		'&uarr;' => '&#8593;',
3637
		'&rarr;' => '&#8594;',
3638
		'&darr;' => '&#8595;',
3639
		'&harr;' => '&#8596;',
3640
		'&loz;' => '&#9674;',
3641
		'&spades;' => '&#9824;',
3642
		'&clubs;' => '&#9827;',
3643
		'&hearts;' => '&#9829;',
3644
		'&diams;' => '&#9830;'
3645
	);
3646
3647
	return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
3648
}
3649
3650
/**
3651
 * Formats text for the editor.
3652
 *
3653
 * Generally the browsers treat everything inside a textarea as text, but
3654
 * it is still a good idea to HTML entity encode `<`, `>` and `&` in the content.
3655
 *
3656
 * The filter {@see 'format_for_editor'} is applied here. If `$text` is empty the
3657
 * filter will be applied to an empty string.
3658
 *
3659
 * @since 4.3.0
3660
 *
3661
 * @see _WP_Editors::editor()
3662
 *
3663
 * @param string $text           The text to be formatted.
3664
 * @param string $default_editor The default editor for the current user.
3665
 *                               It is usually either 'html' or 'tinymce'.
3666
 * @return string The formatted text after filter is applied.
3667
 */
3668
function format_for_editor( $text, $default_editor = null ) {
3669
	if ( $text ) {
3670
		$text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) );
3671
	}
3672
3673
	/**
3674
	 * Filters the text after it is formatted for the editor.
3675
	 *
3676
	 * @since 4.3.0
3677
	 *
3678
	 * @param string $text           The formatted text.
3679
	 * @param string $default_editor The default editor for the current user.
3680
	 *                               It is usually either 'html' or 'tinymce'.
3681
	 */
3682
	return apply_filters( 'format_for_editor', $text, $default_editor );
3683
}
3684
3685
/**
3686
 * Perform a deep string replace operation to ensure the values in $search are no longer present
3687
 *
3688
 * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
3689
 * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
3690
 * str_replace would return
3691
 *
3692
 * @since 2.8.1
3693
 * @access private
3694
 *
3695
 * @param string|array $search  The value being searched for, otherwise known as the needle.
3696
 *                              An array may be used to designate multiple needles.
3697
 * @param string       $subject The string being searched and replaced on, otherwise known as the haystack.
3698
 * @return string The string with the replaced svalues.
3699
 */
3700
function _deep_replace( $search, $subject ) {
3701
	$subject = (string) $subject;
3702
3703
	$count = 1;
3704
	while ( $count ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
3705
		$subject = str_replace( $search, '', $subject, $count );
3706
	}
3707
3708
	return $subject;
3709
}
3710
3711
/**
3712
 * Escapes data for use in a MySQL query.
3713
 *
3714
 * Usually you should prepare queries using wpdb::prepare().
3715
 * Sometimes, spot-escaping is required or useful. One example
3716
 * is preparing an array for use in an IN clause.
3717
 *
3718
 * @since 2.8.0
3719
 *
3720
 * @global wpdb $wpdb WordPress database abstraction object.
3721
 *
3722
 * @param string|array $data Unescaped data
3723
 * @return string|array Escaped data
3724
 */
3725
function esc_sql( $data ) {
3726
	global $wpdb;
3727
	return $wpdb->_escape( $data );
3728
}
3729
3730
/**
3731
 * Checks and cleans a URL.
3732
 *
3733
 * A number of characters are removed from the URL. If the URL is for displaying
3734
 * (the default behaviour) ampersands are also replaced. The {@see 'clean_url'} filter
3735
 * is applied to the returned cleaned URL.
3736
 *
3737
 * @since 2.8.0
3738
 *
3739
 * @param string $url       The URL to be cleaned.
3740
 * @param array  $protocols Optional. An array of acceptable protocols.
3741
 *		                    Defaults to return value of wp_allowed_protocols()
3742
 * @param string $_context  Private. Use esc_url_raw() for database usage.
3743
 * @return string The cleaned $url after the {@see 'clean_url'} filter is applied.
3744
 */
3745
function esc_url( $url, $protocols = null, $_context = 'display' ) {
3746
	$original_url = $url;
3747
3748
	if ( '' == $url )
3749
		return $url;
3750
3751
	$url = str_replace( ' ', '%20', $url );
3752
	$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url);
3753
3754
	if ( '' === $url ) {
3755
		return $url;
3756
	}
3757
3758
	if ( 0 !== stripos( $url, 'mailto:' ) ) {
3759
		$strip = array('%0d', '%0a', '%0D', '%0A');
3760
		$url = _deep_replace($strip, $url);
3761
	}
3762
3763
	$url = str_replace(';//', '://', $url);
3764
	/* If the URL doesn't appear to contain a scheme, we
3765
	 * presume it needs http:// prepended (unless a relative
3766
	 * link starting with /, # or ? or a php file).
3767
	 */
3768
	if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
3769
		! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
3770
		$url = 'http://' . $url;
3771
3772
	// Replace ampersands and single quotes only when displaying.
3773
	if ( 'display' == $_context ) {
3774
		$url = wp_kses_normalize_entities( $url );
3775
		$url = str_replace( '&amp;', '&#038;', $url );
3776
		$url = str_replace( "'", '&#039;', $url );
3777
	}
3778
3779
	if ( ( false !== strpos( $url, '[' ) ) || ( false !== strpos( $url, ']' ) ) ) {
3780
3781
		$parsed = wp_parse_url( $url );
3782
		$front  = '';
3783
3784
		if ( isset( $parsed['scheme'] ) ) {
3785
			$front .= $parsed['scheme'] . '://';
3786
		} elseif ( '/' === $url[0] ) {
3787
			$front .= '//';
3788
		}
3789
3790
		if ( isset( $parsed['user'] ) ) {
3791
			$front .= $parsed['user'];
3792
		}
3793
3794
		if ( isset( $parsed['pass'] ) ) {
3795
			$front .= ':' . $parsed['pass'];
3796
		}
3797
3798
		if ( isset( $parsed['user'] ) || isset( $parsed['pass'] ) ) {
3799
			$front .= '@';
3800
		}
3801
3802
		if ( isset( $parsed['host'] ) ) {
3803
			$front .= $parsed['host'];
3804
		}
3805
3806
		if ( isset( $parsed['port'] ) ) {
3807
			$front .= ':' . $parsed['port'];
3808
		}
3809
3810
		$end_dirty = str_replace( $front, '', $url );
3811
		$end_clean = str_replace( array( '[', ']' ), array( '%5B', '%5D' ), $end_dirty );
3812
		$url       = str_replace( $end_dirty, $end_clean, $url );
3813
3814
	}
3815
3816
	if ( '/' === $url[0] ) {
3817
		$good_protocol_url = $url;
3818
	} else {
3819
		if ( ! is_array( $protocols ) )
3820
			$protocols = wp_allowed_protocols();
3821
		$good_protocol_url = wp_kses_bad_protocol( $url, $protocols );
3822
		if ( strtolower( $good_protocol_url ) != strtolower( $url ) )
3823
			return '';
3824
	}
3825
3826
	/**
3827
	 * Filters a string cleaned and escaped for output as a URL.
3828
	 *
3829
	 * @since 2.3.0
3830
	 *
3831
	 * @param string $good_protocol_url The cleaned URL to be returned.
3832
	 * @param string $original_url      The URL prior to cleaning.
3833
	 * @param string $_context          If 'display', replace ampersands and single quotes only.
3834
	 */
3835
	return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context );
3836
}
3837
3838
/**
3839
 * Performs esc_url() for database usage.
3840
 *
3841
 * @since 2.8.0
3842
 *
3843
 * @param string $url       The URL to be cleaned.
3844
 * @param array  $protocols An array of acceptable protocols.
3845
 * @return string The cleaned URL.
3846
 */
3847
function esc_url_raw( $url, $protocols = null ) {
3848
	return esc_url( $url, $protocols, 'db' );
3849
}
3850
3851
/**
3852
 * Convert entities, while preserving already-encoded entities.
3853
 *
3854
 * @link https://secure.php.net/htmlentities Borrowed from the PHP Manual user notes.
3855
 *
3856
 * @since 1.2.2
3857
 *
3858
 * @param string $myHTML The text to be converted.
3859
 * @return string Converted text.
3860
 */
3861
function htmlentities2( $myHTML ) {
3862
	$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
3863
	$translation_table[chr(38)] = '&';
3864
	return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
3865
}
3866
3867
/**
3868
 * Escape single quotes, htmlspecialchar " < > &, and fix line endings.
3869
 *
3870
 * Escapes text strings for echoing in JS. It is intended to be used for inline JS
3871
 * (in a tag attribute, for example onclick="..."). Note that the strings have to
3872
 * be in single quotes. The {@see 'js_escape'} filter is also applied here.
3873
 *
3874
 * @since 2.8.0
3875
 *
3876
 * @param string $text The text to be escaped.
3877
 * @return string Escaped text.
3878
 */
3879
function esc_js( $text ) {
3880
	$safe_text = wp_check_invalid_utf8( $text );
3881
	$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
3882
	$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
3883
	$safe_text = str_replace( "\r", '', $safe_text );
3884
	$safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
3885
	/**
3886
	 * Filters a string cleaned and escaped for output in JavaScript.
3887
	 *
3888
	 * Text passed to esc_js() is stripped of invalid or special characters,
3889
	 * and properly slashed for output.
3890
	 *
3891
	 * @since 2.0.6
3892
	 *
3893
	 * @param string $safe_text The text after it has been escaped.
3894
 	 * @param string $text      The text prior to being escaped.
3895
	 */
3896
	return apply_filters( 'js_escape', $safe_text, $text );
3897
}
3898
3899
/**
3900
 * Escaping for HTML blocks.
3901
 *
3902
 * @since 2.8.0
3903
 *
3904
 * @param string $text
3905
 * @return string
3906
 */
3907 View Code Duplication
function esc_html( $text ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3908
	$safe_text = wp_check_invalid_utf8( $text );
3909
	$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3910
	/**
3911
	 * Filters a string cleaned and escaped for output in HTML.
3912
	 *
3913
	 * Text passed to esc_html() is stripped of invalid or special characters
3914
	 * before output.
3915
	 *
3916
	 * @since 2.8.0
3917
	 *
3918
	 * @param string $safe_text The text after it has been escaped.
3919
 	 * @param string $text      The text prior to being escaped.
3920
	 */
3921
	return apply_filters( 'esc_html', $safe_text, $text );
3922
}
3923
3924
/**
3925
 * Escaping for HTML attributes.
3926
 *
3927
 * @since 2.8.0
3928
 *
3929
 * @param string $text
3930
 * @return string
3931
 */
3932 View Code Duplication
function esc_attr( $text ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3933
	$safe_text = wp_check_invalid_utf8( $text );
3934
	$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3935
	/**
3936
	 * Filters a string cleaned and escaped for output in an HTML attribute.
3937
	 *
3938
	 * Text passed to esc_attr() is stripped of invalid or special characters
3939
	 * before output.
3940
	 *
3941
	 * @since 2.0.6
3942
	 *
3943
	 * @param string $safe_text The text after it has been escaped.
3944
 	 * @param string $text      The text prior to being escaped.
3945
	 */
3946
	return apply_filters( 'attribute_escape', $safe_text, $text );
3947
}
3948
3949
/**
3950
 * Escaping for textarea values.
3951
 *
3952
 * @since 3.1.0
3953
 *
3954
 * @param string $text
3955
 * @return string
3956
 */
3957
function esc_textarea( $text ) {
3958
	$safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
3959
	/**
3960
	 * Filters a string cleaned and escaped for output in a textarea element.
3961
	 *
3962
	 * @since 3.1.0
3963
	 *
3964
	 * @param string $safe_text The text after it has been escaped.
3965
 	 * @param string $text      The text prior to being escaped.
3966
	 */
3967
	return apply_filters( 'esc_textarea', $safe_text, $text );
3968
}
3969
3970
/**
3971
 * Escape an HTML tag name.
3972
 *
3973
 * @since 2.5.0
3974
 *
3975
 * @param string $tag_name
3976
 * @return string
3977
 */
3978
function tag_escape( $tag_name ) {
3979
	$safe_tag = strtolower( preg_replace('/[^a-zA-Z0-9_:]/', '', $tag_name) );
3980
	/**
3981
	 * Filters a string cleaned and escaped for output as an HTML tag.
3982
	 *
3983
	 * @since 2.8.0
3984
	 *
3985
	 * @param string $safe_tag The tag name after it has been escaped.
3986
 	 * @param string $tag_name The text before it was escaped.
3987
	 */
3988
	return apply_filters( 'tag_escape', $safe_tag, $tag_name );
3989
}
3990
3991
/**
3992
 * Convert full URL paths to absolute paths.
3993
 *
3994
 * Removes the http or https protocols and the domain. Keeps the path '/' at the
3995
 * beginning, so it isn't a true relative link, but from the web root base.
3996
 *
3997
 * @since 2.1.0
3998
 * @since 4.1.0 Support was added for relative URLs.
3999
 *
4000
 * @param string $link Full URL path.
4001
 * @return string Absolute path.
4002
 */
4003
function wp_make_link_relative( $link ) {
4004
	return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link );
4005
}
4006
4007
/**
4008
 * Sanitises various option values based on the nature of the option.
4009
 *
4010
 * This is basically a switch statement which will pass $value through a number
4011
 * of functions depending on the $option.
4012
 *
4013
 * @since 2.0.5
4014
 *
4015
 * @global wpdb $wpdb WordPress database abstraction object.
4016
 *
4017
 * @param string $option The name of the option.
4018
 * @param string $value  The unsanitised value.
4019
 * @return string Sanitized value.
4020
 */
4021
function sanitize_option( $option, $value ) {
4022
	global $wpdb;
4023
4024
	$original_value = $value;
4025
	$error = '';
4026
4027
	switch ( $option ) {
4028
		case 'admin_email' :
4029 View Code Duplication
		case 'new_admin_email' :
4030
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4031
			if ( is_wp_error( $value ) ) {
4032
				$error = $value->get_error_message();
4033
			} else {
4034
				$value = sanitize_email( $value );
4035
				if ( ! is_email( $value ) ) {
4036
					$error = __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' );
4037
				}
4038
			}
4039
			break;
4040
4041
		case 'thumbnail_size_w':
4042
		case 'thumbnail_size_h':
4043
		case 'medium_size_w':
4044
		case 'medium_size_h':
4045
		case 'medium_large_size_w':
4046
		case 'medium_large_size_h':
4047
		case 'large_size_w':
4048
		case 'large_size_h':
4049
		case 'mailserver_port':
4050
		case 'comment_max_links':
4051
		case 'page_on_front':
4052
		case 'page_for_posts':
4053
		case 'rss_excerpt_length':
4054
		case 'default_category':
4055
		case 'default_email_category':
4056
		case 'default_link_category':
4057
		case 'close_comments_days_old':
4058
		case 'comments_per_page':
4059
		case 'thread_comments_depth':
4060
		case 'users_can_register':
4061
		case 'start_of_week':
4062
		case 'site_icon':
4063
			$value = absint( $value );
4064
			break;
4065
4066
		case 'posts_per_page':
4067
		case 'posts_per_rss':
4068
			$value = (int) $value;
4069
			if ( empty($value) )
4070
				$value = 1;
4071
			if ( $value < -1 )
4072
				$value = abs($value);
4073
			break;
4074
4075
		case 'default_ping_status':
4076
		case 'default_comment_status':
4077
			// Options that if not there have 0 value but need to be something like "closed"
4078
			if ( $value == '0' || $value == '')
4079
				$value = 'closed';
4080
			break;
4081
4082
		case 'blogdescription':
4083
		case 'blogname':
4084
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4085
			if ( $value !== $original_value ) {
4086
				$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', wp_encode_emoji( $original_value ) );
4087
			}
4088
4089
			if ( is_wp_error( $value ) ) {
4090
				$error = $value->get_error_message();
4091
			} else {
4092
				$value = esc_html( $value );
4093
			}
4094
			break;
4095
4096
		case 'blog_charset':
4097
			$value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
4098
			break;
4099
4100
		case 'blog_public':
4101
			// This is the value if the settings checkbox is not checked on POST. Don't rely on this.
4102
			if ( null === $value )
4103
				$value = 1;
4104
			else
4105
				$value = intval( $value );
4106
			break;
4107
4108
		case 'date_format':
4109
		case 'time_format':
4110
		case 'mailserver_url':
4111
		case 'mailserver_login':
4112
		case 'mailserver_pass':
4113
		case 'upload_path':
4114
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4115
			if ( is_wp_error( $value ) ) {
4116
				$error = $value->get_error_message();
4117
			} else {
4118
				$value = strip_tags( $value );
4119
				$value = wp_kses_data( $value );
4120
			}
4121
			break;
4122
4123
		case 'ping_sites':
4124
			$value = explode( "\n", $value );
4125
			$value = array_filter( array_map( 'trim', $value ) );
4126
			$value = array_filter( array_map( 'esc_url_raw', $value ) );
4127
			$value = implode( "\n", $value );
4128
			break;
4129
4130
		case 'gmt_offset':
4131
			$value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
4132
			break;
4133
4134 View Code Duplication
		case 'siteurl':
4135
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4136
			if ( is_wp_error( $value ) ) {
4137
				$error = $value->get_error_message();
4138
			} else {
4139
				if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
4140
					$value = esc_url_raw( $value );
4141
				} else {
4142
					$error = __( 'The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.' );
4143
				}
4144
			}
4145
			break;
4146
4147 View Code Duplication
		case 'home':
4148
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4149
			if ( is_wp_error( $value ) ) {
4150
				$error = $value->get_error_message();
4151
			} else {
4152
				if ( preg_match( '#http(s?)://(.+)#i', $value ) ) {
4153
					$value = esc_url_raw( $value );
4154
				} else {
4155
					$error = __( 'The Site address you entered did not appear to be a valid URL. Please enter a valid URL.' );
4156
				}
4157
			}
4158
			break;
4159
4160
		case 'WPLANG':
4161
			$allowed = get_available_languages();
4162 View Code Duplication
			if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG ) {
4163
				$allowed[] = WPLANG;
4164
			}
4165
			if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) {
4166
				$value = get_option( $option );
4167
			}
4168
			break;
4169
4170
		case 'illegal_names':
4171
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4172
			if ( is_wp_error( $value ) ) {
4173
				$error = $value->get_error_message();
4174
			} else {
4175
				if ( ! is_array( $value ) )
4176
					$value = explode( ' ', $value );
4177
4178
				$value = array_values( array_filter( array_map( 'trim', $value ) ) );
4179
4180
				if ( ! $value )
4181
					$value = '';
4182
			}
4183
			break;
4184
4185
		case 'limited_email_domains':
4186
		case 'banned_email_domains':
4187
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4188
			if ( is_wp_error( $value ) ) {
4189
				$error = $value->get_error_message();
4190
			} else {
4191
				if ( ! is_array( $value ) )
4192
					$value = explode( "\n", $value );
4193
4194
				$domains = array_values( array_filter( array_map( 'trim', $value ) ) );
4195
				$value = array();
4196
4197
				foreach ( $domains as $domain ) {
4198
					if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) ) {
4199
						$value[] = $domain;
4200
					}
4201
				}
4202
				if ( ! $value )
4203
					$value = '';
4204
			}
4205
			break;
4206
4207
		case 'timezone_string':
4208
			$allowed_zones = timezone_identifiers_list();
4209
			if ( ! in_array( $value, $allowed_zones ) && ! empty( $value ) ) {
4210
				$error = __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
4211
			}
4212
			break;
4213
4214
		case 'permalink_structure':
4215
		case 'category_base':
4216
		case 'tag_base':
4217
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4218
			if ( is_wp_error( $value ) ) {
4219
				$error = $value->get_error_message();
4220
			} else {
4221
				$value = esc_url_raw( $value );
4222
				$value = str_replace( 'http://', '', $value );
4223
			}
4224
4225
			if ( 'permalink_structure' === $option && '' !== $value && ! preg_match( '/%[^\/%]+%/', $value ) ) {
4226
				$error = sprintf(
4227
					/* translators: %s: Codex URL */
4228
					__( 'A structure tag is required when using custom permalinks. <a href="%s">Learn more</a>' ),
4229
					__( 'https://codex.wordpress.org/Using_Permalinks#Choosing_your_permalink_structure' )
4230
				);
4231
			}
4232
			break;
4233
4234
		case 'default_role' :
4235
			if ( ! get_role( $value ) && get_role( 'subscriber' ) )
4236
				$value = 'subscriber';
4237
			break;
4238
4239
		case 'moderation_keys':
4240
		case 'blacklist_keys':
4241
			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
4242
			if ( is_wp_error( $value ) ) {
4243
				$error = $value->get_error_message();
4244
			} else {
4245
				$value = explode( "\n", $value );
4246
				$value = array_filter( array_map( 'trim', $value ) );
4247
				$value = array_unique( $value );
4248
				$value = implode( "\n", $value );
4249
			}
4250
			break;
4251
	}
4252
4253
	if ( ! empty( $error ) ) {
4254
		$value = get_option( $option );
4255
		if ( function_exists( 'add_settings_error' ) ) {
4256
			add_settings_error( $option, "invalid_{$option}", $error );
4257
		}
4258
	}
4259
4260
	/**
4261
	 * Filters an option value following sanitization.
4262
	 *
4263
	 * @since 2.3.0
4264
	 * @since 4.3.0 Added the `$original_value` parameter.
4265
	 *
4266
	 * @param string $value          The sanitized option value.
4267
	 * @param string $option         The option name.
4268
	 * @param string $original_value The original value passed to the function.
4269
	 */
4270
	return apply_filters( "sanitize_option_{$option}", $value, $option, $original_value );
4271
}
4272
4273
/**
4274
 * Maps a function to all non-iterable elements of an array or an object.
4275
 *
4276
 * This is similar to `array_walk_recursive()` but acts upon objects too.
4277
 *
4278
 * @since 4.4.0
4279
 *
4280
 * @param mixed    $value    The array, object, or scalar.
4281
 * @param callable $callback The function to map onto $value.
4282
 * @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
4283
 */
4284
function map_deep( $value, $callback ) {
4285
	if ( is_array( $value ) ) {
4286
		foreach ( $value as $index => $item ) {
4287
			$value[ $index ] = map_deep( $item, $callback );
4288
		}
4289
	} elseif ( is_object( $value ) ) {
4290
		$object_vars = get_object_vars( $value );
4291
		foreach ( $object_vars as $property_name => $property_value ) {
4292
			$value->$property_name = map_deep( $property_value, $callback );
4293
		}
4294
	} else {
4295
		$value = call_user_func( $callback, $value );
4296
	}
4297
4298
	return $value;
4299
}
4300
4301
/**
4302
 * Parses a string into variables to be stored in an array.
4303
 *
4304
 * Uses {@link https://secure.php.net/parse_str parse_str()} and stripslashes if
4305
 * {@link https://secure.php.net/magic_quotes magic_quotes_gpc} is on.
4306
 *
4307
 * @since 2.2.1
4308
 *
4309
 * @param string $string The string to be parsed.
4310
 * @param array  $array  Variables will be stored in this array.
4311
 */
4312
function wp_parse_str( $string, &$array ) {
4313
	parse_str( $string, $array );
4314
	if ( get_magic_quotes_gpc() )
4315
		$array = stripslashes_deep( $array );
4316
	/**
4317
	 * Filters the array of variables derived from a parsed string.
4318
	 *
4319
	 * @since 2.3.0
4320
	 *
4321
	 * @param array $array The array populated with variables.
4322
	 */
4323
	$array = apply_filters( 'wp_parse_str', $array );
4324
}
4325
4326
/**
4327
 * Convert lone less than signs.
4328
 *
4329
 * KSES already converts lone greater than signs.
4330
 *
4331
 * @since 2.3.0
4332
 *
4333
 * @param string $text Text to be converted.
4334
 * @return string Converted text.
4335
 */
4336
function wp_pre_kses_less_than( $text ) {
4337
	return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
4338
}
4339
4340
/**
4341
 * Callback function used by preg_replace.
4342
 *
4343
 * @since 2.3.0
4344
 *
4345
 * @param array $matches Populated by matches to preg_replace.
4346
 * @return string The text returned after esc_html if needed.
4347
 */
4348
function wp_pre_kses_less_than_callback( $matches ) {
4349
	if ( false === strpos($matches[0], '>') )
4350
		return esc_html($matches[0]);
4351
	return $matches[0];
4352
}
4353
4354
/**
4355
 * WordPress implementation of PHP sprintf() with filters.
4356
 *
4357
 * @since 2.5.0
4358
 * @link https://secure.php.net/sprintf
4359
 *
4360
 * @param string $pattern   The string which formatted args are inserted.
4361
 * @param mixed  $args ,... Arguments to be formatted into the $pattern string.
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
4362
 * @return string The formatted string.
4363
 */
4364
function wp_sprintf( $pattern ) {
4365
	$args = func_get_args();
4366
	$len = strlen($pattern);
4367
	$start = 0;
4368
	$result = '';
4369
	$arg_index = 0;
4370
	while ( $len > $start ) {
4371
		// Last character: append and break
4372
		if ( strlen($pattern) - 1 == $start ) {
4373
			$result .= substr($pattern, -1);
4374
			break;
4375
		}
4376
4377
		// Literal %: append and continue
4378
		if ( substr($pattern, $start, 2) == '%%' ) {
4379
			$start += 2;
4380
			$result .= '%';
4381
			continue;
4382
		}
4383
4384
		// Get fragment before next %
4385
		$end = strpos($pattern, '%', $start + 1);
4386
		if ( false === $end )
4387
			$end = $len;
4388
		$fragment = substr($pattern, $start, $end - $start);
4389
4390
		// Fragment has a specifier
4391
		if ( $pattern[$start] == '%' ) {
4392
			// Find numbered arguments or take the next one in order
4393
			if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
4394
				$arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
4395
				$fragment = str_replace("%{$matches[1]}$", '%', $fragment);
4396
			} else {
4397
				++$arg_index;
4398
				$arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
4399
			}
4400
4401
			/**
4402
			 * Filters a fragment from the pattern passed to wp_sprintf().
4403
			 *
4404
			 * If the fragment is unchanged, then sprintf() will be run on the fragment.
4405
			 *
4406
			 * @since 2.5.0
4407
			 *
4408
			 * @param string $fragment A fragment from the pattern.
4409
			 * @param string $arg      The argument.
4410
			 */
4411
			$_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
4412
			if ( $_fragment != $fragment )
4413
				$fragment = $_fragment;
4414
			else
4415
				$fragment = sprintf($fragment, strval($arg) );
4416
		}
4417
4418
		// Append to result and move to next fragment
4419
		$result .= $fragment;
4420
		$start = $end;
4421
	}
4422
	return $result;
4423
}
4424
4425
/**
4426
 * Localize list items before the rest of the content.
4427
 *
4428
 * The '%l' must be at the first characters can then contain the rest of the
4429
 * content. The list items will have ', ', ', and', and ' and ' added depending
4430
 * on the amount of list items in the $args parameter.
4431
 *
4432
 * @since 2.5.0
4433
 *
4434
 * @param string $pattern Content containing '%l' at the beginning.
4435
 * @param array  $args    List items to prepend to the content and replace '%l'.
4436
 * @return string Localized list items and rest of the content.
4437
 */
4438
function wp_sprintf_l( $pattern, $args ) {
4439
	// Not a match
4440
	if ( substr($pattern, 0, 2) != '%l' )
4441
		return $pattern;
4442
4443
	// Nothing to work with
4444
	if ( empty($args) )
4445
		return '';
4446
4447
	/**
4448
	 * Filters the translated delimiters used by wp_sprintf_l().
4449
	 * Placeholders (%s) are included to assist translators and then
4450
	 * removed before the array of strings reaches the filter.
4451
	 *
4452
	 * Please note: Ampersands and entities should be avoided here.
4453
	 *
4454
	 * @since 2.5.0
4455
	 *
4456
	 * @param array $delimiters An array of translated delimiters.
4457
	 */
4458
	$l = apply_filters( 'wp_sprintf_l', array(
4459
		/* translators: used to join items in a list with more than 2 items */
4460
		'between'          => sprintf( __('%s, %s'), '', '' ),
4461
		/* translators: used to join last two items in a list with more than 2 times */
4462
		'between_last_two' => sprintf( __('%s, and %s'), '', '' ),
4463
		/* translators: used to join items in a list with only 2 items */
4464
		'between_only_two' => sprintf( __('%s and %s'), '', '' ),
4465
	) );
4466
4467
	$args = (array) $args;
4468
	$result = array_shift($args);
4469
	if ( count($args) == 1 )
4470
		$result .= $l['between_only_two'] . array_shift($args);
4471
	// Loop when more than two args
4472
	$i = count($args);
4473
	while ( $i ) {
4474
		$arg = array_shift($args);
4475
		$i--;
4476
		if ( 0 == $i )
4477
			$result .= $l['between_last_two'] . $arg;
4478
		else
4479
			$result .= $l['between'] . $arg;
4480
	}
4481
	return $result . substr($pattern, 2);
4482
}
4483
4484
/**
4485
 * Safely extracts not more than the first $count characters from html string.
4486
 *
4487
 * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
4488
 * be counted as one character. For example &amp; will be counted as 4, &lt; as
4489
 * 3, etc.
4490
 *
4491
 * @since 2.5.0
4492
 *
4493
 * @param string $str   String to get the excerpt from.
4494
 * @param int    $count Maximum number of characters to take.
4495
 * @param string $more  Optional. What to append if $str needs to be trimmed. Defaults to empty string.
4496
 * @return string The excerpt.
4497
 */
4498
function wp_html_excerpt( $str, $count, $more = null ) {
4499
	if ( null === $more )
4500
		$more = '';
4501
	$str = wp_strip_all_tags( $str, true );
4502
	$excerpt = mb_substr( $str, 0, $count );
4503
	// remove part of an entity at the end
4504
	$excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt );
4505
	if ( $str != $excerpt )
4506
		$excerpt = trim( $excerpt ) . $more;
4507
	return $excerpt;
4508
}
4509
4510
/**
4511
 * Add a Base url to relative links in passed content.
4512
 *
4513
 * By default it supports the 'src' and 'href' attributes. However this can be
4514
 * changed via the 3rd param.
4515
 *
4516
 * @since 2.7.0
4517
 *
4518
 * @global string $_links_add_base
4519
 *
4520
 * @param string $content String to search for links in.
4521
 * @param string $base    The base URL to prefix to links.
4522
 * @param array  $attrs   The attributes which should be processed.
4523
 * @return string The processed content.
4524
 */
4525
function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
4526
	global $_links_add_base;
4527
	$_links_add_base = $base;
4528
	$attrs = implode('|', (array)$attrs);
4529
	return preg_replace_callback( "!($attrs)=(['\"])(.+?)\\2!i", '_links_add_base', $content );
4530
}
4531
4532
/**
4533
 * Callback to add a base url to relative links in passed content.
4534
 *
4535
 * @since 2.7.0
4536
 * @access private
4537
 *
4538
 * @global string $_links_add_base
4539
 *
4540
 * @param string $m The matched link.
4541
 * @return string The processed link.
4542
 */
4543
function _links_add_base( $m ) {
4544
	global $_links_add_base;
4545
	//1 = attribute name  2 = quotation mark  3 = URL
4546
	return $m[1] . '=' . $m[2] .
4547
		( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ?
4548
			$m[3] :
4549
			WP_Http::make_absolute_url( $m[3], $_links_add_base )
4550
		)
4551
		. $m[2];
4552
}
4553
4554
/**
4555
 * Adds a Target attribute to all links in passed content.
4556
 *
4557
 * This function by default only applies to `<a>` tags, however this can be
4558
 * modified by the 3rd param.
4559
 *
4560
 * *NOTE:* Any current target attributed will be stripped and replaced.
4561
 *
4562
 * @since 2.7.0
4563
 *
4564
 * @global string $_links_add_target
4565
 *
4566
 * @param string $content String to search for links in.
4567
 * @param string $target  The Target to add to the links.
4568
 * @param array  $tags    An array of tags to apply to.
4569
 * @return string The processed content.
4570
 */
4571
function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
4572
	global $_links_add_target;
4573
	$_links_add_target = $target;
4574
	$tags = implode('|', (array)$tags);
4575
	return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
4576
}
4577
4578
/**
4579
 * Callback to add a target attribute to all links in passed content.
4580
 *
4581
 * @since 2.7.0
4582
 * @access private
4583
 *
4584
 * @global string $_links_add_target
4585
 *
4586
 * @param string $m The matched link.
4587
 * @return string The processed link.
4588
 */
4589
function _links_add_target( $m ) {
4590
	global $_links_add_target;
4591
	$tag = $m[1];
4592
	$link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]);
4593
	return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
4594
}
4595
4596
/**
4597
 * Normalize EOL characters and strip duplicate whitespace.
4598
 *
4599
 * @since 2.7.0
4600
 *
4601
 * @param string $str The string to normalize.
4602
 * @return string The normalized string.
4603
 */
4604
function normalize_whitespace( $str ) {
4605
	$str  = trim( $str );
4606
	$str  = str_replace( "\r", "\n", $str );
4607
	$str  = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
4608
	return $str;
4609
}
4610
4611
/**
4612
 * Properly strip all HTML tags including script and style
4613
 *
4614
 * This differs from strip_tags() because it removes the contents of
4615
 * the `<script>` and `<style>` tags. E.g. `strip_tags( '<script>something</script>' )`
4616
 * will return 'something'. wp_strip_all_tags will return ''
4617
 *
4618
 * @since 2.9.0
4619
 *
4620
 * @param string $string        String containing HTML tags
4621
 * @param bool   $remove_breaks Optional. Whether to remove left over line breaks and white space chars
4622
 * @return string The processed string.
4623
 */
4624
function wp_strip_all_tags($string, $remove_breaks = false) {
4625
	$string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
4626
	$string = strip_tags($string);
4627
4628
	if ( $remove_breaks )
4629
		$string = preg_replace('/[\r\n\t ]+/', ' ', $string);
4630
4631
	return trim( $string );
4632
}
4633
4634
/**
4635
 * Sanitizes a string from user input or from the database.
4636
 *
4637
 * - Checks for invalid UTF-8,
4638
 * - Converts single `<` characters to entities
4639
 * - Strips all tags
4640
 * - Removes line breaks, tabs, and extra whitespace
4641
 * - Strips octets
4642
 *
4643
 * @since 2.9.0
4644
 *
4645
 * @see wp_check_invalid_utf8()
4646
 * @see wp_strip_all_tags()
4647
 *
4648
 * @param string $str String to sanitize.
4649
 * @return string Sanitized string.
4650
 */
4651
function sanitize_text_field( $str ) {
4652
	$filtered = wp_check_invalid_utf8( $str );
4653
4654
	if ( strpos($filtered, '<') !== false ) {
4655
		$filtered = wp_pre_kses_less_than( $filtered );
4656
		// This will strip extra whitespace for us.
4657
		$filtered = wp_strip_all_tags( $filtered, true );
4658
	} else {
4659
		$filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );
4660
	}
4661
4662
	$found = false;
4663
	while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
4664
		$filtered = str_replace($match[0], '', $filtered);
4665
		$found = true;
4666
	}
4667
4668
	if ( $found ) {
4669
		// Strip out the whitespace that may now exist after removing the octets.
4670
		$filtered = trim( preg_replace('/ +/', ' ', $filtered) );
4671
	}
4672
4673
	/**
4674
	 * Filters a sanitized text field string.
4675
	 *
4676
	 * @since 2.9.0
4677
	 *
4678
	 * @param string $filtered The sanitized string.
4679
	 * @param string $str      The string prior to being sanitized.
4680
	 */
4681
	return apply_filters( 'sanitize_text_field', $filtered, $str );
4682
}
4683
4684
/**
4685
 * i18n friendly version of basename()
4686
 *
4687
 * @since 3.1.0
4688
 *
4689
 * @param string $path   A path.
4690
 * @param string $suffix If the filename ends in suffix this will also be cut off.
4691
 * @return string
4692
 */
4693
function wp_basename( $path, $suffix = '' ) {
4694
	return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
4695
}
4696
4697
/**
4698
 * Forever eliminate "Wordpress" from the planet (or at least the little bit we can influence).
4699
 *
4700
 * Violating our coding standards for a good function name.
4701
 *
4702
 * @since 3.0.0
4703
 *
4704
 * @staticvar string|false $dblq
4705
 *
4706
 * @param string $text The text to be modified.
4707
 * @return string The modified text.
4708
 */
4709
function capital_P_dangit( $text ) {
4710
	// Simple replacement for titles
4711
	$current_filter = current_filter();
4712
	if ( 'the_title' === $current_filter || 'wp_title' === $current_filter )
4713
		return str_replace( 'Wordpress', 'WordPress', $text );
4714
	// Still here? Use the more judicious replacement
4715
	static $dblq = false;
4716
	if ( false === $dblq ) {
4717
		$dblq = _x( '&#8220;', 'opening curly double quote' );
4718
	}
4719
	return str_replace(
4720
		array( ' Wordpress', '&#8216;Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ),
4721
		array( ' WordPress', '&#8216;WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
4722
	$text );
4723
}
4724
4725
/**
4726
 * Sanitize a mime type
4727
 *
4728
 * @since 3.1.3
4729
 *
4730
 * @param string $mime_type Mime type
4731
 * @return string Sanitized mime type
4732
 */
4733
function sanitize_mime_type( $mime_type ) {
4734
	$sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
4735
	/**
4736
	 * Filters a mime type following sanitization.
4737
	 *
4738
	 * @since 3.1.3
4739
	 *
4740
	 * @param string $sani_mime_type The sanitized mime type.
4741
	 * @param string $mime_type      The mime type prior to sanitization.
4742
	 */
4743
	return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
4744
}
4745
4746
/**
4747
 * Sanitize space or carriage return separated URLs that are used to send trackbacks.
4748
 *
4749
 * @since 3.4.0
4750
 *
4751
 * @param string $to_ping Space or carriage return separated URLs
4752
 * @return string URLs starting with the http or https protocol, separated by a carriage return.
4753
 */
4754
function sanitize_trackback_urls( $to_ping ) {
4755
	$urls_to_ping = preg_split( '/[\r\n\t ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY );
4756
	foreach ( $urls_to_ping as $k => $url ) {
4757
		if ( !preg_match( '#^https?://.#i', $url ) )
4758
			unset( $urls_to_ping[$k] );
4759
	}
4760
	$urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping );
4761
	$urls_to_ping = implode( "\n", $urls_to_ping );
4762
	/**
4763
	 * Filters a list of trackback URLs following sanitization.
4764
	 *
4765
	 * The string returned here consists of a space or carriage return-delimited list
4766
	 * of trackback URLs.
4767
	 *
4768
	 * @since 3.4.0
4769
	 *
4770
	 * @param string $urls_to_ping Sanitized space or carriage return separated URLs.
4771
	 * @param string $to_ping      Space or carriage return separated URLs before sanitization.
4772
	 */
4773
	return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
4774
}
4775
4776
/**
4777
 * Add slashes to a string or array of strings.
4778
 *
4779
 * This should be used when preparing data for core API that expects slashed data.
4780
 * This should not be used to escape data going directly into an SQL query.
4781
 *
4782
 * @since 3.6.0
4783
 *
4784
 * @param string|array $value String or array of strings to slash.
4785
 * @return string|array Slashed $value
4786
 */
4787
function wp_slash( $value ) {
4788
	if ( is_array( $value ) ) {
4789 View Code Duplication
		foreach ( $value as $k => $v ) {
4790
			if ( is_array( $v ) ) {
4791
				$value[$k] = wp_slash( $v );
4792
			} else {
4793
				$value[$k] = addslashes( $v );
4794
			}
4795
		}
4796
	} else {
4797
		$value = addslashes( $value );
4798
	}
4799
4800
	return $value;
4801
}
4802
4803
/**
4804
 * Remove slashes from a string or array of strings.
4805
 *
4806
 * This should be used to remove slashes from data passed to core API that
4807
 * expects data to be unslashed.
4808
 *
4809
 * @since 3.6.0
4810
 *
4811
 * @param string|array $value String or array of strings to unslash.
4812
 * @return string|array Unslashed $value
4813
 */
4814
function wp_unslash( $value ) {
4815
	return stripslashes_deep( $value );
4816
}
4817
4818
/**
4819
 * Extract and return the first URL from passed content.
4820
 *
4821
 * @since 3.6.0
4822
 *
4823
 * @param string $content A string which might contain a URL.
4824
 * @return string|false The found URL.
4825
 */
4826
function get_url_in_content( $content ) {
4827
	if ( empty( $content ) ) {
4828
		return false;
4829
	}
4830
4831
	if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
4832
		return esc_url_raw( $matches[2] );
4833
	}
4834
4835
	return false;
4836
}
4837
4838
/**
4839
 * Returns the regexp for common whitespace characters.
4840
 *
4841
 * By default, spaces include new lines, tabs, nbsp entities, and the UTF-8 nbsp.
4842
 * This is designed to replace the PCRE \s sequence.  In ticket #22692, that
4843
 * sequence was found to be unreliable due to random inclusion of the A0 byte.
4844
 *
4845
 * @since 4.0.0
4846
 *
4847
 * @staticvar string $spaces
4848
 *
4849
 * @return string The spaces regexp.
4850
 */
4851
function wp_spaces_regexp() {
4852
	static $spaces = '';
4853
4854
	if ( empty( $spaces ) ) {
4855
		/**
4856
		 * Filters the regexp for common whitespace characters.
4857
		 *
4858
		 * This string is substituted for the \s sequence as needed in regular
4859
		 * expressions. For websites not written in English, different characters
4860
		 * may represent whitespace. For websites not encoded in UTF-8, the 0xC2 0xA0
4861
		 * sequence may not be in use.
4862
		 *
4863
		 * @since 4.0.0
4864
		 *
4865
		 * @param string $spaces Regexp pattern for matching common whitespace characters.
4866
		 */
4867
		$spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0|&nbsp;' );
4868
	}
4869
4870
	return $spaces;
4871
}
4872
4873
/**
4874
 * Print the important emoji-related styles.
4875
 *
4876
 * @since 4.2.0
4877
 *
4878
 * @staticvar bool $printed
4879
 */
4880
function print_emoji_styles() {
4881
	static $printed = false;
4882
4883
	if ( $printed ) {
4884
		return;
4885
	}
4886
4887
	$printed = true;
4888
?>
4889
<style type="text/css">
4890
img.wp-smiley,
4891
img.emoji {
4892
	display: inline !important;
4893
	border: none !important;
4894
	box-shadow: none !important;
4895
	height: 1em !important;
4896
	width: 1em !important;
4897
	margin: 0 .07em !important;
4898
	vertical-align: -0.1em !important;
4899
	background: none !important;
4900
	padding: 0 !important;
4901
}
4902
</style>
4903
<?php
4904
}
4905
4906
/**
4907
 * Print the inline Emoji detection script if it is not already printed.
4908
 *
4909
 * @since 4.2.0
4910
 * @staticvar bool $printed
4911
 */
4912
function print_emoji_detection_script() {
4913
	static $printed = false;
4914
4915
	if ( $printed ) {
4916
		return;
4917
	}
4918
4919
	$printed = true;
4920
4921
	_print_emoji_detection_script();
4922
}
4923
4924
/**
4925
 * Prints inline Emoji dection script
4926
 *
4927
 * @ignore
4928
 * @since 4.6.0
4929
 * @access private
4930
 *
4931
 * @global string $wp_version WordPress version string.
4932
 */
4933
function _print_emoji_detection_script() {
4934
	global $wp_version;
4935
4936
	$settings = array(
4937
		/**
4938
		 * Filters the URL where emoji png images are hosted.
4939
		 *
4940
		 * @since 4.2.0
4941
		 *
4942
		 * @param string The emoji base URL for png images.
4943
		 */
4944
		'baseUrl' => apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/2/72x72/' ),
4945
4946
		/**
4947
		 * Filters the extension of the emoji png files.
4948
		 *
4949
		 * @since 4.2.0
4950
		 *
4951
		 * @param string The emoji extension for png files. Default .png.
4952
		 */
4953
		'ext' => apply_filters( 'emoji_ext', '.png' ),
4954
4955
		/**
4956
		 * Filters the URL where emoji SVG images are hosted.
4957
		 *
4958
		 * @since 4.6.0
4959
		 *
4960
		 * @param string The emoji base URL for svg images.
4961
		 */
4962
		'svgUrl' => apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ),
4963
4964
		/**
4965
		 * Filters the extension of the emoji SVG files.
4966
		 *
4967
		 * @since 4.6.0
4968
		 *
4969
		 * @param string The emoji extension for svg files. Default .svg.
4970
		 */
4971
		'svgExt' => apply_filters( 'emoji_svg_ext', '.svg' ),
4972
	);
4973
4974
	$version = 'ver=' . $wp_version;
4975
4976
	if ( SCRIPT_DEBUG ) {
4977
		$settings['source'] = array(
4978
			/** This filter is documented in wp-includes/class.wp-scripts.php */
4979
			'wpemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji.js?$version" ), 'wpemoji' ),
4980
			/** This filter is documented in wp-includes/class.wp-scripts.php */
4981
			'twemoji' => apply_filters( 'script_loader_src', includes_url( "js/twemoji.js?$version" ), 'twemoji' ),
4982
		);
4983
4984
		?>
4985
		<script type="text/javascript">
4986
			window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
4987
			<?php readfile( ABSPATH . WPINC . "/js/wp-emoji-loader.js" ); ?>
4988
		</script>
4989
		<?php
4990
	} else {
4991
		$settings['source'] = array(
4992
			/** This filter is documented in wp-includes/class.wp-scripts.php */
4993
			'concatemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji-release.min.js?$version" ), 'concatemoji' ),
4994
		);
4995
4996
		/*
4997
		 * If you're looking at a src version of this file, you'll see an "include"
4998
		 * statement below. This is used by the `grunt build` process to directly
4999
		 * include a minified version of wp-emoji-loader.js, instead of using the
5000
		 * readfile() method from above.
5001
		 *
5002
		 * If you're looking at a build version of this file, you'll see a string of
5003
		 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
5004
		 * and edit wp-emoji-loader.js directly.
5005
		 */
5006
		?>
5007
		<script type="text/javascript">
5008
			window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
5009
			!function(a,b,c){function d(a){var c,d,e,f,g,h=b.createElement("canvas"),i=h.getContext&&h.getContext("2d"),j=String.fromCharCode;if(!i||!i.fillText)return!1;switch(i.textBaseline="top",i.font="600 32px Arial",a){case"flag":return i.fillText(j(55356,56806,55356,56826),0,0),h.toDataURL().length<3e3?!1:(i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,65039,8205,55356,57096),0,0),c=h.toDataURL(),i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,55356,57096),0,0),d=h.toDataURL(),c!==d);case"diversity":return i.fillText(j(55356,57221),0,0),e=i.getImageData(16,16,1,1).data,f=e[0]+","+e[1]+","+e[2]+","+e[3],i.fillText(j(55356,57221,55356,57343),0,0),e=i.getImageData(16,16,1,1).data,g=e[0]+","+e[1]+","+e[2]+","+e[3],f!==g;case"simple":return i.fillText(j(55357,56835),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode8":return i.fillText(j(55356,57135),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode9":return i.fillText(j(55358,56631),0,0),0!==i.getImageData(16,16,1,1).data[0]}return!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i;for(i=Array("simple","flag","unicode8","diversity","unicode9"),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],"flag"!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
5010
		</script>
5011
		<?php
5012
	}
5013
}
5014
5015
/**
5016
 * Convert any 4 byte emoji in a string to their equivalent HTML entity.
5017
 *
5018
 * Currently, only Unicode 7 emoji are supported. Skin tone modifiers are allowed,
5019
 * all other Unicode 8 emoji will be added when the spec is finalised.
5020
 *
5021
 * This allows us to store emoji in a DB using the utf8 character set.
5022
 *
5023
 * @since 4.2.0
5024
 *
5025
 * @param string $content The content to encode.
5026
 * @return string The encoded content.
5027
 */
5028
function wp_encode_emoji( $content ) {
5029
	if ( function_exists( 'mb_convert_encoding' ) ) {
5030
		$regex = '/(
5031
		     \x23\xE2\x83\xA3               # Digits
5032
		     [\x30-\x39]\xE2\x83\xA3
5033
		   | \xF0\x9F[\x85-\x88][\xA6-\xBF] # Enclosed characters
5034
		   | \xF0\x9F[\x8C-\x97][\x80-\xBF] # Misc
5035
		   | \xF0\x9F\x98[\x80-\xBF]        # Smilies
5036
		   | \xF0\x9F\x99[\x80-\x8F]
5037
		   | \xF0\x9F\x9A[\x80-\xBF]        # Transport and map symbols
5038
		)/x';
5039
5040
		$matches = array();
5041
		if ( preg_match_all( $regex, $content, $matches ) ) {
5042
			if ( ! empty( $matches[1] ) ) {
5043
				foreach ( $matches[1] as $emoji ) {
5044
					/*
5045
					 * UTF-32's hex encoding is the same as HTML's hex encoding.
5046
					 * So, by converting the emoji from UTF-8 to UTF-32, we magically
5047
					 * get the correct hex encoding.
5048
					 */
5049
					$unpacked = unpack( 'H*', mb_convert_encoding( $emoji, 'UTF-32', 'UTF-8' ) );
5050
					if ( isset( $unpacked[1] ) ) {
5051
						$entity = '&#x' . ltrim( $unpacked[1], '0' ) . ';';
5052
						$content = str_replace( $emoji, $entity, $content );
5053
					}
5054
				}
5055
			}
5056
		}
5057
	}
5058
5059
	return $content;
5060
}
5061
5062
/**
5063
 * Convert emoji to a static img element.
5064
 *
5065
 * @since 4.2.0
5066
 *
5067
 * @param string $text The content to encode.
5068
 * @return string The encoded content.
5069
 */
5070
function wp_staticize_emoji( $text ) {
5071
	$text = wp_encode_emoji( $text );
5072
5073
	/** This filter is documented in wp-includes/formatting.php */
5074
	$cdn_url = apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/2/72x72/' );
5075
5076
	/** This filter is documented in wp-includes/formatting.php */
5077
	$ext = apply_filters( 'emoji_ext', '.png' );
5078
5079
	$output = '';
5080
	/*
5081
	 * HTML loop taken from smiley function, which was taken from texturize function.
5082
	 * It'll never be consolidated.
5083
	 *
5084
	 * First, capture the tags as well as in between.
5085
	 */
5086
	$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
5087
	$stop = count( $textarr );
5088
5089
	// Ignore processing of specific tags.
5090
	$tags_to_ignore = 'code|pre|style|script|textarea';
5091
	$ignore_block_element = '';
5092
5093
	for ( $i = 0; $i < $stop; $i++ ) {
5094
		$content = $textarr[$i];
5095
5096
		// If we're in an ignore block, wait until we find its closing tag.
5097 View Code Duplication
		if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
5098
			$ignore_block_element = $matches[1];
5099
		}
5100
5101
		// If it's not a tag and not in ignore block.
5102
		if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
5103
			$matches = array();
5104
			if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $content, $matches ) ) {
5105
				if ( ! empty( $matches[0] ) ) {
5106
					foreach ( $matches[0] as $flag ) {
5107
						$chars = str_replace( array( '&#x', ';'), '', $flag );
5108
5109
						list( $char1, $char2 ) = str_split( $chars, 5 );
5110
						$entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char1 . '-' . $char2 . $ext, html_entity_decode( $flag ) );
5111
5112
						$content = str_replace( $flag, $entity, $content );
5113
					}
5114
				}
5115
			}
5116
5117
			// Loosely match the Emoji Unicode range.
5118
			$regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/';
5119
5120
			$matches = array();
5121
			if ( preg_match_all( $regex, $content, $matches ) ) {
5122
				if ( ! empty( $matches[1] ) ) {
5123
					foreach ( $matches[1] as $emoji ) {
5124
						$char = str_replace( array( '&#x', ';'), '', $emoji );
5125
						$entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char . $ext, html_entity_decode( $emoji ) );
5126
5127
						$content = str_replace( $emoji, $entity, $content );
5128
					}
5129
				}
5130
			}
5131
		}
5132
5133
		// Did we exit ignore block.
5134
		if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
5135
			$ignore_block_element = '';
5136
		}
5137
5138
		$output .= $content;
5139
	}
5140
5141
	return $output;
5142
}
5143
5144
/**
5145
 * Convert emoji in emails into static images.
5146
 *
5147
 * @since 4.2.0
5148
 *
5149
 * @param array $mail The email data array.
5150
 * @return array The email data array, with emoji in the message staticized.
5151
 */
5152
function wp_staticize_emoji_for_email( $mail ) {
5153
	if ( ! isset( $mail['message'] ) ) {
5154
		return $mail;
5155
	}
5156
5157
	/*
5158
	 * We can only transform the emoji into images if it's a text/html email.
5159
	 * To do that, here's a cut down version of the same process that happens
5160
	 * in wp_mail() - get the Content-Type from the headers, if there is one,
5161
	 * then pass it through the wp_mail_content_type filter, in case a plugin
5162
	 * is handling changing the Content-Type.
5163
	 */
5164
	$headers = array();
5165
	if ( isset( $mail['headers'] ) ) {
5166
		if ( is_array( $mail['headers'] ) ) {
5167
			$headers = $mail['headers'];
5168
		} else {
5169
			$headers = explode( "\n", str_replace( "\r\n", "\n", $mail['headers'] ) );
5170
		}
5171
	}
5172
5173
	foreach ( $headers as $header ) {
5174
		if ( strpos($header, ':') === false ) {
5175
			continue;
5176
		}
5177
5178
		// Explode them out.
5179
		list( $name, $content ) = explode( ':', trim( $header ), 2 );
5180
5181
		// Cleanup crew.
5182
		$name    = trim( $name    );
5183
		$content = trim( $content );
5184
5185
		if ( 'content-type' === strtolower( $name ) ) {
5186
			if ( strpos( $content, ';' ) !== false ) {
5187
				list( $type, $charset ) = explode( ';', $content );
5188
				$content_type = trim( $type );
5189
			} else {
5190
				$content_type = trim( $content );
5191
			}
5192
			break;
5193
		}
5194
	}
5195
5196
	// Set Content-Type if we don't have a content-type from the input headers.
5197
	if ( ! isset( $content_type ) ) {
5198
		$content_type = 'text/plain';
5199
	}
5200
5201
	/** This filter is documented in wp-includes/pluggable.php */
5202
	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
5203
5204
	if ( 'text/html' === $content_type ) {
5205
		$mail['message'] = wp_staticize_emoji( $mail['message'] );
5206
	}
5207
5208
	return $mail;
5209
}
5210
5211
/**
5212
 * Shorten a URL, to be used as link text.
5213
 *
5214
 * @since 1.2.0
5215
 * @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param.
5216
 *
5217
 * @param string $url    URL to shorten.
5218
 * @param int    $length Optional. Maximum length of the shortened URL. Default 35 characters.
5219
 * @return string Shortened URL.
5220
 */
5221
function url_shorten( $url, $length = 35 ) {
5222
	$stripped = str_replace( array( 'https://', 'http://', 'www.' ), '', $url );
5223
	$short_url = untrailingslashit( $stripped );
5224
5225
	if ( strlen( $short_url ) > $length ) {
5226
		$short_url = substr( $short_url, 0, $length - 3 ) . '&hellip;';
5227
	}
5228
	return $short_url;
5229
}
5230
5231
/**
5232
 * Sanitizes a hex color.
5233
 *
5234
 * Returns either '', a 3 or 6 digit hex color (with #), or nothing.
5235
 * For sanitizing values without a #, see sanitize_hex_color_no_hash().
5236
 *
5237
 * @since 3.4.0
5238
 *
5239
 * @param string $color
5240
 * @return string|void
5241
 */
5242
function sanitize_hex_color( $color ) {
5243
	if ( '' === $color ) {
5244
		return '';
5245
	}
5246
5247
	// 3 or 6 hex digits, or the empty string.
5248
	if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
5249
		return $color;
5250
	}
5251
}
5252
5253
/**
5254
 * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible.
5255
 *
5256
 * Saving hex colors without a hash puts the burden of adding the hash on the
5257
 * UI, which makes it difficult to use or upgrade to other color types such as
5258
 * rgba, hsl, rgb, and html color names.
5259
 *
5260
 * Returns either '', a 3 or 6 digit hex color (without a #), or null.
5261
 *
5262
 * @since 3.4.0
5263
 *
5264
 * @param string $color
5265
 * @return string|null
5266
 */
5267
function sanitize_hex_color_no_hash( $color ) {
5268
	$color = ltrim( $color, '#' );
5269
5270
	if ( '' === $color ) {
5271
		return '';
5272
	}
5273
5274
	return sanitize_hex_color( '#' . $color ) ? $color : null;
5275
}
5276
5277
/**
5278
 * Ensures that any hex color is properly hashed.
5279
 * Otherwise, returns value untouched.
5280
 *
5281
 * This method should only be necessary if using sanitize_hex_color_no_hash().
5282
 *
5283
 * @since 3.4.0
5284
 *
5285
 * @param string $color
5286
 * @return string
5287
 */
5288
function maybe_hash_hex_color( $color ) {
5289
	if ( $unhashed = sanitize_hex_color_no_hash( $color ) ) {
5290
		return '#' . $unhashed;
5291
	}
5292
5293
	return $color;
5294
}
5295