Skip to:
Content

bbPress.org


Ignore:
Timestamp:
04/26/2013 11:00:38 AM (13 years ago)
Author:
johnjamesjacoby
Message:

Audit procedure for posting pre-formatted code in topics and replies:

  • Invert code-trick & code-trick-reverse filters to happen pre-save and on output.
  • Use esc_html() filter rather than esc_textarea() for textarea output when editing content, to prevent double escaping after above code-trick-reversal.
  • Introduce bbp_rel_nofollow() and callback, to handle this on output rather than input, to prevent mucking up preformatted code, and replace wp_rel_nofollow() usages with this.
  • Disable visual-editor by default. It's causing code formatting issues when switching between editor types (enable at your own risk in a plugin for now.)
  • Fixes #1967 (trunk)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/common/formatting.php

    r4733 r4866  
    121121        // Setup variables
    122122        $openers = array( '<p>', '<br />' );
    123         $content    = preg_replace_callback( "!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", 'bbp_decode_callback', $content );
     123        $content = preg_replace_callback( "!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", 'bbp_decode_callback', $content );
    124124
    125125        // Do the do
    126         $content    = str_replace( $openers,       '',       $content );
    127         $content    = str_replace( '</p>',         "\n",     $content );
    128         $content    = str_replace( '<coded_br />', '<br />', $content );
    129         $content    = str_replace( '<coded_p>',    '<p>',    $content );
    130         $content    = str_replace( '</coded_p>',   '</p>',   $content );
     126        $content = str_replace( $openers,       '',       $content );
     127        $content = str_replace( '</p>',         "\n",     $content );
     128        $content = str_replace( '<coded_br />', '<br />', $content );
     129        $content = str_replace( '<coded_p>',    '<p>',    $content );
     130        $content = str_replace( '</coded_p>',   '</p>',   $content );
    131131
    132132        return $content;
     
    158158        );
    159159
    160         // Add 'p' and 'br' tags to allowed array, so they are not encoded
    161         $allowed['p']  = array();
    162         $allowed['br'] = array();
    163 
    164160        // Loop through allowed tags and compare for empty and normal tags
    165161        foreach ( $allowed as $tag => $args ) {
    166162                $preg = $args ? "{$tag}(?:\s.*?)?" : $tag;
    167163
    168                 // Which walker to use based on the tag and argments
     164                // Which walker to use based on the tag and arguments
    169165                if ( isset( $empty[$tag] ) ) {
    170166                        array_walk( $content, 'bbp_encode_empty_callback',  $preg );
     
    189185 */
    190186function bbp_encode_callback( $matches = array() ) {
    191         $content = trim( $matches[2] );
     187
     188        // Trim inline code, not pre blocks (to prevent removing indentation)
     189        if ( "`" == $matches[1] ) {
     190                $content = trim( $matches[2] );
     191        } else {
     192                $content = $matches[2];
     193        }
     194
     195        // Do some replacing
    192196        $content = htmlspecialchars( $content, ENT_QUOTES );
    193197        $content = str_replace( array( "\r\n", "\r" ), "\n", $content );
     
    196200        $content = str_replace( '&amp;lt;',  '&lt;',  $content );
    197201        $content = str_replace( '&amp;gt;',  '&gt;',  $content );
     202
     203        // Wrap in code tags
    198204        $content = '<code>' . $content . '</code>';
    199205
    200         if ( "`" != $matches[1] )
     206        // Wrap blocks in pre tags
     207        if ( "`" != $matches[1] ) {
    201208                $content = '<pre>' . $content . '</pre>';
     209        }
    202210
    203211        return $content;
     
    262270        }
    263271}
     272
     273/** No Follow *****************************************************************/
     274
     275/**
     276 * Catches links so rel=nofollow can be added (on output, not save)
     277 *
     278 * @since bbPress (r4865)
     279 * @param string $text Post text
     280 * @return string $text Text with rel=nofollow added to any links
     281 */
     282function bbp_rel_nofollow( $text = '' ) {
     283        return preg_replace_callback( '|<a (.+?)>|i', 'bbp_rel_nofollow_callback', $text );
     284}
     285
     286/**
     287 * Adds rel=nofollow to a link
     288 *
     289 * @since bbPress (r4865)
     290 * @param array $matches
     291 * @return string $text Link with rel=nofollow added
     292 */
     293function bbp_rel_nofollow_callback( $matches = array() ) {
     294        $text = $matches[1];
     295        $text = str_replace( array( ' rel="nofollow"', " rel='nofollow'" ), '', $text );
     296        return "<a $text rel=\"nofollow\">";
     297}
Note: See TracChangeset for help on using the changeset viewer.