You are here

adsense_oldcode.module in Google AdSense integration 7

Displays Google AdSense ads on Drupal pages.

This is the core module of the AdSense package, with the Drupal hooks and other administrative functions.

File

old/oldcode/adsense_oldcode.module
View source
<?php

/**
 * @file
 * Displays Google AdSense ads on Drupal pages.
 *
 * This is the core module of the AdSense package, with the Drupal hooks
 * and other administrative functions.
 */

// @codingStandardsIgnoreStart Drupal.Semantics.ConstantName.ConstantStart
define('ADSENSE_MAX_GROUPS', 5);
define('ADSENSE_AD_TYPE_DEFAULT', 0);
define('ADSENSE_ALT_DEFAULT', 0);
define('ADSENSE_ALT_INFO_DEFAULT', '');
define('ADSENSE_COLOR_BG_DEFAULT', '#FFFFFF');
define('ADSENSE_COLOR_BORDER_DEFAULT', '#336699');
define('ADSENSE_COLOR_LINK_DEFAULT', '#0000FF');
define('ADSENSE_COLOR_TEXT_DEFAULT', '#000000');
define('ADSENSE_COLOR_URL_DEFAULT', '#008000');
define('ADSENSE_GROUP_TITLE_DEFAULT', '');
define('ADSENSE_OLDCODE_AD_BLOCK_DEFAULT', '');
define('ADSENSE_OLDCODE_NUMBER_BLOCKS_DEFAULT', 3);
define('ADSENSE_UI_FEATURES_DEFAULT', 'rc:0');

// @codingStandardsIgnoreEnd

/**
 * Implements hook_menu().
 */
function adsense_oldcode_menu() {
  $items = array();
  $items['admin/config/services/adsense/oldcode'] = array(
    'title' => 'Old Code Ads',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_oldcode_settings',
    ),
    'access arguments' => array(
      'administer adsense',
    ),
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
    'file' => 'adsense_oldcode.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_block_info().
 */
function adsense_oldcode_block_info() {
  $blocks = array();
  $max = variable_get('adsense_oldcode_number_blocks', ADSENSE_OLDCODE_NUMBER_BLOCKS_DEFAULT);
  for ($count = 0; $count < $max; $count++) {
    if ($ad = _adsense_oldcode_get_block_config($count)) {
      $title = $ad[0];
    }
    else {
      $title = t('AdSense old code: unconfigured !d', array(
        '!d' => $count + 1,
      ));
    }
    $blocks[$count] = array(
      'info' => $title,
      'cache' => DRUPAL_NO_CACHE,
    );
  }
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function adsense_oldcode_block_configure($delta) {
  $ad = _adsense_oldcode_get_block_config($delta);
  $ad_list = array();
  foreach (adsense_ad_formats(NULL, FALSE) as $format => $data) {
    $ad_list[$format] = $format . ' : ' . $data['desc'];
  }
  $group_list = array();
  for ($group = 1; $group <= ADSENSE_MAX_GROUPS; $group++) {
    $group_list[$group] = $group . ' : ' . variable_get('adsense_group_title_' . $group, ADSENSE_GROUP_TITLE_DEFAULT);
  }
  $channel_list[''] = t('None');
  for ($channel = 1; $channel <= ADSENSE_MAX_CHANNELS; $channel++) {
    $channel_list[$channel] = $channel . ' : ' . variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
  }
  $form['info'] = array(
    '#type' => 'textfield',
    '#title' => t('Block description'),
    '#default_value' => $ad ? $ad[0] : '',
    '#maxlength' => 64,
    '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array(
      '@overview' => url('admin/structure/block'),
    )),
    '#required' => TRUE,
    '#weight' => -19,
  );
  $form['ad_format'] = array(
    '#type' => 'select',
    '#title' => t('Ad format'),
    '#default_value' => $ad ? $ad[1] : '250x250',
    '#options' => $ad_list,
    '#description' => t('Select the ad dimensions you want for this block.'),
  );
  $form['ad_group'] = array(
    '#type' => 'select',
    '#title' => t('Group'),
    '#default_value' => $ad ? $ad[2] : 1,
    '#options' => $group_list,
  );
  $form['ad_channel'] = array(
    '#type' => 'select',
    '#title' => t('Channel'),
    '#default_value' => $ad ? $ad[3] : 1,
    '#options' => $channel_list,
  );
  $form['ad_align'] = array(
    '#type' => 'select',
    '#title' => t('Ad alignment'),
    '#default_value' => $ad ? $ad[4] : 'center',
    '#options' => array(
      '' => t('None'),
      'left' => t('Left'),
      'center' => t('Centered'),
      'right' => t('Right'),
    ),
    '#description' => t('Select the horizontal alignment of the ad within the block.'),
  );
  return $form;
}

/**
 * Implements hook_block_save().
 */
function adsense_oldcode_block_save($delta, $edit) {
  $data = implode(':', array(
    urlencode(check_plain($edit['info'])),
    check_plain($edit['ad_format']),
    check_plain($edit['ad_group']),
    check_plain($edit['ad_channel']),
    check_plain($edit['ad_align']),
  ));
  variable_set('adsense_oldcode_ad_block_' . $delta, $data);
}

/**
 * Implements hook_block_view().
 */
function adsense_oldcode_block_view($delta) {
  $block = array();
  if (_adsense_page_match()) {
    $ad = _adsense_oldcode_get_block_config($delta);
    $block['content'] = $ad ? adsense_display(array(
      'title' => $ad[0],
      'format' => $ad[1],
      'group' => $ad[2],
      'channel' => $ad[3],
    )) : _adsense_configure_block_message(url('admin/structure/block/manage/adsense_oldcode/' . $delta));
    if (!empty($ad[4])) {
      $block['content'] = "<div style='text-align:{$ad[4]}'>{$block['content']}</div>";
    }
  }
  return $block;
}

/**
 * Configuration of the provided block.
 *
 * @param int $delta
 *   Block number.
 *
 * @return array|bool
 *   array with the block configuration or FALSE if no such block was found
 */
function _adsense_oldcode_get_block_config($delta = 0) {
  if ($data = variable_get('adsense_oldcode_ad_block_' . $delta, ADSENSE_OLDCODE_AD_BLOCK_DEFAULT)) {
    $ad = explode(':', $data);
    $ad[0] = urldecode($ad[0]);
    return $ad;
  }
  return FALSE;
}

/**
 * Generates the AdSense ad.
 *
 * @param string $format
 *   Format of the ad.
 * @param string $client
 *   Publisher ID.
 * @param int $group
 *   Group ID of the configured color attributes group (optional).
 * @param int $channel
 *   Channel ID of the configured Ad Channel (optional).
 *
 * @return string
 *   JavaScript that embeds the Google AdSense ad
 */
function _adsense_oldcode_get_ad($format, $client, $group = 1, $channel = 1) {
  $ad = adsense_ad_formats($format, FALSE);
  if ($ad === NULL) {
    $output = "";
  }
  elseif (variable_get('adsense_test_mode', ADSENSE_TEST_MODE_DEFAULT)) {
    $output = theme('adsense_placeholder', array(
      'text' => "client = {$client}<br />format = {$format}<br />width = {$ad['width']}<br />height = {$ad['height']}",
      'width' => $ad['width'],
      'height' => $ad['height'],
    ));
  }
  else {
    if ($group < 1 || $group > ADSENSE_MAX_GROUPS) {

      // Default to 1 if an invalid group is supplied.
      $group = 1;
    }
    switch (variable_get('adsense_ad_type_' . $group, ADSENSE_AD_TYPE_DEFAULT)) {
      case 2:
        $type = 'text_image';
        break;
      case 1:
        $type = 'image';
        break;
      default:
        $type = 'text';
        break;
    }
    $channel = variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
    $border = drupal_substr(variable_get('adsense_color_border_' . $group, ADSENSE_COLOR_BORDER_DEFAULT), 1);
    $bg = drupal_substr(variable_get('adsense_color_bg_' . $group, ADSENSE_COLOR_BG_DEFAULT), 1);
    $link = drupal_substr(variable_get('adsense_color_link_' . $group, ADSENSE_COLOR_LINK_DEFAULT), 1);
    $url = drupal_substr(variable_get('adsense_color_url_' . $group, ADSENSE_COLOR_URL_DEFAULT), 1);
    $text = drupal_substr(variable_get('adsense_color_text_' . $group, ADSENSE_COLOR_TEXT_DEFAULT), 1);
    $corner = variable_get('adsense_ui_features_' . $group, ADSENSE_UI_FEATURES_DEFAULT);
    $alt = variable_get('adsense_alt_' . $group, ADSENSE_ALT_DEFAULT);
    $alt_info = variable_get('adsense_alt_info_' . $group, ADSENSE_ALT_INFO_DEFAULT);
    switch ($alt) {
      case 1:
        $part1 = "google_alternate_ad_url = \"{$alt_info}\";";
        break;
      case 2:
        $part1 = "google_alternate_color = \"{$alt_info}\";";
        break;
      case 0:
      default:

        // Disabled.
        $part1 = "";
    }
    $part2 = "";
    if ($ad['type'] == ADSENSE_TYPE_AD) {
      $part2 .= "google_ad_type = \"{$type}\";\n";
    }
    if (!empty($channel)) {
      $part2 .= "google_ad_channel = \"{$channel}\";";
    }
    $secret = '';
    if ($lang = variable_get('adsense_secret_language', ADSENSE_SECRET_LANGUAGE_DEFAULT)) {
      $secret = "google_language = '{$lang}';\n";
    }
    $output = <<<OLDCODE_TXT
<script type="text/javascript"><!--
google_ad_client = "{<span class="php-variable">$client</span>}";
{<span class="php-variable">$part1</span>}
google_ad_width = {<span class="php-variable">$ad</span>[<span class="php-string">'width'</span>]};
google_ad_height = {<span class="php-variable">$ad</span>[<span class="php-string">'height'</span>]};
google_ad_format = "{<span class="php-variable">$ad</span>[<span class="php-string">'code'</span>]}";
{<span class="php-variable">$part2</span>}
google_color_border = "{<span class="php-variable">$border</span>}";
google_color_bg = "{<span class="php-variable">$bg</span>}";
google_color_link = "{<span class="php-variable">$link</span>}";
google_color_text = "{<span class="php-variable">$text</span>}";
google_color_url = "{<span class="php-variable">$url</span>}";
google_ui_features = "{<span class="php-variable">$corner</span>}";
{<span class="php-variable">$secret</span>}//--></script>
<script type="text/javascript"
  src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
OLDCODE_TXT;
  }
  return $output;
}