You are here

function adsense_injector_nodeapi in Content Injector (formerly AdSense Injector) 6.2

Same name and namespace in other branches
  1. 5.2 adsense_injector.module \adsense_injector_nodeapi()
  2. 5 adsense_injector.module \adsense_injector_nodeapi()
  3. 6.3 adsense_injector.module \adsense_injector_nodeapi()
  4. 6 adsense_injector.module \adsense_injector_nodeapi()

Implementation of hook_nodeapi().

If rendering a full page, and the node type one of the configured types, inject configured adsense content using simple string concatenation.

@todo: Evaluate efficiency of string concat vs. sprintf, other methods.

File

./adsense_injector.module, line 18
Inject adsense ads into node content automatically.

Code

function adsense_injector_nodeapi(&$node, $op, $teaser, $page) {

  // We only consider content types which are enabled for inline adsense.
  $node_types = variable_get('adsense_injector_nodes', array());
  if (empty($node_types[$node->type])) {
    return;
  }

  // insert an ad into the body.
  if ($op == 'alter') {
    if ($page && variable_get('adsense_injector_body_view', TRUE)) {

      // Get the minimum node body wordcount for insertion.
      $minwords = variable_get('adsense_injector_body_view_minwords', 75);

      // Count words in a string.
      // lifted from node.module node_validate() function.
      $wordcount = count(explode(' ', $node->body, $minwords));
      if ($wordcount >= $minwords) {

        // Process adsense module tags in the template text, if enabled and possible.
        $template = _adsense_process_tags(variable_get('adsense_injector_body_view_template', '<div style="float: right; margin: 0; padding: 0 1em .25em 0;">[adsense:250x250:0123456789]</div>%body<br class="clear"/>[adsense:728x90:0123456789]'));
        $node->body = strtr($template, array(
          '%body' => $node->body,
        ));
      }
      else {
        $node->body .= "<!-- adsense_injector: node body word count ({$wordcount}) is insufficient ({$minwords} required), so we won't insert an ad. -->";
      }
    }
    elseif ($teaser && variable_get('adsense_injector_list_view', FALSE)) {

      // Process adsense module tags in the template text, if enabled and possible.
      $template = _adsense_process_tags(variable_get('adsense_injector_list_view_template', '%teaser<br class="clear"/>[adsense:728x90:0123456789]'));
      $node->teaser = strtr($template, array(
        '%teaser' => $node->teaser,
      ));
    }
  }
}