You are here

adsense_injector.module in Content Injector (formerly AdSense Injector) 6

Inject adsense ads into node content automatically.

File

adsense_injector.module
View source
<?php

/**
 * @file
 * Inject adsense ads into node content automatically.
 */
define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', TRUE);
define('ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT', '<div class="ad-auto-inserted" style="float:left; margin: 0 1em .25em 0;">[adsense:120x240:1:1]</div>%body<br class="clear"/>[adsense:468x60:1:1]');
define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);
define('ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT', FALSE);
define('ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT', '%teaser<div class="adsense-injector-list-ad">[adsense:468x60:1:1]</div>');

/**
 * Prefix for variable table entries - append node type name, store as boolean
 * value, nonzero means insert ad content.
 */
define('ADSENSE_INJECTOR_INSERT_AD_NODETYPE', 'adsense_injector_nodetype_');

/**
 * Count words in a string.
 *
 * @param $str
 *   Target string.
 * @param $max
 *   Maximum number of words we care about. Return value will never exceed
 *   this.
 * @return
 *   The count of words, where delimiter is one or more spaces.
 * @todo Efficiency, find better way to do this.
 */
function _adsense_injector_count_words($str, $max) {

  // lifted from node.module node_validate() function.
  return count(explode(' ', $str, $max));
}

/**
 * Get the minimum node body wordcount for insertion.
 *
 * May be content-type specific, but at present, it's global to all node
 * types.
 *
 * @param $nodetype
 *   The node type.
 * @param $defval
 *   The default value.
 * @return
 *   The minimum insertion wordcount.
 */
function _adsense_injector_minwords_cfg($nodetype, $defval = 75) {
  return variable_get('adsense_injector_body_minwords', $defval);
}

/**
 * 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.
 */
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,
        ));
      }
    }
  }
}

/**
 * Implementation of hook_menu().
 */
function adsense_injector_menu() {
  $items['admin/settings/adsense_injector'] = array(
    'title' => 'AdSense Injector',
    'description' => 'Insert Google AdSense ads into full node views automatically.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_injector_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'adsense_injector.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

Functions

Namesort descending Description
adsense_injector_menu Implementation of hook_menu().
adsense_injector_nodeapi Implementation of hook_nodeapi().
_adsense_injector_count_words Count words in a string.
_adsense_injector_minwords_cfg Get the minimum node body wordcount for insertion.

Constants

Namesort descending Description
ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT
ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT
ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT
ADSENSE_INJECTOR_INSERT_AD_NODETYPE Prefix for variable table entries - append node type name, store as boolean value, nonzero means insert ad content.
ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT @file Inject adsense ads into node content automatically.
ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT