Completed
Push — add/new-disconnect-dialog ( b4649f...72298c )
by
unknown
23:43 queued 16:44
created

WordAds::insert_header_ad_amp()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 9
rs 9.9666
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
	 * @return boolean True if supported AMP request
74
	 *
75
	 * @since 7.5.0
76
	 */
77
	public static function is_amp() {
78
		return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
79
	}
80
81
	/**
82
	 * Increment the AMP section ID and return the value
83
	 *
84
	 * @return int
85
	 */
86
	public static function get_amp_section_id() {
87
		return self::$amp_section_id++;
88
	}
89
90
	public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;';
91
92
	/**
93
	 * Convenience function for grabbing options from params->options
94
	 *
95
	 * @param  string $option the option to grab
96
	 * @param  mixed  $default (optional)
97
	 * @return option or $default if not set
98
	 *
99
	 * @since 4.5.0
100
	 */
101
	function option( $option, $default = false ) {
102
		if ( ! isset( $this->params->options[ $option ] ) ) {
103
			return $default;
104
		}
105
106
		return $this->params->options[ $option ];
107
	}
108
109
	/**
110
	 * Returns the ad tag property array for supported ad types.
111
	 * @return array      array with ad tags
112
	 *
113
	 * @since 7.1.0
114
	 */
115
	function get_ad_tags() {
116
		return self::$ad_tag_ids;
117
	}
118
119
	/**
120
	 * Returns the solo css for unit
121
	 * @return string the special css for solo units
122
	 *
123
	 * @since 7.1.0
124
	 */
125
	function get_solo_unit_css() {
126
		return self::$SOLO_UNIT_CSS;
127
	}
128
129
	/**
130
	 * Instantiate the plugin
131
	 *
132
	 * @since 4.5.0
133
	 */
134
	function __construct() {
135
		add_action( 'wp', array( $this, 'init' ) );
136
		add_action( 'rest_api_init', array( $this, 'init' ) );
137
	}
138
139
	/**
140
	 * Code to run on WordPress 'init' hook
141
	 *
142
	 * @since 4.5.0
143
	 */
144
	function init() {
145
		require_once WORDADS_ROOT . '/php/params.php';
146
		$this->params = new WordAds_Params();
147
148
		if ( $this->should_bail() || self::is_infinite_scroll() ) {
149
			return;
150
		}
151
152
		if ( is_admin() ) {
153
			require_once WORDADS_ROOT . '/php/admin.php';
154
			return;
155
		}
156
157
		$this->insert_adcode();
158
159
		if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) {
160
161
			if ( false === ( $ads_txt_transient = get_transient( 'jetpack_ads_txt' ) ) ) {
162
				$ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : '';
163
				set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
164
			}
165
166
			/**
167
			 * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
168
			 *
169
			 * @module wordads
170
			 *
171
			 * @since 6.1.0
172
			 *
173
			 * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
174
			 */
175
			$ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
176
177
			header( 'Content-Type: text/plain; charset=utf-8' );
178
			echo esc_html( $ads_txt_content );
179
			die();
180
		}
181
	}
182
183
	/**
184
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
185
	 *
186
	 * @return boolean true if load came from infinite scroll
187
	 *
188
	 * @since 4.5.0
189
	 */
190
	public static function is_infinite_scroll() {
191
		return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
192
	}
193
194
	/**
195
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
196
	 *
197
	 * @since 4.5.0
198
	 */
199
	private function insert_adcode() {
200
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
201
		add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
202
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
203
		add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
204
205
		/**
206
		 * Filters enabling ads in `the_content` filter
207
		 *
208
		 * @see https://jetpack.com/support/ads/
209
		 *
210
		 * @module wordads
211
		 *
212
		 * @since 5.8.0
213
		 *
214
		 * @param bool True to disable ads in `the_content`
215
		 */
216
		if ( ! apply_filters( 'wordads_content_disable', false ) ) {
217
			add_filter( 'the_content', array( $this, 'insert_ad' ) );
218
		}
219
220
		/**
221
		 * Filters enabling ads in `the_excerpt` filter
222
		 *
223
		 * @see https://jetpack.com/support/ads/
224
		 *
225
		 * @module wordads
226
		 *
227
		 * @since 5.8.0
228
		 *
229
		 * @param bool True to disable ads in `the_excerpt`
230
		 */
231
		if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
232
			add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
233
		}
234
235
		if ( $this->option( 'enable_header_ad', true ) ) {
236
			if ( self::is_amp() ) {
237
				add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) );
238
			} else {
239
				switch ( get_stylesheet() ) {
240
					case 'twentyseventeen':
241
					case 'twentyfifteen':
242
					case 'twentyfourteen':
243
						add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
244
						break;
245
					default:
246
						add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
247
						break;
248
				}
249
			}
250
		}
251
	}
252
253
	/**
254
	 * Register desktop scripts and styles
255
	 *
256
	 * @since 4.5.0
257
	 */
258
	function enqueue_scripts() {
259
		wp_enqueue_style(
260
			'wordads',
261
			WORDADS_URL . 'css/style.css',
262
			array(),
263
			'2015-12-18'
264
		);
265
	}
266
267
	/**
268
	 * IPONWEB metadata used by the various scripts
269
	 *
270
	 * @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...
271
	 */
272
	function insert_head_meta() {
273
		if ( self::is_amp() ) {
274
			return;
275
		}
276
		$themename = esc_js( get_stylesheet() );
277
		$pagetype  = intval( $this->params->get_page_type_ipw() );
278
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
279
		$site_id   = $this->params->blog_id;
280
		$consent   = intval( isset( $_COOKIE['personalized-ads-consent'] ) );
281
		echo <<<HTML
282
		<script$data_tags type="text/javascript">
283
			var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id, consent: $consent };
284
			var __ATA = __ATA || {};
285
			__ATA.cmd = __ATA.cmd || [];
286
			__ATA.criteo = __ATA.criteo || {};
287
			__ATA.criteo.cmd = __ATA.criteo.cmd || [];
288
		</script>
289
HTML;
290
	}
291
292
	/**
293
	 * IPONWEB scripts in <head>
294
	 *
295
	 * @since 4.5.0
296
	 */
297
	function insert_head_iponweb() {
298
		if ( self::is_amp() ) {
299
			return;
300
		}
301
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
302
		echo <<<HTML
303
		<link rel='dns-prefetch' href='//s.pubmine.com' />
304
		<link rel='dns-prefetch' href='//x.bidswitch.net' />
305
		<link rel='dns-prefetch' href='//static.criteo.net' />
306
		<link rel='dns-prefetch' href='//ib.adnxs.com' />
307
		<link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
308
		<link rel='dns-prefetch' href='//bidder.criteo.com' />
309
		<link rel='dns-prefetch' href='//cas.criteo.com' />
310
		<link rel='dns-prefetch' href='//gum.criteo.com' />
311
		<link rel='dns-prefetch' href='//ads.pubmatic.com' />
312
		<link rel='dns-prefetch' href='//gads.pubmatic.com' />
313
		<link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
314
		<link rel='dns-prefetch' href='//ad.doubleclick.net' />
315
		<link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
316
		<link rel='dns-prefetch' href='//www.googletagservices.com' />
317
		<script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
318
HTML;
319
	}
320
321
	/**
322
	 * Insert the ad onto the page
323
	 *
324
	 * @since 4.5.0
325
	 */
326
	function insert_ad( $content ) {
327
		// Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
328
		if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
329
			return $content;
330
		}
331
		/**
332
		 * Allow third-party tools to disable the display of in post ads.
333
		 *
334
		 * @module wordads
335
		 *
336
		 * @since 4.5.0
337
		 *
338
		 * @param bool true Should the in post unit be disabled. Default to false.
339
		 */
340
		$disable = apply_filters( 'wordads_inpost_disable', false );
341
		if ( ! $this->params->should_show() || $disable ) {
342
			return $content;
343
		}
344
345
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
346
		return $content . $this->get_ad( 'belowpost', $ad_type );
347
	}
348
349
	/**
350
	 * Insert an inline ad into a post content
351
	 * Used for rendering the `wordads` shortcode.
352
	 *
353
	 * @since 6.1.0
354
	 */
355
	function insert_inline_ad( $content ) {
356
		// Ad JS won't work in XML feeds.
357
		if ( is_feed() ) {
358
			return $content;
359
		}
360
		/**
361
		 * Allow third-party tools to disable the display of in post ads.
362
		 *
363
		 * @module wordads
364
		 *
365
		 * @since 4.5.0
366
		 *
367
		 * @param bool true Should the in post unit be disabled. Default to false.
368
		 */
369
		$disable = apply_filters( 'wordads_inpost_disable', false );
370
		if ( $disable ) {
371
			return $content;
372
		}
373
374
		$ad_type  = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
375
		$content .= $this->get_ad( 'inline', $ad_type );
376
		return $content;
377
	}
378
379
	/**
380
	 * Inserts ad into header
381
	 *
382
	 * @since 4.5.0
383
	 */
384
	function insert_header_ad() {
385
		/**
386
		 * Allow third-party tools to disable the display of header ads.
387
		 *
388
		 * @module wordads
389
		 *
390
		 * @since 4.5.0
391
		 *
392
		 * @param bool true Should the header unit be disabled. Default to false.
393
		 */
394
		if ( apply_filters( 'wordads_header_disable', false ) ) {
395
			return;
396
		}
397
398
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
399
		echo $this->get_ad( 'top', $ad_type );
400
	}
401
402
	/**
403
	 * Special cases for inserting header unit via jQuery
404
	 *
405
	 * @since 4.5.0
406
	 */
407
	function insert_header_ad_special() {
408
		/**
409
		 * Allow third-party tools to disable the display of header ads.
410
		 *
411
		 * @module wordads
412
		 *
413
		 * @since 4.5.0
414
		 *
415
		 * @param bool true Should the header unit be disabled. Default to false.
416
		 */
417
		if ( apply_filters( 'wordads_header_disable', false ) ) {
418
			return;
419
		}
420
421
		$selector = '#content';
422
		switch ( get_stylesheet() ) {
423
			case 'twentyseventeen':
424
				$selector = '#content';
425
				break;
426
			case 'twentyfifteen':
427
				$selector = '#main';
428
				break;
429
			case 'twentyfourteen':
430
				$selector = 'article:first';
431
				break;
432
		}
433
434
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
435
		echo $this->get_ad( 'top', $ad_type );
436
		if ( ! self::is_amp() ) {
437
			echo <<<HTML
438
		<script type="text/javascript">
439
			jQuery('.wpcnt-header').insertBefore('$selector');
440
		</script>
441
HTML;
442
		}
443
	}
444
445
	/**
446
	 * Header unit for AMP
447
	 *
448
	 * @param string $content Content of the page.
449
	 *
450
	 * @since 7.5.0
451
	 */
452
	public function insert_header_ad_amp( $content ) {
453
454
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
455
		if ( 'house' === $ad_type ) {
456
			return $content;
457
		}
458
		return $this->get_ad( 'top_amp', $ad_type ) . $content;
459
460
	}
461
462
	/**
463
	 * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
464
	 *
465
	 * @param  string $adstxt The ads.txt being filtered
466
	 * @return string         Filtered ads.txt with custom entries, if applicable
467
	 *
468
	 * @since 6.5.0
469
	 */
470
	function insert_custom_adstxt( $adstxt ) {
471
		$custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
472
		if ( $custom_adstxt ) {
473
			$adstxt .= "\n\n#Jetpack - User Custom Entries\n";
474
			$adstxt .= $custom_adstxt . "\n";
475
		}
476
477
		return $adstxt;
478
	}
479
480
	/**
481
	 * Get the ad for the spot and type.
482
	 *
483
	 * @param  string $spot top, side, inline, or belowpost
484
	 * @param  string $type iponweb or adsense
485
	 */
486
	function get_ad( $spot, $type = 'iponweb' ) {
487
		$snippet = '';
488
		if ( 'iponweb' == $type ) {
489
			// Default to mrec
490
			$width  = 300;
491
			$height = 250;
492
493
			$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...
494
			$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...
495
			$snippet          = '';
496
			if ( 'top' == $spot ) {
497
				// mrec for mobile, leaderboard for desktop
498
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
499
				$width      = $this->params->mobile_device ? 300 : 728;
500
				$height     = $this->params->mobile_device ? 250 : 90;
501
				$snippet    = $this->get_ad_snippet( $section_id, $height, $width, $spot );
502
			} elseif ( 'belowpost' == $spot ) {
503
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
504
				$width      = 300;
505
				$height     = 250;
506
507
				$snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
508
				if ( $this->option( 'wordads_second_belowpost', true ) ) {
509
					$section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
510
					$snippet    .= $this->get_ad_snippet( $section_id2, $height, $width, $spot . '2', 'float:left;margin-top:0px;' );
511
				}
512
			} elseif ( 'inline' === $spot ) {
513
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
514
				$snippet    = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
515
			} elseif ( 'top_amp' === $spot ) {
516
				// 320x50 unit which can safely be inserted below title, above content in a variety of themes.
517
				$width   = 320;
518
				$height  = 50;
519
				$snippet = $this->get_ad_snippet( null, $height, $width );
520
			}
521
		} elseif ( 'house' == $type ) {
522
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
523
			$snippet     = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
524
			if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
525
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
526
			}
527
		}
528
529
		return $this->get_ad_div( $spot, $snippet );
530
	}
531
532
533
	/**
534
	 * Returns the snippet to be inserted into the ad unit
535
	 *
536
	 * @param  int    $section_id
537
	 * @param  int    $height
538
	 * @param  int    $width
539
	 * @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...
540
	 * @param  string $css
541
	 * @return string
542
	 *
543
	 * @since 5.7
544
	 */
545
	public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
546
		$this->ads[] = array(
547
			'location' => $location,
548
			'width'    => $width,
549
			'height'   => $height,
550
		);
551
552
		if ( self::is_amp() ) {
553
			$height         = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad"
554
			$width          = esc_attr( $width );
555
			$amp_section_id = esc_attr( self::get_amp_section_id() );
556
			$site_id        = esc_attr( $this->params->blog_id );
557
			return <<<HTML
558
			<amp-ad width="$width" height="$height"
559
			    type="pubmine"
560
			    data-siteid="$site_id"
561
			    data-section="$amp_section_id">
562
			</amp-ad>
563
HTML;
564
		}
565
566
		$ad_number = count( $this->ads ) . '-' . uniqid();
567
		$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
568
		$css = esc_attr( $css );
569
570
		$loc_id = 100;
571
		if ( ! empty( self::$ad_location_ids[ $location ] ) ) {
572
			$loc_id = self::$ad_location_ids[ $location ];
573
		}
574
575
		return <<<HTML
576
		<div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
577
			<div id="atatags-{$ad_number}">
578
				<script$data_tags type="text/javascript">
579
				__ATA.cmd.push(function() {
580
					__ATA.initSlot('atatags-{$ad_number}',  {
581
						collapseEmpty: 'before',
582
						sectionId: '{$section_id}',
583
						location: {$loc_id},
584
						width: {$width},
585
						height: {$height}
586
					});
587
				});
588
				</script>
589
			</div>
590
		</div>
591
HTML;
592
	}
593
594
	/**
595
	 * Returns the complete ad div with snippet to be inserted into the page
596
	 *
597
	 * @param  string  $spot top, side, inline, or belowpost
598
	 * @param  string  $snippet The snippet to insert into the div
599
	 * @param  array  $css_classes
600
	 * @return string The supporting ad unit div
601
	 *
602
	 * @since 7.1
603
	 */
604
	function get_ad_div( $spot, $snippet, array $css_classes = array() ) {
605
		if ( empty( $css_classes ) ) {
606
			$css_classes = array();
607
		}
608
609
		$css_classes[] = 'wpcnt';
610
		if ( 'top' == $spot ) {
611
			$css_classes[] = 'wpcnt-header';
612
		}
613
614
		$spot = esc_attr( $spot );
615
		$classes = esc_attr( implode( ' ', $css_classes ) );
616
		$about  = esc_html__( 'Advertisements', 'jetpack' );
617
		return <<<HTML
618
		<div class="$classes">
619
			<div class="wpa">
620
				<span class="wpa-about">$about</span>
621
				<div class="u $spot">
622
					$snippet
623
				</div>
624
			</div>
625
		</div>
626
HTML;
627
	}
628
629
	/**
630
	 * Check the reasons to bail before we attempt to insert ads.
631
	 *
632
	 * @return true if we should bail (don't insert ads)
633
	 *
634
	 * @since 4.5.0
635
	 */
636
	public function should_bail() {
637
		return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' );
638
	}
639
640
	/**
641
	 * Returns markup for HTML5 house ad base on unit
642
	 *
643
	 * @param  string $unit mrec, widesky, or leaderboard
644
	 * @return string       markup for HTML5 house ad
645
	 *
646
	 * @since 4.7.0
647
	 */
648
	public function get_house_ad( $unit = 'mrec' ) {
649
650
		switch ( $unit ) {
651
			case 'widesky':
652
				$width  = 160;
653
				$height = 600;
654
				break;
655
			case 'leaderboard':
656
				$width  = 728;
657
				$height = 90;
658
				break;
659
			case 'mrec':
660
			default:
661
				$width  = 300;
662
				$height = 250;
663
				break;
664
		}
665
666
		return <<<HTML
667
		<iframe
668
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
669
			width="$width"
670
			height="$height"
671
			frameborder="0"
672
			scrolling="no"
673
			marginheight="0"
674
			marginwidth="0">
675
		</iframe>
676
HTML;
677
	}
678
679
	/**
680
	 * Activation hook actions
681
	 *
682
	 * @since 4.5.0
683
	 */
684
	public static function activate() {
685
		WordAds_API::update_wordads_status_from_api();
686
	}
687
}
688
689
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
690
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
691
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
692
693
global $wordads;
694
$wordads = new WordAds();
695