PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | modules/wordads/class-wordads.php +193 -53 13.7.216.3-a.1 View file →
@@ -6,8 +6,12 @@
6 6 */
7 7
8 8 use Automattic\Jetpack\Status\Host;
9 9
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit( 0 );
12 +}
13 +
10 14 define( 'WORDADS_ROOT', __DIR__ );
11 15 define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
12 16 define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
13 17 define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
@@ -19,9 +23,11 @@
19 23 require_once WORDADS_ROOT . '/php/class-wordads-cron.php';
20 24 require_once WORDADS_ROOT . '/php/class-wordads-california-privacy.php';
21 25 require_once WORDADS_ROOT . '/php/class-wordads-ccpa-do-not-sell-link-widget.php';
22 26 require_once WORDADS_ROOT . '/php/class-wordads-consent-management-provider.php';
27 +require_once WORDADS_ROOT . '/php/class-wordads-formats.php';
23 28 require_once WORDADS_ROOT . '/php/class-wordads-smart.php';
29 +require_once WORDADS_ROOT . '/php/class-wordads-shortcode.php';
24 30
25 31 /**
26 32 * Primary WordAds class.
27 33 */
@@ -29,9 +35,9 @@
29 35
30 36 /**
31 37 * Ads parameters.
32 38 *
33 - * @var null
39 + * @var WordAds_Params
34 40 */
35 41 public $params = null;
36 42
37 43 /**
@@ -107,9 +113,9 @@
107 113 * Solo unit CSS string.
108 114 *
109 115 * @var string
110 116 */
111 - public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0px;';
117 + public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0;';
112 118
113 119 /**
114 120 * Checks for AMP support and returns true iff active & AMP request
115 121 *
@@ -214,8 +220,11 @@
214 220 if ( isset( $this->params->options['wordads_cmp_enabled'] ) && $this->params->options['wordads_cmp_enabled'] ) {
215 221 WordAds_Consent_Management_Provider::init();
216 222 }
217 223
224 + // Initialize [wordads] shortcode.
225 + WordAds_Shortcode::init();
226 +
218 227 // Initialize Smart.
219 228 WordAds_Smart::instance()->init( $this->params );
220 229
221 230 if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] )
@@ -239,12 +248,12 @@
239 248 * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
240 249 */
241 250 $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
242 251
243 - http_response_code( 200 );
252 + status_header( 200 );
244 253 header( 'Content-Type: text/plain; charset=utf-8' );
245 254 echo esc_html( $ads_txt_content );
246 - die();
255 + die( 0 );
247 256 }
248 257 }
249 258
250 259 /**
@@ -264,10 +273,13 @@
264 273 * @since 4.5.0
265 274 */
266 275 private function insert_adcode() {
267 276 add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
277 +
278 + // These 'wp_head' actions add the IPONWEB JavaScript snippets to the page.
279 + // We need to remove these calls whenever migration to Aditude is complete.
268 280 add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
269 - add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
281 +
270 282 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
271 283 add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
272 284
273 285 /**
@@ -315,8 +327,13 @@
315 327 *
316 328 * @since 4.5.0
317 329 */
318 330 public function enqueue_scripts() {
331 + // Ads are never shown on 404 pages.
332 + if ( is_404() ) {
333 + return;
334 + }
335 +
319 336 wp_enqueue_style(
320 337 'wordads',
321 338 WORDADS_URL . 'css/style.css',
322 339 array(),
@@ -334,9 +351,9 @@
334 351 *
335 352 * @return array Domains for hinting.
336 353 */
337 354 public function resource_hints( $hints, $relation_type ) {
338 - if ( 'dns-prefetch' === $relation_type ) {
355 + if ( 'dns-prefetch' === $relation_type && ! is_404() ) {
339 356 $hints[] = '//s.pubmine.com';
340 357 $hints[] = '//x.bidswitch.net';
341 358 $hints[] = '//static.criteo.net';
342 359 $hints[] = '//ib.adnxs.com';
@@ -361,9 +378,9 @@
361 378 /**
362 379 * IPONWEB metadata used by the various scripts
363 380 */
364 381 public function insert_head_meta() {
365 - if ( self::is_amp() ) {
382 + if ( self::is_amp() || is_404() ) {
366 383 return;
367 384 }
368 385 $hosting_type = ( new Host() )->is_woa_site() ? 1 : 2; // 1 = WPCOM, 2 = Jetpack.
369 386 $pagetype = (int) $this->params->get_page_type_ipw();
@@ -370,11 +387,45 @@
370 387 $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
371 388 $site_id = $this->params->blog_id;
372 389 $consent = (int) isset( $_COOKIE['personalized-ads-consent'] );
373 390 $is_logged_in = is_user_logged_in() ? '1' : '0';
391 +
392 + $disabled_slot_formats = apply_filters( 'wordads_disabled_slot_formats', array() );
393 +
394 + if ( apply_filters( 'wordads_iponweb_bottom_sticky_ad_disable', false ) ) {
395 + $disabled_slot_formats[] = 'MTS';
396 + }
397 +
398 + if ( apply_filters( 'wordads_iponweb_sidebar_sticky_right_ad_disable', false ) ) {
399 + $disabled_slot_formats[] = 'DPR';
400 + }
401 +
402 + $config = array(
403 + 'pt' => $pagetype,
404 + 'ht' => $hosting_type,
405 + 'tn' => get_stylesheet(),
406 + 'uloggedin' => $is_logged_in,
407 + 'amp' => false,
408 + 'siteid' => $site_id,
409 + 'consent' => $consent,
410 + 'ad' => array(
411 + 'label' => array(
412 + 'text' => __( 'Advertisements', 'jetpack' ),
413 + ),
414 + 'reportAd' => array(
415 + 'text' => __( 'Report this ad', 'jetpack' ),
416 + ),
417 + 'privacySettings' => array(
418 + 'text' => __( 'Privacy', 'jetpack' ),
419 + 'onClick' => 'js:function() { window.__tcfapi && window.__tcfapi(\'showUi\'); }',
420 + ),
421 + ),
422 + 'disabled_slot_formats' => $disabled_slot_formats,
423 + );
424 + $js_config = WordAds_Array_Utils::array_to_js_object( $config );
374 425 ?>
375 426 <script<?php echo esc_attr( $data_tags ); ?> type="text/javascript">
376 - var __ATA_PP = { pt: <?php echo esc_js( $pagetype ); ?>, ht: <?php echo esc_js( $hosting_type ); ?>, tn: '<?php echo esc_js( get_stylesheet() ); ?>', uloggedin: <?php echo esc_js( $is_logged_in ); ?>, amp: false, siteid: <?php echo esc_js( $site_id ); ?>, consent: <?php echo esc_js( $consent ); ?>, ad: { label: { text: '<?php echo esc_js( __( 'Advertisements', 'jetpack' ) ); ?>' }, reportAd: { text: '<?php echo esc_js( __( 'Report this ad', 'jetpack' ) ); ?>' }, privacySettings: { text: '<?php echo esc_js( __( 'Privacy', 'jetpack' ) ); ?>', onClick: function() { window.__tcfapi && window.__tcfapi('showUi'); } } } };
427 + var __ATA_PP = <?php echo $js_config; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>;
377 428 var __ATA = __ATA || {};
378 429 __ATA.cmd = __ATA.cmd || [];
379 430 __ATA.criteo = __ATA.criteo || {};
380 431 __ATA.criteo.cmd = __ATA.criteo.cmd || [];
@@ -380,47 +431,42 @@
380 431 __ATA.criteo.cmd = __ATA.criteo.cmd || [];
381 432 </script>
382 433 <?php
383 434
435 + $section_id = $this->params->blog_id . 5;
436 +
437 + // Get below post tag.
438 + $tag_belowpost = $this->get_fallback_ad_snippet( $section_id, 'square', 'belowpost', '', '{{unique_id}}' );
439 +
440 + // Remove linebreaks and sanitize.
441 + $tag_belowpost = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_belowpost ) );
442 +
384 443 // Get an inline tag with a macro as id handled on JS side to use as a fallback.
385 - $tag_inline = $this->get_dynamic_ad_snippet( $this->params->blog_id . 5, 'square', 'inline', '', '{{unique_id}}' );
444 + $tag_inline = $this->get_fallback_ad_snippet( $section_id, 'square', 'inline', '', '{{unique_id}}' );
386 445
387 446 // Remove linebreaks and sanitize.
388 447 $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) );
389 448
449 + // Get top tag.
450 + $tag_top = $this->get_fallback_ad_snippet( $section_id, 'leaderboard', 'top', '', '{{unique_id}}' );
451 +
452 + // Remove linebreaks and sanitize.
453 + $tag_top = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_top ) );
454 +
390 455 // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
391 456 echo <<<HTML
392 - <script>
393 - var sas_fallback = sas_fallback || [];
394 - sas_fallback.push(
395 - { tag: "$tag_inline", type: 'inline' }
396 - );
397 - </script>
457 + <script type="text/javascript">
458 + window.sas_fallback = window.sas_fallback || [];
459 + window.sas_fallback.push(
460 + { tag: "$tag_inline", type: 'inline' },
461 + { tag: "$tag_belowpost", type: 'belowpost' },
462 + { tag: "$tag_top", type: 'top' }
463 + );
464 + </script>
398 465 HTML;
399 466 }
400 467
401 468 /**
402 - * IPONWEB scripts in <head>
403 - *
404 - * @since 4.5.0
405 - */
406 - public function insert_head_iponweb() {
407 - if ( self::is_amp() ) {
408 - return;
409 - }
410 -
411 - $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
412 - ?>
413 - <script<?php echo esc_attr( $data_tags ); ?> type="text/javascript">
414 - (function(){var g=Date.now||function(){return+new Date};function h(a,b){a:{for(var c=a.length,d="string"==typeof a?a.split(""):a,e=0;e<c;e++)if(e in d&&b.call(void 0,d[e],e,a)){b=e;break a}b=-1}return 0>b?null:"string"==typeof a?a.charAt(b):a[b]};function k(a,b,c){c=null!=c?"="+encodeURIComponent(String(c)):"";if(b+=c){c=a.indexOf("#");0>c&&(c=a.length);var d=a.indexOf("?");if(0>d||d>c){d=c;var e=""}else e=a.substring(d+1,c);a=[a.substr(0,d),e,a.substr(c)];c=a[1];a[1]=b?c?c+"&"+b:b:c;a=a[0]+(a[1]?"?"+a[1]:"")+a[2]}return a};var l=0;function m(a,b){var c=document.createElement("script");c.src=a;c.onload=function(){b&&b(void 0)};c.onerror=function(){b&&b("error")};a=document.getElementsByTagName("head");var d;a&&0!==a.length?d=a[0]:d=document.documentElement;d.appendChild(c)}function n(a){var b=void 0===b?document.cookie:b;return(b=h(b.split("; "),function(c){return-1!=c.indexOf(a+"=")}))?b.split("=")[1]:""}function p(a){return"string"==typeof a&&0<a.length}
415 - function r(a,b,c){b=void 0===b?"":b;c=void 0===c?".":c;var d=[];Object.keys(a).forEach(function(e){var f=a[e],q=typeof f;"object"==q&&null!=f||"function"==q?d.push(r(f,b+e+c)):null!==f&&void 0!==f&&(e=encodeURIComponent(b+e),d.push(e+"="+encodeURIComponent(f)))});return d.filter(p).join("&")}function t(a,b){a||((window.__ATA||{}).config=b.c,m(b.url))}var u=Math.floor(1E13*Math.random()),v=window.__ATA||{};window.__ATA=v;window.__ATA.cmd=v.cmd||[];v.rid=u;v.createdAt=g();var w=window.__ATA||{},x="s.pubmine.com";
416 - w&&w.serverDomain&&(x=w.serverDomain);var y="//"+x+"/conf",z=window.top===window,A=window.__ATA_PP&&window.__ATA_PP.gdpr_applies,B="boolean"===typeof A?Number(A):null,C=window.__ATA_PP||null,D=z?document.referrer?document.referrer:null:null,E=z?window.location.href:document.referrer?document.referrer:null,F,G=n("__ATA_tuuid");F=G?G:null;var H=window.innerWidth+"x"+window.innerHeight,I=n("usprivacy"),J=r({gdpr:B,pp:C,rid:u,src:D,ref:E,tuuid:F,vp:H,us_privacy:I?I:null},"",".");
417 - (function(a){var b=void 0===b?"cb":b;l++;var c="callback__"+g().toString(36)+"_"+l.toString(36);a=k(a,b,c);window[c]=function(d){t(void 0,d)};m(a,function(d){d&&t(d)})})(y+"?"+J);}).call(this);
418 - </script>
419 - <?php
420 - }
421 -
422 - /**
423 469 * Insert the ad onto the page
424 470 *
425 471 * @since 4.5.0
426 472 *
@@ -476,10 +522,17 @@
476 522 return $content;
477 523 }
478 524
479 525 $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
480 - $content .= $this->get_ad( 'inline', $ad_type );
481 - return $content;
526 + $location = 'shortcode';
527 +
528 + // Not house ad and WATL enabled.
529 + // phpcs:disable WordPress.Security.NonceVerification.Recommended
530 + if ( 'house' !== $ad_type && ( isset( $_GET['wordads-logging'] ) && isset( $_GET[ $location ] ) && 'true' === $_GET[ $location ] ) ) {
531 + return $content . $this->get_watl_ad_html_tag( $location );
532 + }
533 +
534 + return $content . $this->get_ad( 'inline', $ad_type );
482 535 }
483 536
484 537 /**
485 538 * Inserts ad into header
@@ -588,9 +641,8 @@
588 641 public function get_ad( $spot, $type = 'iponweb' ) {
589 642 $snippet = '';
590 643 if ( 'iponweb' === $type ) {
591 644 $section_id = WORDADS_API_TEST_ID;
592 - $snippet = '';
593 645
594 646 if ( 'top' === $spot ) {
595 647 // mrec for mobile, leaderboard for desktop.
596 648 $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
@@ -676,21 +728,52 @@
676 728 $loc_id = self::$ad_location_ids[ $location ];
677 729 }
678 730
679 731 $loc_id = esc_js( $loc_id );
732 +
733 + // Determine supported Gutenberg format by width and height.
734 + $format = WordAds_Formats::get_format_slug( (int) $width, (int) $height );
735 + if ( $format === '' ) {
736 + return ''; // When format is not supported, ignore placement.
737 + }
738 +
739 + // For a while return elements for both IPONWEB (__ATA) and Aditude (tudeMappings).
740 + // We will remove using IPONWEB after partnership termination on June 30, 2025.
680 741 return <<<HTML
681 742 <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
682 743 <div id="atatags-{$ad_number}">
683 744 <script$data_tags type="text/javascript">
684 - __ATA.cmd.push(function() {
685 - __ATA.initSlot('atatags-{$ad_number}', {
686 - collapseEmpty: 'before',
687 - sectionId: '{$section_id}',
688 - location: {$loc_id},
689 - width: {$width},
690 - height: {$height}
691 - });
692 - });
745 + window.getAdSnippetCallback = function () {
746 + if ( true === ( window.isWatlV1 ?? false ) ) {
747 + // Use IPONWEB scripts.
748 + window.__ATA = window.__ATA || {};
749 + window.__ATA.cmd = __ATA.cmd || [];
750 + window.__ATA.cmd.push(function() {
751 + window.__ATA.initSlot('atatags-{$ad_number}', {
752 + collapseEmpty: 'before',
753 + sectionId: '{$section_id}',
754 + location: {$loc_id},
755 + width: {$width},
756 + height: {$height}
757 + });
758 + });
759 + } else {
760 + // Use Aditude scripts.
761 + window.tudeMappings = window.tudeMappings || [];
762 + window.tudeMappings.push( {
763 + divId: 'atatags-{$ad_number}',
764 + format: '{$format}',
765 + width: {$width},
766 + height: {$height},
767 + } );
768 + }
769 + }
770 +
771 + if ( document.readyState === 'loading' ) {
772 + document.addEventListener( 'DOMContentLoaded', window.getAdSnippetCallback );
773 + } else {
774 + window.getAdSnippetCallback();
775 + }
693 776 </script>
694 777 </div>
695 778 </div>
696 779 HTML;
@@ -719,8 +802,31 @@
719 802 *
720 803 * @since 8.7
721 804 */
722 805 public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) {
806 + $is_location_enabled = in_array( $location, array( 'top', 'belowpost', 'inline' ), true );
807 +
808 + if ( $is_location_enabled ) {
809 + return self::get_watl_ad_html_tag( $location );
810 + }
811 +
812 + return $this->get_fallback_ad_snippet( $section_id, $form_factor, $location, $relocate, $id );
813 + }
814 +
815 + /**
816 + * Returns the fallback dynamic snippet to be inserted into the ad unit
817 + *
818 + * @param int $section_id section_id.
819 + * @param string $form_factor form_factor.
820 + * @param string $location location.
821 + * @param string $relocate location to be moved after the fact for themes without required hook.
822 + * @param string | null $id A unique string ID or placeholder.
823 + *
824 + * @return string
825 + *
826 + * @since 8.7
827 + */
828 + public function get_fallback_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '', $id = null ) {
723 829 $div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() );
724 830 $div_id = esc_attr( $div_id );
725 831
726 832 // Default form factor.
@@ -740,9 +846,9 @@
740 846 $privacy_settings_text = esc_js( __( 'Privacy settings', 'jetpack' ) );
741 847
742 848 $relocate_script = '';
743 849 if ( ! empty( $relocate ) ) {
744 - $selector = wp_json_encode( $relocate );
850 + $selector = wp_json_encode( $relocate, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
745 851 $relocate_script = <<<JS
746 852 <script type="text/javascript">
747 853 var adNode = document.getElementById( '{$div_id}' );
748 854 var selector = {$selector};
@@ -793,12 +899,8 @@
793 899 if ( strpos( strtolower( $spot ), 'amp' ) === false && ! 'inline' === $spot ) {
794 900 return $snippet; // we don't want dynamic ads to be inserted for AMP & Gutenberg.
795 901 }
796 902
797 - if ( empty( $css_classes ) ) {
798 - $css_classes = array();
799 - }
800 -
801 903 $css_classes[] = 'wpcnt';
802 904 if ( 'top' === $spot ) {
803 905 $css_classes[] = 'wpcnt-header';
804 906 }
@@ -864,8 +966,46 @@
864 966 scrolling="no"
865 967 marginheight="0"
866 968 marginwidth="0">
867 969 </iframe>
970 +HTML;
971 + }
972 +
973 + /**
974 + * Returns the html ad tag used by WordAds Tag Library
975 + *
976 + * @param string $slot_type e.g belowpost, gutenberg_rectangle.
977 + *
978 + * @return string
979 + *
980 + * @since 8.7
981 + */
982 + public static function get_watl_ad_html_tag( string $slot_type ): string {
983 + $uid = uniqid( 'atatags-dynamic-' . $slot_type . '-' );
984 +
985 + return <<<HTML
986 + <div style="padding-bottom:15px;" class="wordads-tag" data-slot-type="{$slot_type}">
987 + <div id="{$uid}">
988 + <script type="text/javascript">
989 + window.getAdSnippetCallback = function () {
990 + if ( false === ( window.isWatlV1 ?? false ) ) {
991 + // Use Aditude scripts.
992 + window.tudeMappings = window.tudeMappings || [];
993 + window.tudeMappings.push( {
994 + divId: '{$uid}',
995 + format: '{$slot_type}',
996 + } );
997 + }
998 + }
999 +
1000 + if ( document.readyState === 'loading' ) {
1001 + document.addEventListener( 'DOMContentLoaded', window.getAdSnippetCallback );
1002 + } else {
1003 + window.getAdSnippetCallback();
1004 + }
1005 + </script>
1006 + </div>
1007 + </div>
868 1008 HTML;
869 1009 }
870 1010
871 1011 /**