Completed
Push — fix/visibility-condition-issue... ( eee6fd...a71c15 )
by
unknown
11:07
created

WordAds::should_bail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
define( 'WORDADS_ROOT', dirname( __FILE__ ) );
4
define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
5
define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
6
define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
7
define( 'WORDADS_API_TEST_ID', '26942' );
8
9
require_once( WORDADS_ROOT . '/php/widgets.php' );
10
require_once( WORDADS_ROOT . '/php/api.php' );
11
require_once( WORDADS_ROOT . '/php/cron.php' );
12
13
class WordAds {
14
15
	public $params = null;
16
17
	/**
18
	 * The different supported ad types.
19
	 * v0.1 - mrec only for now
20
	 * @var array
21
	 */
22
	public static $ad_tag_ids = array(
23
		'mrec' => array(
24
			'tag'       => '300x250_mediumrectangle',
25
			'height'    => '250',
26
			'width'     => '300',
27
		),
28
		'lrec' => array(
29
			'tag'       => '336x280_largerectangle',
30
			'height'    => '280',
31
			'width'     => '336',
32
		),
33
		'leaderboard' => array(
34
			'tag'       => '728x90_leaderboard',
35
			'height'    => '90',
36
			'width'     => '728',
37
		),
38
		'wideskyscraper' => array(
39
			'tag'       => '160x600_wideskyscraper',
40
			'height'    => '600',
41
			'width'     => '160',
42
		),
43
	);
44
45
	/**
46
	 * Convenience function for grabbing options from params->options
47
	 * @param  string $option the option to grab
48
	 * @param  mixed  $default (optional)
49
	 * @return option or $default if not set
50
	 *
51
	 * @since 4.5.0
52
	 */
53
	function option( $option, $default = false ) {
54
		if ( ! isset( $this->params->options[ $option ] ) ) {
55
			return $default;
56
		}
57
58
		return $this->params->options[ $option ];
59
	}
60
61
	/**
62
	 * Instantiate the plugin
63
	 *
64
	 * @since 4.5.0
65
	 */
66
	function __construct() {
67
		add_action( 'init', array( $this, 'init' ) );
68
	}
69
70
	/**
71
	 * Code to run on WordPress 'init' hook
72
	 *
73
	 * @since 4.5.0
74
	 */
75
	function init() {
76
		// bail on infinite scroll
77
		if ( self::is_infinite_scroll() ) {
78
			return;
79
		}
80
81
		require_once( WORDADS_ROOT . '/php/params.php' );
82
		$this->params = new WordAds_Params();
83
84
		if ( is_admin() ) {
85
			require_once( WORDADS_ROOT . '/php/admin.php' );
86
			return;
87
		}
88
89
		if ( $this->should_bail() ) {
90
			return;
91
		}
92
93
		$this->insert_adcode();
94
		$this->insert_extras();
95
	}
96
97
	/**
98
	 * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
99
	 * @return boolean true if load came from infinite scroll
100
	 *
101
	 * @since 4.5.0
102
	 */
103
	public static function is_infinite_scroll() {
104
		return current_theme_supports( 'infinite-scroll' ) &&
105
				class_exists( 'The_Neverending_Home_Page' ) &&
106
				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
		add_filter( 'the_content', array( $this, 'insert_ad' ) );
119
		add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
120
121
		if ( $this->option( 'enable_header_ad' ) ) {
122
			switch ( get_stylesheet() ) {
123
				case 'twentyseventeen':
124
				case 'twentyfifteen':
125
				case 'twentyfourteen':
126
					add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
127
					break;
128
				default:
129
					add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
130
					break;
131
			}
132
		}
133
	}
134
135
	/**
136
	 * Add the actions/filters to insert extra-network features.
137
	 *
138
	 * @since 4.5.0
139
	 */
140
	private function insert_extras() {
141
		require_once( WORDADS_ROOT . '/php/networks/amazon.php' );
142
	}
143
144
	/**
145
	 * Register desktop scripts and styles
146
	 *
147
	 * @since 4.5.0
148
	 */
149
	function enqueue_scripts() {
150
		wp_enqueue_style(
151
			'wordads',
152
			WORDADS_URL . 'css/style.css',
153
			array(),
154
			'2015-12-18'
155
		);
156
	}
157
158
	/**
159
	 * IPONWEB metadata used by the various scripts
160
	 * @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...
161
	 */
162
	function insert_head_meta() {
163
		$domain = $this->params->targeting_tags['Domain'];
164
		$pageURL = $this->params->targeting_tags['PageURL'];
165
		$adsafe = $this->params->targeting_tags['AdSafe'];
166
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
167
		echo <<<HTML
168
		<script$data_tags type="text/javascript">
169
			var _ipw_custom = {
170
				wordAds: '1',
171
				domain: '$domain',
172
				pageURL: '$pageURL',
173
				adSafe: '$adsafe'
174
			};
175
		</script>
176
HTML;
177
	}
178
179
	/**
180
	 * IPONWEB scripts in <head>
181
	 *
182
	 * @since 4.5.0
183
	 */
184
	function insert_head_iponweb() {
185
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
186
		echo "<script$data_tags type='text/javascript' src='//s.pubmine.com/head.js'></script>";
187
	}
188
189
	/**
190
	 * Insert the ad onto the page
191
	 *
192
	 * @since 4.5.0
193
	 */
194
	function insert_ad( $content ) {
195
		/**
196
		 * Allow third-party tools to disable the display of in post ads.
197
		 *
198
		 * @module wordads
199
		 *
200
		 * @since 4.5.0
201
		 *
202
		 * @param bool true Should the in post unit be disabled. Default to false.
203
		 */
204
		$disable = apply_filters( 'wordads_inpost_disable', false );
205
		if ( ! $this->params->should_show() || $disable ) {
206
			return $content;
207
		}
208
209
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
210
		return $content . $this->get_ad( 'belowpost', $ad_type );
211
	}
212
213
	/**
214
	 * Inserts ad into header
215
	 *
216
	 * @since 4.5.0
217
	 */
218
	function insert_header_ad() {
219
		/**
220
		 * Allow third-party tools to disable the display of header ads.
221
		 *
222
		 * @module wordads
223
		 *
224
		 * @since 4.5.0
225
		 *
226
		 * @param bool true Should the header unit be disabled. Default to false.
227
		 */
228
		if ( apply_filters( 'wordads_header_disable', false ) ) {
229
			return;
230
		}
231
232
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
233
		echo $this->get_ad( 'top', $ad_type );
234
	}
235
236
	/**
237
	 * Special cases for inserting header unit via jQuery
238
	 *
239
	 * @since 4.5.0
240
	 */
241
	function insert_header_ad_special() {
242
		/**
243
		 * Allow third-party tools to disable the display of header ads.
244
		 *
245
		 * @module wordads
246
		 *
247
		 * @since 4.5.0
248
		 *
249
		 * @param bool true Should the header unit be disabled. Default to false.
250
		 */
251
		if ( apply_filters( 'wordads_header_disable', false ) ) {
252
			return;
253
		}
254
255
		$selector = '#content';
256
		switch ( get_stylesheet() ) {
257
			case 'twentyseventeen':
258
				$selector = '#content';
259
				break;
260
			case 'twentyfifteen':
261
				$selector = '#main';
262
				break;
263
			case 'twentyfourteen':
264
				$selector = 'article:first';
265
				break;
266
		}
267
268
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
269
		echo $this->get_ad( 'top', $ad_type );
270
		echo <<<HTML
271
		<script type="text/javascript">
272
			jQuery('.wpcnt-header').insertBefore('$selector');
273
		</script>
274
HTML;
275
	}
276
277
	/**
278
	 * Get the ad for the spot and type.
279
	 * @param  string $spot top, side, or belowpost
280
	 * @param  string $type iponweb or adsense
281
	 */
282
	function get_ad( $spot, $type = 'iponweb' ) {
283
		$snippet = '';
284
		if ( 'iponweb' == $type ) {
285
			$section_id = WORDADS_API_TEST_ID;
286
			$width = 300;
287
			$height = 250;
288
			if ( 'top' == $spot ) {
289
				// mrec for mobile, leaderboard for desktop
290
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
291
				$width = $this->params->mobile_device ? 300 : 728;
292
				$height = $this->params->mobile_device ? 250 : 90;
293
			} else if ( 'belowpost' ) {
294
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
295
				$width = 300;
296
				$height = 250;
297
			}
298
			$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
299
			$snippet = <<<HTML
300
			<script$data_tags type='text/javascript'>
301
				(function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window);
302
			</script>
303
HTML;
304
		} else if ( 'house' == $type ) {
305
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
306
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
307
		}
308
309
		$header = 'top' == $spot ? 'wpcnt-header' : '';
310
		$about = __( 'Advertisements', 'jetpack' );
311
		return <<<HTML
312
		<div class="wpcnt $header">
313
			<div class="wpa">
314
				<span class="wpa-about">$about</span>
315
				<div class="u $spot">
316
					$snippet
317
				</div>
318
			</div>
319
		</div>
320
HTML;
321
	}
322
323
	/**
324
	 * Check the reasons to bail before we attempt to insert ads.
325
	 * @return true if we should bail (don't insert ads)
326
	 *
327
	 * @since 4.5.0
328
	 */
329
	public function should_bail() {
330
		return ! $this->option( 'wordads_approved' );
331
	}
332
333
	/**
334
	 * Returns markup for HTML5 house ad base on unit
335
	 * @param  string $unit mrec, widesky, or leaderboard
336
	 * @return string       markup for HTML5 house ad
337
	 *
338
	 * @since 4.7.0
339
	 */
340
	public function get_house_ad( $unit = 'mrec' ) {
341
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
342
			$unit = 'mrec';
343
		}
344
345
		$width  = 300;
346
		$height = 250;
347
		if ( 'widesky' == $unit ) {
348
			$width  = 160;
349
			$height = 600;
350
		} else if ( 'leaderboard' == $unit ) {
351
			$width  = 728;
352
			$height = 90;
353
		}
354
355
		return <<<HTML
356
		<iframe
357
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
358
			width="$width"
359
			height="$height"
360
			frameborder="0"
361
			scrolling="no"
362
			marginheight="0"
363
			marginwidth="0">
364
		</iframe>
365
HTML;
366
	}
367
368
	/**
369
	 * Activation hook actions
370
	 *
371
	 * @since 4.5.0
372
	 */
373
	public static function activate() {
374
		WordAds_API::update_wordads_status_from_api();
375
	}
376
}
377
378
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
379
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
380
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
381
382
global $wordads;
383
$wordads = new WordAds();
384