You are here

function adsense_display in Google AdSense integration 6

Same name and namespace in other branches
  1. 8 adsense.module \adsense_display()
  2. 5.3 adsense.module \adsense_display()
  3. 5 adsense.module \adsense_display()
  4. 5.2 adsense.module \adsense_display()
  5. 7 adsense.module \adsense_display()

Generates the Google AdSense Ad.

This function is capable of handling two types of arguments: 1. an array of arguments (format, group, channel or slot) 2. 0 to 4 arguments:

  • 1st arg: format (default '160x600')
  • 2nd arg: group (default 1)
  • 3rd arg: channel (default 1)
  • 4th arg: slot (default '')

A valid format must always be provided. If a slot is provided, the ad is generated by the new format modules, if not then the 'old' format modules are attempted.

Return value

string Publisher ID string

See also

adsense_ad_formats()

_adsense_page_match()

_adsense_check_if_enabled()

theme_adsense_placeholder()

_adsense_can_insert_another()

_adsense_cse_get_searchbox()

_adsense_search_get_searchbox()

_adsense_managed_get_ad()

_adsense_oldcode_get_ad()

5 calls to adsense_display()
adsense_cse_block in cse/adsense_cse.module
Implementation of hook_block().
adsense_managed_block in managed/adsense_managed.module
Implementation of hook_block().
adsense_oldcode_block in old/oldcode/adsense_oldcode.module
Implementation of hook_block().
adsense_search_block in old/search/adsense_search.module
Implementation of hook_block().
_adsense_process_tags in ./adsense.module
Helper function to process the adsense input filter.

File

./adsense.module, line 371
Displays Google AdSense ads on Drupal pages.

Code

function adsense_display() {
  $numargs = func_num_args();
  if ($numargs == 1 && is_array(func_get_arg(0))) {
    $args = func_get_arg(0);
  }
  else {

    // Handle the 'old' method of calling this function.
    // adsense_display($format = '160x600', $group = 1, $channel = 1, $slot = '', $referral = 0, $cpa = '')
    $args['format'] = '160x600';
    $args['group'] = 1;
    $args['channel'] = 1;
    switch ($numargs) {
      case 6:

      // CPA isn't used anymore.
      case 5:

      // Referral is obsolete.
      case 4:
        $args['slot'] = func_get_arg(3);
      case 3:
        $args['channel'] = func_get_arg(2);
      case 2:
        $args['group'] = func_get_arg(1);
      case 1:
        $args['format'] = func_get_arg(0);
    }
  }
  $ad = adsense_ad_formats($args['format']);
  if ($ad === NULL) {
    $ad = '<!--adsense: invalid format: ' . $args['format'] . '-->';
  }
  elseif (!_adsense_page_match()) {

    // Check first if disabled or if we are at adsense limit or if this page
    // doesn't allow adsense.
    $ad = '<!--adsense: page not in match list-->';
  }
  elseif (!_adsense_can_insert_another($ad['type'])) {
    $ad = '<!--adsense: ad limit reached for type-->';
  }
  elseif (!_adsense_check_if_enabled()) {
    global $user;

    // Ads are disabled.
    if (variable_get('adsense_placeholder', ADSENSE_PLACEHOLDER_DEFAULT) || ($user->uid == 1 || user_access('show adsense placeholders'))) {
      $width = array_key_exists('width', $ad) ? $ad['width'] : 0;
      $height = array_key_exists('height', $ad) ? $ad['height'] : 0;

      // The text to display in the placeholder starts with the block title,
      // and then the default text as specified in the admin settings.
      $text = isset($args['title']) ? t('Block') . ': ' . $args['title'] . '<br />' : '';
      $text .= variable_get('adsense_placeholder_text', ADSENSE_PLACEHOLDER_TEXT_DEFAULT) . ' ' . $args['format'];
      if (isset($user) && $user->uid == 1 || user_access('show adsense placeholders')) {
        $text = t('Ads disabled for %name', array(
          '%name' => $user->name,
        )) . '<br />' . $text;
      }
      $ad = "<!--adsense: placeholder-->\n" . theme('adsense_placeholder', $text, $width, $height);
    }
    else {
      $ad = '<!--adsense: ads disabled -->';
    }
  }
  else {

    // If site Slot ID for this ad was passed, pass the format as argument
    // in case Publisher ID modules are enabled that can return different.
    // Slot IDs per ad format.
    $client_id_arg = !empty($args['slot']) ? $args['format'] : NULL;
    $client = adsense_get_client_slot_id($client_id_arg);
    if (is_array($client)) {

      // An array was received, use that Slot ID.
      $slot = $client['slot'];
      $client = $client['client'];
    }
    elseif (isset($args['slot'])) {

      // Use the original site Slot ID.
      $slot = $args['slot'];
    }

    // Ad should be displayed.
    switch ($args['format']) {
      case 'Search Box':
        if (!empty($slot) && module_exists('adsense_cse')) {
          $ad = _adsense_cse_get_searchbox($client, $slot);
          $module = 'adsense_cse';
        }
        elseif (module_exists('adsense_search')) {
          $ad = _adsense_search_get_searchbox($client, $args['channel']);
          $module = 'adsense_search';
        }
        else {
          $ad = '<!--adsense: no AdSense for Search module found-->';
        }
        break;
      default:
        if (!empty($slot) && module_exists('adsense_managed')) {
          $ad = _adsense_managed_get_ad($args['format'], $client, $slot);
          $module = 'adsense_managed';
        }
        elseif (module_exists('adsense_oldcode')) {
          $ad = _adsense_oldcode_get_ad($args['format'], $client, $args['group'], $args['channel']);
          $module = 'adsense_oldcode';
        }
        else {
          $ad = '<!--adsense: no AdSense for Content module found-->';
          $module = '';
        }

        // Display ad-block disabling request.
        if (variable_get('adsense_unblock_ads', ADSENSE_UNBLOCK_ADS_DEFAULT)) {
          adsense_request_unblock();
        }
        $ad = theme('adsense_ad', $ad, $module, $args['format']);
        break;
    }

    // Remove empty lines.
    $ad = str_replace("\n\n", "\n", $ad);
  }
  return $ad;
}