You are here

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

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

/**
 * Implements hook_node_build_alter().
 */
function adsense_injector_node_build_alter($build) {
  if (TRUE) {
    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,
      ));
    }
  }
}

/**
 * Implements hook_node_view().
 *
 * 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_node_view($node, $view_mode) {

  //return;

  // TODO Remaining code in this function needs to be moved to the appropriate new hook function.
  // 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.

  /*&& node_is_page($node)*/
  if ($view_mode == 'full' && variable_get('adsense_injector_body_view', TRUE)) {

    //dpm($node);

    // 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['und'][0]['value'], $minwords));
    if ($wordcount >= $minwords) {

      // Process adsense module tags in the template text, if enabled and possible.
      $template = 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]');

      //dpm($template);
      if (function_exists('_adsense_process_tags')) {
        $template = _adsense_process_tags($template, null);
      }

      // $node->body['und'][0]['value'] = strtr($template, array('%body' => $node->body['und'][0]['value']));
      $node->content['body'][0]['#markup'] = strtr($template, array(
        '%body' => $node->content['body'][0]['#markup'],
      ));
    }
    else {
      $node->content['body'][0]['#markup'] .= "<!-- adsense_injector: node body word count ({$wordcount}) is insufficient ({$minwords} required), so we won't insert an ad. -->";
    }
  }
  elseif ($view_mode == 'teaser' && variable_get('adsense_injector_list_view', FALSE)) {

    // Process adsense module tags in the template text, if enabled and possible.

    //dpm($node, '$node');
    $template = variable_get('adsense_injector_list_view_template', '%teaser<br class="clear"/>[adsense:728x90:0123456789]');
    if (function_exists('_adsense_process_tags')) {
      $template = _adsense_process_tags($template, null);
    }
    $node->content['body'][0]['#markup'] = strtr($template, array(
      '%teaser' => $node->content['body'][0]['#markup'],
    ));
  }
}

/**
 * Implements hook_menu().
 */
function adsense_injector_menu() {

  // debug('here');
  $items = array();
  $items['admin/config/content/adsense_injector'] = array(
    'title' => 'AdSense Injector',
    'description' => 'Insert Google AdSense ads content 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 Implements hook_menu().
adsense_injector_node_build_alter Implements hook_node_build_alter().
adsense_injector_node_view Implements hook_node_view().