View source
<?php
function google_admanager_block($op = 'list', $delta = 0, $edit = array()) {
$id = variable_get('google_admanager_account', '');
if (!empty($id)) {
$ad_slots = variable_get('google_admanager_ad_slots', '');
$ad_slots = explode("\n", str_replace(array(
"\r",
"\t",
"\0",
"\v",
" ",
), '', $ad_slots));
if ($op == 'list') {
foreach ($ad_slots as $ad_slot) {
$blocks[] = array(
'info' => 'Ad Slot: ' . $ad_slot,
);
}
return $blocks;
}
else {
if ($op == 'view') {
if ($ad_slot = $ad_slots[$delta]) {
$block = array(
'subject' => '',
'content' => theme('google_admanager_block', $id, $ad_slot),
);
}
return $block;
}
}
}
}
function google_admanager_perm() {
return array(
'administer google admanager',
);
}
function google_admanager_menu($may_cache) {
$items = array();
if (!$maycache) {
$items[] = array(
'path' => 'admin/settings/google_admanager',
'title' => t('Google Admanager'),
'description' => t('Configure the settings used to generate the Google Admanager Slot Ad code.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'google_admanager_admin_settings_form',
'access' => user_access('administer google admanager'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
function google_admanager_admin_settings_form() {
$form['google_admanager_account'] = array(
'#type' => 'textfield',
'#title' => t('Google Admanager account number'),
'#default_value' => variable_get('google_admanager_account', 'ca-pub-'),
'#size' => 30,
'#maxlength' => 40,
'#required' => TRUE,
'#description' => t('You can obtain a user account under the <strong>Admin</strong> tab on the <a href="@url">Google Admanager</a> website.', array(
'@url' => 'https://www.google.com/admanager/',
)),
);
$form['google_admanager_ad_slots'] = array(
'#type' => 'textarea',
'#title' => t('Ad slots'),
'#default_value' => variable_get('google_admanager_ad_slots', ''),
'#description' => t('Enter one Ad Slot name per line.'),
);
return system_settings_form($form);
}
function google_admanager_admin_settings_form_validate($form_id, $form_values) {
if (!preg_match('/^ca-pub-\\d+$/', $form_values['google_admanager_account'])) {
form_set_error('google_admanager_account', t('A valid Google Admanager account number is case sensitive and formatted like ca-pub-xxxxxxxxxxxxxxx.'));
}
}
function theme_google_admanager_block($id, $ad_slot) {
google_admanager_add_js('GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");');
return '<script type="text/javascript">GA_googleFillSlot("' . $ad_slot . '");</script>';
}
function google_admanager_add_js($ad_slot = NULL) {
static $google_admanager_slots;
if (isset($ad_slot)) {
$google_admanager_slots .= $ad_slot;
}
else {
if (isset($google_admanager_slots)) {
$id = variable_get('google_admanager_account', '');
$js = '<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>' . "\n";
$js .= '<script type="text/javascript">GS_googleAddAdSenseService("' . $id . '"); GS_googleEnableAllServices();</script>' . "\n";
$js .= '<script type="text/javascript">' . $google_admanager_slots . '</script>' . "\n";
$js .= '<script type="text/javascript">GA_googleFetchAds();</script>' . "\n";
return $js;
}
}
}
function google_admanager_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Google Admanager filter'),
);
case 'description':
return t('Substitutes [google_ad:ad_slot] tags with the Google Admanager script.');
case 'prepare':
return $text;
case 'process':
return _google_admanager_substitute_tags($text);
}
}
function google_admanager_filter_tips($delta, $format, $long = FALSE) {
return t('You may use [google_ad:ad_slot] to display Google Admanager ads within your content.');
}
function _google_admanager_substitute_tags($text) {
if (preg_match_all("/\\[(google_ad):([^=\\]]+)=?([^\\]]*)?\\]/i", $text, $match)) {
$id = variable_get('google_admanager_account', '');
$s = $r = array();
foreach ($match[2] as $key => $ad_slot) {
$s[] = $match[0][$key];
$r[] = theme('google_admanager_block', $id, $ad_slot);
}
return str_replace($s, $r, $text);
}
return $text;
}