~canonical-sysadmins/wordpress/4.7

« back to all changes in this revision

Viewing changes to wp-includes/formatting.php

  • Committer: Nick Moffitt
  • Date: 2015-01-15 11:04:26 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: nick.moffitt@canonical.com-20150115110426-5stm1p14cfnxrtme
New Upstream Version 4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 * Replaces common plain text characters into formatted entities
12
12
 *
13
13
 * As an example,
14
 
 * <code>
15
 
 * 'cause today's effort makes it worth tomorrow's "holiday"...
16
 
 * </code>
 
14
 *
 
15
 *     'cause today's effort makes it worth tomorrow's "holiday" ...
 
16
 *
17
17
 * Becomes:
18
 
 * <code>
19
 
 * &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;
20
 
 * </code>
 
18
 *
 
19
 *     &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221; &#8230;
 
20
 *
21
21
 * Code within certain html blocks are skipped.
22
22
 *
23
23
 * @since 0.71
215
215
                .     '-(?!->)' // Dash not followed by end of comment.
216
216
                .     '[^\-]*+' // Consume non-dashes.
217
217
                . ')*+'         // Loop possessively.
218
 
                . '-->';        // End of comment.
219
 
 
220
 
        $regex =  '/('                  // Capture the entire match.
221
 
                .       '<'             // Find start of element.
222
 
                .       '(?(?=!--)'     // Is this a comment?
223
 
                .               $comment_regex  // Find end of comment
224
 
                .       '|'
225
 
                .               '[^>]+>'        // Find end of element
226
 
                .       ')'
227
 
                . '|'
228
 
                .       '\['            // Find start of shortcode.
229
 
                .       '[\/\[]?'       // Shortcodes may begin with [/ or [[
230
 
                .       $tagregexp      // Only match registered shortcodes, because performance.
231
 
                .       '(?:'
232
 
                .               '[^\[\]<>]+'    // Shortcodes do not contain other shortcodes. Quantifier critical.
233
 
                .       '|'
234
 
                .               '<[^\[\]>]*>'   // HTML elements permitted. Prevents matching ] before >.
235
 
                .       ')*+'           // Possessive critical.
236
 
                .       '\]'            // Find end of shortcode.
237
 
                .       '\]?'           // Shortcodes may end with ]]
 
218
                . '(?:-->)?';   // End of comment. If not found, match all input.
 
219
 
 
220
        $shortcode_regex =
 
221
                  '\['              // Find start of shortcode.
 
222
                . '[\/\[]?'         // Shortcodes may begin with [/ or [[
 
223
                . $tagregexp        // Only match registered shortcodes, because performance.
 
224
                . '(?:'
 
225
                .     '[^\[\]<>]+'  // Shortcodes do not contain other shortcodes. Quantifier critical.
 
226
                . '|'
 
227
                .     '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >.
 
228
                . ')*+'             // Possessive critical.
 
229
                . '\]'              // Find end of shortcode.
 
230
                . '\]?';            // Shortcodes may end with ]]
 
231
 
 
232
        $regex =
 
233
                  '/('                   // Capture the entire match.
 
234
                .     '<'                // Find start of element.
 
235
                .     '(?(?=!--)'        // Is this a comment?
 
236
                .         $comment_regex // Find end of comment.
 
237
                .     '|'
 
238
                .         '[^>]+>'       // Find end of element.
 
239
                .     ')'
 
240
                . '|'
 
241
                .     $shortcode_regex   // Find shortcodes.
238
242
                . ')/s';
239
243
 
240
244
        $textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
242
246
        foreach ( $textarr as &$curl ) {
243
247
                // Only call _wptexturize_pushpop_element if $curl is a delimiter.
244
248
                $first = $curl[0];
245
 
                if ( '<' === $first && '>' === substr( $curl, -1 ) ) {
246
 
                        // This is an HTML delimiter.
247
 
 
248
 
                        if ( '<!--' !== substr( $curl, 0, 4 ) ) {
249
 
                                _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
250
 
                        }
 
249
                if ( '<' === $first && '<!--' === substr( $curl, 0, 4 ) ) {
 
250
                        // This is an HTML comment delimeter.
 
251
 
 
252
                        continue;
 
253
 
 
254
                } elseif ( '<' === $first && '>' === substr( $curl, -1 ) ) {
 
255
                        // This is an HTML element delimiter.
 
256
 
 
257
                        _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
251
258
 
252
259
                } elseif ( '' === trim( $curl ) ) {
253
260
                        // This is a newline between delimiters.  Performance improves when we check this.
254
261
 
255
262
                        continue;
256
263
 
257
 
                } elseif ( '[' === $first && 1 === preg_match( '/^\[\/?' . $tagregexp . '(?:[^\[\]<>]+|<[^\[\]>]*>)*+\]$/', $curl ) ) {
 
264
                } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
258
265
                        // This is a shortcode delimiter.
259
266
 
260
 
                        _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
261
 
 
262
 
                } elseif ( '[' === $first && 1 === preg_match( '/^\[[\/\[]?' . $tagregexp . '(?:[^\[\]<>]+|<[^\[\]>]*>)*+\]\]?$/', $curl ) ) {
263
 
                        // This is an escaped shortcode delimiter.
264
 
 
265
 
                        // Do not texturize.
266
 
                        // Do not push to the shortcodes stack.
267
 
 
268
 
                        continue;
 
267
                        if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
 
268
                                // Looks like a normal shortcode.
 
269
                                _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
 
270
                        } else {
 
271
                                // Looks like an escaped shortcode.
 
272
                                continue;
 
273
                        }
269
274
 
270
275
                } elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
271
276
                        // This is neither a delimiter, nor is this content inside of no_texturize pairs.  Do texturize.
283
288
                        }
284
289
 
285
290
                        // 9x9 (times), but never 0x9999
286
 
                        if ( 1 === preg_match( '/(?<=\d)x-?\d/', $curl ) ) {
 
291
                        if ( 1 === preg_match( '/(?<=\d)x\d/', $curl ) ) {
287
292
                                // Searching for a digit is 10 times more expensive than for the x, so we avoid doing this one!
288
 
                                $curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(-?\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
 
293
                                $curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
289
294
                        }
290
295
                }
291
296
        }
307
312
 * @since 2.9.0
308
313
 * @access private
309
314
 *
310
 
 * @param string $text Text to check. Must be a tag like <html> or [shortcode].
 
315
 * @param string $text Text to check. Must be a tag like `<html>` or `[shortcode]`.
311
316
 * @param array $stack List of open tag elements.
312
317
 * @param array $disabled_elements The tag names to match against. Spaces are not allowed in tag names.
313
318
 */
326
331
 
327
332
        // Parse out the tag name.
328
333
        $space = strpos( $text, ' ' );
329
 
        if ( FALSE === $space ) {
 
334
        if ( false === $space ) {
330
335
                $space = -1;
331
336
        } else {
332
337
                $space -= $name_offset;
475
480
/**
476
481
 * Don't auto-p wrap shortcodes that stand alone
477
482
 *
478
 
 * Ensures that shortcodes are not wrapped in <<p>>...<</p>>.
 
483
 * Ensures that shortcodes are not wrapped in `<p>...</p>`.
479
484
 *
480
485
 * @since 2.9.0
481
486
 *
572
577
 * @access private
573
578
 *
574
579
 * @param string $string The text which is to be encoded.
575
 
 * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
 
580
 * @param int $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
576
581
 * @param string $charset Optional. The character encoding of the string. Default is false.
577
582
 * @param boolean $double_encode Optional. Whether to encode existing html entities. Default is false.
578
583
 * @return string The encoded text with HTML entities.
1067
1072
         */
1068
1073
        $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
1069
1074
        $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
1070
 
        $filename = str_replace($special_chars, '', $filename);
 
1075
        $filename = str_replace( $special_chars, '', $filename );
1071
1076
        $filename = str_replace( array( '%20', '+' ), '-', $filename );
1072
 
        $filename = preg_replace('/[\s-]+/', '-', $filename);
1073
 
        $filename = trim($filename, '.-_');
 
1077
        $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
 
1078
        $filename = trim( $filename, '.-_' );
1074
1079
 
1075
1080
        // Split the filename into a base and extension[s]
1076
1081
        $parts = explode('.', $filename);
1229
1234
 * Used for querying the database for a value from URL.
1230
1235
 *
1231
1236
 * @since 3.1.0
1232
 
 * @uses sanitize_title()
1233
1237
 *
1234
1238
 * @param string $title The string to be sanitized.
1235
1239
 * @return string The sanitized string.
1313
1317
 * @since 2.5.1
1314
1318
 *
1315
1319
 * @param string $orderby Order by string to be checked.
1316
 
 * @return string|bool Returns the order by clause if it is a match, false otherwise.
 
1320
 * @return false|string Returns the order by clause if it is a match, false otherwise.
1317
1321
 */
1318
1322
function sanitize_sql_orderby( $orderby ){
1319
1323
        preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
1362
1366
/**
1363
1367
 * Converts a number of characters from a string.
1364
1368
 *
1365
 
 * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are
 
1369
 * Metadata tags `<title>` and `<category>` are removed, `<br>` and `<hr>` are
1366
1370
 * converted into correct XHTML and Unicode characters are converted to the
1367
1371
 * valid range.
1368
1372
 *
1931
1935
 *
1932
1936
 * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
1933
1937
 *
1934
 
 * <code>
1935
 
 * _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234   890 123456789 1234567890a    45678   1 3 5 7 90 ", 10 ) ==
1936
 
 * array (
1937
 
 *   0 => '1234 67890 ',  // 11 characters: Perfect split
1938
 
 *   1 => '1234 ',        //  5 characters: '1234 67890a' was too long
1939
 
 *   2 => '67890a cd ',   // 10 characters: '67890a cd 1234' was too long
1940
 
 *   3 => '1234   890 ',  // 11 characters: Perfect split
1941
 
 *   4 => '123456789 ',   // 10 characters: '123456789 1234567890a' was too long
1942
 
 *   5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
1943
 
 *   6 => '   45678   ',  // 11 characters: Perfect split
1944
 
 *   7 => '1 3 5 7 9',    //  9 characters: End of $string
1945
 
 * );
1946
 
 * </code>
 
1938
 *     _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234   890 123456789 1234567890a    45678   1 3 5 7 90 ", 10 ) ==
 
1939
 *     array (
 
1940
 *         0 => '1234 67890 ',  // 11 characters: Perfect split
 
1941
 *         1 => '1234 ',        //  5 characters: '1234 67890a' was too long
 
1942
 *         2 => '67890a cd ',   // 10 characters: '67890a cd 1234' was too long
 
1943
 *         3 => '1234   890 ',  // 11 characters: Perfect split
 
1944
 *         4 => '123456789 ',   // 10 characters: '123456789 1234567890a' was too long
 
1945
 *         5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
 
1946
 *         6 => '   45678   ',  // 11 characters: Perfect split
 
1947
 *         7 => '1 3 5 7 90 ',  // 11 characters: End of $string
 
1948
 *     );
1947
1949
 *
1948
1950
 * @since 3.4.0
1949
1951
 * @access private
2017
2019
 *
2018
2020
 * Callback handler for {@link convert_smilies()}.
2019
2021
 * Looks up one smiley code in the $wpsmiliestrans global array and returns an
2020
 
 * <img> string for that smiley.
 
2022
 * `<img>` string for that smiley.
2021
2023
 *
2022
2024
 * @global array $wpsmiliestrans
2023
2025
 * @since 2.8.0
2217
2219
 * @access private
2218
2220
 *
2219
2221
 * @param array $match The preg_replace_callback matches array
2220
 
 * @return array Converted chars
 
2222
 * @return string Converted chars
2221
2223
 */
2222
2224
function _wp_iso_convert( $match ) {
2223
2225
        return chr( hexdec( strtolower( $match[1] ) ) );
2233
2235
 *
2234
2236
 * @since 1.2.0
2235
2237
 *
2236
 
 * @uses get_option() to retrieve the value of 'gmt_offset'.
2237
2238
 * @param string $string The date to be converted.
2238
2239
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
2239
2240
 * @return string GMT version of the date provided.
3026
3027
 * is applied to the returned cleaned URL.
3027
3028
 *
3028
3029
 * @since 2.8.0
3029
 
 * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
3030
 
 *              via $protocols or the common ones set in the function.
3031
3030
 *
3032
3031
 * @param string $url The URL to be cleaned.
3033
3032
 * @param array $protocols Optional. An array of acceptable protocols.
3085
3084
 * Performs esc_url() for database usage.
3086
3085
 *
3087
3086
 * @since 2.8.0
3088
 
 * @uses esc_url()
3089
3087
 *
3090
3088
 * @param string $url The URL to be cleaned.
3091
3089
 * @param array $protocols An array of acceptable protocols.
3242
3240
 * beginning, so it isn't a true relative link, but from the web root base.
3243
3241
 *
3244
3242
 * @since 2.1.0
 
3243
 * @since 4.1.0 Support was added for relative URLs.
3245
3244
 *
3246
3245
 * @param string $link Full URL path.
3247
3246
 * @return string Absolute path.
3248
3247
 */
3249
3248
function wp_make_link_relative( $link ) {
3250
 
        return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
 
3249
        return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link );
3251
3250
}
3252
3251
 
3253
3252
/**
3481
3480
 *
3482
3481
 * KSES already converts lone greater than signs.
3483
3482
 *
3484
 
 * @uses wp_pre_kses_less_than_callback in the callback function.
3485
3483
 * @since 2.3.0
3486
3484
 *
3487
3485
 * @param string $text Text to be converted.
3494
3492
/**
3495
3493
 * Callback function used by preg_replace.
3496
3494
 *
3497
 
 * @uses esc_html to format the $matches text.
3498
3495
 * @since 2.3.0
3499
3496
 *
3500
3497
 * @param array $matches Populated by matches to preg_replace.
3697
3694
        return $m[1] . '=' . $m[2] .
3698
3695
                ( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ?
3699
3696
                        $m[3] :
3700
 
                        path_join( $_links_add_base, $m[3] ) )
 
3697
                        WP_HTTP::make_absolute_url( $m[3], $_links_add_base )
 
3698
                )
3701
3699
                . $m[2];
3702
3700
}
3703
3701
 
3704
3702
/**
3705
3703
 * Adds a Target attribute to all links in passed content.
3706
3704
 *
3707
 
 * This function by default only applies to <a> tags, however this can be
 
3705
 * This function by default only applies to `<a>` tags, however this can be
3708
3706
 * modified by the 3rd param.
3709
3707
 *
3710
 
 * <b>NOTE:</b> Any current target attributed will be stripped and replaced.
 
3708
 * *NOTE:* Any current target attributed will be stripped and replaced.
3711
3709
 *
3712
3710
 * @since 2.7.0
3713
3711
 *
3758
3756
 * Properly strip all HTML tags including script and style
3759
3757
 *
3760
3758
 * This differs from strip_tags() because it removes the contents of
3761
 
 * the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' )
 
3759
 * the `<script>` and `<style>` tags. E.g. `strip_tags( '<script>something</script>' )`
3762
3760
 * will return 'something'. wp_strip_all_tags will return ''
3763
3761
 *
3764
3762
 * @since 2.9.0