Completed
Push — update/security-notice ( 43dc45...78cf5f )
by
unknown
100:34 queued 91:04
created

WordAds::enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
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
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
		/**
194
		 * Allow third-party tools to disable the display of in post ads.
195
		 *
196
		 * @module wordads
197
		 *
198
		 * @since 4.5.0
199
		 *
200
		 * @param bool true Should the in post unit be disabled. Default to false.
201
		 */
202
		$disable = apply_filters( 'wordads_inpost_disable', false );
203
		if ( ! $this->params->should_show() || $disable ) {
204
			return $content;
205
		}
206
207
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
208
		return $content . $this->get_ad( 'belowpost', $ad_type );
209
	}
210
211
	/**
212
	 * Inserts ad into header
213
	 *
214
	 * @since 4.5.0
215
	 */
216
	function insert_header_ad() {
217
		/**
218
		 * Allow third-party tools to disable the display of header ads.
219
		 *
220
		 * @module wordads
221
		 *
222
		 * @since 4.5.0
223
		 *
224
		 * @param bool true Should the header unit be disabled. Default to false.
225
		 */
226
		if ( apply_filters( 'wordads_header_disable', false ) ) {
227
			return;
228
		}
229
230
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
231
		echo $this->get_ad( 'top', $ad_type );
232
	}
233
234
	/**
235
	 * Special cases for inserting header unit via jQuery
236
	 *
237
	 * @since 4.5.0
238
	 */
239
	function insert_header_ad_special() {
240
		/**
241
		 * Allow third-party tools to disable the display of header ads.
242
		 *
243
		 * @module wordads
244
		 *
245
		 * @since 4.5.0
246
		 *
247
		 * @param bool true Should the header unit be disabled. Default to false.
248
		 */
249
		if ( apply_filters( 'wordads_header_disable', false ) ) {
250
			return;
251
		}
252
253
		$selector = '#content';
254
		switch ( get_stylesheet() ) {
255
			case 'twentyseventeen':
256
				$selector = '#content';
257
				break;
258
			case 'twentyfifteen':
259
				$selector = '#main';
260
				break;
261
			case 'twentyfourteen':
262
				$selector = 'article:first';
263
				break;
264
		}
265
266
		$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
267
		echo $this->get_ad( 'top', $ad_type );
268
		echo <<<HTML
269
		<script type="text/javascript">
270
			jQuery('.wpcnt-header').insertBefore('$selector');
271
		</script>
272
HTML;
273
	}
274
275
	/**
276
	 * Get the ad for the spot and type.
277
	 * @param  string $spot top, side, or belowpost
278
	 * @param  string $type iponweb or adsense
279
	 */
280
	function get_ad( $spot, $type = 'iponweb' ) {
281
		$snippet = '';
282
		if ( 'iponweb' == $type ) {
283
			$section_id = WORDADS_API_TEST_ID;
284
			$width = 300;
285
			$height = 250;
286
			if ( 'top' == $spot ) {
287
				// mrec for mobile, leaderboard for desktop
288
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
289
				$width = $this->params->mobile_device ? 300 : 728;
290
				$height = $this->params->mobile_device ? 250 : 90;
291
			} else if ( 'belowpost' ) {
292
				$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
293
				$width = 300;
294
				$height = 250;
295
			}
296
			$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
297
			$snippet = <<<HTML
298
			<script$data_tags type='text/javascript'>
299
				(function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window);
300
			</script>
301
HTML;
302
		} else if ( 'house' == $type ) {
303
			$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
304
			$snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
305
		}
306
307
		$header = 'top' == $spot ? 'wpcnt-header' : '';
308
		$about = __( 'Advertisements', 'jetpack' );
309
		return <<<HTML
310
		<div class="wpcnt $header">
311
			<div class="wpa">
312
				<span class="wpa-about">$about</span>
313
				<div class="u $spot">
314
					$snippet
315
				</div>
316
			</div>
317
		</div>
318
HTML;
319
	}
320
321
	/**
322
	 * Check the reasons to bail before we attempt to insert ads.
323
	 * @return true if we should bail (don't insert ads)
324
	 *
325
	 * @since 4.5.0
326
	 */
327
	public function should_bail() {
328
		return ! $this->option( 'wordads_approved' );
329
	}
330
331
	/**
332
	 * Returns markup for HTML5 house ad base on unit
333
	 * @param  string $unit mrec, widesky, or leaderboard
334
	 * @return string       markup for HTML5 house ad
335
	 *
336
	 * @since 4.7.0
337
	 */
338
	public function get_house_ad( $unit = 'mrec' ) {
339
		if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) {
340
			$unit = 'mrec';
341
		}
342
343
		$width  = 300;
344
		$height = 250;
345
		if ( 'widesky' == $unit ) {
346
			$width  = 160;
347
			$height = 600;
348
		} else if ( 'leaderboard' == $unit ) {
349
			$width  = 728;
350
			$height = 90;
351
		}
352
353
		return <<<HTML
354
		<iframe
355
			src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
356
			width="$width"
357
			height="$height"
358
			frameborder="0"
359
			scrolling="no"
360
			marginheight="0"
361
			marginwidth="0">
362
		</iframe>
363
HTML;
364
	}
365
366
	/**
367
	 * Activation hook actions
368
	 *
369
	 * @since 4.5.0
370
	 */
371
	public static function activate() {
372
		WordAds_API::update_wordads_status_from_api();
373
	}
374
}
375
376
add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
377
add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
378
add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
379
380
global $wordads;
381
$wordads = new WordAds();
382