PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | modules/wordads/class-wordads.php +1043 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,1043 @@
1 +<?php
2 +/**
3 + * Main WordAds file.
4 + *
5 + * @package automattic/jetpack
6 + */
7 +
8 +use Automattic\Jetpack\Status\Host;
9 +
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit( 0 );
12 +}
13 +
14 +define( 'WORDADS_ROOT', __DIR__ );
15 +define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
16 +define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
17 +define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
18 +define( 'WORDADS_API_TEST_ID', '26942' );
19 +define( 'WORDADS_API_TEST_ID2', '114160' );
20 +
21 +require_once WORDADS_ROOT . '/php/class-wordads-sidebar-widget.php';
22 +require_once WORDADS_ROOT . '/php/class-wordads-api.php';
23 +require_once WORDADS_ROOT . '/php/class-wordads-cron.php';
24 +require_once WORDADS_ROOT . '/php/class-wordads-california-privacy.php';
25 +require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php';
26 +require_once WORDADS_ROOT . '/php/class-wordads-consent-management-provider.php';
27 +require_once WORDADS_ROOT . '/php/class-wordads-formats.php';
28 +require_once WORDADS_ROOT . '/php/class-wordads-smart.php';
29 +require_once WORDADS_ROOT . '/php/class-wordads-shortcode.php';
30 +require_once WORDADS_ROOT . '/php/class-wordads-premium-analytics.php';
31 +
32 +/**
33 + * Primary WordAds class.
34 + */
35 +class WordAds {
36 +
37 + /**
38 + * Ads parameters.
39 + *
40 + * @var WordAds_Params
41 + */
42 + public $params = null;
43 +
44 + /**
45 + * Ads.
46 + *
47 + * @var array
48 + */
49 + public $ads = array();
50 +
51 + /**
52 + * Array of supported ad types.
53 + *
54 + * @var array
55 + */
56 + public static $ad_tag_ids = array(
57 + 'mrec' => array(
58 + 'tag' => '300x250_mediumrectangle',
59 + 'height' => '250',
60 + 'width' => '300',
61 + ),
62 + 'leaderboard' => array(
63 + 'tag' => '728x90_leaderboard',
64 + 'height' => '90',
65 + 'width' => '728',
66 + ),
67 + 'mobile_leaderboard' => array(
68 + 'tag' => '320x50_mobileleaderboard',
69 + 'height' => '50',
70 + 'width' => '320',
71 + ),
72 + 'wideskyscraper' => array(
73 + 'tag' => '160x600_wideskyscraper',
74 + 'height' => '600',
75 + 'width' => '160',
76 + ),
77 + );
78 +
79 + /**
80 + * Mapping array of location slugs to placement ids
81 + *
82 + * @var array
83 + */
84 + public static $ad_location_ids = array(
85 + 'top' => 110,
86 + 'belowpost' => 120,
87 + 'belowpost2' => 130,
88 + 'sidebar' => 140,
89 + 'widget' => 150,
90 + 'gutenberg' => 200,
91 + 'inline' => 310,
92 + 'inline-plugin' => 320,
93 + );
94 +
95 + /**
96 + * Mapping array of form factor slugs to form factor ids
97 + *
98 + * @var array
99 + */
100 + public static $form_factor_ids = array(
101 + 'square' => '001', // 250x250
102 + 'leaderboard' => '002', // 728x90
103 + 'skyscraper' => '003', // 120x600
104 + );
105 +
106 + /**
107 + * Counter to enable unique, sequential section IDs for all amp-ad units
108 + *
109 + * @var int
110 + */
111 + public static $amp_section_id = 1;
112 +
113 + /**
114 + * Solo unit CSS string.
115 + *
116 + * @var string
117 + */
118 + public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0;';
119 +
120 + /**
121 + * Checks for AMP support and returns true iff active & AMP request
122 + *
123 + * @return boolean True if supported AMP request
124 + *
125 + * @since 7.5.0
126 + */
127 + public static function is_amp() {
128 + return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
129 + }
130 +
131 + /**
132 + * Increment the AMP section ID and return the value
133 + *
134 + * @return int
135 + */
136 + public static function get_amp_section_id() {
137 + return self::$amp_section_id++;
138 + }
139 +
140 + /**
141 + * Convenience function for grabbing options from params->options
142 + *
143 + * @param string $option the option to grab.
144 + * @param mixed $default (optional).
145 + * @return mixed option or $default if not set
146 + *
147 + * @since 4.5.0
148 + */
149 + public function option( $option, $default = false ) {
150 + if ( ! isset( $this->params->options[ $option ] ) ) {
151 + return $default;
152 + }
153 +
154 + return $this->params->options[ $option ];
155 + }
156 +
157 + /**
158 + * Returns the ad tag property array for supported ad types.
159 + *
160 + * @return array array with ad tags
161 + *
162 + * @since 7.1.0
163 + */
164 + public function get_ad_tags() {
165 + return self::$ad_tag_ids;
166 + }
167 +
168 + /**
169 + * Returns the solo css for unit
170 + *
171 + * @return string the special css for solo units
172 + *
173 + * @since 7.1.0
174 + */
175 + public function get_solo_unit_css() {
176 + return self::$solo_unit_css;
177 + }
178 +
179 + /**
180 + * Instantiate the plugin
181 + *
182 + * @since 4.5.0
183 + */
184 + public function __construct() {
185 + add_action( 'wp', array( $this, 'init' ) );
186 + add_action( 'rest_api_init', array( $this, 'init' ) );
187 + add_action( 'widgets_init', array( $this, 'widget_callback' ) );
188 +
189 + if ( is_admin() ) {
190 + WordAds_California_Privacy::init_ajax_actions();
191 + WordAds_Consent_Management_Provider::init_ajax_actions();
192 + }
193 + }
194 +
195 + /**
196 + * Code to run on WordPress 'init' hook
197 + *
198 + * @since 4.5.0
199 + */
200 + public function init() {
201 + require_once WORDADS_ROOT . '/php/class-wordads-params.php';
202 + $this->params = new WordAds_Params();
203 +
204 + if ( $this->should_bail() || self::is_infinite_scroll() ) {
205 + return;
206 + }
207 +
208 + if ( is_admin() ) {
209 + require_once WORDADS_ROOT . '/php/class-wordads-admin.php';
210 + return;
211 + }
212 +
213 + $this->insert_adcode();
214 +
215 + // Include California Privacy Act related features if enabled.
216 + if ( $this->params->options['wordads_ccpa_enabled'] ) {
217 + WordAds_California_Privacy::init();
218 + }
219 +
220 + // Initialize CMP if enabled.
221 + if ( isset( $this->params->options['wordads_cmp_enabled'] ) && $this->params->options['wordads_cmp_enabled'] ) {
222 + WordAds_Consent_Management_Provider::init();
223 + }
224 +
225 + // Initialize [wordads] shortcode.
226 + WordAds_Shortcode::init();
227 +
228 + // Initialize Smart.
229 + WordAds_Smart::instance()->init( $this->params );
230 +
231 + if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] )
232 + || ( site_url( 'ads.txt', 'relative' ) === $_SERVER['REQUEST_URI'] ) ) {
233 +
234 + $ads_txt_transient = get_transient( 'wordads_ads_txt' );
235 +
236 + if ( false === ( $ads_txt_transient ) ) {
237 + $wordads_ads_txt = WordAds_API::get_wordads_ads_txt();
238 + $ads_txt_transient = is_wp_error( $wordads_ads_txt ) ? '' : $wordads_ads_txt;
239 + set_transient( 'wordads_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
240 + }
241 +
242 + /**
243 + * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
244 + *
245 + * @module wordads
246 + *
247 + * @since 6.1.0
248 + *
249 + * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
250 + */
251 + $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
252 +
253 + status_header( 200 );
254 + header( 'Content-Type: text/plain; charset=utf-8' );
255 + echo esc_html( $ads_txt_content );
256 + die( 0 );
257 + }
258 + }
259 +
260 + /**
261 + * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
262 + *
263 + * @return boolean true if load came from infinite scroll
264 + *
265 + * @since 4.5.0
266 + */
267 + public static function is_infinite_scroll() {
268 + return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
269 + }
270 +
271 + /**
272 + * Add the actions/filters to insert the ads. Checks for mobile or desktop.
273 + *
274 + * @since 4.5.0
275 + */
276 + private function insert_adcode() {
277 + add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
278 +
279 + // These 'wp_head' actions add the IPONWEB JavaScript snippets to the page.
280 + // We need to remove these calls whenever migration to Aditude is complete.
281 + add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
282 +
283 + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
284 + add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
285 +
286 + /**
287 + * Filters enabling ads in `the_content` filter
288 + *
289 + * @see https://jetpack.com/support/ads/
290 + *
291 + * @module wordads
292 + *
293 + * @since 5.8.0
294 + *
295 + * @param bool True to disable ads in `the_content`
296 + */
297 + if ( ! apply_filters( 'wordads_content_disable', false ) ) {
298 + add_filter( 'the_content', array( $this, 'insert_ad' ) );
299 + }
300 +
301 + /**
302 + * Filters enabling ads in `the_excerpt` filter
303 + *
304 + * @see https://jetpack.com/support/ads/
305 + *
306 + * @module wordads
307 + *
308 + * @since 5.8.0
309 + *
310 + * @param bool True to disable ads in `the_excerpt`
311 + */
312 + if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
313 + add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
314 + }
315 +
316 + if ( $this->option( 'enable_header_ad', true ) ) {
317 + if ( self::is_amp() ) {
318 + add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) );
319 + } else {
320 + add_action( 'wordads_header_ad', array( $this, 'insert_header_ad' ), 100 );
321 + add_action( 'wp_footer', array( $this, 'insert_header_ad' ), 100 );
322 + }
323 + }
324 + }
325 +
326 + /**
327 + * Register desktop scripts and styles
328 + *
329 + * @since 4.5.0
330 + */
331 + public function enqueue_scripts() {
332 + // Ads are never shown on 404 pages.
333 + if ( is_404() ) {
334 + return;
335 + }
336 +
337 + wp_enqueue_style(
338 + 'wordads',
339 + WORDADS_URL . 'css/style.css',
340 + array(),
341 + '2015-12-18'
342 + );
343 + }
344 +
345 + /**
346 + * Add the IPW resource hints
347 + *
348 + * @since 7.9
349 + *
350 + * @param array $hints Domains for hinting.
351 + * @param string $relation_type Resource type.
352 + *
353 + * @return array Domains for hinting.
354 + */
355 + public function resource_hints( $hints, $relation_type ) {
356 + if ( 'dns-prefetch' === $relation_type && ! is_404() ) {
357 + $hints[] = '//s.pubmine.com';
358 + $hints[] = '//x.bidswitch.net';
359 + $hints[] = '//static.criteo.net';
360 + $hints[] = '//ib.adnxs.com';
361 + $hints[] = '//aax.amazon-adsystem.com';
362 + $hints[] = '//bidder.criteo.com';
363 + $hints[] = '//cas.criteo.com';
364 + $hints[] = '//gum.criteo.com';
365 + $hints[] = '//ads.pubmatic.com';
366 + $hints[] = '//gads.pubmatic.com';
367 + $hints[] = '//tpc.googlesyndication.com';
368 + $hints[] = '//ad.doubleclick.net';
369 + $hints[] = '//googleads.g.doubleclick.net';
370 + $hints[] = '//www.googletagservices.com';
371 + $hints[] = '//cdn.switchadhub.com';
372 + $hints[] = '//delivery.g.switchadhub.com';
373 + $hints[] = '//delivery.swid.switchadhub.com';
374 + }
375 +
376 + return $hints;
377 + }
378 +
379 + /**
380 + * IPONWEB metadata used by the various scripts
381 + */
382 + public function insert_head_meta() {
383 + if ( self::is_amp() || is_404() ) {
384 + return;
385 + }
386 + $hosting_type = ( new Host() )->is_woa_site() ? 1 : 2; // 1 = WPCOM, 2 = Jetpack.
387 + $pagetype = (int) $this->params->get_page_type_ipw();
388 + $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
389 + $site_id = $this->params->blog_id;
390 + $consent = (int) isset( $_COOKIE['personalized-ads-consent'] );
391 + $is_logged_in = is_user_logged_in() ? '1' : '0';
392 +
393 + $disabled_slot_formats = apply_filters( 'wordads_disabled_slot_formats', array() );
394 +
395 + if ( apply_filters( 'wordads_iponweb_bottom_sticky_ad_disable', false ) ) {
396 + $disabled_slot_formats[] = 'MTS';
397 + }
398 +
399 + if ( apply_filters( 'wordads_iponweb_sidebar_sticky_right_ad_disable', false ) ) {
400 + $disabled_slot_formats[] = 'DPR';
401 + }
402 +
403 + $config = array(
404 + 'pt' => $pagetype,
405 + 'ht' => $hosting_type,
406 + 'tn' => get_stylesheet(),
407 + 'uloggedin' => $is_logged_in,
408 + 'amp' => false,
409 + 'siteid' => $site_id,
410 + 'consent' => $consent,
411 + 'ad' => array(
412 + 'label' => array(
413 + 'text' => __( 'Advertisements', 'jetpack' ),
414 + ),
415 + 'reportAd' => array(
416 + 'text' => __( 'Report this ad', 'jetpack' ),
417 + ),
418 + 'privacySettings' => array(
419 + 'text' => __( 'Privacy', 'jetpack' ),
420 + 'onClick' => 'js:function() { window.__tcfapi && window.__tcfapi(\'showUi\'); }',
421 + ),
422 + ),
423 + 'disabled_slot_formats' => $disabled_slot_formats,
424 + );
425 + $js_config = WordAds_Array_Utils::array_to_js_object( $config );
426 + ?>
427 + <script<?php echo esc_attr( $data_tags ); ?> type="text/javascript">
428 + var __ATA_PP = <?php echo $js_config; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>;
429 + var __ATA = __ATA || {};
430 + __ATA.cmd = __ATA.cmd || [];
431 + __ATA.criteo = __ATA.criteo || {};
432 + __ATA.criteo.cmd = __ATA.criteo.cmd || [];
433 + </script>
434 + <?php
435 +
436 + $section_id = $this->params->blog_id . 5;
437 +
438 + // Get below post tag.
439 + $tag_belowpost = $this->get_fallback_ad_snippet( $section_id, 'square', 'belowpost', '', '{{unique_id}}' );
440 +
441 + // Remove linebreaks and sanitize.
442 + $tag_belowpost = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_belowpost ) );
443 +
444 + // Get an inline tag with a macro as id handled on JS side to use as a fallback.
445 + $tag_inline = $this->get_fallback_ad_snippet( $section_id, 'square', 'inline', '', '{{unique_id}}' );
446 +
447 + // Remove linebreaks and sanitize.
448 + $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) );
449 +
450 + // Get top tag.
451 + $tag_top = $this->get_fallback_ad_snippet( $section_id, 'leaderboard', 'top', '', '{{unique_id}}' );
452 +
453 + // Remove linebreaks and sanitize.
454 + $tag_top = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_top ) );
455 +
456 + // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
457 + echo <<<HTML
458 + <script type="text/javascript">
459 + window.sas_fallback = window.sas_fallback || [];
460 + window.sas_fallback.push(
461 + { tag: "$tag_inline", type: 'inline' },
462 + { tag: "$tag_belowpost", type: 'belowpost' },
463 + { tag: "$tag_top", type: 'top' }
464 + );
465 + </script>
466 +HTML;
467 + }
468 +
469 + /**
470 + * Insert the ad onto the page
471 + *
472 + * @since 4.5.0
473 + *
474 + * @param string $content HTML content.
475 + */
476 + public function insert_ad( $content ) {
477 + // Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
478 + if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
479 + return $content;
480 + }
481 + /**
482 + * Allow third-party tools to disable the display of in post ads.
483 + *
484 + * @module wordads
485 + *
486 + * @since 4.5.0
487 + *
488 + * @param bool true Should the in post unit be disabled. Default to false.
489 + */
490 + $disable = apply_filters( 'wordads_inpost_disable', false );
491 + if ( ! $this->params->should_show() || $disable ) {
492 + return $content;
493 + }
494 +
495 + $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
496 + return $content . $this->get_ad( 'belowpost', $ad_type );
497 + }
498 +
499 + /**
500 + * Insert an inline ad into a post content
501 + * Used for rendering the `wordads` shortcode.
502 + *
503 + * @since 6.1.0
504 + *
505 + * @param string $content HTML content.
506 + */
507 + public function insert_inline_ad( $content ) {
508 + // Ad JS won't work in XML feeds.
509 + if ( is_feed() ) {
510 + return $content;
511 + }
512 + /**
513 + * Allow third-party tools to disable the display of in post ads.
514 + *
515 + * @module wordads
516 + *
517 + * @since 4.5.0
518 + *
519 + * @param bool true Should the in post unit be disabled. Default to false.
520 + */
521 + $disable = apply_filters( 'wordads_inpost_disable', false );
522 + if ( $disable ) {
523 + return $content;
524 + }
525 +
526 + $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
527 + $location = 'shortcode';
528 +
529 + // Not house ad and WATL enabled.
530 + // phpcs:disable WordPress.Security.NonceVerification.Recommended
531 + if ( 'house' !== $ad_type && ( isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ) ) {
532 + return $content . $this->get_watl_ad_html_tag( $location );
533 + }
534 +
535 + return $content . $this->get_ad( 'inline', $ad_type );
536 + }
537 +
538 + /**
539 + * Inserts ad into header
540 + *
541 + * @since 4.5.0
542 + */
543 + public function insert_header_ad() {
544 + /**
545 + * Allow third-party tools to disable the display of header ads.
546 + *
547 + * @module wordads
548 + *
549 + * @since 4.5.0
550 + *
551 + * @param bool true Should the header unit be disabled. Default to false.
552 + */
553 + if ( apply_filters( 'wordads_header_disable', false ) ) {
554 + return;
555 + }
556 +
557 + // Prevent multiple manual ads.
558 + if ( 2 <= did_action( 'wordads_header_ad' ) ) {
559 + return;
560 + }
561 +
562 + // Prevent placing an automatic ad if a manual ad has already been placed.
563 + if ( doing_action( 'wp_footer' ) && did_action( 'wordads_header_ad' ) ) {
564 + return;
565 + }
566 +
567 + // Prevent placing a manual ad if an automatic ad has already been placed.
568 + if ( doing_action( 'wordads_header_ad' ) && did_action( 'wp_footer' ) ) {
569 + return;
570 + }
571 +
572 + // Default ad placement to just after the <body> tag.
573 + $selector = 'body > :first-child';
574 +
575 + // Special theme cases.
576 + switch ( get_stylesheet() ) {
577 + case 'twentyseventeen':
578 + $selector = '#content';
579 + break;
580 + case 'twentyfifteen':
581 + $selector = '#main';
582 + break;
583 + case 'twentyfourteen':
584 + $selector = 'article';
585 + break;
586 + }
587 +
588 + // Don't relocate if the ad is being placed manually.
589 + if ( doing_action( 'wordads_header_ad' ) ) {
590 + $selector = '';
591 + }
592 +
593 + $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
594 + $form_factor = $this->params->mobile_device ? 'square' : 'leaderboard';
595 + echo $this->get_dynamic_ad_snippet( $section_id, $form_factor, 'top', $selector ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
596 + }
597 +
598 + /**
599 + * Header unit for AMP
600 + *
601 + * @param string $content Content of the page.
602 + *
603 + * @since 7.5.0
604 + */
605 + public function insert_header_ad_amp( $content ) {
606 +
607 + $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
608 + if ( 'house' === $ad_type ) {
609 + return $content;
610 + }
611 + return $this->get_ad( 'top_amp', $ad_type ) . $content;
612 + }
613 +
614 + /**
615 + * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
616 + *
617 + * @param string $adstxt The ads.txt being filtered.
618 + * @return string Filtered ads.txt with custom entries, if applicable.
619 + *
620 + * @since 6.5.0
621 + */
622 + public function insert_custom_adstxt( $adstxt ) {
623 + if ( ! $this->option( 'wordads_custom_adstxt_enabled' ) ) {
624 + return $adstxt;
625 + }
626 +
627 + $custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
628 + if ( $custom_adstxt ) {
629 + $adstxt .= "\n\n#Jetpack - User Custom Entries\n";
630 + $adstxt .= $custom_adstxt . "\n";
631 + }
632 +
633 + return $adstxt;
634 + }
635 +
636 + /**
637 + * Get the ad for the spot and type.
638 + *
639 + * @param string $spot top, side, inline, or belowpost.
640 + * @param string $type iponweb or adsense.
641 + */
642 + public function get_ad( $spot, $type = 'iponweb' ) {
643 + $snippet = '';
644 + if ( 'iponweb' === $type ) {
645 + $section_id = WORDADS_API_TEST_ID;
646 +
647 + if ( 'top' === $spot ) {
648 + // mrec for mobile, leaderboard for desktop.
649 + $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
650 + $form_factor = $this->params->mobile_device ? 'square' : 'leaderboard';
651 + $snippet = $this->get_dynamic_ad_snippet( $section_id, $form_factor, $spot );
652 + } elseif ( 'belowpost' === $spot ) {
653 + $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
654 + $snippet = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot );
655 + } elseif ( 'inline' === $spot ) {
656 + $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
657 + $snippet = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot );
658 + } elseif ( 'top_amp' === $spot ) {
659 + // Ad unit which can safely be inserted below title, above content in a variety of themes.
660 + $width = 300;
661 + $height = 250;
662 + $snippet = $this->get_ad_div( $spot, $this->get_amp_snippet( $height, $width ) );
663 + }
664 + } elseif ( 'house' === $type ) {
665 + $leaderboard = 'top' === $spot && ! $this->params->mobile_device;
666 + $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
667 + if ( 'belowpost' === $spot && $this->option( 'wordads_second_belowpost', true ) ) {
668 + $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
669 + }
670 + }
671 +
672 + return $snippet;
673 + }
674 +
675 + /**
676 + * Returns the AMP snippet to be inserted
677 + *
678 + * @param int $height Height.
679 + * @param int $width Width.
680 + * @return string
681 + *
682 + * @since 8.7
683 + */
684 + public function get_amp_snippet( $height, $width ) {
685 + $height = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad".
686 + $width = esc_attr( $width );
687 + $amp_section_id = esc_attr( self::get_amp_section_id() );
688 + $site_id = esc_attr( $this->params->blog_id );
689 + return <<<HTML
690 + <amp-ad width="$width" height="$height"
691 + type="pubmine"
692 + data-siteid="$site_id"
693 + data-section="$amp_section_id">
694 + </amp-ad>
695 +HTML;
696 + }
697 +
698 + /**
699 + * Compatibility function -- main functionality replaced with get_dynamic_ad_snippet
700 + *
701 + * @param int $section_id Ad section.
702 + * @param int $height Ad height.
703 + * @param int $width Ad width.
704 + * @param string $location Location.
705 + * @param string $css CSS.
706 + *
707 + * @return string
708 + *
709 + * @since 5.7
710 + */
711 + public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
712 + if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
713 + return $this->get_amp_snippet( $height, $width );
714 + }
715 +
716 + $this->ads[] = array(
717 + 'location' => $location,
718 + 'width' => $width,
719 + 'height' => $height,
720 + );
721 +
722 + if ( 'gutenberg' === $location ) {
723 + $ad_number = count( $this->ads ) . '-' . uniqid();
724 + $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
725 + $css = esc_attr( $css );
726 +
727 + $loc_id = 100;
728 + if ( ! empty( self::$ad_location_ids[ $location ] ) ) {
729 + $loc_id = self::$ad_location_ids[ $location ];
730 + }
731 +
732 + $loc_id = esc_js( $loc_id );
733 +
734 + // Determine supported Gutenberg format by width and height.
735 + $format = WordAds_Formats::get_format_slug( (int) $width, (int) $height );
736 + if ( $format === '' ) {
737 + return ''; // When format is not supported, ignore placement.
738 + }
739 +
740 + // For a while return elements for both IPONWEB (__ATA) and Aditude (tudeMappings).
741 + // We will remove using IPONWEB after partnership termination on June 30, 2025.
742 + return <<<HTML
743 + <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
744 + <div id="atatags-{$ad_number}">
745 + <script$data_tags type="text/javascript">
746 + window.getAdSnippetCallback = function () {
747 + if ( true === ( window.isWatlV1 ?? false ) ) {
748 + // Use IPONWEB scripts.
749 + window.__ATA = window.__ATA || {};
750 + window.__ATA.cmd = __ATA.cmd || [];
751 + window.__ATA.cmd.push(function() {
752 + window.__ATA.initSlot('atatags-{$ad_number}', {
753 + collapseEmpty: 'before',
754 + sectionId: '{$section_id}',
755 + location: {$loc_id},
756 + width: {$width},
757 + height: {$height}
758 + });
759 + });
760 + } else {
761 + // Use Aditude scripts.
762 + window.tudeMappings = window.tudeMappings || [];
763 + window.tudeMappings.push( {
764 + divId: 'atatags-{$ad_number}',
765 + format: '{$format}',
766 + width: {$width},
767 + height: {$height},
768 + } );
769 + }
770 + }
771 +
772 + if ( document.readyState === 'loading' ) {
773 + document.addEventListener( 'DOMContentLoaded', window.getAdSnippetCallback );
774 + } else {
775 + window.getAdSnippetCallback();
776 + }
777 + </script>
778 + </div>
779 + </div>
780 +HTML;
781 + }
782 +
783 + $form_factor = 'square';
784 + if ( 250 > $width ) {
785 + $form_factor = 'skyscraper';
786 + } elseif ( 300 < $width ) {
787 + $form_factor = 'leaderboard';
788 + }
789 +
790 + return $this->get_dynamic_ad_snippet( $section_id, $form_factor, $location );
791 + }
792 +
793 + /**
794 + * Returns the dynamic snippet to be inserted into the ad unit
795 + *
796 + * @param int $section_id section_id.
797 + * @param string $form_factor form_factor.
798 + * @param string $location location.
799 + * @param string $relocate location to be moved after the fact for themes without required hook.
800 + * @param string | null $id A unique string ID or placeholder.
801 + *
802 + * @return string
803 + *
804 + * @since 8.7
805 + */
806 + public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) {
807 + $is_location_enabled = in_array( $location, array( 'top', 'belowpost', 'inline' ), true );
808 +
809 + if ( $is_location_enabled ) {
810 + return self::get_watl_ad_html_tag( $location );
811 + }
812 +
813 + return $this->get_fallback_ad_snippet( $section_id, $form_factor, $location, $relocate, $id );
814 + }
815 +
816 + /**
817 + * Returns the fallback dynamic snippet to be inserted into the ad unit
818 + *
819 + * @param int $section_id section_id.
820 + * @param string $form_factor form_factor.
821 + * @param string $location location.
822 + * @param string $relocate location to be moved after the fact for themes without required hook.
823 + * @param string | null $id A unique string ID or placeholder.
824 + *
825 + * @return string
826 + *
827 + * @since 8.7
828 + */
829 + public function get_fallback_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) {
830 + $div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() );
831 + $div_id = esc_attr( $div_id );
832 +
833 + // Default form factor.
834 + $form_factor_id = self::$form_factor_ids['square'];
835 + if ( isset( self::$form_factor_ids[ $form_factor ] ) ) {
836 + $form_factor_id = self::$form_factor_ids[ $form_factor ];
837 + }
838 +
839 + $loc_id = 100;
840 + if ( isset( self::$ad_location_ids[ $location ] ) ) {
841 + $loc_id = self::$ad_location_ids[ $location ];
842 + }
843 +
844 + $form_factor_id = esc_js( $form_factor_id );
845 + $advertisements_text = esc_js( __( 'Advertisements', 'jetpack' ) );
846 + $report_ad_text = esc_js( __( 'Report this ad', 'jetpack' ) );
847 + $privacy_settings_text = esc_js( __( 'Privacy settings', 'jetpack' ) );
848 +
849 + $relocate_script = '';
850 + if ( ! empty( $relocate ) ) {
851 + $selector = wp_json_encode( $relocate, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
852 + $relocate_script = <<<JS
853 + <script type="text/javascript">
854 + var adNode = document.getElementById( '{$div_id}' );
855 + var selector = {$selector};
856 + var relocateNode = document.querySelector( selector );
857 + relocateNode.parentNode.insertBefore( adNode, relocateNode );
858 + </script>
859 +JS;
860 + }
861 +
862 + return <<<HTML
863 + <div id="{$div_id}"></div>
864 + {$relocate_script}
865 + <script>
866 + __ATA.cmd.push(function() {
867 + __ATA.initDynamicSlot({
868 + id: '{$div_id}',
869 + location: {$loc_id},
870 + formFactor: '{$form_factor_id}',
871 + label: {
872 + text: '{$advertisements_text}',
873 + },
874 + creative: {
875 + reportAd: {
876 + text: '{$report_ad_text}',
877 + },
878 + privacySettings: {
879 + text: '{$privacy_settings_text}',
880 + onClick: function() { window.__tcfapi && window.__tcfapi('showUi'); },
881 + }
882 + }
883 + });
884 + });
885 + </script>
886 +HTML;
887 + }
888 +
889 + /**
890 + * Returns the complete ad div with snippet to be inserted into the page
891 + *
892 + * @param string $spot top, side, inline, or belowpost.
893 + * @param string $snippet The snippet to insert into the div.
894 + * @param array $css_classes CSS classes.
895 + * @return string The supporting ad unit div.
896 + *
897 + * @since 7.1
898 + */
899 + public function get_ad_div( $spot, $snippet, array $css_classes = array() ) {
900 + if ( strpos( strtolower( $spot ), 'amp' ) === false && ! 'inline' === $spot ) {
901 + return $snippet; // we don't want dynamic ads to be inserted for AMP & Gutenberg.
902 + }
903 +
904 + $css_classes[] = 'wpcnt';
905 + if ( 'top' === $spot ) {
906 + $css_classes[] = 'wpcnt-header';
907 + }
908 +
909 + $spot = esc_attr( $spot );
910 + $classes = esc_attr( implode( ' ', $css_classes ) );
911 + $about = esc_html__( 'Advertisements', 'jetpack' );
912 + return <<<HTML
913 + <div class="$classes">
914 + <div class="wpa">
915 + <span class="wpa-about">$about</span>
916 + <div class="u $spot">
917 + $snippet
918 + </div>
919 + </div>
920 + </div>
921 +HTML;
922 + }
923 +
924 + /**
925 + * Check the reasons to bail before we attempt to insert ads.
926 + *
927 + * @return true if we should bail (don't insert ads)
928 + *
929 + * @since 4.5.0
930 + */
931 + public function should_bail() {
932 + return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' );
933 + }
934 +
935 + /**
936 + * Returns markup for HTML5 house ad base on unit
937 + *
938 + * @param string $unit mrec, widesky, or leaderboard.
939 + * @return string markup for HTML5 house ad
940 + *
941 + * @since 4.7.0
942 + */
943 + public function get_house_ad( $unit = 'mrec' ) {
944 +
945 + switch ( $unit ) {
946 + case 'widesky':
947 + $width = 160;
948 + $height = 600;
949 + break;
950 + case 'leaderboard':
951 + $width = 728;
952 + $height = 90;
953 + break;
954 + case 'mrec':
955 + default:
956 + $width = 300;
957 + $height = 250;
958 + break;
959 + }
960 +
961 + return <<<HTML
962 + <iframe
963 + src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
964 + width="$width"
965 + height="$height"
966 + frameborder="0"
967 + scrolling="no"
968 + marginheight="0"
969 + marginwidth="0">
970 + </iframe>
971 +HTML;
972 + }
973 +
974 + /**
975 + * Returns the html ad tag used by WordAds Tag Library
976 + *
977 + * @param string $slot_type e.g belowpost, gutenberg_rectangle.
978 + *
979 + * @return string
980 + *
981 + * @since 8.7
982 + */
983 + public static function get_watl_ad_html_tag( string $slot_type ): string {
984 + $uid = uniqid( 'atatags-dynamic-' . $slot_type . '-' );
985 +
986 + return <<<HTML
987 + <div style="padding-bottom:15px;" class="wordads-tag" data-slot-type="{$slot_type}">
988 + <div id="{$uid}">
989 + <script type="text/javascript">
990 + window.getAdSnippetCallback = function () {
991 + if ( false === ( window.isWatlV1 ?? false ) ) {
992 + // Use Aditude scripts.
993 + window.tudeMappings = window.tudeMappings || [];
994 + window.tudeMappings.push( {
995 + divId: '{$uid}',
996 + format: '{$slot_type}',
997 + } );
998 + }
999 + }
1000 +
1001 + if ( document.readyState === 'loading' ) {
1002 + document.addEventListener( 'DOMContentLoaded', window.getAdSnippetCallback );
1003 + } else {
1004 + window.getAdSnippetCallback();
1005 + }
1006 + </script>
1007 + </div>
1008 + </div>
1009 +HTML;
1010 + }
1011 +
1012 + /**
1013 + * Activation hook actions
1014 + *
1015 + * @since 4.5.0
1016 + */
1017 + public static function activate() {
1018 + WordAds_API::update_wordads_status_from_api();
1019 + }
1020 +
1021 + /**
1022 + * Registers the widgets.
1023 + */
1024 + public function widget_callback() {
1025 + register_widget( 'WordAds_Sidebar_Widget' );
1026 +
1027 + $ccpa_enabled = get_option( 'wordads_ccpa_enabled' );
1028 +
1029 + if ( $ccpa_enabled ) {
1030 + register_widget( 'WordAds_Ccpa_Do_Not_Sell_Link_Widget' );
1031 + }
1032 + }
1033 +}
1034 +
1035 +add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
1036 +add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
1037 +add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
1038 +
1039 +// The Ads tab of the Premium Analytics dashboard follows the module.
1040 +WordAds_Premium_Analytics::init();
1041 +
1042 +global $wordads;
1043 +$wordads = new WordAds();