You are here

function _shortcode_process in Shortcode 7

Same name and namespace in other branches
  1. 6 shortcode.module \_shortcode_process()
  2. 7.2 shortcode.module \_shortcode_process()

Process the shortcodes according to the text and the text format.

1 call to _shortcode_process()
shortcode_filter in ./shortcode.module
Implementation of hook_filter().

File

./shortcode.module, line 223

Code

function _shortcode_process($text, $format) {
  $shortcodes = shortcode_list_all();
  $enabled_names = array();
  foreach ($shortcodes as $name => $item) {
    $shortcodes[$name]->enabled = shortcode_shortcode_is_enabled($name, $format);
    if ($shortcodes[$name]->enabled) {
      $enabled_names[] = $name;
      $shortcodes[$name]->function = $shortcodes[$name]->module . '_shortcode_' . $name;
    }
  }
  if (!$enabled_names) {
    return $text;
  }

  // remove the p tags around the lines whitch contains shortcodes only

  //$text = preg_replace('!<p>(\[.*?\])<\/p>!is', '\\1', $text);

  // we have to insert a special chars between the shortcode elements because
  // without it it is not works korrectly

  //$text = preg_replace('!<p>\s*(\[.*?\])\s*<\/p>!is', '#!#\\1', $text);

  //$text = preg_replace('!(\[.*?\])!is', '#!#\\1', $text);

  // save the shortcodes
  _shortcode_tags($shortcodes);

  //dpr($shortcodes);

  // old, wp way

  //$tag_regexp = _shortcode_get_shortcode_regex($enabled_names);

  //$text = preg_replace_callback('/' . $tag_regexp . '/s', '_shortcode_process_tag', $text);

  //dpr('after process:');

  //dpr($text);

  //$text = preg_replace('/#!#/is', '', $text);

  // imroved version - recursive processing - embed tags within tags supported!
  $chunks = preg_split('!(\\[.*?\\])!', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

  //dpr($chunks);
  $heap = array();
  $heap_index = array();

  // array_shift — Shift an element off the beginning of array
  // mixed array_shift  ( array &$array  )
  // array_unshift — Prepend one or more elements to the beginning of an array
  // int array_unshift  ( array &$array  , mixed $var  [, mixed $...  ] )
  foreach ($chunks as $c) {
    if (!$c) {
      continue;
    }

    // shortcode or not
    if ($c[0] == '[' && substr($c, -1, 1) == ']') {

      // $c contains shortcode
      // self-closing tag or not
      $c = substr($c, 1, -1);

      //dpr('process: ' . $c);
      if (substr($c, -1, 1) == '/') {

        // process a self closing tag - it has / at the end!

        //dpr('self closing: ' . $c);

        /*
         * 0 - the full tag text?
         * 1/5 - An extra [ or ] to allow for escaping shortcodes with double [[]]
         * 2 - The shortcode name
         * 3 - The shortcode argument list
         * 4 - The content of a shortcode when it wraps some content.
         * */
        $ts = explode(' ', trim($c));
        $tag = array_shift($ts);
        $m = array(
          $c,
          '',
          $tag,
          implode(' ', $ts),
          NULL,
          '',
        );
        array_unshift($heap_index, '_string_');
        array_unshift($heap, _shortcode_process_tag($m));
      }
      elseif ($c[0] == '/') {

        // closing tag - process the heap
        $closing_tag = substr($c, 1);

        //dpr('closing tag: ' . $closing_tag );
        $process_heap = array();
        $process_heap_index = array();
        $found = FALSE;

        // get elements from heap and process
        do {
          $tag = array_shift($heap_index);
          $heap_text = array_shift($heap);
          if ($closing_tag == $tag) {

            // process the whole tag
            $m = array(
              $tag . ' ' . $heap_text,
              '',
              $tag,
              $heap_text,
              implode('', $process_heap),
              '',
            );
            $str = _shortcode_process_tag($m);

            //dpr($str);
            array_unshift($heap_index, '_string_');
            array_unshift($heap, $str);
            $found = TRUE;
          }
          else {
            array_unshift($process_heap, $heap_text);
            array_unshift($process_heap_index, $tag);
          }
        } while (!$found && $heap);
        if (!$found) {
          foreach ($process_heap as $val) {
            array_unshift($heap, $val);
          }
          foreach ($process_heap_index as $val) {
            array_unshift($heap_index, $val);
          }

          //array_unshift($heap_index, '_string_');

          //array_unshift($heap, 'notfound: ' . $closing_tag . ':' . implode('', $process_heap));
        }
      }
      else {

        // starting tag. put to the heap

        //dpr('tag pattern: ' . $c);
        $ts = explode(' ', trim($c));
        $tag = array_shift($ts);

        // dpr('start tag: ' . $tag);
        array_unshift($heap_index, $tag);
        array_unshift($heap, implode(' ', $ts));
      }
    }
    else {

      // not found a pair?
      array_unshift($heap_index, '_string_');
      array_unshift($heap, $c);
    }
  }
  return implode('', array_reverse($heap));

  //d(array_reverse($heap));

  //return $text;
}