You are here

function _adsense_process_tags in Google AdSense integration 6

Same name and namespace in other branches
  1. 5.3 adsense.module \_adsense_process_tags()
  2. 5 adsense.module \_adsense_process_tags()
  3. 5.2 adsense.module \_adsense_process_tags()
  4. 7 adsense.module \_adsense_process_tags()

Helper function to process the adsense input filter.

Parameters

string $text: Text of the node being processed.

Return value

string modified text with the adsense tags replaced by Google AdSense ads

See also

adsense_filter()

adsense_display()

1 call to _adsense_process_tags()
adsense_filter in ./adsense.module
Implementation of hook_filter().

File

./adsense.module, line 244
Displays Google AdSense ads on Drupal pages.

Code

function _adsense_process_tags($text) {
  $patterns = array(
    'block' => '/\\[adsense:block:([^\\]]+)\\]/x',
    'oldtag' => '/\\[adsense:([^:]+):(\\d*):(\\d*):?(\\w*)\\]/x',
    'tag' => '/\\[adsense:([^:]+):([^\\]]+)\\]/x',
  );
  foreach ($patterns as $mode => $pattern) {
    if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        switch ($mode) {
          case 'block':
            $mods = array(
              'adsense_managed',
              'adsense_cse',
              'adsense_oldcode',
              'adsense_search',
            );
            foreach ($mods as $module) {
              $module_blocks = module_invoke($module, 'block', 'list');
              if ($module_blocks) {
                foreach ($module_blocks as $delta => $block) {
                  if ($block['info'] == $match[1]) {

                    // Found the block with the same name as the passed arg.
                    $block = module_invoke($module, 'block', 'view', $delta);
                    $ad = $block['content'];
                  }
                }
              }
            }
            break;
          case 'oldtag':

            // If not specified, default group and channel to 1
            if (empty($match[2])) {
              $match[2] = 1;
            }
            if (empty($match[3])) {
              $match[3] = 1;
            }
            $args = array(
              'format' => $match[1],
              'group' => $match[2],
              'channel' => $match[3],
              'slot' => $match[4],
            );
            $ad = adsense_display($args);
            unset($args);
            break;
          case 'tag':
            $args = array(
              'format' => $match[1],
              'slot' => $match[2],
            );
            $ad = adsense_display($args);
            unset($args);
            break;
        }

        // Replace the first occurance of the tag, in case we have the same
        // tag more than once.
        $str = '/\\' . $match[0] . '/';
        $text = preg_replace($str, $ad, $text, 1);
      }
    }
  }
  return $text;
}