Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

WordAds::should_bail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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
117
		/**
118
		 * Filters enabling ads in `the_content` filter
119
		 *
120
		 * @see https://jetpack.com/support/ads/
121
		 *
122
		 * @module wordads
123
		 *
124
		 * @since 5.8.0
125
		 *
126
		 * @param bool True to disable ads in `the_content`
127
		 */
128
		if ( ! apply_filters( 'wordads_content_disable', false ) ) {
129
			add_filter( 'the_content', array( $this, 'insert_ad' ) );
130
		}
131
132
		/**
133
		 * Filters enabling ads in `the_excerpt` filter
134
		 *
135
		 * @see https://jetpack.com/support/ads/
136
		 *
137
		 * @module wordads
138
		 *
139
		 * @since 5.8.0
140
		 *
141
		 * @param bool True to disable ads in `the_excerpt`
142
		 */
143
		if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
144
			add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
145
		}
146
147
		if ( $this->option( 'enable_header_ad', true ) ) {
148
			switch ( get_stylesheet() ) {
149
				case 'twentyseventeen':
150
				case 'twentyfifteen':
151
				case 'twentyfourteen':
152
					add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
153
					break;
154
				default:
155
					add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
156
					break;
157
			}
158
		}
159
	}
160
161
	/**
162
	 * Register desktop scripts and styles
163
	 *
164
	 * @since 4.5.0
165
	 */
166
	function enqueue_scripts() {
167
		wp_enqueue_style(
168
			'wordads',
169
			WORDADS_URL . 'css/style.css',
170
			array(),
171
			'2015-12-18'
172
		);
173
	}
174
175
	/**
176
	 * IPONWEB metadata used by the various scripts
177
	 * @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...
178
	 */
179
	function insert_head_meta() {
180
		$themename = esc_js( get_stylesheet() );
181
		$pagetype = intval( $this->params->get_page_type_ipw() );
182
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
183
		$site_id = $this->params->blog_id;
184
		echo <<<HTML
185
		<script$data_tags type="text/javascript">
186
			var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id };
187
			var __ATA = __ATA || {};
188
			__ATA.cmd = __ATA.cmd || [];
189
			__ATA.criteo = __ATA.criteo || {};
190
			__ATA.criteo.cmd = __ATA.criteo.cmd || [];
191
		</script>
192
HTML;
193
	}
194
195
	/**
196
	 * IPONWEB scripts in <head>
197
	 *
198
	 * @since 4.5.0
199
	 */
200
	function insert_head_iponweb() {
201
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
202
		echo <<<HTML
203
		<link rel='dns-prefetch' href='//s.pubmine.com' />
204
		<link rel='dns-prefetch' href='//x.bidswitch.net' />
205
		<link rel='dns-prefetch' href='//static.criteo.net' />
206
		<link rel='dns-prefetch' href='//ib.adnxs.com' />
207
		<link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
208
		<link rel='dns-prefetch' href='//bidder.criteo.com' />
209
		<link rel='dns-prefetch' href='//cas.criteo.com' />
210
		<link rel='dns-prefetch' href='//gum.criteo.com' />
211
		<link rel='dns-prefetch' href='//ads.pubmatic.com' />
212
		<link rel='dns-prefetch' href='//gads.pubmatic.com' />
213
		<link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
214
		<link rel='dns-prefetch' href='//ad.doubleclick.net' />
215
		<link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
216
		<link rel='dns-prefetch' href='//www.googletagservices.com' />
217
		<link rel='dns-prefetch' href='//cdn.switchadhub.com' />
218
		<link rel='dns-prefetch' href='//delivery.g.switchadhub.com' />
219
		<link rel='dns-prefetch' href='//delivery.swid.switchadhub.com' />
220
		<script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
221
HTML;
222
	}
223
224
	/**
225
	 * Insert the ad onto the page
226
	 *
227
	 * @since 4.5.0
228
	 */
229
	function insert_ad( $content ) {
230
		// Ad JS won't work in XML feeds.
231
		if ( is_feed() ) {
232
			return $content;
233
		}
234
		/**
235
		 * Allow third-party tools to disable the display of in post ads.
236
		 *
237
		 * @module wordads
238
		 *
239
		 * @since 4.5.0
240
		 *
241
		 * @param bool true Should the in post unit be disabled. Default to false.
242
		 */
243
		$disable = apply_filters( 'wordads_inpost_disable', false );
244
		if ( ! $this->params->should_show() || $disable ) {
245
			return $content;
246
		}
247
248
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
249
		return $content . $this->get_ad( 'belowpost', $ad_type );
250
	}
251
252
	/**
253
	 * Inserts ad into header
254
	 *
255
	 * @since 4.5.0
256
	 */
257
	function insert_header_ad() {
258
		/**
259
		 * Allow third-party tools to disable the display of header ads.
260
		 *
261
		 * @module wordads
262
		 *
263
		 * @since 4.5.0
264
		 *
265
		 * @param bool true Should the header unit be disabled. Default to false.
266
		 */
267
		if ( apply_filters( 'wordads_header_disable', false ) ) {
268
			return;
269
		}
270
271
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
272
		echo $this->get_ad( 'top', $ad_type );
273
	}
274
275
	/**
276
	 * Special cases for inserting header unit via jQuery
277
	 *
278
	 * @since 4.5.0
279
	 */
280
	function insert_header_ad_special() {
281
		/**
282
		 * Allow third-party tools to disable the display of header ads.
283
		 *
284
		 * @module wordads
285
		 *
286
		 * @since 4.5.0
287
		 *
288
		 * @param bool true Should the header unit be disabled. Default to false.
289
		 */
290
		if ( apply_filters( 'wordads_header_disable', false ) ) {
291
			return;
292
		}
293
294
		$selector = '#content';
295
		switch ( get_stylesheet() ) {
296
			case 'twentyseventeen':
297
				$selector = '#content';
298
				break;
299
			case 'twentyfifteen':
300
				$selector = '#main';
301
				break;
302
			case 'twentyfourteen':
303
				$selector = 'article:first';
304
				break;
305
		}
306
307
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
308
		echo $this->get_ad( 'top', $ad_type );
309
		echo <<<HTML
310
		<script type="text/javascript">
311
			jQuery('.wpcnt-header').insertBefore('$selector');
312
		</script>
313
HTML;
314
	}
315
316
	/**
317
	 * Get the ad for the spot and type.
318
	 * @param  string $spot top, side, or belowpost
319
	 * @param  string $type iponweb or adsense
320
	 */
321
	function get_ad( $spot, $type = 'iponweb' ) {
322
		$snippet = '';
323
		$blocker_unit = 'mrec';
0 ignored issues
show
Unused Code introduced by
$blocker_unit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
324
		if ( 'iponweb' == $type ) {
325
			$section_id = WORDADS_API_TEST_ID;
0 ignored issues
show
Unused Code introduced by
$section_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
326
			$width = 300;
0 ignored issues
show
Unused Code introduced by
$width is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
327
			$height = 250;
0 ignored issues
show
Unused Code introduced by
$height is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
328
			$second_belowpost = '';
0 ignored issues
show
Unused Code introduced by
$second_belowpost is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
329
			$snippet = '';
330
			if ( 'top' == $spot ) {
331
				// mrec for mobile, leaderboard for desktop
332
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
333
				$width = $this->params->mobile_device ? 300 : 728;
334
				$height = $this->params->mobile_device ? 250 : 90;
335
				$blocker_unit = $this->params->mobile_device ? 'top_mrec' : 'top';
336
				$snippet = $this->get_ad_snippet( $section_id, $height, $width, $blocker_unit );
337
			} else if ( 'belowpost' == $spot ) {
338
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
339
				$width = 300;
340
				$height = 250;
341
342
				$snippet = $this->get_ad_snippet( $section_id, $height, $width, 'mrec', 'float:left;margin-right:5px;margin-top:0px;' );
343
				if ( $this->option( 'wordads_second_belowpost', true ) ) {
344
					$section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
345
					$snippet .= $this->get_ad_snippet( $section_id2, $height, $width, 'mrec2', 'float:left;margin-top:0px;' );
346
				}
347
			}
348
		} else if ( 'house' == $type ) {
349
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
350
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
351
			if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
352
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
353
			}
354
		}
355
356
		$header = 'top' == $spot ? 'wpcnt-header' : '';
357
		$about = __( 'Advertisements', 'jetpack' );
358
		return <<<HTML
359
		<div class="wpcnt $header">
360
			<div class="wpa">
361
				<span class="wpa-about">$about</span>
362
				<div class="u $spot">
363
					$snippet
364
				</div>
365
			</div>
366
		</div>
367
HTML;
368
	}
369
370
371
	/**
372
	 * Returns the snippet to be inserted into the ad unit
373
	 * @param  int $section_id
374
	 * @param  int $height
375
	 * @param  int $width
376
	 * @param  string $css
377
	 * @return string
378
	 *
379
	 * @since 5.7
380
	 */
381
	function get_ad_snippet( $section_id, $height, $width, $adblock_unit = 'mrec', $css = '' ) {
382
		$this->ads[] = array( 'id' => $section_id, 'width' => $width, 'height' => $height );
0 ignored issues
show
Bug introduced by
The property ads does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
383
		$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
384
		$adblock_ad = $this->get_adblocker_ad( $adblock_unit );
385
386
		return <<<HTML
387
		<div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
388
			<div id="atatags-{$section_id}">
389
				<script$data_tags type="text/javascript">
390
				__ATA.cmd.push(function() {
391
					__ATA.initSlot('atatags-{$section_id}',  {
392
						collapseEmpty: 'before',
393
						sectionId: '{$section_id}',
394
						width: {$width},
395
						height: {$height}
396
					});
397
				});
398
				</script>
399
				$adblock_ad
400
			</div>
401
		</div>
402
HTML;
403
	}
404
405
	/**
406
	 * Get Criteo Acceptable Ad unit
407
	 * @param  string $unit mrec, mrec2, widesky, top, top_mrec
408
	 *
409
	 * @since 5.3
410
	 */
411
	public function get_adblocker_ad( $unit = 'mrec' ) {
412
		$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
413
		$criteo_id = mt_rand();
414
		$height = 250;
415
		$width = 300;
416
		$zone_id = 388248;
417
		if ( 'mrec2' == $unit ) { // 2nd belowpost
418
			$zone_id = 837497;
419
		} else if ( 'widesky' == $unit ) { // sidebar
420
			$zone_id = 563902;
421
			$width = 160;
422
			$height= 600;
423
		} else if ( 'top' == $unit ) { // top leaderboard
424
			$zone_id = 563903;
425
			$width = 728;
426
			$height = 90;
427
		} else if ( 'top_mrec' == $unit ) { // top mrec
428
			$zone_id = 563903;
429
		}
430
431
		return <<<HTML
432
		<div id="crt-$criteo_id" style="width:{$width}px;height:{$height}px;display:none !important;"></div>
433
		<script$data_tags type="text/javascript">
434
		(function(){var c=function(){var a=document.getElementById("crt-{$criteo_id}");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:{$zone_id},containerid:"crt-{$criteo_id}",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})();
435
		</script>
436
HTML;
437
	}
438
439
	/**
440
	 * Check the reasons to bail before we attempt to insert ads.
441
	 * @return true if we should bail (don't insert ads)
442
	 *
443
	 * @since 4.5.0
444
	 */
445
	public function should_bail() {
446
		return ! $this->option( 'wordads_approved' );
447
	}
448
449
	/**
450
	 * Returns markup for HTML5 house ad base on unit
451
	 * @param  string $unit mrec, widesky, or leaderboard
452
	 * @return string       markup for HTML5 house ad
453
	 *
454
	 * @since 4.7.0
455
	 */
456
	public function get_house_ad( $unit = 'mrec' ) {
457
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
458
			$unit = 'mrec';
459
		}
460
461
		$width  = 300;
462
		$height = 250;
463
		if ( 'widesky' == $unit ) {
464
			$width  = 160;
465
			$height = 600;
466
		} else if ( 'leaderboard' == $unit ) {
467
			$width  = 728;
468
			$height = 90;
469
		}
470
471
		return <<<HTML
472
		<iframe
473
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
474
			width="$width"
475
			height="$height"
476
			frameborder="0"
477
			scrolling="no"
478
			marginheight="0"
479
			marginwidth="0">
480
		</iframe>
481
HTML;
482
	}
483
484
	/**
485
	 * Activation hook actions
486
	 *
487
	 * @since 4.5.0
488
	 */
489
	public static function activate() {
490
		WordAds_API::update_wordads_status_from_api();
491
	}
492
}
493
494
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
495
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
496
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
497
498
global $wordads;
499
$wordads = new WordAds();
500