View source
<?php
function google_admanager_menu() {
$items = array();
$base = array(
'access arguments' => array(
'administer google admanager',
),
'file' => 'google_admanager.admin.inc',
);
$items['admin/config/system/google_admanager'] = $base + array(
'title' => 'Google Admanager',
'description' => 'Configure the settings used to generate the Google Admanager Slot Ad code.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_admanager_admin_settings_form',
),
);
$items['admin/config/system/google_admanager/account'] = $base + array(
'title' => 'Account',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/google_admanager/superslot'] = $base + array(
'title' => 'Superslot',
'description' => 'Manage superslot',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_admanager_admin_superslot_form',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/system/google_admanager/superslot/delete'] = $base + array(
'title' => 'Delete superslot',
'page callback' => 'google_admanager_admin_superslot_delete',
'type' => MENU_CALLBACK,
);
return $items;
}
function google_admanager_init() {
drupal_add_css(drupal_get_path('module', 'google_admanager') . '/google_admanager.css');
if (variable_get('google_admanager_lazy', FALSE)) {
drupal_add_js(drupal_get_path('module', 'google_admanager') . '/google_admanager.js');
}
if (arg(0) == "taxonomy" && arg(1) == "term" && is_numeric(arg(2))) {
$enabled_vocabs = variable_get('google_admanager_vocab_attributes', array());
if (count($enabled_vocabs) >= 1) {
$term = taxonomy_term_load(arg(2));
if ($term && !empty($enabled_vocabs[$term->vid])) {
google_admanager_add_term_attribute($term);
}
}
}
}
function google_admanager_block_view($delta = '') {
$ad_slots = _google_admanager_get_ad_slots();
$block = array(
'subject' => '',
'content' => '',
);
if ($id = variable_get('google_admanager_account', '')) {
if (isset($ad_slots[$delta])) {
$block['content'] = theme('google_admanager_block', array(
'id' => $id,
'ad_slot' => $ad_slots[$delta],
));
}
elseif (substr($delta, 0, 10) == 'superslot:') {
$superslots = variable_get('google_admanager_superslots', array());
if ($superslot = $superslots[substr($delta, 10)]) {
foreach ($superslot as $ad_slot => $php) {
if (eval($php)) {
$block['content'] .= theme('google_admanager_block', array(
'id' => $id,
'ad_slot' => $ad_slot,
));
}
}
}
}
}
return $block;
}
function google_admanager_permission() {
return array(
'administer google admanager' => array(
'title' => t('Administer google admanager'),
'description' => t('Access the Google Admanager administration pages.'),
),
);
}
function google_admanager_block_info() {
$blocks = array();
if (!variable_get('google_admanager_noblock', FALSE)) {
$ad_slots = _google_admanager_get_ad_slots();
foreach ($ad_slots as $delta => $name) {
$blocks[$delta] = array(
'info' => 'GAM Ad slot: ' . $name,
'cache' => DRUPAL_NO_CACHE,
);
}
}
$superslots = variable_get('google_admanager_superslots', array());
foreach ($superslots as $name => $slots) {
$blocks['superslot:' . $name] = array(
'info' => 'GAM Superslot: ' . $name,
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
function google_admanager_block_configure($delta = 0) {
if (!user_access('use PHP for settings') || substr($delta, 0, 10) !== 'superslot:') {
return;
}
$superslots = variable_get('google_admanager_superslots', array());
$name = substr($delta, 10);
if (!isset($superslots[$name])) {
return;
}
$form = array();
$form['google_admanager_visibility'] = array(
'#type' => 'fieldset',
'#title' => t('Ad slots visibility'),
'#description' => t('Use PHP code to define ad slot visibility. For example, to display an ad slot only to anonymous user, use <em>return empty($GLOBALS[\'user\']->uid);</em>. Or, to simple enable an ad slot, use <em>return TRUE;</em>'),
);
$ad_slots = array_values(_google_admanager_get_ad_slots());
$ad_slots = array_combine($ad_slots, $ad_slots);
$superslot = $superslots[$name];
$superslot += array(
'fake slot1' => '',
'fake slot2' => '',
'fake slot3' => '',
);
$i = 1;
foreach ($superslot as $ad_slot => $php) {
$form['google_admanager_visibility']['superslot_' . $i . '_adslot'] = array(
'#type' => 'select',
'#title' => t('Ad slot'),
'#default_value' => $ad_slot,
'#options' => $ad_slots,
);
$form['google_admanager_visibility']['superslot_' . $i++ . '_php'] = array(
'#type' => 'textfield',
'#title' => t('PHP code for visibility condition'),
'#default_value' => $php,
);
}
return $form;
}
function google_admanager_block_save($delta = 0, $edit = array()) {
if (!user_access('use PHP for settings') || substr($delta, 0, 10) !== 'superslot:') {
return;
}
$superslot = array();
foreach ($edit as $key => $value) {
if (preg_match('/superslot_(\\d+)_adslot/', $key)) {
$php = $edit[str_replace('adslot', 'php', $key)];
if (!empty($php)) {
$superslot[$value] = $php;
}
}
}
$superslots = variable_get('google_admanager_superslots', array());
$superslots[substr($delta, 10)] = $superslot;
variable_set('google_admanager_superslots', $superslots);
}
function google_admanager_node_view($node, $view_mode = 'full') {
if (arg(0) == 'node' && $view_mode == 'full') {
if (variable_get('google_admanager_nodetype_attributes', FALSE)) {
$type = _google_admanager_clean_string($node->type);
google_admanager_add_attribute("node_type", $type);
}
$enabled_vocabs = variable_get('google_admanager_vocab_attributes', array());
if (count($enabled_vocabs) == 0) {
return;
}
$taxonomy = db_query('SELECT * FROM {taxonomy_term_data} LEFT JOIN {taxonomy_index} ON {taxonomy_term_data}.tid = {taxonomy_index}.tid WHERE nid = :nid', array(
':nid' => $node->nid,
));
if ($taxonomy
->rowCount() == 0) {
return;
}
foreach ($taxonomy as $tid => $term) {
if (!empty($enabled_vocabs[$term->vid])) {
google_admanager_add_term_attribute($term);
}
}
}
}
function google_admanager_theme() {
return array(
'google_admanager_block' => array(
'variables' => array(
'id' => NULL,
'ad_slot' => NULL,
'cache' => FALSE,
),
),
);
}
function theme_google_admanager_block($variables) {
$id = $variables['id'];
$ad_slot = $variables['ad_slot'];
$script = '<script type="text/javascript">GA_googleFillSlot("' . $ad_slot . '");</script>';
if ($variables['cache']) {
$script = '<script type="text/javascript">GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");</script>' . $script;
}
else {
google_admanager_add_js('GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");');
}
$style = '';
if (variable_get('google_admanager_lazy', FALSE)) {
if (variable_get('google_admanager_autodetect', FALSE)) {
if (preg_match('/(\\d+)x(\\d+)(_.*|)$/', $ad_slot, $match)) {
$style = ' style="width:' . $match[1] . 'px;height:' . $match[2] . 'px;"';
}
}
google_admanager_add_block('<div id="gam-content-' . $ad_slot . '" class="gam-banner">' . $script . '</div>');
$script = '';
}
return '<div id="gam-holder-' . $ad_slot . '" class="gam-holder"' . $style . '>' . $script . '</div>';
}
function google_admanager_add_attribute($key, $value) {
google_admanager_add_js('GA_googleAddAttr("' . check_plain($key) . '", "' . check_plain($value) . '");', 'attr');
}
function google_admanager_add_js($js = NULL, $type = 'slot') {
static $ga_js = array();
if (isset($js) && isset($type)) {
$ga_js[$type][] = $js;
if (!isset($ga_js['service'])) {
drupal_add_js('//partner.googleadservices.com/gampad/google_service.js', array(
'type' => 'external',
'inline' => TRUE,
));
$id = variable_get('google_admanager_account', '');
google_admanager_add_js('GS_googleAddAdSenseService("' . $id . '");', 'service');
google_admanager_add_js('GS_googleEnableAllServices();', 'service');
google_admanager_add_js('GA_googleFetchAds();', 'close');
}
return;
}
if (!isset($js)) {
return $ga_js;
}
}
function google_admanager_add_block($text = NULL) {
static $ga_block = array();
if (!$text) {
return $ga_block;
}
$ga_block[] = $text;
}
function google_admanager_get_js($scope = 'header') {
$ga_js = google_admanager_add_js();
if (isset($ga_js)) {
$output_order = array(
'service',
'attr',
'slot',
'close',
);
foreach ($output_order as $type) {
if (empty($ga_js[$type])) {
continue;
}
$output = '';
foreach ($ga_js[$type] as $js) {
$output .= $js . "\n";
}
drupal_add_js($output, array(
'type' => 'inline',
'scope' => $scope,
));
}
}
}
function google_admanager_preprocess_page(&$vars) {
if (variable_get('google_admanager_lazy', FALSE)) {
return;
}
google_admanager_get_js();
$vars['scripts'] = drupal_get_js();
}
function google_admanager_page_alter(&$page) {
if ($lazy = google_admanager_add_block()) {
if ($ga_js = google_admanager_add_js()) {
$output_order = array(
'service',
'attr',
'slot',
'close',
);
$gam_script = '';
foreach ($output_order as $type) {
if (empty($ga_js[$type])) {
continue;
}
$output = "\n";
foreach ($ga_js[$type] as $js) {
$output .= $js . "\n";
}
$gam_script .= '<script type="text/javascript">' . $output . '</script>';
}
array_unshift($lazy, $gam_script);
}
$page['page_bottom']['google_admanager'] = array(
'#type' => 'markup',
'#markup' => implode("\n", $lazy),
);
}
}
function google_admanager_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'google_admanager_admin_settings_form') {
$form['#submit'][] = 'google_admanager_admin_settings_form_submit';
}
}
function google_admanager_filter_info() {
$filters['google_admanager_filter'] = array(
'title' => t('Google Admanager filter'),
'description' => t('Substitutes [google_ad:ad_slot] tags with the Google Admanager script.'),
'process callback' => '_google_admanager_substitute_tags',
'tips callback' => '_google_admanager_filter_tips',
);
return $filters;
}
function _google_admanager_filter_tips($filter, $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', array(
'id' => $id,
'ad_slot' => $ad_slot,
'cache' => TRUE,
));
}
return str_replace($s, $r, $text);
}
return $text;
}
function _google_admanager_get_ad_slots() {
$ad_slots = array();
$list = array_filter(explode("\n", str_replace(array(
"\r",
"\t",
"\0",
"\v",
" ",
), '', variable_get('google_admanager_ad_slots', ''))));
foreach ($list as $ad_slot) {
$ad_slots[md5(trim($ad_slot))] = $ad_slot;
}
asort($ad_slots);
return $ad_slots;
}
function google_admanager_add_term_attribute($term) {
static $vocab_cache = array(), $added_terms = array();
if (isset($added_terms[$term->tid])) {
return;
}
if (!isset($vocab_cache[$term->vid])) {
$vocab = taxonomy_vocabulary_load($term->vid);
$orig_key = $key = 'v-' . _google_admanager_clean_string($vocab->name, 8);
$counter = 1;
while ($vid = array_search($key, $vocab_cache) && $vid != $term->vid) {
$key = drupal_substr($orig_key, 0, 9) . $counter++;
}
$vocab_cache[$vocab->vid] = $key;
}
google_admanager_add_attribute($vocab_cache[$term->vid], $term->name);
$added_terms[$term->tid] = TRUE;
}
function _google_admanager_clean_string($string, $length = 40) {
return drupal_substr(preg_replace('/[^a-z0-9]+/', '-', drupal_strtolower($string)), 0, $length);
}