You are here

function adsense_display in Google AdSense integration 7

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. 6 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

See also

adsense_ad_formats()

_adsense_page_match()

_adsense_check_if_enabled()

theme_adsense_placeholder()

_adsense_cse_get_searchbox()

_adsense_search_get_searchbox()

_adsense_managed_get_ad()

_adsense_oldcode_get_ad()

5 calls to adsense_display()
adsense_cse_block_view in cse/adsense_cse.module
Implements hook_block_view().
adsense_managed_block_view in managed/adsense_managed.module
Implements hook_block_view().
adsense_oldcode_block_view in old/oldcode/adsense_oldcode.module
Implements hook_block_view().
adsense_search_block_view in old/search/adsense_search.module
Implements hook_block_view().
_adsense_process_tags in ./adsense.module
Implements callback_filter_process().

File

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

Code

function adsense_display() {
  drupal_add_css(drupal_get_path('module', 'adsense') . '/css/adsense.css');
  $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) {

      // Obsolete, referral [5] and cpa [6].
      case 6:
      case 5:
      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'], !empty($args['slot']));
  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_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.
      // @codingStandardsIgnoreStart Drupal.Semantics.FunctionT.ConcatString
      $text = isset($args['title']) ? t('Block') . ': ' . $args['title'] . '<br />' : '';

      // @codingStandardsIgnoreEnd
      $text .= variable_get('adsense_placeholder_text', ADSENSE_PLACEHOLDER_TEXT_DEFAULT) . ' ' . $args['format'];
      if (user_access('show adsense placeholders')) {
        $name = user_is_anonymous() ? variable_get('anonymous', t('Anonymous')) : $user->name;
        $text = t('Ads disabled for %name', array(
          '%name' => $name,
        )) . '<br />' . $text;
      }
      $ad = "<!--adsense: placeholder-->\n" . theme('adsense_placeholder', array(
        'text' => $text,
        'width' => $width,
        'height' => $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);
        }
        elseif (module_exists('adsense_search')) {
          $ad = _adsense_search_get_searchbox($client, $args['channel']);
        }
        else {
          $ad = '<!--adsense: no AdSense for Search module found-->';
        }
        break;
      case 'Search Box v2':
        if (!empty($slot) && module_exists('adsense_cse')) {
          $ad = _adsense_csev2_get_searchbox($client, $slot);
        }
        else {
          $ad = '<!--adsense: no AdSense for Search module found-->';
        }
        break;
      default:
        if (!empty($slot) && module_exists('adsense_managed')) {
          $shape = empty($args['shape']) ? 'auto' : $args['shape'];
          $layout_key = empty($args['layout_key']) ? '' : $args['layout_key'];
          $ad = _adsense_managed_get_ad($args['format'], $client, $slot, $shape, $layout_key);
          $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();
        }

        // If test mode, ad is already themed.
        if (!variable_get('adsense_test_mode', ADSENSE_TEST_MODE_DEFAULT)) {
          $ad = theme('adsense_ad', array(
            'ad' => $ad,
            'module' => $module,
            'format' => $args['format'],
          ));
        }
        break;
    }

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