-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfw-init.php
executable file
·512 lines (382 loc) · 9.68 KB
/
dfw-init.php
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
/**
* Global instances for DoubleClick object.
*
*/
$DoubleClick = new DoubleClick();
/**
* A wrapper for wp_loaded
*
* This means that if the plugin is not installed,
* the setup function will not run and throw an error.
*/
function dfw_add_action() {
/**
* Use this action to setup
* breakpoints and network tracking code
* in your theme's functions.php.
*
* @since v0.1
*/
do_action('dfw_setup');
}
add_action('wp_loaded', 'dfw_add_action');
class DoubleClick {
/**
* Network code from DFP.
*
* @var int
*/
public $networkCode;
/**
* If true, plugin prints debug units instead of
* making a call to dfp.
*
* @var boolean
*/
public $debug = false;
/**
* Array of defined breakpoints
*
* @var Array
*/
public $breakpoints = array();
/**
* Array of placed ads.
*
* @var Array
*/
public $adSlots = array();
/**
* Whether we have hooked enqueue of the script
* to wp head.
*
* @var boolean
*/
private static $enqueued = false;
/**
* Size mappings for ad units.
*
* @var Array
*/
private static $mapping = array();
/**
* The number of ads on a page. Also appended to
* ad identifiers to create unique strings.
*
* @var int
*/
public static $count = 0;
/**
* Create a new DoubleClick object
*
* @param string $networkCode The code for your dfp instance.
*/
public function __construct($networkCode = null) {
$this->networkCode = $networkCode;
// Script enqueue is static because we only ever want to print it once.
if(!$this::$enqueued) {
add_action('wp_footer', array($this, 'enqueue_scripts'));
$this->enqueued = true;
}
add_action('wp_print_footer_scripts', array($this, 'footer_script'));
// Get breakpoints from db
$breakpoints = maybe_unserialize( get_option('dfw_breakpoints') );
if( !empty($breakpoints) ):
foreach($breakpoints as $b) {
$args = array(
'minWidth' => $b['min-width'],
'maxWidth' => $b['max-width'],
'_option' => true // this breakpoint is set in WordPress options.
);
$this->register_breakpoint($b['identifier'],$args);
}
else:
// Define breakpoints
$this->register_breakpoint('small', array('minWidth'=> 0,'maxWidth'=>511));
$this->register_breakpoint('medium', array('minWidth'=>512,'maxWidth'=>1199));
$this->register_breakpoint('large', array('minWidth'=>1200,'maxWidth'=>99999));
endif;
}
/**
* Register Breakpoint
*
* @param DoubleClickBreakpoint
*/
public function register_breakpoint($identifier,$args = null) {
$this->breakpoints[$identifier] = new DoubleClickBreakpoint($identifier,$args);
}
public function enqueue_scripts() {
wp_register_script( 'jquery.dfp.min.js', plugins_url( 'js/jquery.dfp.min.js', __FILE__ ) , array('main'), '1.1.5', true );
wp_register_script( 'jquery.dfw.js', plugins_url( 'js/jquery.dfw.js', __FILE__ ) , array('main'), '1.1.5', true );
// Localize the script with other data
// from the plugin.
$mappings = array();
foreach($this->adSlots as $ad) {
if($ad->hasMapping()) {
$mappings["mapping{$ad->id}"] = $ad->mapping();
}
}
$data = array(
'networkCode' => $this->networkCode,
'mappings' => $mappings,
'targeting' => $this->targeting()
);
wp_localize_script( 'jquery.dfw.js', 'dfw', $data );
wp_enqueue_script( 'jquery.dfp.min.js' );
wp_enqueue_script( 'jquery.dfw.js' );
}
/**
* If the network code is set by the theme, return that.
* Else, try to return the front end option.
*
* @return String network code.
*/
private function networkCode() {
return isset($this->networkCode) ? $this->networkCode : get_option('dfw_network_code','xxxxxx');
}
public function footer_script() {
if(!$this->debug) :
echo "\n<script type='text/javascript'>\n";
$mappings = array();
foreach($this->adSlots as $ad) {
if($ad->hasMapping()) {
$mappings["mapping{$ad->id}"] = $ad->mapping();
}
}
echo "\tjQuery('.dfw-unit:not(.dfw-lazy-load)').dfp({ \n";
echo "\t\tdfpID: '". $this->networkCode() ."',\n";
echo "\t\tcollapseEmptyDivs:false,\n";
echo "\t\tsetTargeting: " . json_encode($this->targeting()) . ",\n";
echo "\t\tsizeMapping: " . json_encode($mappings);
echo "\t});\n";
echo "\n</script>\n";
endif;
}
private function targeting() {
/** @see http://codex.wordpress.org/Conditional_Tags */
$targeting = array();
$targeting['Page'] = array();
// Homepage
if( is_home() )
$targeting['Page'][] = 'home';
if( is_front_page() )
$targeting['Page'][] = 'front-page';
// Admin
if( is_admin() )
$targeting['Page'][] = 'admin';
if( is_admin_bar_showing() )
$targeting['Page'][] = 'admin-bar-showing';
// Templates
if( is_single() )
$targeting['Page'][] = 'single';
if( is_post_type_archive() )
$targeting['Page'][] = 'archive';
if( is_author() )
$targeting['Page'][] = 'author';
if( is_date() )
$targeting['Page'][] = 'date';
if( is_search() )
$targeting['Page'][] = 'search';
if( is_single() ) {
$cats = get_the_category();
$targeting['Category'] = array();
if ($cats) {
foreach($cats as $c)
$targeting['Category'][] = $c->slug;
}
}
if( is_single() ) {
$tags = get_the_tags();
if ($tags) {
$targeting['Tag'] = array();
foreach($tags as $t)
$targeting['Tag'][] = $t->slug;
}
}
// return the array of targeting criteria.
return $targeting;
}
/**
* Place a DFP ad.
*
* @param string $identifier A DFP
* @param string|array $dimensions the dimensions the ad could be.
* @param string|array $breakpoint breakpoints to target.
* @param array $targeting additional targeting options.
* @param $return Boolean. If this is true it will return a string instead.
*/
public function place_ad($identifier,$sizes,$args = null) {
return $this->get_ad_placement($identifier,$sizes,$args);
}
public function get_ad_placement($identifier,$sizes,$args = null) {
global $post;
if( $args === null ) {
$args = array();
}
$defaults = array(
"lazyLoad" => false
);
$args = array_replace_recursive($defaults, $args);
$adObject = new DoubleClickAdSlot($identifier,$sizes);
$this->adSlots[] = $adObject;
// Print the ad tag.
$classes = "dfw-unit";
if($args['lazyLoad']) {
$classes .= " dfw-lazy-load";
}
$id = $adObject->id;
if( $adObject->hasMapping() ) {
$ad = "<div
class='$classes'
data-adunit='$identifier'
data-size-mapping='mapping{$id}'></div>";
} else {
$ad = "<div
class='$classes'
data-adunit='$identifier'
data-dimensions='$sizes'></div>";
}
return $ad;
}
}
class DoubleClickAdSlot {
/**
* DFP ad code.
*
* @var String.
*/
public $identifer;
/**
* Either a string of sizes, or a size mapping.
*
* @var Array|String
*/
public $sizes;
/**
* Each ad gets a unique number to identify it.
*
* @var int
*/
public $id;
/**
*
*
* @param String $identifier
* @param Mixed $size
*/
public function __construct($identifer,$size) {
global $DoubleClick;
// doubleclick escapes '/' with '//' for some odd reason.
// currently we don't try to fix this, but could with this line:
// $this->identifier = str_replace('/','//',$identifier);
$this->identifier = $identifer;
$this->sizes = $size;
$this->id = ++ DoubleClick::$count;
}
public function breakpointIdentifier() {
return null;
}
/**
* If this ad unit has a size mapping.
*
*/
public function hasMapping() {
if( is_string( $this->sizes ) ) {
return false;
} else {
return true;
}
}
public function mapping() {
global $DoubleClick;
// Return false if there is no mapping
if( !$this->hasMapping() )
return false;
foreach($this->sizes as $breakpointIdentifier=>$size) {
$breakpoint = $DoubleClick->breakpoints[$breakpointIdentifier];
// The minimum browser width/height for this sizemapping.
$browserHeight = 1;
$browserWidth = (int) $breakpoint->minWidth;
$sizeStrings = explode(",",$size); // eg. 300x250,336x300
$sizeArray = array();
foreach($sizeStrings as $s) {
if( !empty($s) ) :
$arr = explode("x",$s); // eg. 300x250
$w = (int)$arr[0];
$h = (int)$arr[1];
$sizeArray[] = array($w,$h);
else :
// $sizeArray[] = array();
endif;
}
$mapping[] = array(
'browser' => array($browserWidth,$browserHeight),
'ad_sizes' => $sizeArray
);
}
return $mapping;
}
}
class DoubleClickBreakpoint {
/**
* Slug of the breakpoint
*
* @var string
*/
public $identifier = '';
/**
* Minimum width for the breakpoint
*
* @var integer
*/
public $minWidth;
/**
* Maximum width for the breakpoint
*
* @var integer
*/
public $maxWidth;
/**
* Was this breakpoint added by a theme or
* through an option?
*
* @var boolean
*/
public $option;
public function __construct($identifier,$args = null) {
if(isset($args['minWidth']))
$this->minWidth = $args['minWidth'];
if(isset($args['maxWidth']))
$this->maxWidth = $args['maxWidth'];
if(isset($args['_option']) && $args['_option'] ) {
$this->option = true;
}
$this->identifier = $identifier;
}
/**
* Prints a javascript boolean statement for this breakpoint
*
*/
public function js_logic() {
echo $this->get_js_logic();
}
/**
* Returns a string with the boolean logic for the breakpoint.
*
* @return String boolean logic for breakpoint.
*/
public function get_js_logic() {
return "($this->minWidth <= document.documentElement.clientWidth && document.documentElement.clientWidth < $this->maxWidth)";
}
}
/**
* The dfp front end widget.
*
*/
include plugin_dir_path(__FILE__) . '/dfw-widget.php';
/**
* Front end options for the widget.
*
*/
include plugin_dir_path(__FILE__) . '/dfw-options.php';