summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/wordads/wordads.php')
-rw-r--r--plugins/jetpack/modules/wordads/wordads.php153
1 files changed, 125 insertions, 28 deletions
diff --git a/plugins/jetpack/modules/wordads/wordads.php b/plugins/jetpack/modules/wordads/wordads.php
index 43a4e7d8..3fc03fb1 100644
--- a/plugins/jetpack/modules/wordads/wordads.php
+++ b/plugins/jetpack/modules/wordads/wordads.php
@@ -61,6 +61,32 @@ class WordAds {
'inline-plugin' => 320,
);
+ /**
+ * Counter to enable unique, sequential section IDs for all amp-ad units
+ *
+ * @var int
+ */
+ public static $amp_section_id = 1;
+
+ /**
+ * Checks for AMP support and returns true iff active & AMP request
+ * @return boolean True if supported AMP request
+ *
+ * @since 7.5.0
+ */
+ public static function is_amp() {
+ return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
+ }
+
+ /**
+ * Increment the AMP section ID and return the value
+ *
+ * @return int
+ */
+ public static function get_amp_section_id() {
+ return self::$amp_section_id++;
+ }
+
public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;';
/**
@@ -106,7 +132,8 @@ class WordAds {
* @since 4.5.0
*/
function __construct() {
- add_action( 'init', array( $this, 'init' ) );
+ add_action( 'wp', array( $this, 'init' ) );
+ add_action( 'rest_api_init', array( $this, 'init' ) );
}
/**
@@ -170,6 +197,7 @@ class WordAds {
* @since 4.5.0
*/
private function insert_adcode() {
+ add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
@@ -206,15 +234,19 @@ class WordAds {
}
if ( $this->option( 'enable_header_ad', true ) ) {
- switch ( get_stylesheet() ) {
- case 'twentyseventeen':
- case 'twentyfifteen':
- case 'twentyfourteen':
- add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
- break;
- default:
- add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
- break;
+ if ( self::is_amp() ) {
+ add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) );
+ } else {
+ switch ( get_stylesheet() ) {
+ case 'twentyseventeen':
+ case 'twentyfifteen':
+ case 'twentyfourteen':
+ add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
+ break;
+ default:
+ add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
+ break;
+ }
}
}
}
@@ -234,11 +266,43 @@ class WordAds {
}
/**
+ * Add the IPW resource hints
+ *
+ * @since 7.9
+ */
+ public function resource_hints( $hints, $relation_type ) {
+ if ( 'dns-prefetch' === $relation_type ) {
+ $hints[] = '//s.pubmine.com';
+ $hints[] = '//x.bidswitch.net';
+ $hints[] = '//static.criteo.net';
+ $hints[] = '//ib.adnxs.com';
+ $hints[] = '//aax.amazon-adsystem.com';
+ $hints[] = '//bidder.criteo.com';
+ $hints[] = '//cas.criteo.com';
+ $hints[] = '//gum.criteo.com';
+ $hints[] = '//ads.pubmatic.com';
+ $hints[] = '//gads.pubmatic.com';
+ $hints[] = '//tpc.googlesyndication.com';
+ $hints[] = '//ad.doubleclick.net';
+ $hints[] = '//googleads.g.doubleclick.net';
+ $hints[] = '//www.googletagservices.com';
+ $hints[] = '//cdn.switchadhub.com';
+ $hints[] = '//delivery.g.switchadhub.com';
+ $hints[] = '//delivery.swid.switchadhub.com';
+ }
+
+ return $hints;
+ }
+
+ /**
* IPONWEB metadata used by the various scripts
*
* @return [type] [description]
*/
function insert_head_meta() {
+ if ( self::is_amp() ) {
+ return;
+ }
$themename = esc_js( get_stylesheet() );
$pagetype = intval( $this->params->get_page_type_ipw() );
$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
@@ -261,23 +325,18 @@ HTML;
* @since 4.5.0
*/
function insert_head_iponweb() {
+ if ( self::is_amp() ) {
+ return;
+ }
$data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
echo <<<HTML
- <link rel='dns-prefetch' href='//s.pubmine.com' />
- <link rel='dns-prefetch' href='//x.bidswitch.net' />
- <link rel='dns-prefetch' href='//static.criteo.net' />
- <link rel='dns-prefetch' href='//ib.adnxs.com' />
- <link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
- <link rel='dns-prefetch' href='//bidder.criteo.com' />
- <link rel='dns-prefetch' href='//cas.criteo.com' />
- <link rel='dns-prefetch' href='//gum.criteo.com' />
- <link rel='dns-prefetch' href='//ads.pubmatic.com' />
- <link rel='dns-prefetch' href='//gads.pubmatic.com' />
- <link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
- <link rel='dns-prefetch' href='//ad.doubleclick.net' />
- <link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
- <link rel='dns-prefetch' href='//www.googletagservices.com' />
- <script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
+ <script$data_tags type="text/javascript">
+ (function(){function g(a,c){a:{for(var b=a.length,d="string"==typeof a?a.split(""):a,e=0;e<b;e++)if(e in d&&c.call(void 0,d[e],e,a)){c=e;break a}c=-1}return 0>c?null:"string"==typeof a?a.charAt(c):a[c]};function h(a,c,b){b=null!=b?"="+encodeURIComponent(String(b)):"";if(c+=b){b=a.indexOf("#");0>b&&(b=a.length);var d=a.indexOf("?");if(0>d||d>b){d=b;var e=""}else e=a.substring(d+1,b);a=[a.substr(0,d),e,a.substr(b)];b=a[1];a[1]=c?b?b+"&"+c:c:b;a=a[0]+(a[1]?"?"+a[1]:"")+a[2]}return a};var k=0;function l(a,c){var b=document.createElement("script");b.src=a;b.onload=function(){c&&c(void 0)};b.onerror=function(){c("error")};a=document.getElementsByTagName("head");var d;a&&0!==a.length?d=a[0]:d=document.documentElement;d.appendChild(b)}function m(a){return"string"==typeof a&&0<a.length}
+ function p(a,c,b){c=void 0===c?"":c;b=void 0===b?".":b;var d=[];Object.keys(a).forEach(function(e){var f=a[e],n=typeof f;"object"==n&&null!=f||"function"==n?d.push(p(f,c+e+b)):null!==f&&void 0!==f&&(e=encodeURIComponent(c+e),d.push(e+"="+encodeURIComponent(f)))});return d.filter(m).join("&")}function q(){return window.__ATA||{}}function r(a,c){a||(q().config=c.c,l(c.url))}var t=Math.floor(1E13*Math.random());q().rid=t;
+ var u=q().pageParams,v="//"+(q().serverDomain||"s.pubmine.com")+"/conf",w=window.top===window,x;try{var y=JSON.parse(document.getElementById("oil-configuration").innerText);if("boolean"!==typeof y.gdpr_applies)throw Error("Config doesn't contain gdpr_applies");x=y.gdpr_applies?1:0}catch(a){x=null}
+ var z=x,A=window.__ATA_PP||u||null,B=w?document.referrer?document.referrer:null:null,C=w?null:document.referrer?document.referrer:null,D=function(){var a=void 0===a?document.cookie:a;return(a=g(a.split("; "),function(c){return-1!=c.indexOf("__ATA_tuuid=")}))?a.split("=")[1]:""}(),E=p({gdpr:z,pp:A,rid:t,src:B,ref:C,tuuid:D?D:null,vp:window.innerWidth+"x"+window.innerHeight},"",".");
+ (function(a){var c;k++;var b="callback__"+Date.now().toString(36)+"_"+k.toString(36);a=h(a,void 0===c?"cb":c,b);window[b]=function(d){r(void 0,d)};l(a,function(d){d&&r(d)})})(v+"?"+E);}).call(this);
+ </script>
HTML;
}
@@ -396,11 +455,30 @@ HTML;
$ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
echo $this->get_ad( 'top', $ad_type );
- echo <<<HTML
+ if ( ! self::is_amp() ) {
+ echo <<<HTML
<script type="text/javascript">
jQuery('.wpcnt-header').insertBefore('$selector');
</script>
HTML;
+ }
+ }
+
+ /**
+ * Header unit for AMP
+ *
+ * @param string $content Content of the page.
+ *
+ * @since 7.5.0
+ */
+ public function insert_header_ad_amp( $content ) {
+
+ $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
+ if ( 'house' === $ad_type ) {
+ return $content;
+ }
+ return $this->get_ad( 'top_amp', $ad_type ) . $content;
+
}
/**
@@ -456,6 +534,11 @@ HTML;
} elseif ( 'inline' === $spot ) {
$section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
$snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
+ } elseif ( 'top_amp' === $spot ) {
+ // 320x50 unit which can safely be inserted below title, above content in a variety of themes.
+ $width = 320;
+ $height = 50;
+ $snippet = $this->get_ad_snippet( null, $height, $width );
}
} elseif ( 'house' == $type ) {
$leaderboard = 'top' == $spot && ! $this->params->mobile_device;
@@ -481,14 +564,28 @@ HTML;
*
* @since 5.7
*/
- function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
+ public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
$this->ads[] = array(
'location' => $location,
'width' => $width,
'height' => $height,
);
- $ad_number = count( $this->ads ) . '-' . uniqid();
+ if ( self::is_amp() ) {
+ $height = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad"
+ $width = esc_attr( $width );
+ $amp_section_id = esc_attr( self::get_amp_section_id() );
+ $site_id = esc_attr( $this->params->blog_id );
+ return <<<HTML
+ <amp-ad width="$width" height="$height"
+ type="pubmine"
+ data-siteid="$site_id"
+ data-section="$amp_section_id">
+ </amp-ad>
+HTML;
+ }
+
+ $ad_number = count( $this->ads ) . '-' . uniqid();
$data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
$css = esc_attr( $css );