-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfp-2383341-2383919-2629154.patch
264 lines (253 loc) · 11.3 KB
/
dfp-2383341-2383919-2629154.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
diff --git a/dfp.admin.inc b/dfp.admin.inc
index 0d27fdc..9b4bb7a 100644
--- a/dfp.admin.inc
+++ b/dfp.admin.inc
@@ -450,14 +450,14 @@ function dfp_breakpoint_form_validate($element, &$form_state) {
form_error($element['browser_size'], t('The browser size cannot be empty if ad size(s) exists.'));
}
elseif (!empty($element['browser_size']['#value']) && empty($element['ad_sizes']['#value'])) {
- form_error($element['ad_sizes'], t('The ad size(s) cannot be empty if a browser size exists.'));
+ form_error($element['ad_sizes'], t('The ad size(s) cannot be empty if a browser size exists. If you wish to suppress an ad slot for a given browser size, you can enter "!none" in the ad size(s) field.', array('!none' => htmlspecialchars('<none>'))));
}
if (!empty($element['browser_size']['#value']) && !empty($element['ad_sizes']['#value'])) {
if (preg_match('/[^x|0-9]/', $element['browser_size']['#value'])) {
form_error($element['browser_size'], t('The browser size can only contain numbers and the character x.'));
}
- elseif (preg_match('/[^x|,|0-9]/', $element['ad_sizes']['#value'])) {
- form_error($element['ad_sizes'], t('The ad size(s) can only contain numbers, the character x and commas.'));
+ elseif ($element['ad_sizes']['#value'] != '<none>' && preg_match('/[^x|,|0-9]/', $element['ad_sizes']['#value'])) {
+ form_error($element['ad_sizes'], t('The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "!none").', array('!none' => htmlspecialchars('<none>'))));
}
}
}
@@ -583,7 +583,7 @@ function _dfp_breakpoint_form(&$form, $key, $data = array()) {
);
if (empty($data)) {
$form['breakpoints'][$key]['browser_size']['#description'] = t('Example: 1024x768');
- $form['breakpoints'][$key]['ad_sizes']['#description'] = t('Example: 300x600,300x250');
+ $form['breakpoints'][$key]['ad_sizes']['#description'] = t('Example: 300x600,300x250. Enter "!none" to suppress this slot for a given browser size.', array('!none' => htmlspecialchars('<none>')));
}
}
diff --git a/dfp.module b/dfp.module
index e1ad37e..c87ba94 100755
--- a/dfp.module
+++ b/dfp.module
@@ -554,15 +554,15 @@ function dfp_format_targeting($targeting, $tag = '') {
$target['target'] = '"' . check_plain($target['target']) . '"';
$target['value'] = dfp_token_replace(check_plain($target['value']), $tag, array('sanitize' => TRUE, 'clear' => TRUE));
+ // Allow other modules to alter the target.
+ drupal_alter('dfp_target', $target);
+
// The target value could be blank if tokens are used. If so, removed it.
if (empty($target['value'])) {
unset($targeting[$key]);
continue;
}
- // Allow other modules to alter the target.
- drupal_alter('dfp_target', $target);
-
// Convert the values into an array and trim the whitespace from each value.
$values = explode(',', $target['value']);
$values = array_map('trim', $values);
@@ -660,14 +660,6 @@ function _dfp_get_ad_category($term, $clean_string = FALSE) {
* slot definitions.
*/
function _dfp_js_global_settings() {
- // Initialize the google varibales and inject user-defined javascript.
- $js = 'var googletag = googletag || {};' . "\n";
- $js .= 'googletag.cmd = googletag.cmd || [];';
- $js .= variable_get('dfp_injected_js', '') . "\n";
-
- // Add a place to store the slot name variable
- $js .= 'googletag.slots = googletag.slots || {};';
-
$options = array(
'type' => 'inline',
'group' => JS_LIBRARY,
@@ -675,27 +667,54 @@ function _dfp_js_global_settings() {
'weight' => -10,
'force header' => TRUE,
);
- drupal_add_js($js, $options);
// Include a script tag for the Google Tag Services.
// @todo: One day this should include an async=true attribute. See #1140356.
// However, there is a good chance this might break <= IE8.
- $options['type'] = 'external';
- $options['weight']++;
- drupal_add_js(($GLOBALS['is_https'] ? "https://" : "http://") . DFP_GOOGLE_TAG_SERVICES_URL, $options);
+
+ // Initialize the google variables and inject user-defined javascript.
+ $js = variable_get('dfp_injected_js', '') . "\n";
+
+ // Add a place to store the slot name variable.
+ $js .= " var dfp_slots = new Array();\n";
+
+ // Add vendor code to include third party gpt.js file.
+ $js .= " var googletag = googletag || {};
+ googletag.cmd = googletag.cmd || [];
+ (function() {
+ var gads = document.createElement('script');
+ gads.type = 'text/javascript';
+ var useSSL = 'https:' == document.location.protocol;
+ gads.src = (useSSL ? 'https:' : 'http:') +
+ '//www.googletagservices.com/tag/js/gpt.js';
+ var node = document.getElementsByTagName('script')[0];
+ node.parentNode.insertBefore(gads, node);
+ })();";
+ drupal_add_js($js, $options);
// Add global settings with a heavy weight so that they appear after all the
// defineSlot() calls otherwise IE8 and Opera fail to display ads properly.
$js = 'googletag.cmd.push(function() {' . "\n";
+
+ // Cycles through available ad slots in the array, executes them.
+ $js .= ' for (slot in dfp_slots) {' . "\n";
+ $js .= ' dfp_slots[slot]();' . "\n";
+ $js .= ' };' . "\n";
+
+ // Async / Sync loading of ads
if (variable_get('dfp_async_rendering', 1)) {
$js .= ' googletag.pubads().enableAsyncRendering();' . "\n";
}
else {
$js .= ' googletag.pubads().enableSyncRendering();' . "\n";
}
+
+ // Trigger single request.
if (variable_get('dfp_single_request', 1)) {
$js .= ' googletag.pubads().enableSingleRequest();' . "\n";
}
+
+ // Toggles visibility of empty ads.
switch (variable_get('dfp_collapse_empty_divs', 1)) {
case 1:
$js .= ' googletag.pubads().collapseEmptyDivs();' . "\n";
@@ -704,6 +723,8 @@ function _dfp_js_global_settings() {
$js .= ' googletag.pubads().collapseEmptyDivs(true);' . "\n";
break;
}
+
+ // Initial Loads
if (variable_get('dfp_disable_init_load', 0)) {
$js .= ' googletag.pubads().disableInitialLoad();' . "\n";
}
@@ -721,13 +742,18 @@ function _dfp_js_global_settings() {
}
}
+ // Activate service.
+ $js .= ' googletag.enableServices();' . "\n";
$js .= '});' . "\n";
+
+ // Inject second set of custom code.
$js .= variable_get('dfp_injected_js2', '') . "\n";
- $js .= 'googletag.enableServices();';
+ // Embed code into header.
$options = array(
'type' => 'inline',
'group' => JS_DEFAULT,
+ 'defer' => TRUE,
'every_page' => TRUE,
'weight' => 10,
'force header' => TRUE,
@@ -748,50 +774,81 @@ function _dfp_js_slot_definition($tag) {
// Add the js needed to define this adSlot to <head>.
$js = '';
+
+ // Encapsulate code into an entry in the slots array.
+ $js .= 'dfp_slots["' . $tag->machinename . '"] = function() { ';
+
// Start by defining breakpoints for this ad.
if (!empty($tag->breakpoints)) {
$breakpoints = $tag->breakpoints;
- $js .= 'var mapping = googletag.sizeMapping()' . "\n";
+ $js .= 'var mapping = googletag.sizeMapping()';
foreach ($breakpoints as $breakpoint) {
- $js .= ' .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($breakpoint['ad_sizes']) . ')' . "\n";
+ // If the ad sizes string contains the special keyword "<none>," use an
+ // empty size list in order to suppress slot display.
+ $ad_sizes = strpos($breakpoint['ad_sizes'], '<none>') !== FALSE ? '' : $breakpoint['ad_sizes'];
+ $js .= ' .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($ad_sizes) . ')' . "\n";
}
- $js .= ' .build();' . "\n";
+ $js .= '.build();' . "\n";
}
+
+ // Is this ad an out of page institial?
if (!empty($tag->settings['out_of_page'])) {
- $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineOutOfPageSlot("' . $tag->adunit . '", "' . $tag->placeholder_id . '")' . "\n";
+ $js .= 'googletag.defineOutOfPageSlot("' . $tag->adunit . '", "' . $tag->placeholder_id . '")';
}
else {
- $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineSlot("' . $tag->adunit . '", ' . $tag->size . ', "' . $tag->placeholder_id . '")' . "\n";
+ $js .= 'googletag.defineSlot("' . $tag->adunit . '", ' . $tag->size . ', "' . $tag->placeholder_id . '")';
}
+
+ // Set DFP Click URL (sync mode only).
$click_url = variable_get('dfp_click_url', '');
if (!empty($click_url)) {
- $js .= ' .setClickUrl("' . url($click_url, array('absolute' => TRUE)) . '")' . "\n";
+ if (!preg_match("/^https?:\/\//", $click_url)) {
+ // relative URL; prepend site domain
+ $click_url = (($GLOBALS['is_https']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . "/" . $click_url;
+ }
+ $js .= '.setClickUrl("' . url($click_url, array('absolute' => TRUE)) . '")' . "\n";
}
- $js .= ' .addService(googletag.pubads())' . "\n";
+ // Set ad type if explicitly defined.
if (!empty($tag->adsense_ad_types)) {
- $js .= ' .set("adsense_ad_types", "' . $tag->adsense_ad_types . '")' . "\n";
+ $js .= '.set("adsense_ad_types", "' . $tag->adsense_ad_types . '")';
}
+
+ // Set channel type if explicitly defined.
if (!empty($tag->adsense_channel_ids)) {
- $js .= ' .set("adsense_channel_ids", "' . $tag->adsense_channel_ids . '")' . "\n";
+ $js .= '.set("adsense_channel_ids", "' . $tag->adsense_channel_ids . '")';
}
+
+ // Set styling for the ad.
foreach ($tag->adsense_colors as $key => $val) {
if (!empty($val)) {
$key = 'adsense_' . $key . '_color';
$val = '#' . drupal_strtoupper($val);
- $js .= ' .set("' . $key . '", "' . $val . '")' . "\n";
+ $js .= '.set("' . $key . '", "' . $val . '")';
}
}
+
+ // Apply ad-specific targeting parameters.
$targeting = dfp_format_targeting($tag->targeting, $tag);
foreach ($targeting as $target) {
- $js .= ' .setTargeting(' . $target['target'] . ', ' . $target['value'] . ')' . "\n";
+ $js .= '.setTargeting(' . $target['target'] . ', ' . $target['value'] . ')';
}
+
// Apply size mapping when there are breakpoints.
if (!empty($tag->breakpoints)) {
- $js .= ' .defineSizeMapping(mapping)' . "\n";
+ $js .= '.defineSizeMapping(mapping)';
}
+
+ // Execute method to publish ads.
+ $js .= '.addService(googletag.pubads())';
+
+ // Clean up.
$js = rtrim($js, "\n") . ';';
+ // Close the function declaration.
+ $js .= ' }';
+
+ // Add this bit after the declaration of all the ad slots.
$options = array(
'type' => 'inline',
'group' => JS_LIBRARY,
diff --git a/tests/dfp.test b/tests/dfp.test
index 2026cf7..6780c5f 100644
--- a/tests/dfp.test
+++ b/tests/dfp.test
@@ -191,7 +191,7 @@ class dfpDisplayTagTest extends dfpBaseTest {
$edit['breakpoints[0][browser_size]'] = $this->dfpGenerateSize();
$edit['breakpoints[0][ad_sizes]'] = $this->randomName(8);
$this->editTag($tag->machinename, $edit);
- $this->assertText(t('The ad size(s) can only contain numbers, the character x and commas.'), 'An error was correctly thrown when invalid characters.');
+ $this->assertText(t('The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "!none").', array('!none' => htmlspecialchars('<none>'))), 'An error was correctly thrown when the Ad Size(s) field contained an invalid pattern.');
}
function testSlug() {