Changeset 52789 for trunk/src/wp-includes/formatting.php
- Timestamp:
- 02/23/2022 08:01:55 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r52640 r52789 434 434 * @since 0.71 435 435 * 436 * @param string $ peeThe text which has to be formatted.437 * @param bool $br Optional. If set, this will convert all remaining line breaks438 * after paragraphing. Line breaks within `<script>`, `<style>`,439 * and `<svg>` tags are not affected. Default true.436 * @param string $text The text which has to be formatted. 437 * @param bool $br Optional. If set, this will convert all remaining line breaks 438 * after paragraphing. Line breaks within `<script>`, `<style>`, 439 * and `<svg>` tags are not affected. Default true. 440 440 * @return string Text which has been converted into correct paragraph tags. 441 441 */ 442 function wpautop( $ pee, $br = true ) {442 function wpautop( $text, $br = true ) { 443 443 $pre_tags = array(); 444 444 445 if ( trim( $ pee) === '' ) {445 if ( trim( $text ) === '' ) { 446 446 return ''; 447 447 } 448 448 449 449 // Just to make things a little easier, pad the end. 450 $ pee = $pee. "\n";450 $text = $text . "\n"; 451 451 452 452 /* … … 454 454 * Replace pre tags with placeholders and bring them back after autop. 455 455 */ 456 if ( strpos( $ pee, '<pre' ) !== false ) {457 $ pee_parts = explode( '</pre>', $pee);458 $last_p ee = array_pop( $pee_parts );459 $ pee= '';460 $i = 0;461 462 foreach ( $ pee_parts as $pee_part ) {463 $start = strpos( $ pee_part, '<pre' );456 if ( strpos( $text, '<pre' ) !== false ) { 457 $text_parts = explode( '</pre>', $text ); 458 $last_part = array_pop( $text_parts ); 459 $text = ''; 460 $i = 0; 461 462 foreach ( $text_parts as $text_part ) { 463 $start = strpos( $text_part, '<pre' ); 464 464 465 465 // Malformed HTML? 466 466 if ( false === $start ) { 467 $ pee .= $pee_part;467 $text .= $text_part; 468 468 continue; 469 469 } 470 470 471 471 $name = "<pre wp-pre-tag-$i></pre>"; 472 $pre_tags[ $name ] = substr( $ pee_part, $start ) . '</pre>';473 474 $ pee .= substr( $pee_part, 0, $start ) . $name;472 $pre_tags[ $name ] = substr( $text_part, $start ) . '</pre>'; 473 474 $text .= substr( $text_part, 0, $start ) . $name; 475 475 $i++; 476 476 } 477 477 478 $ pee .= $last_pee;478 $text .= $last_part; 479 479 } 480 480 // Change multiple <br>'s into two line breaks, which will turn into paragraphs. 481 $ pee = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);481 $text = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text ); 482 482 483 483 $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)'; 484 484 485 485 // Add a double line break above block-level opening tags. 486 $ pee = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $pee);486 $text = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $text ); 487 487 488 488 // Add a double line break below block-level closing tags. 489 $ pee = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $pee);489 $text = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $text ); 490 490 491 491 // Add a double line break after hr tags, which are self closing. 492 $ pee = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $pee);492 $text = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $text ); 493 493 494 494 // Standardize newline characters to "\n". 495 $ pee = str_replace( array( "\r\n", "\r" ), "\n", $pee);495 $text = str_replace( array( "\r\n", "\r" ), "\n", $text ); 496 496 497 497 // Find newlines in all elements and add placeholders. 498 $ pee = wp_replace_in_html_tags( $pee, array( "\n" => ' <!-- wpnl --> ' ) );498 $text = wp_replace_in_html_tags( $text, array( "\n" => ' <!-- wpnl --> ' ) ); 499 499 500 500 // Collapse line breaks before and after <option> elements so they don't get autop'd. 501 if ( strpos( $ pee, '<option' ) !== false ) {502 $ pee = preg_replace( '|\s*<option|', '<option', $pee);503 $ pee = preg_replace( '|</option>\s*|', '</option>', $pee);501 if ( strpos( $text, '<option' ) !== false ) { 502 $text = preg_replace( '|\s*<option|', '<option', $text ); 503 $text = preg_replace( '|</option>\s*|', '</option>', $text ); 504 504 } 505 505 … … 508 508 * so they don't get autop'd. 509 509 */ 510 if ( strpos( $ pee, '</object>' ) !== false ) {511 $ pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee);512 $ pee = preg_replace( '|\s*</object>|', '</object>', $pee);513 $ pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee);510 if ( strpos( $text, '</object>' ) !== false ) { 511 $text = preg_replace( '|(<object[^>]*>)\s*|', '$1', $text ); 512 $text = preg_replace( '|\s*</object>|', '</object>', $text ); 513 $text = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $text ); 514 514 } 515 515 … … 518 518 * before and after <source> and <track> elements. 519 519 */ 520 if ( strpos( $ pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {521 $ pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee);522 $ pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee);523 $ pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee);520 if ( strpos( $text, '<source' ) !== false || strpos( $text, '<track' ) !== false ) { 521 $text = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $text ); 522 $text = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $text ); 523 $text = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $text ); 524 524 } 525 525 526 526 // Collapse line breaks before and after <figcaption> elements. 527 if ( strpos( $ pee, '<figcaption' ) !== false ) {528 $ pee = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $pee);529 $ pee = preg_replace( '|</figcaption>\s*|', '</figcaption>', $pee);527 if ( strpos( $text, '<figcaption' ) !== false ) { 528 $text = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $text ); 529 $text = preg_replace( '|</figcaption>\s*|', '</figcaption>', $text ); 530 530 } 531 531 532 532 // Remove more than two contiguous line breaks. 533 $ pee = preg_replace( "/\n\n+/", "\n\n", $pee);533 $text = preg_replace( "/\n\n+/", "\n\n", $text ); 534 534 535 535 // Split up the contents into an array of strings, separated by double line breaks. 536 $p ees = preg_split( '/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY );537 538 // Reset $ peeprior to rebuilding.539 $ pee= '';536 $paragraphs = preg_split( '/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY ); 537 538 // Reset $text prior to rebuilding. 539 $text = ''; 540 540 541 541 // Rebuild the content as a string, wrapping every bit with a <p>. 542 foreach ( $p ees as $tinkle) {543 $ pee .= '<p>' . trim( $tinkle, "\n" ) . "</p>\n";542 foreach ( $paragraphs as $paragraph ) { 543 $text .= '<p>' . trim( $paragraph, "\n" ) . "</p>\n"; 544 544 } 545 545 546 546 // Under certain strange conditions it could create a P of entirely whitespace. 547 $ pee = preg_replace( '|<p>\s*</p>|', '', $pee);547 $text = preg_replace( '|<p>\s*</p>|', '', $text ); 548 548 549 549 // Add a closing <p> inside <div>, <address>, or <form> tag if missing. 550 $ pee = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $pee);550 $text = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $text ); 551 551 552 552 // If an opening or closing block element tag is wrapped in a <p>, unwrap it. 553 $ pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee);553 $text = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $text ); 554 554 555 555 // In some cases <li> may get wrapped in <p>, fix them. 556 $ pee = preg_replace( '|<p>(<li.+?)</p>|', '$1', $pee);556 $text = preg_replace( '|<p>(<li.+?)</p>|', '$1', $text ); 557 557 558 558 // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>. 559 $ pee = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $pee);560 $ pee = str_replace( '</blockquote></p>', '</p></blockquote>', $pee);559 $text = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $text ); 560 $text = str_replace( '</blockquote></p>', '</p></blockquote>', $text ); 561 561 562 562 // If an opening or closing block element tag is preceded by an opening <p> tag, remove it. 563 $ pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', '$1', $pee);563 $text = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', '$1', $text ); 564 564 565 565 // If an opening or closing block element tag is followed by a closing <p> tag, remove it. 566 $ pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee);566 $text = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $text ); 567 567 568 568 // Optionally insert line breaks. 569 569 if ( $br ) { 570 570 // Replace newlines that shouldn't be touched with a placeholder. 571 $ pee = preg_replace_callback( '/<(script|style|svg).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);571 $text = preg_replace_callback( '/<(script|style|svg).*?<\/\\1>/s', '_autop_newline_preservation_helper', $text ); 572 572 573 573 // Normalize <br> 574 $ pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee);574 $text = str_replace( array( '<br>', '<br/>' ), '<br />', $text ); 575 575 576 576 // Replace any new line characters that aren't preceded by a <br /> with a <br />. 577 $ pee = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $pee);577 $text = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $text ); 578 578 579 579 // Replace newline placeholders with newlines. 580 $ pee = str_replace( '<WPPreserveNewline />', "\n", $pee);580 $text = str_replace( '<WPPreserveNewline />', "\n", $text ); 581 581 } 582 582 583 583 // If a <br /> tag is after an opening or closing block tag, remove it. 584 $ pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', '$1', $pee);584 $text = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', '$1', $text ); 585 585 586 586 // If a <br /> tag is before a subset of opening or closing block tags, remove it. 587 $ pee = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);588 $ pee = preg_replace( "|\n</p>$|", '</p>', $pee);587 $text = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $text ); 588 $text = preg_replace( "|\n</p>$|", '</p>', $text ); 589 589 590 590 // Replace placeholder <pre> tags with their original content. 591 591 if ( ! empty( $pre_tags ) ) { 592 $ pee = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $pee);592 $text = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $text ); 593 593 } 594 594 595 595 // Restore newlines in all elements. 596 if ( false !== strpos( $ pee, '<!-- wpnl -->' ) ) {597 $ pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee);598 } 599 600 return $ pee;596 if ( false !== strpos( $text, '<!-- wpnl -->' ) ) { 597 $text = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $text ); 598 } 599 600 return $text; 601 601 } 602 602 … … 815 815 * @global array $shortcode_tags 816 816 * 817 * @param string $ peeThe content.817 * @param string $text The content. 818 818 * @return string The filtered content. 819 819 */ 820 function shortcode_unautop( $ pee) {820 function shortcode_unautop( $text ) { 821 821 global $shortcode_tags; 822 822 823 823 if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { 824 return $ pee;824 return $text; 825 825 } 826 826 … … 862 862 // phpcs:enable 863 863 864 return preg_replace( $pattern, '$1', $ pee);864 return preg_replace( $pattern, '$1', $text ); 865 865 } 866 866
Note: See TracChangeset
for help on using the changeset viewer.