You are here

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

Inject adsense ads into node content automatically.

File

adsense_injector.module
View source
<?php

/**
 * @file
 * Inject adsense ads into node content automatically.
 *
 * @ingroup adsense_injector
 */

/**
 * 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) {

  // 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 == 'view') {
    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->content['body']['#value'], $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->content['body']['#value'] = strtr($template, array(
          '%body' => $node->content['body']['#value'],
        ));
      }
      else {
        $node->content['body']['#value'] .= "<!-- 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->content['body']['#value'] = strtr($template, array(
        '%teaser' => $node->content['body']['#value'],
      ));
    }
  }
}

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

/**
 * Implementation of hook_settings().
 */
function adsense_injector_admin_settings() {

  // 'body view' insertion (i.e. show with node complete view).
  $form['body_view'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Node body ad insertion'),
    '#description' => t('Requires adsense.module'),
  );
  $form['body_view']['adsense_injector_body_view'] = array(
    '#type' => 'checkbox',
    '#title' => t('Insert inline ad in node body on page views'),
    '#default_value' => variable_get('adsense_injector_body_view', TRUE),
    '#description' => t('Description'),
    '#required' => FALSE,
  );
  $form['body_view']['adsense_injector_body_view_minwords'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum node body word count'),
    '#default_value' => variable_get('adsense_injector_body_view_minwords', 75),
    '#description' => t('The minimum node body word count threshold - only inject if node body has at least this many words.'),
  );
  $form['body_view']['adsense_injector_body_view_template'] = array(
    '#type' => 'textarea',
    '#title' => t('Node body ad insertion template'),
    '#rows' => 5,
    '#cols' => 40,
    '#default_value' => 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]'),
    '#description' => t('Ad insertion template. Substitution variables: %body = full node body text. Insert adsense module filter tags. See the <a href="/admin/settings/adsense">adsense.module settings page</a> for a list of supported formats and help with filter tags.'),
    '#required' => TRUE,
  );

  // 'list view' insertion (frontpage, taxonomy, etc).
  $form['list_view'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Node list ad insertion'),
    '#description' => t('Ad insertion in node lists or other non-page view, like front page, taxonomy views.'),
  );
  $form['list_view']['adsense_injector_list_view'] = array(
    '#type' => 'checkbox',
    '#title' => t('Append an ad after teaser on frontpage and taxonomy lists'),
    '#default_value' => variable_get('adsense_injector_list_view', FALSE),
    '#description' => t('Note: this does not currently support Views module based lists.'),
    '#required' => FALSE,
  );
  $form['list_view']['adsense_injector_list_view_template'] = array(
    '#type' => 'textarea',
    '#title' => t('List ad insertion template'),
    '#rows' => 3,
    '#cols' => 40,
    '#default_value' => variable_get('adsense_injector_list_view_template', '%teaser<br class="clear"/>[adsense:728x90:0123456789]'),
    '#description' => t('Template to use when inserting adsense ad. "%teaser" will be replaced with the node teaser. Insert adsense filter tags. See the <a href="/admin/settings/adsense">adsense.module settings page</a> for a list of supported formats and help with filter tags.'),
    '#required' => TRUE,
  );

  // What kinds of nodes do we want to insert on...?
  $form['content_types'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Content types'),
  );
  $form['content_types']['adsense_injector_nodes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Content types'),
    '#default_value' => variable_get('adsense_injector_nodes', array()),
    '#options' => array_map('check_plain', node_get_types('names')),
    '#description' => t('Select content types to display inline ads.'),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
adsense_injector_admin_settings Implementation of hook_settings().
adsense_injector_menu Implementation of hook_menu().
adsense_injector_nodeapi Implementation of hook_nodeapi().