Allow shortcodes to run before or after wpautop()/texturize() formatting. Default to before for WP 2.5 compat. Props AaronCampbell

git-svn-id: http://svn.automattic.com/wordpress/trunk@7699 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2008-04-16 21:07:04 +00:00
parent 7f5548e55b
commit 9df4077f95
3 changed files with 41 additions and 21 deletions

View File

@ -26,7 +26,7 @@ function wptexturize($text) {
for ( $i = 0; $i < $stop; $i++ ) {
$curl = $textarr[$i];
if (isset($curl{0}) && '<' != $curl{0} && '[' != $curl{0} && $next) { // If it's not a tag
if (isset($curl{0}) && '<' != $curl{0} && '[' != $curl{0} && $next) { // If it's not a tag or shortcode
// static strings
$curl = str_replace($static_characters, $static_replacements, $curl);
// regular expressions
@ -74,6 +74,7 @@ function wpautop($pee, $br = 1) {
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
$pee = preg_replace('/<p>(\s*?' . get_shortcode_regex(true) . '\s*)<\/p>/s', '$1', $pee); // don't auto-p wrap post-formatting shortcodes
$pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
$pee = preg_replace( '|<p>|', "$1<p>", $pee );
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
@ -840,6 +841,7 @@ function wp_trim_excerpt($text) { // Fakes an excerpt if needed
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
$text = preg_replace('|//\s*<!\[CDATA\[|', '<![CDATA[', $text);
$text = strip_tags($text);
$excerpt_length = 55;
$words = explode(' ', $text, $excerpt_length + 1);

View File

@ -286,7 +286,7 @@ function image_get_intermediate_size($post_id, $size='thumbnail') {
if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
return false;
$data = $imagedata['sizes'][$size];
// include the full filesystem path of the intermediate file
if ( empty($data['path']) && !empty($data['file']) ) {
@ -300,7 +300,7 @@ function image_get_intermediate_size($post_id, $size='thumbnail') {
// get an image to represent an attachment - a mime icon for files, thumbnail or intermediate size for images
// returns an array (url, width, height), or false if no image is available
function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
// get a thumbnail or intermediate image if there is one
if ( $image = image_downsize($attachment_id, $size) )
return $image;
@ -327,11 +327,11 @@ function wp_get_attachment_image($attachment_id, $size='thumbnail', $icon = fals
$size = join('x', $size);
$html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';
}
return $html;
}
add_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'gallery_shortcode', true);
function gallery_shortcode($attr) {
global $post;
@ -376,7 +376,7 @@ function gallery_shortcode($attr) {
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$output = apply_filters('gallery_style', "
<style type='text/css'>
.gallery {

View File

@ -47,17 +47,18 @@ add_shortcode('baztag', 'baztag_func');
$shortcode_tags = array();
function add_shortcode($tag, $func) {
function add_shortcode($tag, $func, $after_formatting = false) {
global $shortcode_tags;
if ( is_callable($func) )
$shortcode_tags[$tag] = $func;
if ( is_callable($func) ) {
$shortcode_tags[($after_formatting)? 11:9][$tag] = $func;
}
}
function remove_shortcode($tag) {
global $shortcode_tags;
unset($shortcode_tags[$tag]);
unset($shortcode_tags[9][$tag], $shortcode_tags[11][$tag]);
}
function remove_all_shortcodes() {
@ -66,21 +67,37 @@ function remove_all_shortcodes() {
$shortcode_tags = array();
}
function do_shortcode($content) {
function do_shortcode_after_formatting($content) {
return do_shortcode($content, true);
}
function do_shortcode($content, $after_formatting = false) {
$pattern = get_shortcode_regex($after_formatting);
if (!$pattern) {
return $content;
} else {
$callback_func = 'do_shortcode_tag';
if ($after_formatting)
$callback_func .= '_after_formatting';
return preg_replace_callback('/' . $pattern . '/s', $callback_func, $content);
}
}
function get_shortcode_regex($after_formatting) {
global $shortcode_tags;
if (empty($shortcode_tags) || !is_array($shortcode_tags))
return $content;
if (empty($shortcode_tags[($after_formatting)? 11:9]) || !is_array($shortcode_tags[($after_formatting)? 11:9]))
return false;
$tagnames = array_keys($shortcode_tags);
$tagnames = array_keys($shortcode_tags[($after_formatting)? 11:9]);
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
$pattern = '/\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?/s';
return preg_replace_callback($pattern, 'do_shortcode_tag', $content);
return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
}
function do_shortcode_tag($m) {
function do_shortcode_tag_after_formatting($m) {
return do_shortcode_tag($m, true);
}
function do_shortcode_tag($m, $after_formatting = false) {
global $shortcode_tags;
$tag = $m[1];
@ -88,10 +105,10 @@ function do_shortcode_tag($m) {
if ( isset($m[4]) ) {
// enclosing tag - extra parameter
return call_user_func($shortcode_tags[$tag], $attr, $m[4]);
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr, $m[4]);
} else {
// self-closing tag
return call_user_func($shortcode_tags[$tag], $attr);
return call_user_func($shortcode_tags[($after_formatting)? 11:9][$tag], $attr);
}
}
@ -129,6 +146,7 @@ function shortcode_atts($pairs, $atts) {
return $out;
}
add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
add_filter( 'the_content', 'do_shortcode', 9 );
add_filter( 'the_content', 'do_shortcode_after_formatting', 11 );
?>