Completed
Push — master ( eddc43...cf66da )
by
unknown
10:04
created

WordAds::get_ad()   C

Complexity

Conditions 16
Paths 15

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 15
nop 2
dl 0
loc 33
rs 5.5666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Main WordAds file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
define( 'WORDADS_ROOT', __DIR__ );
9
define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
10
define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
11
define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
12
define( 'WORDADS_API_TEST_ID', '26942' );
13
define( 'WORDADS_API_TEST_ID2', '114160' );
14
15
require_once WORDADS_ROOT . '/php/class-wordads-sidebar-widget.php';
16
require_once WORDADS_ROOT . '/php/class-wordads-api.php';
17
require_once WORDADS_ROOT . '/php/class-wordads-cron.php';
18
require_once WORDADS_ROOT . '/php/class-wordads-california-privacy.php';
19
require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php';
20
21
/**
22
 * Primary WordAds class.
23
 */
24
class WordAds {
25
26
	/**
27
	 * Ads parameters.
28
	 *
29
	 * @var null
30
	 */
31
	public $params = null;
32
33
	/**
34
	 * Ads.
35
	 *
36
	 * @var array
37
	 */
38
	public $ads = array();
39
40
	/**
41
	 * Array of supported ad types.
42
	 *
43
	 * @var array
44
	 */
45
	public static $ad_tag_ids = array(
46
		'mrec'               => array(
47
			'tag'    => '300x250_mediumrectangle',
48
			'height' => '250',
49
			'width'  => '300',
50
		),
51
		'leaderboard'        => array(
52
			'tag'    => '728x90_leaderboard',
53
			'height' => '90',
54
			'width'  => '728',
55
		),
56
		'mobile_leaderboard' => array(
57
			'tag'    => '320x50_mobileleaderboard',
58
			'height' => '50',
59
			'width'  => '320',
60
		),
61
		'wideskyscraper'     => array(
62
			'tag'    => '160x600_wideskyscraper',
63
			'height' => '600',
64
			'width'  => '160',
65
		),
66
	);
67
68
	/**
69
	 * Mapping array of location slugs to placement ids
70
	 *
71
	 * @var array
72
	 */
73
	public static $ad_location_ids = array(
74
		'top'           => 110,
75
		'belowpost'     => 120,
76
		'belowpost2'    => 130,
77
		'sidebar'       => 140,
78
		'widget'        => 150,
79
		'gutenberg'     => 200,
80
		'inline'        => 310,
81
		'inline-plugin' => 320,
82
	);
83
84
	/**
85
	 * Mapping array of form factor slugs to form factor ids
86
	 *
87
	 * @var array
88
	 */
89
	public static $form_factor_ids = array(
90
		'square'      => '001', // 250x250
91
		'leaderboard' => '002', // 728x90
92
		'skyscraper'  => '003', // 120x600
93
	);
94
95
	/**
96
	 * Counter to enable unique, sequential section IDs for all amp-ad units
97
	 *
98
	 * @var int
99
	 */
100
	public static $amp_section_id = 1;
101
102
	/**
103
	 * Solo unit CSS string.
104
	 *
105
	 * @var string
106
	 */
107
	public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0px;';
108
109
	/**
110
	 * Checks for AMP support and returns true iff active & AMP request
111
	 *
112
	 * @return boolean True if supported AMP request
113
	 *
114
	 * @since 7.5.0
115
	 */
116
	public static function is_amp() {
117
		return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
118
	}
119
120
	/**
121
	 * Increment the AMP section ID and return the value
122
	 *
123
	 * @return int
124
	 */
125
	public static function get_amp_section_id() {
126
		return self::$amp_section_id++;
127
	}
128
129
	/**
130
	 * Convenience function for grabbing options from params->options
131
	 *
132
	 * @param  string $option the option to grab.
133
	 * @param  mixed  $default (optional).
134
	 * @return option or $default if not set
135
	 *
136
	 * @since 4.5.0
137
	 */
138
	public function option( $option, $default = false ) {
139
		if ( ! isset( $this->params->options[ $option ] ) ) {
140
			return $default;
141
		}
142
143
		return $this->params->options[ $option ];
144
	}
145
146
	/**
147
	 * Returns the ad tag property array for supported ad types.
148
	 *
149
	 * @return array      array with ad tags
150
	 *
151
	 * @since 7.1.0
152
	 */
153
	public function get_ad_tags() {
154
		return self::$ad_tag_ids;
155
	}
156
157
	/**
158
	 * Returns the solo css for unit
159
	 *
160
	 * @return string the special css for solo units
161
	 *
162
	 * @since 7.1.0
163
	 */
164
	public function get_solo_unit_css() {
165
		return self::$solo_unit_css;
166
	}
167
168
	/**
169
	 * Instantiate the plugin
170
	 *
171
	 * @since 4.5.0
172
	 */
173
	public function __construct() {
174
		add_action( 'wp', array( $this, 'init' ) );
175
		add_action( 'rest_api_init', array( $this, 'init' ) );
176
		add_action( 'widgets_init', array( $this, 'widget_callback' ) );
177
178
		if ( is_admin() ) {
179
			WordAds_California_Privacy::init_ajax_actions();
180
		}
181
	}
182
183
	/**
184
	 * Code to run on WordPress 'init' hook
185
	 *
186
	 * @since 4.5.0
187
	 */
188
	public function init() {
189
		require_once WORDADS_ROOT . '/php/class-wordads-params.php';
190
		$this->params = new WordAds_Params();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \WordAds_Params() of type object<WordAds_Params> is incompatible with the declared type null of property $params.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
191
192
		if ( $this->should_bail() || self::is_infinite_scroll() ) {
193
			return;
194
		}
195
196
		if ( is_admin() ) {
197
			require_once WORDADS_ROOT . '/php/class-wordads-admin.php';
198
			return;
199
		}
200
201
		$this->insert_adcode();
202
203
		// Include California Privacy Act related features if enabled.
204
		if ( $this->params->options['wordads_ccpa_enabled'] ) {
0 ignored issues
show
Bug introduced by
The property options does not seem to exist in WordAds_Params.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
205
			WordAds_California_Privacy::init();
206
		}
207
208
		if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) {
209
210
			$ads_txt_transient = get_transient( 'jetpack_ads_txt' );
211
212
			if ( false === ( $ads_txt_transient ) ) {
213
				$ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : '';
214
				set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
215
			}
216
217
			/**
218
			 * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
219
			 *
220
			 * @module wordads
221
			 *
222
			 * @since 6.1.0
223
			 *
224
			 * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
225
			 */
226
			$ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
227
228
			http_response_code( 200 );
229
			header( 'Content-Type: text/plain; charset=utf-8' );
230
			echo esc_html( $ads_txt_content );
231
			die();
232
		}
233
	}
234
235
	/**
236
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
237
	 *
238
	 * @return boolean true if load came from infinite scroll
239
	 *
240
	 * @since 4.5.0
241
	 */
242
	public static function is_infinite_scroll() {
243
		return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
244
	}
245
246
	/**
247
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
248
	 *
249
	 * @since 4.5.0
250
	 */
251
	private function insert_adcode() {
252
		add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
253
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
254
		add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
255
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
256
		add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
257
258
		/**
259
		 * Filters enabling ads in `the_content` filter
260
		 *
261
		 * @see https://jetpack.com/support/ads/
262
		 *
263
		 * @module wordads
264
		 *
265
		 * @since 5.8.0
266
		 *
267
		 * @param bool True to disable ads in `the_content`
268
		 */
269
		if ( ! apply_filters( 'wordads_content_disable', false ) ) {
270
			add_filter( 'the_content', array( $this, 'insert_ad' ) );
271
		}
272
273
		/**
274
		 * Filters enabling ads in `the_excerpt` filter
275
		 *
276
		 * @see https://jetpack.com/support/ads/
277
		 *
278
		 * @module wordads
279
		 *
280
		 * @since 5.8.0
281
		 *
282
		 * @param bool True to disable ads in `the_excerpt`
283
		 */
284
		if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
285
			add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
286
		}
287
288
		if ( $this->option( 'enable_header_ad', true ) ) {
289
			if ( self::is_amp() ) {
290
				add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) );
291
			} else {
292
				switch ( get_stylesheet() ) {
293
					case 'twentyseventeen':
294
					case 'twentyfifteen':
295
					case 'twentyfourteen':
296
						add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
297
						break;
298
					default:
299
						add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
300
						break;
301
				}
302
			}
303
		}
304
	}
305
306
	/**
307
	 * Register desktop scripts and styles
308
	 *
309
	 * @since 4.5.0
310
	 */
311
	public function enqueue_scripts() {
312
		wp_enqueue_style(
313
			'wordads',
314
			WORDADS_URL . 'css/style.css',
315
			array(),
316
			'2015-12-18'
317
		);
318
	}
319
320
	/**
321
	 * Add the IPW resource hints
322
	 *
323
	 * @since 7.9
324
	 *
325
	 * @param array  $hints Domains for hinting.
326
	 * @param string $relation_type Resource type.
327
	 *
328
	 * @return array Domains for hinting.
329
	 */
330
	public function resource_hints( $hints, $relation_type ) {
331
		if ( 'dns-prefetch' === $relation_type ) {
332
			$hints[] = '//s.pubmine.com';
333
			$hints[] = '//x.bidswitch.net';
334
			$hints[] = '//static.criteo.net';
335
			$hints[] = '//ib.adnxs.com';
336
			$hints[] = '//aax.amazon-adsystem.com';
337
			$hints[] = '//bidder.criteo.com';
338
			$hints[] = '//cas.criteo.com';
339
			$hints[] = '//gum.criteo.com';
340
			$hints[] = '//ads.pubmatic.com';
341
			$hints[] = '//gads.pubmatic.com';
342
			$hints[] = '//tpc.googlesyndication.com';
343
			$hints[] = '//ad.doubleclick.net';
344
			$hints[] = '//googleads.g.doubleclick.net';
345
			$hints[] = '//www.googletagservices.com';
346
			$hints[] = '//cdn.switchadhub.com';
347
			$hints[] = '//delivery.g.switchadhub.com';
348
			$hints[] = '//delivery.swid.switchadhub.com';
349
		}
350
351
		return $hints;
352
	}
353
354
	/**
355
	 * IPONWEB metadata used by the various scripts
356
	 *
357
	 * @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...
358
	 */
359
	public function insert_head_meta() {
360
		if ( self::is_amp() ) {
361
			return;
362
		}
363
		$hosting_type = jetpack_is_atomic_site() ? 1 : 2; // 1 = WPCOM, 2 = Jetpack.
364
		$pagetype     = (int) $this->params->get_page_type_ipw();
0 ignored issues
show
Bug introduced by
The method get_page_type_ipw cannot be called on $this->params (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
365
		$data_tags    = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
366
		$site_id      = $this->params->blog_id;
367
		$consent      = (int) isset( $_COOKIE['personalized-ads-consent'] );
368
		?>
369
		<script<?php echo esc_attr( $data_tags ); ?> type="text/javascript">
370
			var __ATA_PP = { pt: <?php echo esc_js( $pagetype ); ?>, ht: <?php echo esc_js( $hosting_type ); ?>, tn: '<?php echo esc_js( get_stylesheet() ); ?>', amp: false, siteid: <?php echo esc_js( $site_id ); ?>, consent: <?php echo esc_js( $consent ); ?>, ad: { label: { text: '<?php echo esc_js( __( 'Advertisements', 'jetpack' ) ); ?>' }, reportAd: { text: '<?php echo esc_js( __( 'Report this ad', 'jetpack' ) ); ?>' } } };
371
			var __ATA = __ATA || {};
372
			__ATA.cmd = __ATA.cmd || [];
373
			__ATA.criteo = __ATA.criteo || {};
374
			__ATA.criteo.cmd = __ATA.criteo.cmd || [];
375
		</script>
376
		<?php
377
	}
378
379
	/**
380
	 * IPONWEB scripts in <head>
381
	 *
382
	 * @since 4.5.0
383
	 */
384
	public function insert_head_iponweb() {
385
		if ( self::is_amp() ) {
386
			return;
387
		}
388
389
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
390
		?>
391
		<script<?php echo esc_attr( $data_tags ); ?> type="text/javascript">
392
		(function(){var g=Date.now||function(){return+new Date};function h(a,b){a:{for(var c=a.length,d="string"==typeof a?a.split(""):a,e=0;e<c;e++)if(e in d&&b.call(void 0,d[e],e,a)){b=e;break a}b=-1}return 0>b?null:"string"==typeof a?a.charAt(b):a[b]};function k(a,b,c){c=null!=c?"="+encodeURIComponent(String(c)):"";if(b+=c){c=a.indexOf("#");0>c&&(c=a.length);var d=a.indexOf("?");if(0>d||d>c){d=c;var e=""}else e=a.substring(d+1,c);a=[a.substr(0,d),e,a.substr(c)];c=a[1];a[1]=b?c?c+"&"+b:b:c;a=a[0]+(a[1]?"?"+a[1]:"")+a[2]}return a};var l=0;function m(a,b){var c=document.createElement("script");c.src=a;c.onload=function(){b&&b(void 0)};c.onerror=function(){b&&b("error")};a=document.getElementsByTagName("head");var d;a&&0!==a.length?d=a[0]:d=document.documentElement;d.appendChild(c)}function n(a){var b=void 0===b?document.cookie:b;return(b=h(b.split("; "),function(c){return-1!=c.indexOf(a+"=")}))?b.split("=")[1]:""}function p(a){return"string"==typeof a&&0<a.length}
393
		function r(a,b,c){b=void 0===b?"":b;c=void 0===c?".":c;var d=[];Object.keys(a).forEach(function(e){var f=a[e],q=typeof f;"object"==q&&null!=f||"function"==q?d.push(r(f,b+e+c)):null!==f&&void 0!==f&&(e=encodeURIComponent(b+e),d.push(e+"="+encodeURIComponent(f)))});return d.filter(p).join("&")}function t(a,b){a||((window.__ATA||{}).config=b.c,m(b.url))}var u=Math.floor(1E13*Math.random()),v=window.__ATA||{};window.__ATA=v;window.__ATA.cmd=v.cmd||[];v.rid=u;v.createdAt=g();var w=window.__ATA||{},x="s.pubmine.com";
394
		w&&w.serverDomain&&(x=w.serverDomain);var y="//"+x+"/conf",z=window.top===window,A=window.__ATA_PP&&window.__ATA_PP.gdpr_applies,B="boolean"===typeof A?Number(A):null,C=window.__ATA_PP||null,D=z?document.referrer?document.referrer:null:null,E=z?window.location.href:document.referrer?document.referrer:null,F,G=n("__ATA_tuuid");F=G?G:null;var H=window.innerWidth+"x"+window.innerHeight,I=n("usprivacy"),J=r({gdpr:B,pp:C,rid:u,src:D,ref:E,tuuid:F,vp:H,us_privacy:I?I:null},"",".");
395
		(function(a){var b=void 0===b?"cb":b;l++;var c="callback__"+g().toString(36)+"_"+l.toString(36);a=k(a,b,c);window[c]=function(d){t(void 0,d)};m(a,function(d){d&&t(d)})})(y+"?"+J);}).call(this);
396
		</script>
397
		<?php
398
	}
399
400
	/**
401
	 * Insert the ad onto the page
402
	 *
403
	 * @since 4.5.0
404
	 *
405
	 * @param string $content HTML content.
406
	 */
407
	public function insert_ad( $content ) {
408
		// Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
409
		if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
410
			return $content;
411
		}
412
		/**
413
		 * Allow third-party tools to disable the display of in post ads.
414
		 *
415
		 * @module wordads
416
		 *
417
		 * @since 4.5.0
418
		 *
419
		 * @param bool true Should the in post unit be disabled. Default to false.
420
		 */
421
		$disable = apply_filters( 'wordads_inpost_disable', false );
422
		if ( ! $this->params->should_show() || $disable ) {
0 ignored issues
show
Bug introduced by
The method should_show cannot be called on $this->params (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
423
			return $content;
424
		}
425
426
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
427
		return $content . $this->get_ad( 'belowpost', $ad_type );
428
	}
429
430
	/**
431
	 * Insert an inline ad into a post content
432
	 * Used for rendering the `wordads` shortcode.
433
	 *
434
	 * @since 6.1.0
435
	 *
436
	 * @param string $content HTML content.
437
	 */
438
	public function insert_inline_ad( $content ) {
439
		// Ad JS won't work in XML feeds.
440
		if ( is_feed() ) {
441
			return $content;
442
		}
443
		/**
444
		 * Allow third-party tools to disable the display of in post ads.
445
		 *
446
		 * @module wordads
447
		 *
448
		 * @since 4.5.0
449
		 *
450
		 * @param bool true Should the in post unit be disabled. Default to false.
451
		 */
452
		$disable = apply_filters( 'wordads_inpost_disable', false );
453
		if ( $disable ) {
454
			return $content;
455
		}
456
457
		$ad_type  = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
458
		$content .= $this->get_ad( 'inline', $ad_type );
459
		return $content;
460
	}
461
462
	/**
463
	 * Inserts ad into header
464
	 *
465
	 * @since 4.5.0
466
	 */
467
	public function insert_header_ad() {
468
		/**
469
		 * Allow third-party tools to disable the display of header ads.
470
		 *
471
		 * @module wordads
472
		 *
473
		 * @since 4.5.0
474
		 *
475
		 * @param bool true Should the header unit be disabled. Default to false.
476
		 */
477
		if ( apply_filters( 'wordads_header_disable', false ) ) {
478
			return;
479
		}
480
481
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
482
		echo $this->get_ad( 'top', $ad_type ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
483
	}
484
485
	/**
486
	 * Special cases for inserting header unit via JS
487
	 *
488
	 * @since 4.5.0
489
	 */
490
	public function insert_header_ad_special() {
491
		/**
492
		 * Allow third-party tools to disable the display of header ads.
493
		 *
494
		 * @module wordads
495
		 *
496
		 * @since 4.5.0
497
		 *
498
		 * @param bool true Should the header unit be disabled. Default to false.
499
		 */
500
		if ( apply_filters( 'wordads_header_disable', false ) ) {
501
			return;
502
		}
503
504
		$selector = '#content';
505
		switch ( get_stylesheet() ) {
506
			case 'twentyseventeen':
507
				$selector = '#content';
508
				break;
509
			case 'twentyfifteen':
510
				$selector = '#main';
511
				break;
512
			case 'twentyfourteen':
513
				$selector = 'article';
514
				break;
515
		}
516
517
		$section_id  = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
518
		$form_factor = $this->params->mobile_device ? 'square' : 'leaderboard';
519
		echo $this->get_dynamic_ad_snippet( $section_id, $form_factor, 'top', $selector ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
520
	}
521
522
	/**
523
	 * Header unit for AMP
524
	 *
525
	 * @param string $content Content of the page.
526
	 *
527
	 * @since 7.5.0
528
	 */
529
	public function insert_header_ad_amp( $content ) {
530
531
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
532
		if ( 'house' === $ad_type ) {
533
			return $content;
534
		}
535
		return $this->get_ad( 'top_amp', $ad_type ) . $content;
536
537
	}
538
539
	/**
540
	 * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
541
	 *
542
	 * @param  string $adstxt The ads.txt being filtered.
543
	 * @return string         Filtered ads.txt with custom entries, if applicable.
544
	 *
545
	 * @since 6.5.0
546
	 */
547
	public function insert_custom_adstxt( $adstxt ) {
548
		if ( ! $this->option( 'wordads_custom_adstxt_enabled' ) ) {
549
			return $adstxt;
550
		}
551
552
		$custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
553
		if ( $custom_adstxt ) {
554
			$adstxt .= "\n\n#Jetpack - User Custom Entries\n";
555
			$adstxt .= $custom_adstxt . "\n";
556
		}
557
558
		return $adstxt;
559
	}
560
561
	/**
562
	 * Get the ad for the spot and type.
563
	 *
564
	 * @param  string $spot top, side, inline, or belowpost.
565
	 * @param  string $type iponweb or adsense.
566
	 */
567
	public function get_ad( $spot, $type = 'iponweb' ) {
568
		$snippet = '';
569
		if ( 'iponweb' === $type ) {
570
			$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...
571
			$snippet    = '';
572
573
			if ( 'top' === $spot ) {
574
				// mrec for mobile, leaderboard for desktop.
575
				$section_id  = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
576
				$form_factor = $this->params->mobile_device ? 'square' : 'leaderboard';
577
				$snippet     = $this->get_dynamic_ad_snippet( $section_id, $form_factor, $spot );
578
			} elseif ( 'belowpost' === $spot ) {
579
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
580
				$snippet    = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot );
581
			} elseif ( 'inline' === $spot ) {
582
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
583
				$snippet    = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot );
584
			} elseif ( 'top_amp' === $spot ) {
585
				// Ad unit which can safely be inserted below title, above content in a variety of themes.
586
				$width   = 300;
587
				$height  = 250;
588
				$snippet = $this->get_ad_div( $spot, $this->get_amp_snippet( $height, $width ) );
589
			}
590
		} elseif ( 'house' === $type ) {
591
			$leaderboard = 'top' === $spot && ! $this->params->mobile_device;
592
			$snippet     = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
593
			if ( 'belowpost' === $spot && $this->option( 'wordads_second_belowpost', true ) ) {
594
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
595
			}
596
		}
597
598
		return $snippet;
599
	}
600
601
	/**
602
	 * Returns the AMP snippet to be inserted
603
	 *
604
	 * @param  int $height Height.
605
	 * @param  int $width  Width.
606
	 * @return string
607
	 *
608
	 * @since 8.7
609
	 */
610
	public function get_amp_snippet( $height, $width ) {
611
		$height         = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad".
612
		$width          = esc_attr( $width );
613
		$amp_section_id = esc_attr( self::get_amp_section_id() );
614
		$site_id        = esc_attr( $this->params->blog_id );
615
		return <<<HTML
616
		<amp-ad width="$width" height="$height"
617
		    type="pubmine"
618
		    data-siteid="$site_id"
619
		    data-section="$amp_section_id">
620
		</amp-ad>
621
HTML;
622
	}
623
624
	/**
625
	 * Compatibility function -- main functionality replaced with get_dynamic_ad_snippet
626
	 *
627
	 * @param  int    $section_id Ad section.
628
	 * @param  int    $height Ad height.
629
	 * @param  int    $width Ad width.
630
	 * @param  string $location Location.
631
	 * @param  string $css CSS.
632
	 *
633
	 * @return string
634
	 *
635
	 * @since 5.7
636
	 */
637
	public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
638
		if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
639
			return $this->get_amp_snippet( $height, $width );
640
		}
641
642
		$this->ads[] = array(
643
			'location' => $location,
644
			'width'    => $width,
645
			'height'   => $height,
646
		);
647
648
		if ( 'gutenberg' === $location ) {
649
			$ad_number = count( $this->ads ) . '-' . uniqid();
650
			$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
651
			$css       = esc_attr( $css );
652
653
			$loc_id = 100;
654
			if ( ! empty( self::$ad_location_ids[ $location ] ) ) {
655
				$loc_id = self::$ad_location_ids[ $location ];
656
			}
657
658
			$loc_id = esc_js( $loc_id );
659
			return <<<HTML
660
			<div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
661
				<div id="atatags-{$ad_number}">
662
					<script$data_tags type="text/javascript">
663
					__ATA.cmd.push(function() {
664
						__ATA.initSlot('atatags-{$ad_number}',  {
665
							collapseEmpty: 'before',
666
							sectionId: '{$section_id}',
667
							location: {$loc_id},
668
							width: {$width},
669
							height: {$height}
670
						});
671
					});
672
					</script>
673
				</div>
674
			</div>
675
HTML;
676
		}
677
678
		$form_factor = 'square';
679
		if ( 250 > $width ) {
680
			$form_factor = 'skyscraper';
681
		} elseif ( 300 < $width ) {
682
			$form_factor = 'leaderboard';
683
		}
684
685
		return $this->get_dynamic_ad_snippet( $section_id, $form_factor, $location );
686
	}
687
688
	/**
689
	 * Returns the dynamic snippet to be inserted into the ad unit
690
	 *
691
	 * @param  int    $section_id  section_id.
692
	 * @param  string $form_factor form_factor.
693
	 * @param  string $location    location.
694
	 * @param  string $relocate    location to be moved after the fact for themes without required hook.
695
	 * @return string
696
	 *
697
	 * @since 8.7
698
	 */
699
	public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '' ) {
700
		$div_id = 'atatags-' . $section_id . '-' . uniqid();
701
		$div_id = esc_js( $div_id );
702
703
		// Default form factor.
704
		$form_factor_id = self::$form_factor_ids['square'];
705
		if ( isset( self::$form_factor_ids[ $form_factor ] ) ) {
706
			$form_factor_id = self::$form_factor_ids[ $form_factor ];
707
		}
708
709
		$loc_id = 100;
710
		if ( isset( self::$ad_location_ids[ $location ] ) ) {
711
			$loc_id = self::$ad_location_ids[ $location ];
712
		}
713
714
		$form_factor_id        = esc_js( $form_factor_id );
715
		$advertisements_text   = esc_js( __( 'Advertisements', 'jetpack' ) );
716
		$report_ad_text        = esc_js( __( 'Report this ad', 'jetpack' ) );
717
		$privacy_settings_text = esc_js( __( 'Privacy settings', 'jetpack' ) );
718
719
		$relocate_script = '';
720
		if ( ! empty( $relocate ) ) {
721
			$relocate        = esc_js( $relocate );
722
			$relocate_script = <<<JS
723
			<script type="text/javascript">
724
			var adNode       = document.getElementById( '$div_id' );
725
			var relocateNode = document.querySelector( '$relocate' );
726
			relocateNode.parentNode.insertBefore( adNode, relocateNode );
727
			</script>
728
JS;
729
		}
730
731
		return <<<HTML
732
		<div id="{$div_id}"></div>
733
		{$relocate_script}
734
		<script>
735
			__ATA.cmd.push(function() {
736
				__ATA.initDynamicSlot({
737
					id: '{$div_id}',
738
					location: {$loc_id},
739
					formFactor: '{$form_factor_id}',
740
					label: {
741
						text: '{$advertisements_text}',
742
					},
743
					creative: {
744
						reportAd: {
745
							text: '{$report_ad_text}',
746
						},
747
						privacySettings: {
748
							text: '{$privacy_settings_text}',
749
						}
750
					}
751
				});
752
			});
753
		</script>
754
HTML;
755
	}
756
757
	/**
758
	 * Returns the complete ad div with snippet to be inserted into the page
759
	 *
760
	 * @param  string $spot top, side, inline, or belowpost.
761
	 * @param  string $snippet The snippet to insert into the div.
762
	 * @param  array  $css_classes CSS classes.
763
	 * @return string The supporting ad unit div.
764
	 *
765
	 * @since 7.1
766
	 */
767
	public function get_ad_div( $spot, $snippet, array $css_classes = array() ) {
768
		if ( strpos( strtolower( $spot ), 'amp' ) === false && ! 'inline' === $spot ) {
769
			return $snippet; // we don't want dynamic ads to be inserted for AMP & Gutenberg.
770
		}
771
772
		if ( empty( $css_classes ) ) {
773
			$css_classes = array();
774
		}
775
776
		$css_classes[] = 'wpcnt';
777
		if ( 'top' === $spot ) {
778
			$css_classes[] = 'wpcnt-header';
779
		}
780
781
		$spot    = esc_attr( $spot );
782
		$classes = esc_attr( implode( ' ', $css_classes ) );
783
		$about   = esc_html__( 'Advertisements', 'jetpack' );
784
		return <<<HTML
785
		<div class="$classes">
786
			<div class="wpa">
787
				<span class="wpa-about">$about</span>
788
				<div class="u $spot">
789
					$snippet
790
				</div>
791
			</div>
792
		</div>
793
HTML;
794
	}
795
796
	/**
797
	 * Check the reasons to bail before we attempt to insert ads.
798
	 *
799
	 * @return true if we should bail (don't insert ads)
800
	 *
801
	 * @since 4.5.0
802
	 */
803
	public function should_bail() {
804
		return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' );
805
	}
806
807
	/**
808
	 * Returns markup for HTML5 house ad base on unit
809
	 *
810
	 * @param  string $unit mrec, widesky, or leaderboard.
811
	 * @return string       markup for HTML5 house ad
812
	 *
813
	 * @since 4.7.0
814
	 */
815
	public function get_house_ad( $unit = 'mrec' ) {
816
817
		switch ( $unit ) {
818
			case 'widesky':
819
				$width  = 160;
820
				$height = 600;
821
				break;
822
			case 'leaderboard':
823
				$width  = 728;
824
				$height = 90;
825
				break;
826
			case 'mrec':
827
			default:
828
				$width  = 300;
829
				$height = 250;
830
				break;
831
		}
832
833
		return <<<HTML
834
		<iframe
835
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
836
			width="$width"
837
			height="$height"
838
			frameborder="0"
839
			scrolling="no"
840
			marginheight="0"
841
			marginwidth="0">
842
		</iframe>
843
HTML;
844
	}
845
846
	/**
847
	 * Activation hook actions
848
	 *
849
	 * @since 4.5.0
850
	 */
851
	public static function activate() {
852
		WordAds_API::update_wordads_status_from_api();
853
	}
854
855
	/**
856
	 * Registers the widgets.
857
	 */
858
	public function widget_callback() {
859
			register_widget( 'WordAds_Sidebar_Widget' );
860
861
			$ccpa_enabled = get_option( 'wordads_ccpa_enabled' );
862
863
		if ( $ccpa_enabled ) {
864
			register_widget( 'WordAds_Ccpa_Do_Not_Sell_Link_Widget' );
865
		}
866
	}
867
}
868
869
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
870
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
871
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
872
873
global $wordads;
874
$wordads = new WordAds();
875