Make WordPress Core


Ignore:
Timestamp:
05/02/2012 01:14:52 AM (12 years ago)
Author:
azaozz
Message:

Change the image caption shortcode format to [caption ...]<a><img /></a> caption text + htmlcaption. That way HTML tags in captions are better supported and the shortcode wouldn't break when using the wrong quotes. Props sushkov, nacin, fixes #18311

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/media.php

    r20544 r20679  
    146146    $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
    147147    $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
    148     $caption = preg_replace( '/\n+/', '<br />', str_replace('"', '&quot;', $caption) );
     148    // convert any remaining line breaks to <br>
     149    $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
    149150
    150151    $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
     
    152153        $align = 'none';
    153154
    154     $shcode = '[caption id="' . $id . '" align="align' . $align
    155     . '" width="' . $width . '" caption="' . $caption . '"]' . $html . '[/caption]';
     155    $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
    156156
    157157    return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
     
    167167function _cleanup_image_add_caption( $matches ) {
    168168    // remove any line breaks from inside the tags
    169     $s = preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
    170     // look for single quotes inside html attributes (for example in title)
    171     $s = preg_replace_callback( '/="[^"]+"/', '_cleanup_image_add_caption_callback', $s );
    172     return str_replace( '"', "'", $s );
    173 }
    174 
    175 /**
    176  * Private preg_replace callback used in _cleanup_image_add_caption()
    177  *
    178  * @access private
    179  * @since 3.4.0
    180  */
    181 function _cleanup_image_add_caption_callback( $matches ) {
    182     return str_replace( "'", '&#39;', $matches[0] );
     169    return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
    183170}
    184171
     
    15421529            caption = f.caption.value.replace(/\r\n|\r/g, '\n');
    15431530            caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
    1544                 a = a.replace(/[\r\n\t]+/, ' ').replace(/="[^"]+"/, function(b){
    1545                     return b.replace(/'/g, '&#39;');
    1546                 });
    1547                 return a.replace(/"/g, "'");
     1531                return a.replace(/[\r\n\t]+/, ' ');
    15481532            });
    15491533
    1550             caption = caption.replace(/\n+/g, '<br />').replace(/"/g, '&quot;');
     1534            caption = caption.replace(/\s*\n\s*/g, '<br />');
    15511535        }
    15521536<?php } ?>
     
    15621546
    15631547        if ( caption )
    1564             html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
     1548            html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
    15651549
    15661550        var win = window.dialogArguments || opener || parent || top;
Note: See TracChangeset for help on using the changeset viewer.