-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyadslot.module
executable file
·240 lines (206 loc) · 6.41 KB
/
lazyadslot.module
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
<?php
/**
* @file
* Main module file.
*/
/**
* Implements hook_context_registry().
*/
function lazyadslot_context_registry() {
return [
'reactions' => [
'lazyadslot' => [
'title' => t('Lazy Loading Ad Slots'),
'plugin' => 'lazyadslot_context_reaction',
],
],
];
}
/**
* Implements hook_context_plugins().
*/
function lazyadslot_context_plugins() {
$plugins['lazyadslot_context_reaction'] = [
'handler' => [
'path' => drupal_get_path('module', 'lazyadslot') . '/plugins',
'file' => 'lazyadslot_context_reaction.inc',
'class' => 'LazyAdSlotContextReaction',
'parent' => 'context_condition',
],
];
return $plugins;
}
/**
* Implements hook_page_build().
*
* Get all needed settings and add it to the JavaScript which will append
* the rendered dfp tag in specified location.
* All the visibility rules should be controlled by the context.
*/
function lazyadslot_page_build() {
$tags = [];
// Execute lazyadslot plugin.
if ($plugin = context_get_plugin('reaction', 'lazyadslot')) {
$plugin->execute();
foreach ($plugin->lazyadslot_tags as $key => $values) {
$ad_tag = $values['ad_tag'];
// Load the DFP tag.
$tag = dfp_tag_load($ad_tag);
// Check for existence.
if (empty($tag)) {
drupal_set_message(t('Invalid dfp tag: %name', ['%name' => $ad_tag]), 'error');
continue;
}
// Disabled tag.
if (!empty($tag->disabled)) {
continue;
}
// Process the tag.
$dfp_tag = dfp_tag($ad_tag);
$tags[$key] = $values;
// Get the slot Display.
$slot_display = render($dfp_tag);
// Store the slot to be removed later in hook_js_alter().
_lazyadslot_slots_to_remove($ad_tag);
// Get the slot declaration.
$slot_declaration = _lazyload_get_slot_declaration($ad_tag);
// The Ad can't be created if the declaration is not detected.
if (empty($slot_declaration)) {
continue;
}
// Wrap the slot Declaration and Display
// to be handled asynchronously if is setup so.
if (!empty($values['async_rendering'])) {
if (strpos($slot_declaration, 'googletag.cmd.push') === FALSE) {
$slot_declaration = 'googletag.cmd.push(function() {' . PHP_EOL . $slot_declaration . PHP_EOL . '});';
}
if (strpos($slot_display, 'googletag.cmd.push') === FALSE) {
$slot_display = preg_replace('/googletag\.display\(\".*\"\)\;$/m', 'googletag.cmd.push(function() {' . PHP_EOL . '$0' . PHP_EOL . '});', $slot_display);
}
}
// Arraying the selectors.
$tags[$key]['ad_placement'] = explode(PHP_EOL, $values['ad_placement']);
// All Ad needed markup.
$tags[$key]['renderedDfp'] = '<script>' . PHP_EOL . $slot_declaration . PHP_EOL . '</script>' . PHP_EOL . $slot_display;
// Refresh on load.
$tags[$key]['refreshOnLoad'] = variable_get('dfp_disable_init_load', 0);
}
// Allow modules to alter the final tags.
drupal_alter('lazyadslot_tags', $tags);
// Add the javascript.
_lazyadslot_add_js($tags);
}
}
/**
* Helper to retrieve added js for given ad_tag slot.
*
* @param string $ad_tag
* DFP slot machinename.
*
* @return string
* JavaScript code.
*/
function _lazyload_get_slot_declaration($ad_tag) {
foreach (drupal_add_js() as $key => $value) {
if (isset($value['data']) && !is_array($value['data']) && strstr($value['data'], 'googletag')) {
if (preg_match('/' . 'googletag\.slots\[\"' . $ad_tag . '\"\]\s*=\s*googletag\.define[^\(]*?Slot\(' . '/', $value['data'])) {
return $value['data'];
}
}
}
}
/**
* Helper to add the javascript code for Taboola DFP Native.
*/
function _lazyadslot_add_js($tags) {
if (path_is_admin(current_path())) {
return;
}
// Always load the library.
drupal_add_js(drupal_get_path('module', 'lazyadslot') . '/js/lazyadslot.js');
// Add collected JS configuration.
if (!empty($tags)) {
// drupal_add_js(['lazyAdSlot' => ['tags' => $tags,],], 'setting');
drupal_add_js(array('lazyAdSlot' => array('tags' => $tags)), 'setting');
// The usage.
drupal_add_js(drupal_get_path('module', 'lazyadslot') . '/js/lazyadslot_init.js');
}
}
/**
* Helper to statically store the slot
* in order to be removed in hook_js_alter().
*
* @param string $ad_tag
* The machine name of the slot.
*
* @return string $machinename
*/
function _lazyadslot_slots_to_remove($ad_tag = NULL) {
$machinenames = &drupal_static(__FUNCTION__, array());
if (!empty($ad_tag)) {
$machinenames[] = $ad_tag;
}
return $machinenames;
}
/**
* Registry of slots to keep.
*
* @param null $ad_tag
* The ad tag.
*
* @return array
*/
function _lazyadslot_slots_to_keep($ad_tag = NULL) {
$machinenames = &drupal_static(__FUNCTION__, array());
if (!empty($ad_tag)) {
$machinenames[] = $ad_tag;
}
return $machinenames;
}
/**
* Implements hook_js_alter().
*
* Remove unneeded declared DFP slot.
*/
function lazyadslot_js_alter(&$javascript) {
if (path_is_admin(current_path())) {
return;
}
// Return if there are no ad tags to remove.
if (!$ad_tags = _lazyadslot_slots_to_remove()) {
return;
}
// Keep slots that are used by blocks.
$ad_tags = array_diff($ad_tags, _lazyadslot_slots_to_keep());
// Iterate of all javascript items to get dfp slots.
foreach ($javascript as $key => $value) {
if (isset($value['data']) && !is_array($value['data']) && strstr($value['data'], 'googletag')) {
foreach ($ad_tags as $ad_tag) {
if (preg_match('/' . 'googletag\.slots\[\"' . $ad_tag . '\"\]\s*=\s*googletag\.define[^\(]*?Slot\(' . '/', $value['data'])) {
unset($javascript[$key]);
}
}
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function lazyadslot_module_implements_alter(&$implementations, $hook) {
// Ensure that lazyadslot runs after dfp and context.
if (in_array($hook, ['page_build', 'js_alter'])) {
$module = ['lazyadslot' => $implementations['lazyadslot']];
unset($implementations['lazyadslot']);
$implementations = $implementations + $module;
}
}
/**
* Implements hook_preprocess_block().
*
* Keep registry of slots to keep on page.
*/
function lazyadslot_preprocess_block(&$variables) {
if (isset($variables['elements']['dfp_wrapper']['tag']['#tag']->machinename)) {
_lazyadslot_slots_to_keep($variables['elements']['dfp_wrapper']['tag']['#tag']->machinename);
}
}