Completed
Push — try/lazy-wordads ( 4c73f9 )
by
unknown
34:40 queued 27:36
created

WordAds::insert_ad()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 0
loc 22
rs 8.6346
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
	public $ads = array();
19
20
	/**
21
	 * Array of supported ad types.
22
	 *
23
	 * @var array
24
	 */
25
	public static $ad_tag_ids = array(
26
		'mrec'               => array(
27
			'tag'    => '300x250_mediumrectangle',
28
			'height' => '250',
29
			'width'  => '300',
30
		),
31
		'leaderboard'        => array(
32
			'tag'    => '728x90_leaderboard',
33
			'height' => '90',
34
			'width'  => '728',
35
		),
36
		'mobile_leaderboard' => array(
37
			'tag'    => '320x50_mobileleaderboard',
38
			'height' => '50',
39
			'width'  => '320',
40
		),
41
		'wideskyscraper'     => array(
42
			'tag'    => '160x600_wideskyscraper',
43
			'height' => '600',
44
			'width'  => '160',
45
		),
46
	);
47
48
	/**
49
	 * Mapping array of location slugs to placement ids
50
	 *
51
	 * @var array
52
	 */
53
	public static $ad_location_ids = array(
54
		'top'           => 110,
55
		'belowpost'     => 120,
56
		'belowpost2'    => 130,
57
		'sidebar'       => 140,
58
		'widget'        => 150,
59
		'gutenberg'     => 200,
60
		'inline'        => 310,
61
		'inline-plugin' => 320,
62
	);
63
64
	/**
65
	 * Counter to enable unique, sequential section IDs for all amp-ad units
66
	 *
67
	 * @var int
68
	 */
69
	public static $amp_section_id = 1;
70
71
	/**
72
	 * Checks for AMP support and returns true iff active & AMP request
73
	 *
74
	 * @return boolean True if supported AMP request
75
	 *
76
	 * @since 7.5.0
77
	 */
78
	public static function is_amp() {
79
		return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
80
	}
81
82
	/**
83
	 * Increment the AMP section ID and return the value
84
	 *
85
	 * @return int
86
	 */
87
	public static function get_amp_section_id() {
88
		return self::$amp_section_id++;
89
	}
90
91
	public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;';
92
93
	/**
94
	 * Convenience function for grabbing options from params->options
95
	 *
96
	 * @param  string $option the option to grab
97
	 * @param  mixed  $default (optional)
98
	 * @return option or $default if not set
99
	 *
100
	 * @since 4.5.0
101
	 */
102
	function option( $option, $default = false ) {
103
		if ( ! isset( $this->params->options[ $option ] ) ) {
104
			return $default;
105
		}
106
107
		return $this->params->options[ $option ];
108
	}
109
110
	/**
111
	 * Returns the ad tag property array for supported ad types.
112
	 *
113
	 * @return array      array with ad tags
114
	 *
115
	 * @since 7.1.0
116
	 */
117
	function get_ad_tags() {
118
		return self::$ad_tag_ids;
119
	}
120
121
	/**
122
	 * Returns the solo css for unit
123
	 *
124
	 * @return string the special css for solo units
125
	 *
126
	 * @since 7.1.0
127
	 */
128
	function get_solo_unit_css() {
129
		return self::$SOLO_UNIT_CSS;
130
	}
131
132
	/**
133
	 * Instantiate the plugin
134
	 *
135
	 * @since 4.5.0
136
	 */
137
	function __construct() {
138
		add_action( 'wp', array( $this, 'init' ) );
139
		add_action( 'rest_api_init', array( $this, 'init' ) );
140
	}
141
142
	/**
143
	 * Code to run on WordPress 'init' hook
144
	 *
145
	 * @since 4.5.0
146
	 */
147
	function init() {
148
		require_once WORDADS_ROOT . '/php/params.php';
149
		$this->params = new WordAds_Params();
150
151
		if ( $this->should_bail() || self::is_infinite_scroll() ) {
152
			return;
153
		}
154
155
		if ( is_admin() ) {
156
			require_once WORDADS_ROOT . '/php/admin.php';
157
			return;
158
		}
159
160
		$this->insert_adcode();
161
162
		if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) {
163
164
			if ( false === ( $ads_txt_transient = get_transient( 'jetpack_ads_txt' ) ) ) {
165
				$ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : '';
166
				set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
167
			}
168
169
			/**
170
			 * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
171
			 *
172
			 * @module wordads
173
			 *
174
			 * @since 6.1.0
175
			 *
176
			 * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
177
			 */
178
			$ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
179
180
			http_response_code( 200 );
181
			header( 'Content-Type: text/plain; charset=utf-8' );
182
			echo esc_html( $ads_txt_content );
183
			die();
184
		}
185
	}
186
187
	/**
188
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
189
	 *
190
	 * @return boolean true if load came from infinite scroll
191
	 *
192
	 * @since 4.5.0
193
	 */
194
	public static function is_infinite_scroll() {
195
		return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
196
	}
197
198
	/**
199
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
200
	 *
201
	 * @since 4.5.0
202
	 */
203
	private function insert_adcode() {
204
		add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
205
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
206
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
207
		add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
208
209
		/**
210
		 * Filters enabling ads in `the_content` filter
211
		 *
212
		 * @see https://jetpack.com/support/ads/
213
		 *
214
		 * @module wordads
215
		 *
216
		 * @since 5.8.0
217
		 *
218
		 * @param bool True to disable ads in `the_content`
219
		 */
220
		if ( ! apply_filters( 'wordads_content_disable', false ) ) {
221
			add_filter( 'the_content', array( $this, 'insert_ad' ) );
222
		}
223
224
		/**
225
		 * Filters enabling ads in `the_excerpt` filter
226
		 *
227
		 * @see https://jetpack.com/support/ads/
228
		 *
229
		 * @module wordads
230
		 *
231
		 * @since 5.8.0
232
		 *
233
		 * @param bool True to disable ads in `the_excerpt`
234
		 */
235
		if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
236
			add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
237
		}
238
239
		if ( $this->option( 'enable_header_ad', true ) ) {
240
			if ( self::is_amp() ) {
241
				add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) );
242
			} else {
243
				switch ( get_stylesheet() ) {
244
					case 'twentyseventeen':
245
					case 'twentyfifteen':
246
					case 'twentyfourteen':
247
						add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
248
						break;
249
					default:
250
						add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
251
						break;
252
				}
253
			}
254
		}
255
	}
256
257
	/**
258
	 * Register desktop scripts and styles
259
	 *
260
	 * @since 4.5.0
261
	 */
262
	function enqueue_scripts() {
263
		wp_enqueue_style(
264
			'wordads',
265
			WORDADS_URL . 'css/style.css',
266
			array(),
267
			'2015-12-18'
268
		);
269
	}
270
271
	/**
272
	 * Add the IPW resource hints
273
	 *
274
	 * @since 7.9
275
	 */
276
	public function resource_hints( $hints, $relation_type ) {
277
		if ( 'dns-prefetch' === $relation_type ) {
278
			$hints[] = '//s.pubmine.com';
279
			$hints[] = '//x.bidswitch.net';
280
			$hints[] = '//static.criteo.net';
281
			$hints[] = '//ib.adnxs.com';
282
			$hints[] = '//aax.amazon-adsystem.com';
283
			$hints[] = '//bidder.criteo.com';
284
			$hints[] = '//cas.criteo.com';
285
			$hints[] = '//gum.criteo.com';
286
			$hints[] = '//ads.pubmatic.com';
287
			$hints[] = '//gads.pubmatic.com';
288
			$hints[] = '//tpc.googlesyndication.com';
289
			$hints[] = '//ad.doubleclick.net';
290
			$hints[] = '//googleads.g.doubleclick.net';
291
			$hints[] = '//www.googletagservices.com';
292
			$hints[] = '//cdn.switchadhub.com';
293
			$hints[] = '//delivery.g.switchadhub.com';
294
			$hints[] = '//delivery.swid.switchadhub.com';
295
		}
296
297
		return $hints;
298
	}
299
300
	/**
301
	 * IPONWEB metadata used by the various scripts
302
	 *
303
	 * @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...
304
	 */
305
	function insert_head_meta() {
306
		if ( self::is_amp() ) {
307
			return;
308
		}
309
		$themename = esc_js( get_stylesheet() );
310
		$pagetype  = intval( $this->params->get_page_type_ipw() );
311
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
312
		$site_id   = $this->params->blog_id;
313
		$consent   = intval( isset( $_COOKIE['personalized-ads-consent'] ) );
314
		echo <<<HTML
315
		<script$data_tags type="text/javascript">
316
			var __ATA = __ATA || {};
317
			__ATA.cmd = __ATA.cmd || {
318
				push: function( callback ) {
319
					__ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id, consent: $consent };
320
					__ATA.cmd = [ callback ];
321
					__ATA.criteo = __ATA.criteo || {};
322
					__ATA.criteo.cmd = __ATA.criteo.cmd || [];
323
324
					(function(){function g(a,c){a:{for(var b=a.length,d="string"==typeof a?a.split(""):a,e=0;e<b;e++)if(e in d&&c.call(void 0,d[e],e,a)){c=e;break a}c=-1}return 0>c?null:"string"==typeof a?a.charAt(c):a[c]};function h(a,c,b){b=null!=b?"="+encodeURIComponent(String(b)):"";if(c+=b){b=a.indexOf("#");0>b&&(b=a.length);var d=a.indexOf("?");if(0>d||d>b){d=b;var e=""}else e=a.substring(d+1,b);a=[a.substr(0,d),e,a.substr(b)];b=a[1];a[1]=c?b?b+"&"+c:c:b;a=a[0]+(a[1]?"?"+a[1]:"")+a[2]}return a};var k=0;function l(a,c){var b=document.createElement("script");b.src=a;b.onload=function(){c&&c(void 0)};b.onerror=function(){c("error")};a=document.getElementsByTagName("head");var d;a&&0!==a.length?d=a[0]:d=document.documentElement;d.appendChild(b)}function m(a){return"string"==typeof a&&0<a.length}
325
					function p(a,c,b){c=void 0===c?"":c;b=void 0===b?".":b;var d=[];Object.keys(a).forEach(function(e){var f=a[e],n=typeof f;"object"==n&&null!=f||"function"==n?d.push(p(f,c+e+b)):null!==f&&void 0!==f&&(e=encodeURIComponent(c+e),d.push(e+"="+encodeURIComponent(f)))});return d.filter(m).join("&")}function q(){return window.__ATA||{}}function r(a,c){a||(q().config=c.c,l(c.url))}var t=Math.floor(1E13*Math.random());q().rid=t;
326
					var u=q().pageParams,v="//"+(q().serverDomain||"s.pubmine.com")+"/conf",w=window.top===window,x;try{var y=JSON.parse(document.getElementById("oil-configuration").innerText);if("boolean"!==typeof y.gdpr_applies)throw Error("Config doesn't contain gdpr_applies");x=y.gdpr_applies?1:0}catch(a){x=null}
327
					var z=x,A=window.__ATA_PP||u||null,B=w?document.referrer?document.referrer:null:null,C=w?null:document.referrer?document.referrer:null,D=function(){var a=void 0===a?document.cookie:a;return(a=g(a.split("; "),function(c){return-1!=c.indexOf("__ATA_tuuid=")}))?a.split("=")[1]:""}(),E=p({gdpr:z,pp:A,rid:t,src:B,ref:C,tuuid:D?D:null,vp:window.innerWidth+"x"+window.innerHeight},"",".");
328
					(function(a){var c;k++;var b="callback__"+Date.now().toString(36)+"_"+k.toString(36);a=h(a,void 0===c?"cb":c,b);window[b]=function(d){r(void 0,d)};l(a,function(d){d&&r(d)})})(v+"?"+E);}).call(this);
329
				}
330
			}
331
		</script>
332
HTML;
333
	}
334
335
	/**
336
	 * Insert the ad onto the page
337
	 *
338
	 * @since 4.5.0
339
	 */
340
	function insert_ad( $content ) {
341
		// Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
342
		if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
343
			return $content;
344
		}
345
		/**
346
		 * Allow third-party tools to disable the display of in post ads.
347
		 *
348
		 * @module wordads
349
		 *
350
		 * @since 4.5.0
351
		 *
352
		 * @param bool true Should the in post unit be disabled. Default to false.
353
		 */
354
		$disable = apply_filters( 'wordads_inpost_disable', false );
355
		if ( ! $this->params->should_show() || $disable ) {
356
			return $content;
357
		}
358
359
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
360
		return $content . $this->get_ad( 'belowpost', $ad_type );
361
	}
362
363
	/**
364
	 * Insert an inline ad into a post content
365
	 * Used for rendering the `wordads` shortcode.
366
	 *
367
	 * @since 6.1.0
368
	 */
369
	function insert_inline_ad( $content ) {
370
		// Ad JS won't work in XML feeds.
371
		if ( is_feed() ) {
372
			return $content;
373
		}
374
		/**
375
		 * Allow third-party tools to disable the display of in post ads.
376
		 *
377
		 * @module wordads
378
		 *
379
		 * @since 4.5.0
380
		 *
381
		 * @param bool true Should the in post unit be disabled. Default to false.
382
		 */
383
		$disable = apply_filters( 'wordads_inpost_disable', false );
384
		if ( $disable ) {
385
			return $content;
386
		}
387
388
		$ad_type  = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
389
		$content .= $this->get_ad( 'inline', $ad_type );
390
		return $content;
391
	}
392
393
	/**
394
	 * Inserts ad into header
395
	 *
396
	 * @since 4.5.0
397
	 */
398
	function insert_header_ad() {
399
		/**
400
		 * Allow third-party tools to disable the display of header ads.
401
		 *
402
		 * @module wordads
403
		 *
404
		 * @since 4.5.0
405
		 *
406
		 * @param bool true Should the header unit be disabled. Default to false.
407
		 */
408
		if ( apply_filters( 'wordads_header_disable', false ) ) {
409
			return;
410
		}
411
412
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
413
		echo $this->get_ad( 'top', $ad_type );
414
	}
415
416
	/**
417
	 * Special cases for inserting header unit via JS
418
	 *
419
	 * @since 4.5.0
420
	 */
421
	function insert_header_ad_special() {
422
		/**
423
		 * Allow third-party tools to disable the display of header ads.
424
		 *
425
		 * @module wordads
426
		 *
427
		 * @since 4.5.0
428
		 *
429
		 * @param bool true Should the header unit be disabled. Default to false.
430
		 */
431
		if ( apply_filters( 'wordads_header_disable', false ) ) {
432
			return;
433
		}
434
435
		$selector = '#content';
436
		switch ( get_stylesheet() ) {
437
			case 'twentyseventeen':
438
				$selector = '#content';
439
				break;
440
			case 'twentyfifteen':
441
				$selector = '#main';
442
				break;
443
			case 'twentyfourteen':
444
				$selector = 'article';
445
				break;
446
		}
447
448
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
449
		echo $this->get_ad( 'top', $ad_type );
450
		if ( ! self::is_amp() ) {
451
			echo <<<HTML
452
		<script type="text/javascript">
453
			(function ( selector ) {
454
				var main = document.querySelector( selector );
455
				var headerAd = document.querySelector('.wpcnt-header');
456
457
				if ( main ) {
458
					main.parentNode.insertBefore( headerAd, main );
459
				}
460
			})( '$selector' );
461
462
		</script>
463
HTML;
464
		}
465
	}
466
467
	/**
468
	 * Header unit for AMP
469
	 *
470
	 * @param string $content Content of the page.
471
	 *
472
	 * @since 7.5.0
473
	 */
474
	public function insert_header_ad_amp( $content ) {
475
476
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
477
		if ( 'house' === $ad_type ) {
478
			return $content;
479
		}
480
		return $this->get_ad( 'top_amp', $ad_type ) . $content;
481
482
	}
483
484
	/**
485
	 * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
486
	 *
487
	 * @param  string $adstxt The ads.txt being filtered
488
	 * @return string         Filtered ads.txt with custom entries, if applicable
489
	 *
490
	 * @since 6.5.0
491
	 */
492
	function insert_custom_adstxt( $adstxt ) {
493
		$custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
494
		if ( $custom_adstxt ) {
495
			$adstxt .= "\n\n#Jetpack - User Custom Entries\n";
496
			$adstxt .= $custom_adstxt . "\n";
497
		}
498
499
		return $adstxt;
500
	}
501
502
	/**
503
	 * Get the ad for the spot and type.
504
	 *
505
	 * @param  string $spot top, side, inline, or belowpost
506
	 * @param  string $type iponweb or adsense
507
	 */
508
	function get_ad( $spot, $type = 'iponweb' ) {
509
		$snippet = '';
510
		if ( 'iponweb' == $type ) {
511
			// Default to mrec
512
			$width  = 300;
513
			$height = 250;
514
515
			$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...
516
			$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...
517
			$snippet          = '';
518
			if ( 'top' == $spot ) {
519
				// mrec for mobile, leaderboard for desktop
520
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
521
				$width      = $this->params->mobile_device ? 300 : 728;
522
				$height     = $this->params->mobile_device ? 250 : 90;
523
				$snippet    = $this->get_ad_snippet( $section_id, $height, $width, $spot );
524
			} elseif ( 'belowpost' == $spot ) {
525
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
526
				$width      = 300;
527
				$height     = 250;
528
529
				$snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
530
				if ( $this->option( 'wordads_second_belowpost', true ) ) {
531
					$section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
532
					$snippet    .= $this->get_ad_snippet( $section_id2, $height, $width, $spot . '2', 'float:left;margin-top:0px;' );
533
				}
534
			} elseif ( 'inline' === $spot ) {
535
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
536
				$snippet    = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
537
			} elseif ( 'top_amp' === $spot ) {
538
				// 320x50 unit which can safely be inserted below title, above content in a variety of themes.
539
				$width   = 320;
540
				$height  = 50;
541
				$snippet = $this->get_ad_snippet( null, $height, $width );
542
			}
543
		} elseif ( 'house' == $type ) {
544
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
545
			$snippet     = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
546
			if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
547
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
548
			}
549
		}
550
551
		return $this->get_ad_div( $spot, $snippet );
552
	}
553
554
555
	/**
556
	 * Returns the snippet to be inserted into the ad unit
557
	 *
558
	 * @param  int    $section_id
559
	 * @param  int    $height
560
	 * @param  int    $width
561
	 * @param  int    $location
0 ignored issues
show
Documentation introduced by
Should the type for parameter $location not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
562
	 * @param  string $css
563
	 * @return string
564
	 *
565
	 * @since 5.7
566
	 */
567
	public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
568
		$this->ads[] = array(
569
			'location' => $location,
570
			'width'    => $width,
571
			'height'   => $height,
572
		);
573
574
		if ( self::is_amp() ) {
575
			$height         = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad"
576
			$width          = esc_attr( $width );
577
			$amp_section_id = esc_attr( self::get_amp_section_id() );
578
			$site_id        = esc_attr( $this->params->blog_id );
579
			return <<<HTML
580
			<amp-ad width="$width" height="$height"
581
			    type="pubmine"
582
			    data-siteid="$site_id"
583
			    data-section="$amp_section_id">
584
			</amp-ad>
585
HTML;
586
		}
587
588
		$ad_number = count( $this->ads ) . '-' . uniqid();
589
		$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
590
		$css       = esc_attr( $css );
591
592
		$loc_id = 100;
593
		if ( ! empty( self::$ad_location_ids[ $location ] ) ) {
594
			$loc_id = self::$ad_location_ids[ $location ];
595
		}
596
597
		return <<<HTML
598
		<div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
599
			<div id="atatags-{$ad_number}">
600
				<script$data_tags type="text/javascript">
601
				__ATA.cmd.push(function() {
602
					__ATA.initSlot('atatags-{$ad_number}',  {
603
						collapseEmpty: 'before',
604
						sectionId: '{$section_id}',
605
						location: {$loc_id},
606
						width: {$width},
607
						height: {$height}
608
					});
609
				});
610
				</script>
611
			</div>
612
		</div>
613
HTML;
614
	}
615
616
	/**
617
	 * Returns the complete ad div with snippet to be inserted into the page
618
	 *
619
	 * @param  string $spot top, side, inline, or belowpost.
620
	 * @param  string $snippet The snippet to insert into the div.
621
	 * @param  array  $css_classes CSS classes.
622
	 * @return string The supporting ad unit div.
623
	 *
624
	 * @since 7.1
625
	 */
626
	function get_ad_div( $spot, $snippet, array $css_classes = array() ) {
627
		if ( empty( $css_classes ) ) {
628
			$css_classes = array();
629
		}
630
631
		$css_classes[] = 'wpcnt';
632
		if ( 'top' == $spot ) {
633
			$css_classes[] = 'wpcnt-header';
634
		}
635
636
		$spot    = esc_attr( $spot );
637
		$classes = esc_attr( implode( ' ', $css_classes ) );
638
		$about   = esc_html__( 'Advertisements', 'jetpack' );
639
		return <<<HTML
640
		<div class="$classes">
641
			<div class="wpa">
642
				<span class="wpa-about">$about</span>
643
				<div class="u $spot">
644
					$snippet
645
				</div>
646
			</div>
647
		</div>
648
HTML;
649
	}
650
651
	/**
652
	 * Check the reasons to bail before we attempt to insert ads.
653
	 *
654
	 * @return true if we should bail (don't insert ads)
655
	 *
656
	 * @since 4.5.0
657
	 */
658
	public function should_bail() {
659
		return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' );
660
	}
661
662
	/**
663
	 * Returns markup for HTML5 house ad base on unit
664
	 *
665
	 * @param  string $unit mrec, widesky, or leaderboard
666
	 * @return string       markup for HTML5 house ad
667
	 *
668
	 * @since 4.7.0
669
	 */
670
	public function get_house_ad( $unit = 'mrec' ) {
671
672
		switch ( $unit ) {
673
			case 'widesky':
674
				$width  = 160;
675
				$height = 600;
676
				break;
677
			case 'leaderboard':
678
				$width  = 728;
679
				$height = 90;
680
				break;
681
			case 'mrec':
682
			default:
683
				$width  = 300;
684
				$height = 250;
685
				break;
686
		}
687
688
		return <<<HTML
689
		<iframe
690
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
691
			width="$width"
692
			height="$height"
693
			frameborder="0"
694
			scrolling="no"
695
			marginheight="0"
696
			marginwidth="0">
697
		</iframe>
698
HTML;
699
	}
700
701
	/**
702
	 * Activation hook actions
703
	 *
704
	 * @since 4.5.0
705
	 */
706
	public static function activate() {
707
		WordAds_API::update_wordads_status_from_api();
708
	}
709
}
710
711
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
712
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
713
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
714
715
global $wordads;
716
$wordads = new WordAds();
717