| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Google Admanager
|
| 4 |
* https://www.google.com/admanager/
|
| 5 |
*/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Implementing hook_block
|
| 9 |
*/
|
| 10 |
function google_admanager_block($op = 'list', $delta = 0, $edit = array()) {
|
| 11 |
|
| 12 |
$id = variable_get('google_admanager_account', '');
|
| 13 |
if (!empty($id)) {
|
| 14 |
$ad_slots = variable_get('google_admanager_ad_slots', '');
|
| 15 |
$ad_slots = explode("\n", str_replace(array("\r", "\t", "\0", "\x0B", " "), '', $ad_slots));
|
| 16 |
if ($op == 'list') {
|
| 17 |
foreach($ad_slots as $ad_slot) {
|
| 18 |
$blocks[] = array(
|
| 19 |
'info' => 'Ad Slot: ' . $ad_slot,
|
| 20 |
);
|
| 21 |
}
|
| 22 |
return $blocks;
|
| 23 |
}
|
| 24 |
else if ($op == 'view') {
|
| 25 |
|
| 26 |
if ($ad_slot = $ad_slots[$delta]) {
|
| 27 |
$block = array(
|
| 28 |
'subject' => '',
|
| 29 |
'content' => theme_google_admanager_block($id, $ad_slot),
|
| 30 |
'cache' => 'BLOCK_NO_CACHE',
|
| 31 |
);
|
| 32 |
}
|
| 33 |
return $block;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Implementing hook_perm
|
| 40 |
*/
|
| 41 |
function google_admanager_perm() {
|
| 42 |
return array('administer google admanager');
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Implementing hook_menu
|
| 47 |
*/
|
| 48 |
function google_admanager_menu() {
|
| 49 |
$items = array();
|
| 50 |
if (!$maycache) {
|
| 51 |
$items['admin/settings/google_admanager'] = array(
|
| 52 |
'title' => 'Google Admanager',
|
| 53 |
'description' => 'Configure the settings used to generate the Google Admanager Slot Ad code.',
|
| 54 |
'page callback' => 'drupal_get_form',
|
| 55 |
'page arguments' => array('google_admanager_admin_settings_form'),
|
| 56 |
'access arguments' => array('administer google admanager'),
|
| 57 |
'type' => MENU_NORMAL_ITEM,
|
| 58 |
);
|
| 59 |
}
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_admin_settings() for configuring the module
|
| 65 |
*/
|
| 66 |
function google_admanager_admin_settings_form() {
|
| 67 |
|
| 68 |
$form['google_admanager_account'] = array(
|
| 69 |
'#type' => 'textfield',
|
| 70 |
'#title' => t('Google Admanager account number'),
|
| 71 |
'#default_value' => variable_get('google_admanager_account', 'ca-pub-'),
|
| 72 |
'#size' => 30,
|
| 73 |
'#maxlength' => 40,
|
| 74 |
'#required' => TRUE,
|
| 75 |
'#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/')),
|
| 76 |
);
|
| 77 |
$form['google_admanager_ad_slots'] = array(
|
| 78 |
'#type' => 'textarea',
|
| 79 |
'#title' => t('Ad slots'),
|
| 80 |
'#default_value' => variable_get('google_admanager_ad_slots', ''),
|
| 81 |
'#description' => t('Enter one Ad Slot name per line.'),
|
| 82 |
);
|
| 83 |
|
| 84 |
return system_settings_form($form);
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Implementation of hook_admin_settings_form_validate()
|
| 89 |
*/
|
| 90 |
function google_admanager_admin_settings_form_validate($form, &$form_state) {
|
| 91 |
if (!preg_match('/^ca-pub-\d+$/', $form_state['values']['google_admanager_account'])) {
|
| 92 |
form_set_error('google_admanager_account', t('A valid Google Admanager account number is case sensitive and formatted like ca-pub-xxxxxxxxxxxxxxx.'));
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Theme function the Ad Slot code
|
| 98 |
* @param $id google admanager account id
|
| 99 |
* @param $id google admanager slot name
|
| 100 |
* @return google admanager slot script
|
| 101 |
*/
|
| 102 |
function theme_google_admanager_block($id, $ad_slot) {
|
| 103 |
|
| 104 |
google_admanager_add_js('GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");');
|
| 105 |
|
| 106 |
return '<script type="text/javascript">GA_googleFillSlot("' . $ad_slot . '");</script>';
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Stored the ad slots and when called with no slot, return the whole ad manager javascript
|
| 111 |
* This function should be called from phptemplate_preprocess(&$vars) as followed
|
| 112 |
* if (module_exists('google_admanager')) {
|
| 113 |
* $vars['scripts'] .= google_admanager_add_js();
|
| 114 |
* }
|
| 115 |
* @param $ad_slot string with the addSlot script
|
| 116 |
* @return google admanager code
|
| 117 |
*/
|
| 118 |
function google_admanager_add_js($ad_slot = NULL) {
|
| 119 |
static $google_admanager_slots;
|
| 120 |
|
| 121 |
if (isset($ad_slot)) {
|
| 122 |
$google_admanager_slots .= $ad_slot;
|
| 123 |
}
|
| 124 |
else {
|
| 125 |
if (isset($google_admanager_slots)) {
|
| 126 |
$id = variable_get('google_admanager_account', '');
|
| 127 |
// javascript which is supposed to come in the header
|
| 128 |
$js = '<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>' . "\n";
|
| 129 |
$js .= '<script type="text/javascript">GS_googleAddAdSenseService("' . $id . '"); GS_googleEnableAllServices();</script>' . "\n";
|
| 130 |
$js .= '<script type="text/javascript">' . $google_admanager_slots . '</script>' . "\n";
|
| 131 |
$js .= '<script type="text/javascript">GA_googleFetchAds();</script>' . "\n";
|
| 132 |
return $js;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Implementation of hook_filter().
|
| 139 |
*
|
| 140 |
* Filter option to enable Google Admanager filter [google_ad:ad_slot]
|
| 141 |
* @see google_admanager_nodeapi()
|
| 142 |
*/
|
| 143 |
function google_admanager_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 144 |
switch ($op) {
|
| 145 |
case 'list':
|
| 146 |
return array(0 => t('Google Admanager filter'));
|
| 147 |
|
| 148 |
case 'description':
|
| 149 |
return t('Substitutes [google_ad:ad_slot] tags with the Google Admanager script.');
|
| 150 |
|
| 151 |
case 'prepare':
|
| 152 |
return $text;
|
| 153 |
|
| 154 |
case 'process':
|
| 155 |
return _google_admanager_substitute_tags($text);
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Implementation of hook_filter_tips().
|
| 161 |
*/
|
| 162 |
function google_admanager_filter_tips($delta, $format, $long = FALSE) {
|
| 163 |
return t('You may use [google_ad:ad_slot] to display Google Admanager ads within your content.');
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Replace all Admanager tags with their corresponding files or images.
|
| 168 |
*
|
| 169 |
* @param object $text
|
| 170 |
* The text to process.
|
| 171 |
*
|
| 172 |
* @return string
|
| 173 |
* The processed content of the given text.
|
| 174 |
*/
|
| 175 |
function _google_admanager_substitute_tags($text) {
|
| 176 |
if (preg_match_all("/\[(google_ad):([^=\\]]+)=?([^\\]]*)?\]/i", $text, $match)) {
|
| 177 |
$id = variable_get('google_admanager_account', '');
|
| 178 |
$s = $r = array();
|
| 179 |
foreach ($match[2] as $key => $ad_slot) {
|
| 180 |
$s[] = $match[0][$key];
|
| 181 |
$r[] = theme_google_admanager_block($id, $ad_slot);
|
| 182 |
}
|
| 183 |
// Perform the replacements and return processed field.
|
| 184 |
return str_replace($s, $r, $text);
|
| 185 |
}
|
| 186 |
return $text;
|
| 187 |
}
|