Index: caption_filter.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/caption_filter/caption_filter.module,v retrieving revision 1.3 diff -u -p -r1.3 caption_filter.module --- caption_filter.module 1 Feb 2010 04:53:21 -0000 1.3 +++ caption_filter.module 3 Dec 2010 09:50:01 -0000 @@ -56,12 +56,10 @@ function caption_filter_filter_tips($del } function caption_filter_process($text, $format = -1) { - if (stristr($text, '[caption align=') !== FALSE || stristr($text, '[caption]') !== FALSE) { // prevents useless processing - $pattern = "/(\[(caption( align=(left|center|right))?)[^\]]*\])(.*?)(\[\/caption\])/"; - $text = preg_replace_callback($pattern, '_caption_filter_replace', $text); - } - - return $text; + global $shortcode_tags; + module_load_include('inc', 'caption_filter', 'shortcodes'); + add_shortcode('caption', '_caption_filter_replace'); + return do_shortcode($text); } /** @@ -69,52 +67,21 @@ function caption_filter_process($text, $ * It returns a replacement pattern. * */ -function _caption_filter_replace($matches) { - $item = $matches[5]; - $align = $matches[4]; - - $width = _caption_filter_get_width($item); - - // [caption] items do not have an alignment - if (!$align) { - $align = 'center'; +function _caption_filter_replace($matches, $content) { + // To handle "alignleft"-style align strings + $matches['align'] = str_replace('align', '', $matches['align']); + if (!$matches['align'] || !in_array($matches['align'], array('center', 'left', 'right'))) { + $matches['align'] = 'center'; } - - return '
'. $item .'
'; -} -/** - * A function to determine what the width of the img/object that is being - * captioned - */ -function _caption_filter_get_width($item) { - // find an img or object tag within the [caption] to determine the width - if (preg_match_all('/<(img|object)[^>]+>/i', $item, $internal_result)) { - foreach($internal_result['0'] as $tag) { - // If the width is defined, dump that into the $widths array - if (preg_match_all('/width="([0-9]*)"/i', $tag, $width_result)) { - $width = $width_result[1][0]; - } - else { // we're going to have to find the image src - preg_match('/src="([^"]*)"/i', $tag, $src_result); - if (!empty($src_result)) { // if there is a src tag - list($width) = getimagesize($src_result[1]); - } - else { - // We cannot determine the width so just set it to the default - // css value - $width = 'auto'; - } - } - } + if (!$matches['width'] || !is_numeric($matches['width'])) { + $matches['width'] = '300'; } - //free up memory - unset($src_result, $width_result, $internal_result, $tag, $item); - // We need to append the 'px' to any numeric widths - if (is_numeric($width)) { - $width = $width . 'px'; + if (!isset($matches['caption'])) { + $matches['caption'] = ''; } + $matches['caption'] = filter_xss($matches['caption']); - return $width; -} \ No newline at end of file + return '
'. $content . $matches['caption'] . '
'; +} Index: shortcodes.inc =================================================================== RCS file: shortcodes.inc diff -N shortcodes.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ shortcodes.inc 3 Dec 2010 09:50:01 -0000 @@ -0,0 +1,216 @@ + + * $out = do_shortcode($content); + * + * + * @link http://codex.wordpress.org/Shortcode_API + * + * @package WordPress + * @subpackage Shortcodes + * @since 2.5 + */ + +/** + * Container for storing shortcode tags and their hook to call for the shortcode + * + * @since 2.5 + * @name $shortcode_tags + * @var array + * @global array $shortcode_tags + */ +$shortcode_tags = array(); + +/** + * Add hook for shortcode tag. + * + * There can only be one hook for each shortcode. Which means that if another + * plugin has a similar shortcode, it will override yours or yours will override + * theirs depending on which order the plugins are included and/or ran. + * + * Simplest example of a shortcode tag using the API: + * + * + * // [footag foo="bar"] + * function footag_func($atts) { + * return "foo = {$atts[foo]}"; + * } + * add_shortcode('footag', 'footag_func'); + * + * + * Example with nice attribute defaults: + * + * + * // [bartag foo="bar"] + * function bartag_func($atts) { + * extract(shortcode_atts(array( + * 'foo' => 'no foo', + * 'baz' => 'default baz', + * ), $atts)); + * + * return "foo = {$foo}"; + * } + * add_shortcode('bartag', 'bartag_func'); + * + * + * Example with enclosed content: + * + * + * // [baztag]content[/baztag] + * function baztag_func($atts, $content='') { + * return "content = $content"; + * } + * add_shortcode('baztag', 'baztag_func'); + * + * + * @since 2.5 + * @uses $shortcode_tags + * + * @param string $tag Shortcode tag to be searched in post content. + * @param callable $func Hook to run when shortcode is found. + */ +function add_shortcode($tag, $func) { + global $shortcode_tags; + + if ( is_callable($func) ) + $shortcode_tags[$tag] = $func; +} + +/** + * Search content for shortcodes and filter shortcodes through their hooks. + * + * If there are no shortcode tags defined, then the content will be returned + * without any filtering. This might cause issues when plugins are disabled but + * the shortcode will still show up in the post or content. + * + * @since 2.5 + * @uses $shortcode_tags + * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes. + * + * @param string $content Content to search for shortcodes + * @return string Content with shortcodes filtered out. + */ +function do_shortcode($content) { + global $shortcode_tags; + + if (empty($shortcode_tags) || !is_array($shortcode_tags)) + return $content; + + $pattern = get_shortcode_regex(); + return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content); +} + +/** + * Retrieve the shortcode regular expression for searching. + * + * The regular expression combines the shortcode tags in the regular expression + * in a regex class. + * + * The regular expresion contains 6 different sub matches to help with parsing. + * + * 1/6 - An extra [ or ] to allow for escaping shortcodes with double [[]] + * 2 - The shortcode name + * 3 - The shortcode argument list + * 4 - The self closing / + * 5 - The content of a shortcode when it wraps some content. + * + * @since 2.5 + * @uses $shortcode_tags + * + * @return string The shortcode search regular expression + */ +function get_shortcode_regex() { + global $shortcode_tags; + $tagnames = array_keys($shortcode_tags); + $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); + + // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcodes() + return '(.?)\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)'; +} + +/** + * Regular Expression callable for do_shortcode() for calling shortcode hook. + * @see get_shortcode_regex for details of the match array contents. + * + * @since 2.5 + * @access private + * @uses $shortcode_tags + * + * @param array $m Regular expression match array + * @return mixed False on failure. + */ +function do_shortcode_tag( $m ) { + global $shortcode_tags; + + // allow [[foo]] syntax for escaping a tag + if ( $m[1] == '[' && $m[6] == ']' ) { + return substr($m[0], 1, -1); + } + + $tag = $m[2]; + $attr = shortcode_parse_atts( $m[3] ); + + if ( isset( $m[5] ) ) { + // enclosing tag - extra parameter + return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6]; + } else { + // self-closing tag + return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, NULL, $tag ) . $m[6]; + } +} + +/** + * Retrieve all attributes from the shortcodes tag. + * + * The attributes list has the attribute name as the key and the value of the + * attribute as the value in the key/value pair. This allows for easier + * retrieval of the attributes, since all attributes have to be known. + * + * @since 2.5 + * + * @param string $text + * @return array List of attributes and their value. + */ +function shortcode_parse_atts($text) { + $atts = array(); + $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/'; + $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text); + if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) { + foreach ($match as $m) { + if (!empty($m[1])) + $atts[strtolower($m[1])] = stripcslashes($m[2]); + elseif (!empty($m[3])) + $atts[strtolower($m[3])] = stripcslashes($m[4]); + elseif (!empty($m[5])) + $atts[strtolower($m[5])] = stripcslashes($m[6]); + elseif (isset($m[7]) and strlen($m[7])) + $atts[] = stripcslashes($m[7]); + elseif (isset($m[8])) + $atts[] = stripcslashes($m[8]); + } + } else { + $atts = ltrim($text); + } + return $atts; +}