Completed
Push — add/wordads-shortcode ( 524fc4...8ad5fb )
by
unknown
45:47 queued 34:49
created

WordAds::option()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
define( 'WORDADS_ROOT', dirname( __FILE__ ) );
4
define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
5
define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
6
define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
7
define( 'WORDADS_API_TEST_ID', '26942' );
8
define( 'WORDADS_API_TEST_ID2', '114160' );
9
10
require_once( WORDADS_ROOT . '/php/widgets.php' );
11
require_once( WORDADS_ROOT . '/php/api.php' );
12
require_once( WORDADS_ROOT . '/php/cron.php' );
13
14
class WordAds {
15
16
	public $params = null;
17
18
	public $ads = array();
19
20
	/**
21
	 * The different supported ad types.
22
	 * v0.1 - mrec only for now
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
		'lrec' => array(
32
			'tag'       => '336x280_largerectangle',
33
			'height'    => '280',
34
			'width'     => '336',
35
		),
36
		'leaderboard' => array(
37
			'tag'       => '728x90_leaderboard',
38
			'height'    => '90',
39
			'width'     => '728',
40
		),
41
		'wideskyscraper' => array(
42
			'tag'       => '160x600_wideskyscraper',
43
			'height'    => '600',
44
			'width'     => '160',
45
		),
46
	);
47
48
	/**
49
	 * Convenience function for grabbing options from params->options
50
	 * @param  string $option the option to grab
51
	 * @param  mixed  $default (optional)
52
	 * @return option or $default if not set
53
	 *
54
	 * @since 4.5.0
55
	 */
56
	function option( $option, $default = false ) {
57
		if ( ! isset( $this->params->options[ $option ] ) ) {
58
			return $default;
59
		}
60
61
		return $this->params->options[ $option ];
62
	}
63
64
	/**
65
	 * Instantiate the plugin
66
	 *
67
	 * @since 4.5.0
68
	 */
69
	function __construct() {
70
		add_action( 'init', array( $this, 'init' ) );
71
	}
72
73
	/**
74
	 * Code to run on WordPress 'init' hook
75
	 *
76
	 * @since 4.5.0
77
	 */
78
	function init() {
79
		// bail on infinite scroll
80
		if ( self::is_infinite_scroll() ) {
81
			return;
82
		}
83
84
		require_once( WORDADS_ROOT . '/php/params.php' );
85
		$this->params = new WordAds_Params();
86
87
		if ( is_admin() ) {
88
			require_once( WORDADS_ROOT . '/php/admin.php' );
89
			return;
90
		}
91
92
		if ( $this->should_bail() ) {
93
			return;
94
		}
95
96
		$this->insert_adcode();
97
	}
98
99
	/**
100
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
101
	 * @return boolean true if load came from infinite scroll
102
	 *
103
	 * @since 4.5.0
104
	 */
105
	public static function is_infinite_scroll() {
106
		return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
107
	}
108
109
	/**
110
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
111
	 *
112
	 * @since 4.5.0
113
	 */
114
	private function insert_adcode() {
115
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
116
		add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
117
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
118
119
		/**
120
		 * Filters enabling ads in `the_content` filter
121
		 *
122
		 * @see https://jetpack.com/support/ads/
123
		 *
124
		 * @module wordads
125
		 *
126
		 * @since 5.8.0
127
		 *
128
		 * @param bool True to disable ads in `the_content`
129
		 */
130
		if ( ! apply_filters( 'wordads_content_disable', false ) ) {
131
			add_filter( 'the_content', array( $this, 'insert_ad' ) );
132
		}
133
134
		/**
135
		 * Filters enabling ads in `the_excerpt` filter
136
		 *
137
		 * @see https://jetpack.com/support/ads/
138
		 *
139
		 * @module wordads
140
		 *
141
		 * @since 5.8.0
142
		 *
143
		 * @param bool True to disable ads in `the_excerpt`
144
		 */
145
		if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
146
			add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
147
		}
148
149
		if ( $this->option( 'enable_header_ad', true ) ) {
150
			switch ( get_stylesheet() ) {
151
				case 'twentyseventeen':
152
				case 'twentyfifteen':
153
				case 'twentyfourteen':
154
					add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
155
					break;
156
				default:
157
					add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
158
					break;
159
			}
160
		}
161
	}
162
163
	/**
164
	 * Register desktop scripts and styles
165
	 *
166
	 * @since 4.5.0
167
	 */
168
	function enqueue_scripts() {
169
		wp_enqueue_style(
170
			'wordads',
171
			WORDADS_URL . 'css/style.css',
172
			array(),
173
			'2015-12-18'
174
		);
175
	}
176
177
	/**
178
	 * IPONWEB metadata used by the various scripts
179
	 * @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...
180
	 */
181
	function insert_head_meta() {
182
		$themename = esc_js( get_stylesheet() );
183
		$pagetype = intval( $this->params->get_page_type_ipw() );
184
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
185
		$site_id = $this->params->blog_id;
186
		echo <<<HTML
187
		<script$data_tags type="text/javascript">
188
			var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id };
189
			var __ATA = __ATA || {};
190
			__ATA.cmd = __ATA.cmd || [];
191
			__ATA.criteo = __ATA.criteo || {};
192
			__ATA.criteo.cmd = __ATA.criteo.cmd || [];
193
		</script>
194
HTML;
195
	}
196
197
	/**
198
	 * IPONWEB scripts in <head>
199
	 *
200
	 * @since 4.5.0
201
	 */
202
	function insert_head_iponweb() {
203
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
204
		echo <<<HTML
205
		<link rel='dns-prefetch' href='//s.pubmine.com' />
206
		<link rel='dns-prefetch' href='//x.bidswitch.net' />
207
		<link rel='dns-prefetch' href='//static.criteo.net' />
208
		<link rel='dns-prefetch' href='//ib.adnxs.com' />
209
		<link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
210
		<link rel='dns-prefetch' href='//bidder.criteo.com' />
211
		<link rel='dns-prefetch' href='//cas.criteo.com' />
212
		<link rel='dns-prefetch' href='//gum.criteo.com' />
213
		<link rel='dns-prefetch' href='//ads.pubmatic.com' />
214
		<link rel='dns-prefetch' href='//gads.pubmatic.com' />
215
		<link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
216
		<link rel='dns-prefetch' href='//ad.doubleclick.net' />
217
		<link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
218
		<link rel='dns-prefetch' href='//www.googletagservices.com' />
219
		<script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
220
HTML;
221
	}
222
223
	/**
224
	 * Insert the ad onto the page
225
	 *
226
	 * @since 4.5.0
227
	 */
228 View Code Duplication
	function insert_ad( $content ) {
229
		// Ad JS won't work in XML feeds.
230
		if ( is_feed() ) {
231
			return $content;
232
		}
233
		/**
234
		 * Allow third-party tools to disable the display of in post ads.
235
		 *
236
		 * @module wordads
237
		 *
238
		 * @since 4.5.0
239
		 *
240
		 * @param bool true Should the in post unit be disabled. Default to false.
241
		 */
242
		$disable = apply_filters( 'wordads_inpost_disable', false );
243
		if ( ! $this->params->should_show() || $disable ) {
244
			return $content;
245
		}
246
247
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
248
		return $content . $this->get_ad( 'belowpost', $ad_type );
249
	}
250
251
	/**
252
	 * Insert an inline ad into a post content
253
	 * Used for rendering the `wordad` shortcode.
254
	 *
255
	 * @since 6.1.0
256
	 */
257 View Code Duplication
	function insert_inline_ad( $content ) {
258
		// Ad JS won't work in XML feeds.
259
		if ( is_feed() ) {
260
			return $content;
261
		}
262
		/**
263
		 * Allow third-party tools to disable the display of in post ads.
264
		 *
265
		 * @module wordads
266
		 *
267
		 * @since 4.5.0
268
		 *
269
		 * @param bool true Should the in post unit be disabled. Default to false.
270
		 */
271
		$disable = apply_filters( 'wordads_inpost_disable', false );
272
		if ( $disable ) {
273
			return $content;
274
		}
275
276
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
277
		$content .= $this->get_ad( 'inline', $ad_type );
278
		return $content;
279
	}
280
281
	/**
282
	 * Inserts ad into header
283
	 *
284
	 * @since 4.5.0
285
	 */
286
	function insert_header_ad() {
287
		/**
288
		 * Allow third-party tools to disable the display of header ads.
289
		 *
290
		 * @module wordads
291
		 *
292
		 * @since 4.5.0
293
		 *
294
		 * @param bool true Should the header unit be disabled. Default to false.
295
		 */
296
		if ( apply_filters( 'wordads_header_disable', false ) ) {
297
			return;
298
		}
299
300
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
301
		echo $this->get_ad( 'top', $ad_type );
302
	}
303
304
	/**
305
	 * Special cases for inserting header unit via jQuery
306
	 *
307
	 * @since 4.5.0
308
	 */
309
	function insert_header_ad_special() {
310
		/**
311
		 * Allow third-party tools to disable the display of header ads.
312
		 *
313
		 * @module wordads
314
		 *
315
		 * @since 4.5.0
316
		 *
317
		 * @param bool true Should the header unit be disabled. Default to false.
318
		 */
319
		if ( apply_filters( 'wordads_header_disable', false ) ) {
320
			return;
321
		}
322
323
		$selector = '#content';
324
		switch ( get_stylesheet() ) {
325
			case 'twentyseventeen':
326
				$selector = '#content';
327
				break;
328
			case 'twentyfifteen':
329
				$selector = '#main';
330
				break;
331
			case 'twentyfourteen':
332
				$selector = 'article:first';
333
				break;
334
		}
335
336
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
337
		echo $this->get_ad( 'top', $ad_type );
338
		echo <<<HTML
339
		<script type="text/javascript">
340
			jQuery('.wpcnt-header').insertBefore('$selector');
341
		</script>
342
HTML;
343
	}
344
345
	/**
346
	 * Get the ad for the spot and type.
347
	 * @param  string $spot top, side, inline, or belowpost
348
	 * @param  string $type iponweb or adsense
349
	 */
350
	function get_ad( $spot, $type = 'iponweb' ) {
351
		$snippet = '';
352
		if ( 'iponweb' == $type ) {
353
			$width = 300;
354
			$height = 250;
355
			$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...
356
			$snippet = '';
357
			if ( 'top' == $spot ) {
358
				// mrec for mobile, leaderboard for desktop
359
				$width = $this->params->mobile_device ? 300 : 728;
360
				$height = $this->params->mobile_device ? 250 : 90;
361
				$snippet = $this->get_ad_snippet( $height, $width, $spot );
362
			} else if ( 'belowpost' == $spot ) {
363
				$width = 300;
364
				$height = 250;
365
366
				$snippet = $this->get_ad_snippet( $height, $width, $spot, 'float:left;margin-right:5px;margin-top:0px;' );
367
				if ( $this->option( 'wordads_second_belowpost', true ) ) {
368
					$snippet .= $this->get_ad_snippet( $height, $width, $spot, 'float:left;margin-top:0px;' );
369
				}
370
			} else if ( 'inline' === $spot ) {
371
				$snippet = $this->get_ad_snippet( $height, $width, $spot, 'mrec', 'float:left;margin-right:5px;margin-top:0px;' );
0 ignored issues
show
Unused Code introduced by
The call to WordAds::get_ad_snippet() has too many arguments starting with 'float:left;margin-right:5px;margin-top:0px;'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
372
			}
373
		} else if ( 'house' == $type ) {
374
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
375
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
376
			if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
377
				$snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
378
			}
379
		}
380
381
		$header = 'top' == $spot ? 'wpcnt-header' : '';
382
		$about = __( 'Advertisements', 'jetpack' );
383
		return <<<HTML
384
		<div class="wpcnt $header">
385
			<div class="wpa">
386
				<span class="wpa-about">$about</span>
387
				<div class="u $spot">
388
					$snippet
389
				</div>
390
			</div>
391
		</div>
392
HTML;
393
	}
394
395
396
	/**
397
	 * Returns the snippet to be inserted into the ad unit
398
	 * @param  int $height
399
	 * @param  int $width
400
	 * @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...
401
	 * @param  string $css
402
	 * @return string
403
	 *
404
	 * @since 5.7
405
	 */
406
	function get_ad_snippet( $height, $width, $location = '', $css = '' ) {
407
		$this->ads[] = array( 'location' => $location, 'width' => $width, 'height' => $height );
408
		$ad_number = count( $this->ads );
409
		// Max 6 ads per page.
410
		if ( $ad_number > 6 ) {
411
			return;
412
		}
413
		$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
414
		
415
		return <<<HTML
416
		<div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
417
			<div id="atatags-{$ad_number}">
418
				<script$data_tags type="text/javascript">
419
				__ATA.cmd.push(function() {
420
					__ATA.initSlot('atatags-{$ad_number}',  {
421
						collapseEmpty: 'before',
422
						location: '{$location}',
423
						width: {$width},
424
						height: {$height}
425
					});
426
				});
427
				</script>
428
			</div>
429
		</div>
430
HTML;
431
	}
432
433
	/**
434
	 * Check the reasons to bail before we attempt to insert ads.
435
	 * @return true if we should bail (don't insert ads)
436
	 *
437
	 * @since 4.5.0
438
	 */
439
	public function should_bail() {
440
		return ! $this->option( 'wordads_approved' );
441
	}
442
443
	/**
444
	 * Returns markup for HTML5 house ad base on unit
445
	 * @param  string $unit mrec, widesky, or leaderboard
446
	 * @return string       markup for HTML5 house ad
447
	 *
448
	 * @since 4.7.0
449
	 */
450
	public function get_house_ad( $unit = 'mrec' ) {
451
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
452
			$unit = 'mrec';
453
		}
454
455
		$width  = 300;
456
		$height = 250;
457
		if ( 'widesky' == $unit ) {
458
			$width  = 160;
459
			$height = 600;
460
		} else if ( 'leaderboard' == $unit ) {
461
			$width  = 728;
462
			$height = 90;
463
		}
464
465
		return <<<HTML
466
		<iframe
467
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
468
			width="$width"
469
			height="$height"
470
			frameborder="0"
471
			scrolling="no"
472
			marginheight="0"
473
			marginwidth="0">
474
		</iframe>
475
HTML;
476
	}
477
478
	/**
479
	 * Activation hook actions
480
	 *
481
	 * @since 4.5.0
482
	 */
483
	public static function activate() {
484
		WordAds_API::update_wordads_status_from_api();
485
	}
486
}
487
488
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
489
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
490
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
491
492
global $wordads;
493
$wordads = new WordAds();
494