You are here

function adsense_injector_nodeapi in Content Injector (formerly AdSense Injector) 6

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.2 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 62
Inject adsense ads into node content automatically.

Code

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

  // insert an ad into the body.
  if ($op == 'alter') {
    if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) {
      if ($page) {
        if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) {
          $body = $node->body;
          $minwords = _adsense_injector_minwords_cfg($node->type);
          $wordcount = _adsense_injector_count_words($body, $minwords);
          if ($wordcount >= $minwords) {
            $template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT);

            // Process adsense module tags in the template text, if enabled and possible.
            if (function_exists('_adsense_process_tags')) {
              $template = _adsense_process_tags($template);
            }
            else {
              watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
            }
            $node->body = strtr($template, array(
              '%body' => $body,
            ));
          }
          else {
            $node->body = "<!-- adsense_injector: node body word count ({$wordcount}) is insufficient ({$minwords} required), so we won't insert an ad. -->" . $body;
          }
        }
      }
      elseif ($teaser && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) {
        $template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT);

        // Process adsense module tags in the template text, if enabled and possible.
        if (function_exists('_adsense_process_tags')) {
          $template = _adsense_process_tags($template);
        }
        else {
          watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
        }
        $node->body = strtr($template, array(
          '%teaser' => $node->teaser,
        ));
      }
    }
  }
}