Completed
Push — update/dashboard-use-settings-... ( 7ca0ae...a2daf5 )
by
unknown
22:20 queued 06:05
created

WordAds::insert_ad()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 22
rs 8.6737
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 class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
105
	}
106
107
	/**
108
	 * Add the actions/filters to insert the ads. Checks for mobile or desktop.
109
	 *
110
	 * @since 4.5.0
111
	 */
112
	private function insert_adcode() {
113
		add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
114
		add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
115
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
116
		add_filter( 'the_content', array( $this, 'insert_ad' ) );
117
		add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
118
119
		if ( $this->option( 'enable_header_ad' ) ) {
120
			switch ( get_stylesheet() ) {
121
				case 'twentyseventeen':
122
				case 'twentyfifteen':
123
				case 'twentyfourteen':
124
					add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
125
					break;
126
				default:
127
					add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
128
					break;
129
			}
130
		}
131
	}
132
133
	/**
134
	 * Add the actions/filters to insert extra-network features.
135
	 *
136
	 * @since 4.5.0
137
	 */
138
	private function insert_extras() {
139
		require_once( WORDADS_ROOT . '/php/networks/amazon.php' );
140
	}
141
142
	/**
143
	 * Register desktop scripts and styles
144
	 *
145
	 * @since 4.5.0
146
	 */
147
	function enqueue_scripts() {
148
		wp_enqueue_style(
149
			'wordads',
150
			WORDADS_URL . 'css/style.css',
151
			array(),
152
			'2015-12-18'
153
		);
154
	}
155
156
	/**
157
	 * IPONWEB metadata used by the various scripts
158
	 * @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...
159
	 */
160
	function insert_head_meta() {
161
		$domain = $this->params->targeting_tags['Domain'];
162
		$pageURL = $this->params->targeting_tags['PageURL'];
163
		$adsafe = $this->params->targeting_tags['AdSafe'];
164
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
165
		echo <<<HTML
166
		<script$data_tags type="text/javascript">
167
			var _ipw_custom = {
168
				wordAds: '1',
169
				domain: '$domain',
170
				pageURL: '$pageURL',
171
				adSafe: '$adsafe'
172
			};
173
		</script>
174
HTML;
175
	}
176
177
	/**
178
	 * IPONWEB scripts in <head>
179
	 *
180
	 * @since 4.5.0
181
	 */
182
	function insert_head_iponweb() {
183
		$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
184
		echo "<script$data_tags type='text/javascript' src='//s.pubmine.com/head.js'></script>";
185
	}
186
187
	/**
188
	 * Insert the ad onto the page
189
	 *
190
	 * @since 4.5.0
191
	 */
192
	function insert_ad( $content ) {
193
		// Ad JS won't work in XML feeds.
194
		if ( is_feed() ) {
195
			return $content;
196
		}
197
		/**
198
		 * Allow third-party tools to disable the display of in post ads.
199
		 *
200
		 * @module wordads
201
		 *
202
		 * @since 4.5.0
203
		 *
204
		 * @param bool true Should the in post unit be disabled. Default to false.
205
		 */
206
		$disable = apply_filters( 'wordads_inpost_disable', false );
207
		if ( ! $this->params->should_show() || $disable ) {
208
			return $content;
209
		}
210
211
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
212
		return $content . $this->get_ad( 'belowpost', $ad_type );
213
	}
214
215
	/**
216
	 * Inserts ad into header
217
	 *
218
	 * @since 4.5.0
219
	 */
220
	function insert_header_ad() {
221
		/**
222
		 * Allow third-party tools to disable the display of header ads.
223
		 *
224
		 * @module wordads
225
		 *
226
		 * @since 4.5.0
227
		 *
228
		 * @param bool true Should the header unit be disabled. Default to false.
229
		 */
230
		if ( apply_filters( 'wordads_header_disable', false ) ) {
231
			return;
232
		}
233
234
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
235
		echo $this->get_ad( 'top', $ad_type );
236
	}
237
238
	/**
239
	 * Special cases for inserting header unit via jQuery
240
	 *
241
	 * @since 4.5.0
242
	 */
243
	function insert_header_ad_special() {
244
		/**
245
		 * Allow third-party tools to disable the display of header ads.
246
		 *
247
		 * @module wordads
248
		 *
249
		 * @since 4.5.0
250
		 *
251
		 * @param bool true Should the header unit be disabled. Default to false.
252
		 */
253
		if ( apply_filters( 'wordads_header_disable', false ) ) {
254
			return;
255
		}
256
257
		$selector = '#content';
258
		switch ( get_stylesheet() ) {
259
			case 'twentyseventeen':
260
				$selector = '#content';
261
				break;
262
			case 'twentyfifteen':
263
				$selector = '#main';
264
				break;
265
			case 'twentyfourteen':
266
				$selector = 'article:first';
267
				break;
268
		}
269
270
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
271
		echo $this->get_ad( 'top', $ad_type );
272
		echo <<<HTML
273
		<script type="text/javascript">
274
			jQuery('.wpcnt-header').insertBefore('$selector');
275
		</script>
276
HTML;
277
	}
278
279
	/**
280
	 * Get the ad for the spot and type.
281
	 * @param  string $spot top, side, or belowpost
282
	 * @param  string $type iponweb or adsense
283
	 */
284
	function get_ad( $spot, $type = 'iponweb' ) {
285
		$snippet = '';
286
		if ( 'iponweb' == $type ) {
287
			$section_id = WORDADS_API_TEST_ID;
288
			$width = 300;
289
			$height = 250;
290
			if ( 'top' == $spot ) {
291
				// mrec for mobile, leaderboard for desktop
292
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
293
				$width = $this->params->mobile_device ? 300 : 728;
294
				$height = $this->params->mobile_device ? 250 : 90;
295
			} else if ( 'belowpost' ) {
296
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
297
				$width = 300;
298
				$height = 250;
299
			}
300
			$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
301
			$snippet = <<<HTML
302
			<script$data_tags type='text/javascript'>
303
				(function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window);
304
			</script>
305
HTML;
306
		} else if ( 'house' == $type ) {
307
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
308
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
309
		}
310
311
		$header = 'top' == $spot ? 'wpcnt-header' : '';
312
		$about = __( 'Advertisements', 'jetpack' );
313
		return <<<HTML
314
		<div class="wpcnt $header">
315
			<div class="wpa">
316
				<span class="wpa-about">$about</span>
317
				<div class="u $spot">
318
					$snippet
319
				</div>
320
			</div>
321
		</div>
322
HTML;
323
	}
324
325
	/**
326
	 * Check the reasons to bail before we attempt to insert ads.
327
	 * @return true if we should bail (don't insert ads)
328
	 *
329
	 * @since 4.5.0
330
	 */
331
	public function should_bail() {
332
		return ! $this->option( 'wordads_approved' );
333
	}
334
335
	/**
336
	 * Returns markup for HTML5 house ad base on unit
337
	 * @param  string $unit mrec, widesky, or leaderboard
338
	 * @return string       markup for HTML5 house ad
339
	 *
340
	 * @since 4.7.0
341
	 */
342
	public function get_house_ad( $unit = 'mrec' ) {
343
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
344
			$unit = 'mrec';
345
		}
346
347
		$width  = 300;
348
		$height = 250;
349
		if ( 'widesky' == $unit ) {
350
			$width  = 160;
351
			$height = 600;
352
		} else if ( 'leaderboard' == $unit ) {
353
			$width  = 728;
354
			$height = 90;
355
		}
356
357
		return <<<HTML
358
		<iframe
359
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
360
			width="$width"
361
			height="$height"
362
			frameborder="0"
363
			scrolling="no"
364
			marginheight="0"
365
			marginwidth="0">
366
		</iframe>
367
HTML;
368
	}
369
370
	/**
371
	 * Activation hook actions
372
	 *
373
	 * @since 4.5.0
374
	 */
375
	public static function activate() {
376
		WordAds_API::update_wordads_status_from_api();
377
	}
378
}
379
380
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
381
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
382
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
383
384
global $wordads;
385
$wordads = new WordAds();
386