PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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 13.7.2 13.8.3 All 506 releases
← All changes | modules/wordads/class-wordads.php +197 -53 13.7.2 → 16.3-a.7 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,12 @@
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';
30 +require_once WORDADS_ROOT . '/php/class-wordads-premium-analytics.php';
24 31
25 32 /**
26 33 * Primary WordAds class.
27 34 */
@@ -29,9 +36,9 @@
29 36
30 37 /**
31 38 * Ads parameters.
32 39 *
33 - * @var null
40 + * @var WordAds_Params
34 41 */
35 42 public $params = null;
36 43
37 44 /**
@@ -107,9 +114,9 @@
107 114 * Solo unit CSS string.
108 115 *
109 116 * @var string
110 117 */
111 - public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0px;';
118 + public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0;';
112 119
113 120 /**
114 121 * Checks for AMP support and returns true iff active & AMP request
115 122 *
@@ -214,8 +221,11 @@
214 221 if ( isset( $this->params->options['wordads_cmp_enabled'] ) && $this->params->options['wordads_cmp_enabled'] ) {
215 222 WordAds_Consent_Management_Provider::init();
216 223 }
217 224
225 + // Initialize [wordads] shortcode.
226 + WordAds_Shortcode::init();
227 +
218 228 // Initialize Smart.
219 229 WordAds_Smart::instance()->init( $this->params );
220 230
221 231 if ( ( isset( $_SERVER['REQUEST_URI'] ) && '/ads.txt' === $_SERVER['REQUEST_URI'] )
@@ -239,12 +249,12 @@
239 249 * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
240 250 */
241 251 $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
242 252
243 - http_response_code( 200 );
253 + status_header( 200 );
244 254 header( 'Content-Type: text/plain; charset=utf-8' );
245 255 echo esc_html( $ads_txt_content );
246 - die();
256 + die( 0 );
247 257 }
248 258 }
249 259
250 260 /**
@@ -264,10 +274,13 @@
264 274 * @since 4.5.0
265 275 */
266 276 private function insert_adcode() {
267 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.
268 281 add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
269 - add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
282 +
270 283 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
271 284 add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
272 285
273 286 /**
@@ -315,8 +328,13 @@
315 328 *
316 329 * @since 4.5.0
317 330 */
318 331 public function enqueue_scripts() {
332 + // Ads are never shown on 404 pages.
333 + if ( is_404() ) {
334 + return;
335 + }
336 +
319 337 wp_enqueue_style(
320 338 'wordads',
321 339 WORDADS_URL . 'css/style.css',
322 340 array(),
@@ -334,9 +352,9 @@
334 352 *
335 353 * @return array Domains for hinting.
336 354 */
337 355 public function resource_hints( $hints, $relation_type ) {
338 - if ( 'dns-prefetch' === $relation_type ) {
356 + if ( 'dns-prefetch' === $relation_type && ! is_404() ) {
339 357 $hints[] = '//s.pubmine.com';
340 358 $hints[] = '//x.bidswitch.net';
341 359 $hints[] = '//static.criteo.net';
342 360 $hints[] = '//ib.adnxs.com';
@@ -361,9 +379,9 @@
361 379 /**
362 380 * IPONWEB metadata used by the various scripts
363 381 */
364 382 public function insert_head_meta() {
365 - if ( self::is_amp() ) {
383 + if ( self::is_amp() || is_404() ) {
366 384 return;
367 385 }
368 386 $hosting_type = ( new Host() )->is_woa_site() ? 1 : 2; // 1 = WPCOM, 2 = Jetpack.
369 387 $pagetype = (int) $this->params->get_page_type_ipw();
@@ -370,11 +388,45 @@
370 388 $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
371 389 $site_id = $this->params->blog_id;
372 390 $consent = (int) isset( $_COOKIE['personalized-ads-consent'] );
373 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 );
374 426 ?>
375 427 <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'); } } } };
428 + var __ATA_PP = <?php echo $js_config; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>;
377 429 var __ATA = __ATA || {};
378 430 __ATA.cmd = __ATA.cmd || [];
379 431 __ATA.criteo = __ATA.criteo || {};
380 432 __ATA.criteo.cmd = __ATA.criteo.cmd || [];
@@ -380,47 +432,42 @@
380 432 __ATA.criteo.cmd = __ATA.criteo.cmd || [];
381 433 </script>
382 434 <?php
383 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 +
384 444 // 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}}' );
445 + $tag_inline = $this->get_fallback_ad_snippet( $section_id, 'square', 'inline', '', '{{unique_id}}' );
386 446
387 447 // Remove linebreaks and sanitize.
388 448 $tag_inline = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $tag_inline ) );
389 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 +
390 456 // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
391 457 echo <<<HTML
392 - <script>
393 - var sas_fallback = sas_fallback || [];
394 - sas_fallback.push(
395 - { tag: "$tag_inline", type: 'inline' }
396 - );
397 - </script>
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>
398 466 HTML;
399 467 }
400 468
401 469 /**
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 470 * Insert the ad onto the page
424 471 *
425 472 * @since 4.5.0
426 473 *
@@ -476,10 +523,17 @@
476 523 return $content;
477 524 }
478 525
479 526 $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
480 - $content .= $this->get_ad( 'inline', $ad_type );
481 - return $content;
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 );
482 536 }
483 537
484 538 /**
485 539 * Inserts ad into header
@@ -588,9 +642,8 @@
588 642 public function get_ad( $spot, $type = 'iponweb' ) {
589 643 $snippet = '';
590 644 if ( 'iponweb' === $type ) {
591 645 $section_id = WORDADS_API_TEST_ID;
592 - $snippet = '';
593 646
594 647 if ( 'top' === $spot ) {
595 648 // mrec for mobile, leaderboard for desktop.
596 649 $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
@@ -676,21 +729,52 @@
676 729 $loc_id = self::$ad_location_ids[ $location ];
677 730 }
678 731
679 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.
680 742 return <<<HTML
681 743 <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
682 744 <div id="atatags-{$ad_number}">
683 745 <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 - });
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 + }
693 777 </script>
694 778 </div>
695 779 </div>
696 780 HTML;
@@ -719,8 +803,31 @@
719 803 *
720 804 * @since 8.7
721 805 */
722 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 ) {
723 830 $div_id = 'atatags-' . $section_id . '-' . ( $id ?? uniqid() );
724 831 $div_id = esc_attr( $div_id );
725 832
726 833 // Default form factor.
@@ -740,9 +847,9 @@
740 847 $privacy_settings_text = esc_js( __( 'Privacy settings', 'jetpack' ) );
741 848
742 849 $relocate_script = '';
743 850 if ( ! empty( $relocate ) ) {
744 - $selector = wp_json_encode( $relocate );
851 + $selector = wp_json_encode( $relocate, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
745 852 $relocate_script = <<<JS
746 853 <script type="text/javascript">
747 854 var adNode = document.getElementById( '{$div_id}' );
748 855 var selector = {$selector};
@@ -793,12 +900,8 @@
793 900 if ( strpos( strtolower( $spot ), 'amp' ) === false && ! 'inline' === $spot ) {
794 901 return $snippet; // we don't want dynamic ads to be inserted for AMP & Gutenberg.
795 902 }
796 903
797 - if ( empty( $css_classes ) ) {
798 - $css_classes = array();
799 - }
800 -
801 904 $css_classes[] = 'wpcnt';
802 905 if ( 'top' === $spot ) {
803 906 $css_classes[] = 'wpcnt-header';
804 907 }
@@ -868,8 +971,46 @@
868 971 HTML;
869 972 }
870 973
871 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 + /**
872 1013 * Activation hook actions
873 1014 *
874 1015 * @since 4.5.0
875 1016 */
@@ -893,7 +1034,10 @@
893 1034
894 1035 add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
895 1036 add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
896 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();
897 1041
898 1042 global $wordads;
899 1043 $wordads = new WordAds();