You are here

function customfilter_filter in Custom filter 6.2

Same name and namespace in other branches
  1. 5 customfilter.module \customfilter_filter()
  2. 6 customfilter.module \customfilter_filter()

Implements hook_filter().

1 string reference to 'customfilter_filter'
customfilter_update_6207 in ./customfilter.install
Implements hook_update_N().

File

./customfilter.module, line 100
Allow users to define custom filters.

Code

function customfilter_filter($op, $delta = 0, $format = -1, $text = '') {
  $filters = _customfilter_get_filters();
  switch ($op) {
    case 'description':
      return _customfilter_get_filter_descriptions($delta, 'description');
    case 'list':
      $flist = array();
      foreach ($filters as $filter) {
        $flist[$filter['fid']] = _customfilter_get_filter_descriptions($filter['fid'], 'name');
      }
      return $flist;
    case 'no cache':
      return isset($filters[$delta]['cache']) ? !$filters[$delta]['cache'] : TRUE;
    case 'process':

      // If the text passed is an empty string, then return it immediately.
      if (empty($text)) {
        return '';
      }

      // If the filter cannot be loaded from the database, then return the text
      // passed to the function.
      if (!isset($filters[$delta])) {
        return $text;
      }
      $globals =& _customfilter_globals('push');
      $globals->text = $text;
      $rules = _customfilter_get_rules($delta);
      if (is_array($rules) && count($rules)) {

        // Prepare the stack used to save the parent rule.
        $globals->stack = array();
        foreach ($rules as $rule) {
          if ($rule['enabled']) {
            $globals->stack[] = $rule;
            $globals->text = preg_replace_callback($rule['pattern'], '_customfilter_process', $globals->text);
            array_pop($globals->stack);
          }
        }
      }
      $result = $globals->text;
      _customfilter_globals('pop');
      return $result;
    default:
      return $text;
  }
}