You are here

adsense_managed.module in Google AdSense integration 6

Same filename and directory in other branches
  1. 5.3 managed/adsense_managed.module
  2. 7 managed/adsense_managed.module

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

managed/adsense_managed.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.
 */
define('ADSENSE_MANAGED_AD_BLOCK_DEFAULT', '');
define('ADSENSE_MANAGED_NUMBER_BLOCKS_DEFAULT', 3);

/**
 * Implementation of hook_menu().
 */
function adsense_managed_menu() {
  $items = array();
  $items['admin/settings/adsense/managed'] = array(
    'title' => 'Managed Ads',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_managed_settings',
    ),
    'access arguments' => array(
      'administer adsense',
    ),
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'file' => 'adsense_managed.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function adsense_managed_theme_registry_alter(&$theme_registry) {
  $theme_registry['block']['theme paths'][] = drupal_get_path('module', 'adsense_managed');
}

/**
 * Implementation of hook_block().
 */
function adsense_managed_block($op = 'list', $delta = 0, $edit = array()) {
  $block = NULL;
  switch ($op) {
    case 'list':
      $max = variable_get('adsense_managed_number_blocks', ADSENSE_MANAGED_NUMBER_BLOCKS_DEFAULT);
      for ($count = 0; $count < $max; $count++) {
        if ($ad = _adsense_managed_get_block_config($count)) {
          $title = $ad[0];
        }
        else {
          $title = t('AdSense: unconfigured !d', array(
            '!d' => $count + 1,
          ));
        }
        $block[$count]['info'] = $title;
        $block[$count]['cache'] = BLOCK_NO_CACHE;
      }
      break;
    case 'configure':
      $ad = _adsense_managed_get_block_config($delta);
      foreach (adsense_ad_formats() as $format => $data) {
        $ad_list[$format] = $format . ' : ' . $data['desc'];
      }
      $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/build/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.'),
        '#required' => TRUE,
      );
      $form['ad_slot'] = array(
        '#type' => 'textfield',
        '#title' => t('Ad Slot ID'),
        '#default_value' => $ad ? $ad[2] : '',
        '#description' => t('This is the Ad Slot ID from your Google Adsense account, such as 0123456789.'),
        '#required' => TRUE,
      );
      $form['ad_align'] = array(
        '#type' => 'select',
        '#title' => t('Ad alignment'),
        '#default_value' => $ad ? $ad[3] : '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;
    case 'save':
      $data = implode(':', array(
        urlencode($edit['info']),
        $edit['ad_format'],
        $edit['ad_slot'],
        $edit['ad_align'],
      ));
      variable_set('adsense_managed_ad_block_' . $delta, $data);
      return;
    case 'view':
      if (_adsense_page_match()) {
        $ad = _adsense_managed_get_block_config($delta);
        $block['content'] = $ad ? adsense_display(array(
          'title' => $ad[0],
          'format' => $ad[1],
          'slot' => $ad[2],
        )) : t('AdSense unconfigured block. <a href=!url>Click to configure.</a>', array(
          '!url' => url('admin/build/block/configure/adsense_managed/' . $delta),
        ));
        if (!empty($ad[3])) {
          $block['content'] = "<div style='text-align:{$ad[3]}'>{$block['content']}</div>";
        }
      }
      break;
  }
  return $block;
}

/**
 * Configuration of the provided block.
 *
 * @param int $delta
 *   Block number.
 *
 * @return array
 *   array with the block configuration or FALSE if no such block was found
 */
function _adsense_managed_get_block_config($delta = 0) {
  if ($data = variable_get('adsense_managed_ad_block_' . $delta, ADSENSE_MANAGED_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 $slot
 *   Slot Id for the AdSense ad.
 *
 * @return string
 *   JavaScript that embeds the Google AdSense ad
 */
function _adsense_managed_get_ad($format, $client, $slot) {
  $ad = adsense_ad_formats($format);
  if ($ad === NULL || empty($slot)) {
    $output = "";
  }
  elseif (variable_get('adsense_test_mode', ADSENSE_TEST_MODE_DEFAULT)) {
    $output = theme('adsense_placeholder', "client = {$client}<br />slot = {$slot}<br />width = {$ad['width']}<br />height = {$ad['height']}", $ad['width'], $ad['height']);
  }
  else {
    $secret = '';
    if ($lang = variable_get('adsense_secret_language', ADSENSE_SECRET_LANGUAGE_DEFAULT)) {
      $secret = "google_language = '{$lang}';";
    }
    $output = <<<MANAGED_TXT
<script type="text/javascript"><!--
google_ad_client = "ca-{<span class="php-variable">$client</span>}";
/* {<span class="php-variable">$format</span>} */
google_ad_slot = "{<span class="php-variable">$slot</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>]};
{<span class="php-variable">$secret</span>}
//-->
</script>
<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
MANAGED_TXT;
  }
  return $output;
}

Functions

Namesort descending Description
adsense_managed_block Implementation of hook_block().
adsense_managed_menu Implementation of hook_menu().
adsense_managed_theme_registry_alter Implementation of hook_theme_registry_alter().
_adsense_managed_get_ad Generates the AdSense ad.
_adsense_managed_get_block_config Configuration of the provided block.

Constants

Namesort descending Description
ADSENSE_MANAGED_AD_BLOCK_DEFAULT @file Displays Google AdSense ads on Drupal pages.
ADSENSE_MANAGED_NUMBER_BLOCKS_DEFAULT