Make WordPress Core

Ticket #25615: correct-wording-for-wpautop.patch

File correct-wording-for-wpautop.patch, 6.8 KB (added by ricomoorman, 11 years ago)
  • wp-includes/formatting.php

     
    181181 *
    182182 * @since 0.71
    183183 *
    184  * @param string $pee The text which has to be formatted.
     184 * @param string $text The text which has to be formatted.
    185185 * @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
    186186 * @return string Text which has been converted into correct paragraph tags.
    187187 */
    188 function wpautop($pee, $br = true) {
     188function wpautop($text, $br = true) {
    189189        $pre_tags = array();
    190190
    191         if ( trim($pee) === '' )
     191        if ( trim($text) === '' )
    192192                return '';
    193193
    194         $pee = $pee . "\n"; // just to make things a little easier, pad the end
     194        $text = $text . "\n"; // just to make things a little easier, pad the end
    195195
    196         if ( strpos($pee, '<pre') !== false ) {
    197                 $pee_parts = explode( '</pre>', $pee );
    198                 $last_pee = array_pop($pee_parts);
    199                 $pee = '';
     196        if ( strpos($text, '<pre') !== false ) {
     197                $text_parts = explode( '</pre>', $text );
     198                $last_part = array_pop($text_parts);
     199                $text = '';
    200200                $i = 0;
    201201
    202                 foreach ( $pee_parts as $pee_part ) {
    203                         $start = strpos($pee_part, '<pre');
     202                foreach ( $text_parts as $text_part ) {
     203                        $start = strpos($text_part, '<pre');
    204204
    205205                        // Malformed html?
    206206                        if ( $start === false ) {
    207                                 $pee .= $pee_part;
     207                                $text .= $text_part;
    208208                                continue;
    209209                        }
    210210
    211211                        $name = "<pre wp-pre-tag-$i></pre>";
    212                         $pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
     212                        $pre_tags[$name] = substr( $text_part, $start ) . '</pre>';
    213213
    214                         $pee .= substr( $pee_part, 0, $start ) . $name;
     214                        $text .= substr( $text_part, 0, $start ) . $name;
    215215                        $i++;
    216216                }
    217217
    218                 $pee .= $last_pee;
     218                $text .= $last_part;
    219219        }
    220220
    221         $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
     221        $text = preg_replace('|<br />\s*<br />|', "\n\n", $text);
    222222        // Space things out a little
    223223        $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
    224         $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
    225         $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    226         $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    227         if ( strpos($pee, '<object') !== false ) {
    228                 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
    229                 $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
     224        $text = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $text);
     225        $text = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $text);
     226        $text = str_replace(array("\r\n", "\r"), "\n", $text); // cross-platform newlines
     227        if ( strpos($text, '<object') !== false ) {
     228                $text = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $text); // no paragraph inside object/embed
     229                $text = preg_replace('|\s*</embed>\s*|', '</embed>', $text);
    230230        }
    231         $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
     231        $text = preg_replace("/\n\n+/", "\n\n", $text); // take care of duplicates
    232232        // make paragraphs, including one at the end
    233         $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
    234         $pee = '';
    235         foreach ( $pees as $tinkle )
    236                 $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
    237         $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    238         $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
    239         $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    240         $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
    241         $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    242         $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
    243         $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    244         $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
     233        $texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
     234        $text = '';
     235        foreach ( $texts as $paragraph )
     236                $text .= '<p>' . trim($paragraph, "\n") . "</p>\n";
     237        $text = preg_replace('|<p>\s*</p>|', '', $text); // under certain strange conditions it could create a P of entirely whitespace
     238        $text = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $text);
     239        $text = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $text); // don't wrap blocks
     240        $text = preg_replace("|<p>(<li.+?)</p>|", "$1", $text); // problem with nested lists
     241        $text = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $text);
     242        $text = str_replace('</blockquote></p>', '</p></blockquote>', $text);
     243        $text = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $text);
     244        $text = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $text);
    245245        if ( $br ) {
    246                 $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
    247                 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
    248                 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
     246                $text = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $text);
     247                $text = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $text); // optionally make line breaks
     248                $text = str_replace('<WPPreserveNewline />', "\n", $text);
    249249        }
    250         $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
    251         $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
    252         $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
     250        $text = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $text);
     251        $text = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $text);
     252        $text = preg_replace( "|\n</p>$|", '</p>', $text );
    253253
    254254        if ( !empty($pre_tags) )
    255                 $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
     255                $text = str_replace(array_keys($pre_tags), array_values($pre_tags), $text);
    256256
    257         return $pee;
     257        return $text;
    258258}
    259259
    260260/**
     
    277277 *
    278278 * @since 2.9.0
    279279 *
    280  * @param string $pee The content.
     280 * @param string $text The content.
    281281 * @return string The filtered content.
    282282 */
    283 function shortcode_unautop( $pee ) {
     283function shortcode_unautop( $text ) {
    284284        global $shortcode_tags;
    285285
    286286        if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
    287                 return $pee;
     287                return $text;
    288288        }
    289289
    290290        $tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
     
    321321                . '<\\/p>'                           // closing paragraph
    322322                . '/s';
    323323
    324         return preg_replace( $pattern, '$1', $pee );
     324        return preg_replace( $pattern, '$1', $text );
    325325}
    326326
    327327/**