Completed
Push — update/masterbar-plugins-link ( 9ebb3d...ad8a7a )
by
unknown
11:18
created

WordAds::insert_header_ad()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
define( 'WORDADS_ROOT', dirname( __FILE__ ) );
4
define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
5
define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
6
define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
7
define( 'WORDADS_API_TEST_ID', '26942' );
8
define( 'WORDADS_API_TEST_ID2', '114160' );
9
10
require_once( WORDADS_ROOT . '/php/widgets.php' );
11
require_once( WORDADS_ROOT . '/php/api.php' );
12
require_once( WORDADS_ROOT . '/php/cron.php' );
13
14
class WordAds {
15
16
	public $params = null;
17
18
	/**
19
	 * The different supported ad types.
20
	 * v0.1 - mrec only for now
21
	 * @var array
22
	 */
23
	public static $ad_tag_ids = array(
24
		'mrec' => array(
25
			'tag'       => '300x250_mediumrectangle',
26
			'height'    => '250',
27
			'width'     => '300',
28
		),
29
		'lrec' => array(
30
			'tag'       => '336x280_largerectangle',
31
			'height'    => '280',
32
			'width'     => '336',
33
		),
34
		'leaderboard' => array(
35
			'tag'       => '728x90_leaderboard',
36
			'height'    => '90',
37
			'width'     => '728',
38
		),
39
		'wideskyscraper' => array(
40
			'tag'       => '160x600_wideskyscraper',
41
			'height'    => '600',
42
			'width'     => '160',
43
		),
44
	);
45
46
	/**
47
	 * Convenience function for grabbing options from params->options
48
	 * @param  string $option the option to grab
49
	 * @param  mixed  $default (optional)
50
	 * @return option or $default if not set
51
	 *
52
	 * @since 4.5.0
53
	 */
54
	function option( $option, $default = false ) {
55
		if ( ! isset( $this->params->options[ $option ] ) ) {
56
			return $default;
57
		}
58
59
		return $this->params->options[ $option ];
60
	}
61
62
	/**
63
	 * Instantiate the plugin
64
	 *
65
	 * @since 4.5.0
66
	 */
67
	function __construct() {
68
		add_action( 'init', array( $this, 'init' ) );
69
	}
70
71
	/**
72
	 * Code to run on WordPress 'init' hook
73
	 *
74
	 * @since 4.5.0
75
	 */
76
	function init() {
77
		// bail on infinite scroll
78
		if ( self::is_infinite_scroll() ) {
79
			return;
80
		}
81
82
		require_once( WORDADS_ROOT . '/php/params.php' );
83
		$this->params = new WordAds_Params();
84
85
		if ( is_admin() ) {
86
			require_once( WORDADS_ROOT . '/php/admin.php' );
87
			return;
88
		}
89
90
		if ( $this->should_bail() ) {
91
			return;
92
		}
93
94
		$this->insert_adcode();
95
	}
96
97
	/**
98
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
99
	 * @return boolean true if load came from infinite scroll
100
	 *
101
	 * @since 4.5.0
102
	 */
103
	public static function is_infinite_scroll() {
104
		return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
105
	}
106
107
	/**
108
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
109
	 *
110
	 * @since 4.5.0
111
	 */
112
	private function insert_adcode() {
113
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
114
		add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
115
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
116
		add_filter( 'the_content', array( $this, 'insert_ad' ) );
117
		add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
118
119
		if ( $this->option( 'enable_header_ad' ) ) {
120
			switch ( get_stylesheet() ) {
121
				case 'twentyseventeen':
122
				case 'twentyfifteen':
123
				case 'twentyfourteen':
124
					add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
125
					break;
126
				default:
127
					add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
128
					break;
129
			}
130
		}
131
	}
132
133
	/**
134
	 * Register desktop scripts and styles
135
	 *
136
	 * @since 4.5.0
137
	 */
138
	function enqueue_scripts() {
139
		wp_enqueue_style(
140
			'wordads',
141
			WORDADS_URL . 'css/style.css',
142
			array(),
143
			'2015-12-18'
144
		);
145
	}
146
147
	/**
148
	 * IPONWEB metadata used by the various scripts
149
	 * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
150
	 */
151
	function insert_head_meta() {
152
		$domain = $this->params->targeting_tags['Domain'];
153
		$pageURL = $this->params->targeting_tags['PageURL'];
154
		$adsafe = $this->params->targeting_tags['AdSafe'];
155
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
156
		echo <<<HTML
157
		<script$data_tags type="text/javascript">
158
			var _ipw_custom = {
159
				wordAds: '1',
160
				domain: '$domain',
161
				pageURL: '$pageURL',
162
				adSafe: '$adsafe'
163
			};
164
		</script>
165
HTML;
166
	}
167
168
	/**
169
	 * IPONWEB scripts in <head>
170
	 *
171
	 * @since 4.5.0
172
	 */
173
	function insert_head_iponweb() {
174
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
175
		echo <<<HTML
176
		<link rel='dns-prefetch' href='//s.pubmine.com' />
177
		<link rel='dns-prefetch' href='//x.bidswitch.net' />
178
		<link rel='dns-prefetch' href='//static.criteo.net' />
179
		<link rel='dns-prefetch' href='//ib.adnxs.com' />
180
		<link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
181
		<link rel='dns-prefetch' href='//bidder.criteo.com' />
182
		<link rel='dns-prefetch' href='//cas.criteo.com' />
183
		<link rel='dns-prefetch' href='//gum.criteo.com' />
184
		<link rel='dns-prefetch' href='//ads.pubmatic.com' />
185
		<link rel='dns-prefetch' href='//gads.pubmatic.com' />
186
		<link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
187
		<link rel='dns-prefetch' href='//ad.doubleclick.net' />
188
		<link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
189
		<link rel='dns-prefetch' href='//www.googletagservices.com' />
190
		<link rel='dns-prefetch' href='//cdn.switchadhub.com' />
191
		<link rel='dns-prefetch' href='//delivery.g.switchadhub.com' />
192
		<link rel='dns-prefetch' href='//delivery.swid.switchadhub.com' />
193
		<script$data_tags type="text/javascript" src="//s.pubmine.com/head.js"></script>
194
		<script$data_tags type="text/javascript" src="//static.criteo.net/js/ld/publishertag.js"></script>
195
HTML;
196
	}
197
198
	/**
199
	 * Insert the ad onto the page
200
	 *
201
	 * @since 4.5.0
202
	 */
203
	function insert_ad( $content ) {
204
		// Ad JS won't work in XML feeds.
205
		if ( is_feed() ) {
206
			return $content;
207
		}
208
		/**
209
		 * Allow third-party tools to disable the display of in post ads.
210
		 *
211
		 * @module wordads
212
		 *
213
		 * @since 4.5.0
214
		 *
215
		 * @param bool true Should the in post unit be disabled. Default to false.
216
		 */
217
		$disable = apply_filters( 'wordads_inpost_disable', false );
218
		if ( ! $this->params->should_show() || $disable ) {
219
			return $content;
220
		}
221
222
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
223
		return $content . $this->get_ad( 'belowpost', $ad_type );
224
	}
225
226
	/**
227
	 * Inserts ad into header
228
	 *
229
	 * @since 4.5.0
230
	 */
231
	function insert_header_ad() {
232
		/**
233
		 * Allow third-party tools to disable the display of header ads.
234
		 *
235
		 * @module wordads
236
		 *
237
		 * @since 4.5.0
238
		 *
239
		 * @param bool true Should the header unit be disabled. Default to false.
240
		 */
241
		if ( apply_filters( 'wordads_header_disable', false ) ) {
242
			return;
243
		}
244
245
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
246
		echo $this->get_ad( 'top', $ad_type );
247
	}
248
249
	/**
250
	 * Special cases for inserting header unit via jQuery
251
	 *
252
	 * @since 4.5.0
253
	 */
254
	function insert_header_ad_special() {
255
		/**
256
		 * Allow third-party tools to disable the display of header ads.
257
		 *
258
		 * @module wordads
259
		 *
260
		 * @since 4.5.0
261
		 *
262
		 * @param bool true Should the header unit be disabled. Default to false.
263
		 */
264
		if ( apply_filters( 'wordads_header_disable', false ) ) {
265
			return;
266
		}
267
268
		$selector = '#content';
269
		switch ( get_stylesheet() ) {
270
			case 'twentyseventeen':
271
				$selector = '#content';
272
				break;
273
			case 'twentyfifteen':
274
				$selector = '#main';
275
				break;
276
			case 'twentyfourteen':
277
				$selector = 'article:first';
278
				break;
279
		}
280
281
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
282
		echo $this->get_ad( 'top', $ad_type );
283
		echo <<<HTML
284
		<script type="text/javascript">
285
			jQuery('.wpcnt-header').insertBefore('$selector');
286
		</script>
287
HTML;
288
	}
289
290
	/**
291
	 * Get the ad for the spot and type.
292
	 * @param  string $spot top, side, or belowpost
293
	 * @param  string $type iponweb or adsense
294
	 */
295
	function get_ad( $spot, $type = 'iponweb' ) {
296
		$snippet = '';
297
		$blocker_unit = 'mrec';
298
		if ( 'iponweb' == $type ) {
299
			$section_id = WORDADS_API_TEST_ID;
300
			$width = 300;
301
			$height = 250;
302
			$second_belowpost = '';
303
			if ( 'top' == $spot ) {
304
				// mrec for mobile, leaderboard for desktop
305
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
306
				$width = $this->params->mobile_device ? 300 : 728;
307
				$height = $this->params->mobile_device ? 250 : 90;
308
				$blocker_unit = $this->params->mobile_device ? 'top_mrec' : 'top';
309
			} else if ( 'belowpost' == $spot ) {
310
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
311
				$width = 300;
312
				$height = 250;
313
				if ( $this->option( 'wordads_second_belowpost', true ) ) {
314
					$section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
315
					$second_belowpost =
316
						"g.__ATA.initAd({collapseEmpty:'after', sectionId:$section_id2, width:$width, height:$height});";
317
				}
318
			}
319
320
			$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
321
			$snippet = <<<HTML
322
			<script$data_tags id='s$section_id' type='text/javascript'>
323
				(function(g){if('undefined'!=typeof g.__ATA){
324
					g.__ATA.initAd({collapseEmpty:'after', sectionId:$section_id, width:$width, height:$height});
325
					$second_belowpost
326
				}})(window);
327
			</script>
328
HTML;
329
		} else if ( 'house' == $type ) {
330
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
331
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
332
			if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
333
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
334
			}
335
		}
336
337
		$ad_blocker_ad = 'iponweb' == $type ? $this->get_adblocker_ad( $blocker_unit ) : '';
338
		$second_belowpost_css = '';
339
		$double_mrec = '';
340
		if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
341
			if ( 'iponweb' == $type ) {
342
				$ad_blocker_ad .= $this->get_adblocker_ad( 'mrec2' );
343
			}
344
345
			$double_mrec = 'wpmrec2x';
346
			$second_belowpost_css = <<<HTML
347
			<style type="text/css">
348
			div.wpmrec2x{max-width:610px;}
349
			div.wpmrec2x div.u > div{float:left;margin-right:10px;}
350
			div.wpmrec2x div.u > div:nth-child(3n){margin-right:0px;}
351
			</style>
352
HTML;
353
		}
354
355
		$header = 'top' == $spot ? 'wpcnt-header' : '';
356
		$about = __( 'Advertisements', 'jetpack' );
357
		return <<<HTML
358
		$second_belowpost_css
359
		<div class="wpcnt $header $double_mrec">
360
			<div class="wpa">
361
				<span class="wpa-about">$about</span>
362
				<div class="u $spot">
363
					$snippet
364
				</div>
365
				$ad_blocker_ad
366
			</div>
367
		</div>
368
HTML;
369
	}
370
371
	/**
372
	 * Get Criteo Acceptable Ad unit
373
	 * @param  string $unit mrec, mrec2, widesky, top, top_mrec
374
	 *
375
	 * @since 5.3
376
	 */
377
	public function get_adblocker_ad( $unit = 'mrec' ) {
378
		$criteo_id = mt_rand();
379
		$height = 250;
380
		$width = 300;
381
		$zone_id = 388248;
382
		if ( 'mrec2' == $unit ) { // 2nd belowpost
383
			$zone_id = 837497;
384
		} else if ( 'widesky' == $unit ) { // sidebar
385
			$zone_id = 563902;
386
			$width = 160;
387
			$height= 600;
388
		} else if ( 'top' == $unit ) { // top leaderboard
389
			$zone_id = 563903;
390
			$width = 728;
391
			$height = 90;
392
		} else if ( 'top_mrec' == $unit ) { // top mrec
393
			$zone_id = 563903;
394
		}
395
396
		return <<<HTML
397
		<div id="crt-$criteo_id" style="width:{$width}px;height:{$height}px;"></div>
398
		<script type="text/javascript">
399
		var o = document.getElementById('crt-$criteo_id');
400
		if ("undefined"!=typeof Criteo) {
401
			var p = o.parentNode;
402
			p.style.setProperty('display', 'inline-block', 'important');
403
			o.style.setProperty('display', 'block', 'important');
404
			Criteo.DisplayAcceptableAdIfAdblocked({zoneid:$zone_id,containerid:"crt-$criteo_id",collapseContainerIfNotAdblocked:true,"callifnotadblocked": function () {var o = document.getElementById('crt-$criteo_id'); o.style.setProperty('display','none','important');o.style.setProperty('visbility','hidden','important'); } });
405
		} else {
406
			o.style.setProperty('display', 'none', 'important');
407
			o.style.setProperty('visibility', 'hidden', 'important');
408
		}
409
		</script>
410
HTML;
411
	}
412
413
	/**
414
	 * Check the reasons to bail before we attempt to insert ads.
415
	 * @return true if we should bail (don't insert ads)
416
	 *
417
	 * @since 4.5.0
418
	 */
419
	public function should_bail() {
420
		return ! $this->option( 'wordads_approved' );
421
	}
422
423
	/**
424
	 * Returns markup for HTML5 house ad base on unit
425
	 * @param  string $unit mrec, widesky, or leaderboard
426
	 * @return string       markup for HTML5 house ad
427
	 *
428
	 * @since 4.7.0
429
	 */
430
	public function get_house_ad( $unit = 'mrec' ) {
431
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
432
			$unit = 'mrec';
433
		}
434
435
		$width  = 300;
436
		$height = 250;
437
		if ( 'widesky' == $unit ) {
438
			$width  = 160;
439
			$height = 600;
440
		} else if ( 'leaderboard' == $unit ) {
441
			$width  = 728;
442
			$height = 90;
443
		}
444
445
		return <<<HTML
446
		<iframe
447
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
448
			width="$width"
449
			height="$height"
450
			frameborder="0"
451
			scrolling="no"
452
			marginheight="0"
453
			marginwidth="0">
454
		</iframe>
455
HTML;
456
	}
457
458
	/**
459
	 * Activation hook actions
460
	 *
461
	 * @since 4.5.0
462
	 */
463
	public static function activate() {
464
		WordAds_API::update_wordads_status_from_api();
465
	}
466
}
467
468
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
469
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
470
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
471
472
global $wordads;
473
$wordads = new WordAds();
474