Advertisement
Guest User

Shortcode from Wordpress for Codeigniter

a guest
Jun 26th, 2011
2,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.93 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('BASEPATH'))
  4.     exit('No direct script access allowed');
  5. /**
  6.      * API Shortcode do Wordpress adaptada para uma library do CI.
  7.      *
  8.      * A few examples are below:
  9.      *
  10.      * $this->load->library('shortcode');
  11.      * $this->shortcode->add('shortcode', 'myCodeParser');
  12.      * Atenção: "myCodeParser" deve estar disponível!
  13.      *
  14.      * [shortcode /]
  15.      * [shortcode foo="bar" baz="bing" /]
  16.      * [shortcode foo="bar"]content[/shortcode]
  17.      *
  18.      * Shortcode tags support attributes and enclosed content, but does not entirely
  19.      * support inline shortcodes in other shortcodes. You will have to call the
  20.      * shortcode parser in your function to account for that.
  21.      *
  22.      *
  23.      * To apply shortcode tags to content:
  24.      *
  25.      * $out = $this->shortcode->run($content);
  26.      */
  27. class Shortcode {
  28.  
  29.  
  30.     /**
  31.      * Container for storing shortcode tags and their hook to call for the shortcode
  32.      * @name $shortcode_tags
  33.      * @var array
  34.      */
  35.     public $shortcode_tags = array();
  36.  
  37.     /**
  38.      * Add hook for shortcode tag.
  39.      *
  40.      * There can only be one hook for each shortcode. Which means that if another
  41.      * plugin has a similar shortcode, it will override yours or yours will override
  42.      * theirs depending on which order the plugins are included and/or ran.
  43.      *
  44.      * Simplest example of a shortcode tag using the API:
  45.      *
  46.      * <code>
  47.      * // [footag foo="bar"]
  48.      * function footag_func($atts) {
  49.      *  return "foo = {$atts[foo]}";
  50.      * }
  51.      * $this->shortcode->add('footag', 'footag_func');
  52.      * </code>
  53.      *
  54.      * Example with nice attribute defaults:
  55.      *
  56.      * <code>
  57.      * // [bartag foo="bar"]
  58.      * function bartag_func($atts) {
  59.      *  extract(shortcode_atts(array(
  60.      *      'foo' => 'no foo',
  61.      *      'baz' => 'default baz',
  62.      *  ), $atts));
  63.      *
  64.      *  return "foo = {$foo}";
  65.      * }
  66.      * $this->shortcode->add('bartag', 'bartag_func');
  67.      * </code>
  68.      *
  69.      * Example with enclosed content:
  70.      *
  71.      * <code>
  72.      * // [baztag]content[/baztag]
  73.      * function baztag_func($atts, $content='') {
  74.      *  return "content = $content";
  75.      * }
  76.      * $this->shortcode->add('baztag', 'baztag_func');
  77.      * </code>
  78.      *
  79.      * @uses $shortcode_tags
  80.      *
  81.      * @param string $tag Shortcode tag to be searched in post content.
  82.      * @param callable $func Hook to run when shortcode is found.
  83.      */
  84.     function add($tag, $func) {
  85.  
  86.         if (is_callable($func))
  87.             $this->shortcode_tags[$tag] = $func;
  88.     }
  89.  
  90.     /**
  91.      * Removes hook for shortcode.
  92.      *
  93.      * @uses $shortcode_tags
  94.      *
  95.      * @param string $tag shortcode tag to remove hook for.
  96.      */
  97.     function remove($tag) {
  98.  
  99.         unset($this->shortcode_tags[$tag]);
  100.     }
  101.  
  102.     /**
  103.      * Clear all shortcodes.
  104.      *
  105.      * This function is simple, it clears all of the shortcode tags by replacing the
  106.      * shortcodes global by a empty array. This is actually a very efficient method
  107.      * for removing all shortcodes.
  108.      *
  109.       * @uses $shortcode_tags
  110.      */
  111.     function remove_all() {
  112.  
  113.         $this->shortcode_tags = array();
  114.     }
  115.  
  116.     /**
  117.      * Search content for shortcodes and filter shortcodes through their hooks.
  118.      *
  119.      * If there are no shortcode tags defined, then the content will be returned
  120.      * without any filtering. This might cause issues when plugins are disabled but
  121.      * the shortcode will still show up in the post or content.
  122.      *
  123.      * @uses $shortcode_tags
  124.      * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
  125.      *
  126.      * @param string $content Content to search for shortcodes
  127.      * @return string Content with shortcodes filtered out.
  128.      */
  129.     function run($content) {
  130.  
  131.         if (empty($this->shortcode_tags) || !is_array($this->shortcode_tags))
  132.             return $content;
  133.  
  134.         $pattern = $this->get_shortcode_regex();
  135.         return preg_replace_callback('/' . $pattern . '/s', 'Shortcode::do_shortcode_tag', $content);
  136.     }
  137.  
  138.     /**
  139.      * Retrieve the shortcode regular expression for searching.
  140.      *
  141.      * The regular expression combines the shortcode tags in the regular expression
  142.      * in a regex class.
  143.      *
  144.      * The regular expresion contains 6 different sub matches to help with parsing.
  145.      *
  146.      * 1/6 - An extra [ or ] to allow for escaping shortcodes with double [[]]
  147.      * 2 - The shortcode name
  148.      * 3 - The shortcode argument list
  149.      * 4 - The self closing /
  150.      * 5 - The content of a shortcode when it wraps some content.
  151.      *
  152.      * @uses $shortcode_tags
  153.      *
  154.      * @return string The shortcode search regular expression
  155.      */
  156.     function get_shortcode_regex() {
  157.  
  158.         $tagnames = array_keys($this->shortcode_tags);
  159.         $tagregexp = join('|', array_map('preg_quote', $tagnames));
  160.  
  161. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcodes()
  162.         return '(.?)\[(' . $tagregexp . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
  163.     }
  164.  
  165.     /**
  166.      * Regular Expression callable for do_shortcode() for calling shortcode hook.
  167.      * @see get_shortcode_regex for details of the match array contents.
  168.      *
  169.      * @access private
  170.      * @uses $shortcode_tags
  171.      *
  172.      * @param array $m Regular expression match array
  173.      * @return mixed False on failure.
  174.      */
  175.     function do_shortcode_tag($m) {
  176.  
  177.  
  178. // allow [[foo]] syntax for escaping a tag
  179.         if ($m[1] == '[' && $m[6] == ']') {
  180.             return substr($m[0], 1, -1);
  181.         }
  182.  
  183.         $tag = $m[2];
  184.         $attr = $this->shortcode_parse_atts($m[3]);
  185.  
  186.         if (isset($m[5])) {
  187. // enclosing tag - extra parameter
  188.             return $m[1] . call_user_func($this->shortcode_tags[$tag], $attr, $m[5], $tag) . $m[6];
  189.         } else {
  190. // self-closing tag
  191.             return $m[1] . call_user_func($this->shortcode_tags[$tag], $attr, NULL, $tag) . $m[6];
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Retrieve all attributes from the shortcodes tag.
  197.      *
  198.      * The attributes list has the attribute name as the key and the value of the
  199.      * attribute as the value in the key/value pair. This allows for easier
  200.      * retrieval of the attributes, since all attributes have to be known.
  201.      *
  202.      *
  203.      * @param string $text
  204.      * @return array List of attributes and their value.
  205.      */
  206.     function shortcode_parse_atts($text) {
  207.         $atts = array();
  208.         $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
  209.         $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
  210.         if (preg_match_all($pattern, $text, $match, PREG_SET_ORDER)) {
  211.             foreach ($match as $m) {
  212.                 if (!empty($m[1]))
  213.                     $atts[strtolower($m[1])] = stripcslashes($m[2]);
  214.                 elseif (!empty($m[3]))
  215.                     $atts[strtolower($m[3])] = stripcslashes($m[4]);
  216.                 elseif (!empty($m[5]))
  217.                     $atts[strtolower($m[5])] = stripcslashes($m[6]);
  218.                 elseif (isset($m[7]) and strlen($m[7]))
  219.                     $atts[] = stripcslashes($m[7]);
  220.                 elseif (isset($m[8]))
  221.                     $atts[] = stripcslashes($m[8]);
  222.             }
  223.         } else {
  224.             $atts = ltrim($text);
  225.         }
  226.         return $atts;
  227.     }
  228.  
  229.     /**
  230.      * Combine user attributes with known attributes and fill in defaults when needed.
  231.      *
  232.      * The pairs should be considered to be all of the attributes which are
  233.      * supported by the caller and given as a list. The returned attributes will
  234.      * only contain the attributes in the $pairs list.
  235.      *
  236.      * If the $atts list has unsupported attributes, then they will be ignored and
  237.      * removed from the final returned list.
  238.      *
  239.      *
  240.      * @param array $pairs Entire list of supported attributes and their defaults.
  241.      * @param array $atts User defined attributes in shortcode tag.
  242.      * @return array Combined and filtered attribute list.
  243.      */
  244.     function shortcode_atts($pairs, $atts) {
  245.         $atts = (array) $atts;
  246.         $out = array();
  247.         foreach ($pairs as $name => $default) {
  248.             if (array_key_exists($name, $atts))
  249.                 $out[$name] = $atts[$name];
  250.             else
  251.                 $out[$name] = $default;
  252.         }
  253.         return $out;
  254.     }
  255.  
  256.     /**
  257.      * Remove all shortcode tags from the given content.
  258.      *
  259.      * @uses $shortcode_tags
  260.      *
  261.      * @param string $content Content to remove shortcode tags.
  262.      * @return string Content without shortcode tags.
  263.      */
  264.     function strip_shortcodes($content) {
  265.  
  266.         if (empty($this->shortcode_tags) || !is_array($this->shortcode_tags))
  267.             return $content;
  268.  
  269.         $pattern = $this->get_shortcode_regex();
  270.  
  271.         return preg_replace('/' . $pattern . '/s', '$1$6', $content);
  272.     }
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement