wordads.php
| 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 | define( 'WORDADS_API_TEST_ID2', '114160' ); |
| 9 | |
| 10 | require_once WORDADS_ROOT . '/php/widgets.php'; |
| 11 | require_once WORDADS_ROOT . '/php/api.php'; |
| 12 | require_once WORDADS_ROOT . '/php/cron.php'; |
| 13 | |
| 14 | class WordAds { |
| 15 | |
| 16 | public $params = null; |
| 17 | |
| 18 | public $ads = array(); |
| 19 | |
| 20 | /** |
| 21 | * Array of supported ad types. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | public static $ad_tag_ids = array( |
| 26 | 'mrec' => array( |
| 27 | 'tag' => '300x250_mediumrectangle', |
| 28 | 'height' => '250', |
| 29 | 'width' => '300', |
| 30 | ), |
| 31 | 'leaderboard' => array( |
| 32 | 'tag' => '728x90_leaderboard', |
| 33 | 'height' => '90', |
| 34 | 'width' => '728', |
| 35 | ), |
| 36 | 'mobile_leaderboard' => array( |
| 37 | 'tag' => '320x50_mobileleaderboard', |
| 38 | 'height' => '50', |
| 39 | 'width' => '320', |
| 40 | ), |
| 41 | 'wideskyscraper' => array( |
| 42 | 'tag' => '160x600_wideskyscraper', |
| 43 | 'height' => '600', |
| 44 | 'width' => '160', |
| 45 | ), |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * Mapping array of location slugs to placement ids |
| 50 | * |
| 51 | * @var array |
| 52 | */ |
| 53 | public static $ad_location_ids = array( |
| 54 | 'top' => 110, |
| 55 | 'belowpost' => 120, |
| 56 | 'belowpost2' => 130, |
| 57 | 'sidebar' => 140, |
| 58 | 'widget' => 150, |
| 59 | 'gutenberg' => 200, |
| 60 | 'inline' => 310, |
| 61 | 'inline-plugin' => 320, |
| 62 | ); |
| 63 | |
| 64 | /** |
| 65 | * Counter to enable unique, sequential section IDs for all amp-ad units |
| 66 | * |
| 67 | * @var int |
| 68 | */ |
| 69 | public static $amp_section_id = 1; |
| 70 | |
| 71 | /** |
| 72 | * Checks for AMP support and returns true iff active & AMP request |
| 73 | * @return boolean True if supported AMP request |
| 74 | * |
| 75 | * @since 7.5.0 |
| 76 | */ |
| 77 | public static function is_amp() { |
| 78 | return class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Increment the AMP section ID and return the value |
| 83 | * |
| 84 | * @return int |
| 85 | */ |
| 86 | public static function get_amp_section_id() { |
| 87 | return self::$amp_section_id++; |
| 88 | } |
| 89 | |
| 90 | public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;'; |
| 91 | |
| 92 | /** |
| 93 | * Convenience function for grabbing options from params->options |
| 94 | * |
| 95 | * @param string $option the option to grab |
| 96 | * @param mixed $default (optional) |
| 97 | * @return option or $default if not set |
| 98 | * |
| 99 | * @since 4.5.0 |
| 100 | */ |
| 101 | function option( $option, $default = false ) { |
| 102 | if ( ! isset( $this->params->options[ $option ] ) ) { |
| 103 | return $default; |
| 104 | } |
| 105 | |
| 106 | return $this->params->options[ $option ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the ad tag property array for supported ad types. |
| 111 | * @return array array with ad tags |
| 112 | * |
| 113 | * @since 7.1.0 |
| 114 | */ |
| 115 | function get_ad_tags() { |
| 116 | return self::$ad_tag_ids; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the solo css for unit |
| 121 | * @return string the special css for solo units |
| 122 | * |
| 123 | * @since 7.1.0 |
| 124 | */ |
| 125 | function get_solo_unit_css() { |
| 126 | return self::$SOLO_UNIT_CSS; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Instantiate the plugin |
| 131 | * |
| 132 | * @since 4.5.0 |
| 133 | */ |
| 134 | function __construct() { |
| 135 | add_action( 'wp', array( $this, 'init' ) ); |
| 136 | add_action( 'rest_api_init', array( $this, 'init' ) ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Code to run on WordPress 'init' hook |
| 141 | * |
| 142 | * @since 4.5.0 |
| 143 | */ |
| 144 | function init() { |
| 145 | require_once WORDADS_ROOT . '/php/params.php'; |
| 146 | $this->params = new WordAds_Params(); |
| 147 | |
| 148 | if ( $this->should_bail() || self::is_infinite_scroll() ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if ( is_admin() ) { |
| 153 | require_once WORDADS_ROOT . '/php/admin.php'; |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $this->insert_adcode(); |
| 158 | |
| 159 | if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) { |
| 160 | |
| 161 | if ( false === ( $ads_txt_transient = get_transient( 'jetpack_ads_txt' ) ) ) { |
| 162 | $ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : ''; |
| 163 | set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file. |
| 168 | * |
| 169 | * @module wordads |
| 170 | * |
| 171 | * @since 6.1.0 |
| 172 | * |
| 173 | * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file. |
| 174 | */ |
| 175 | $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient ); |
| 176 | |
| 177 | header( 'Content-Type: text/plain; charset=utf-8' ); |
| 178 | echo esc_html( $ads_txt_content ); |
| 179 | die(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
| 185 | * |
| 186 | * @return boolean true if load came from infinite scroll |
| 187 | * |
| 188 | * @since 4.5.0 |
| 189 | */ |
| 190 | public static function is_infinite_scroll() { |
| 191 | return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
| 196 | * |
| 197 | * @since 4.5.0 |
| 198 | */ |
| 199 | private function insert_adcode() { |
| 200 | add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 ); |
| 201 | add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 ); |
| 202 | add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 ); |
| 203 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 204 | add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) ); |
| 205 | |
| 206 | /** |
| 207 | * Filters enabling ads in `the_content` filter |
| 208 | * |
| 209 | * @see https://jetpack.com/support/ads/ |
| 210 | * |
| 211 | * @module wordads |
| 212 | * |
| 213 | * @since 5.8.0 |
| 214 | * |
| 215 | * @param bool True to disable ads in `the_content` |
| 216 | */ |
| 217 | if ( ! apply_filters( 'wordads_content_disable', false ) ) { |
| 218 | add_filter( 'the_content', array( $this, 'insert_ad' ) ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Filters enabling ads in `the_excerpt` filter |
| 223 | * |
| 224 | * @see https://jetpack.com/support/ads/ |
| 225 | * |
| 226 | * @module wordads |
| 227 | * |
| 228 | * @since 5.8.0 |
| 229 | * |
| 230 | * @param bool True to disable ads in `the_excerpt` |
| 231 | */ |
| 232 | if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) { |
| 233 | add_filter( 'the_excerpt', array( $this, 'insert_ad' ) ); |
| 234 | } |
| 235 | |
| 236 | if ( $this->option( 'enable_header_ad', true ) ) { |
| 237 | if ( self::is_amp() ) { |
| 238 | add_filter( 'the_content', array( $this, 'insert_header_ad_amp' ) ); |
| 239 | } else { |
| 240 | switch ( get_stylesheet() ) { |
| 241 | case 'twentyseventeen': |
| 242 | case 'twentyfifteen': |
| 243 | case 'twentyfourteen': |
| 244 | add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) ); |
| 245 | break; |
| 246 | default: |
| 247 | add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 ); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Register desktop scripts and styles |
| 256 | * |
| 257 | * @since 4.5.0 |
| 258 | */ |
| 259 | function enqueue_scripts() { |
| 260 | wp_enqueue_style( |
| 261 | 'wordads', |
| 262 | WORDADS_URL . 'css/style.css', |
| 263 | array(), |
| 264 | '2015-12-18' |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Add the IPW resource hints |
| 270 | * |
| 271 | * @since 7.9 |
| 272 | */ |
| 273 | public function resource_hints( $hints, $relation_type ) { |
| 274 | if ( 'dns-prefetch' === $relation_type ) { |
| 275 | $hints[] = '//s.pubmine.com'; |
| 276 | $hints[] = '//x.bidswitch.net'; |
| 277 | $hints[] = '//static.criteo.net'; |
| 278 | $hints[] = '//ib.adnxs.com'; |
| 279 | $hints[] = '//aax.amazon-adsystem.com'; |
| 280 | $hints[] = '//bidder.criteo.com'; |
| 281 | $hints[] = '//cas.criteo.com'; |
| 282 | $hints[] = '//gum.criteo.com'; |
| 283 | $hints[] = '//ads.pubmatic.com'; |
| 284 | $hints[] = '//gads.pubmatic.com'; |
| 285 | $hints[] = '//tpc.googlesyndication.com'; |
| 286 | $hints[] = '//ad.doubleclick.net'; |
| 287 | $hints[] = '//googleads.g.doubleclick.net'; |
| 288 | $hints[] = '//www.googletagservices.com'; |
| 289 | $hints[] = '//cdn.switchadhub.com'; |
| 290 | $hints[] = '//delivery.g.switchadhub.com'; |
| 291 | $hints[] = '//delivery.swid.switchadhub.com'; |
| 292 | } |
| 293 | |
| 294 | return $hints; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * IPONWEB metadata used by the various scripts |
| 299 | * |
| 300 | * @return [type] [description] |
| 301 | */ |
| 302 | function insert_head_meta() { |
| 303 | if ( self::is_amp() ) { |
| 304 | return; |
| 305 | } |
| 306 | $themename = esc_js( get_stylesheet() ); |
| 307 | $pagetype = intval( $this->params->get_page_type_ipw() ); |
| 308 | $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; |
| 309 | $site_id = $this->params->blog_id; |
| 310 | $consent = intval( isset( $_COOKIE['personalized-ads-consent'] ) ); |
| 311 | echo <<<HTML |
| 312 | <script$data_tags type="text/javascript"> |
| 313 | var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id, consent: $consent }; |
| 314 | var __ATA = __ATA || {}; |
| 315 | __ATA.cmd = __ATA.cmd || []; |
| 316 | __ATA.criteo = __ATA.criteo || {}; |
| 317 | __ATA.criteo.cmd = __ATA.criteo.cmd || []; |
| 318 | </script> |
| 319 | HTML; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * IPONWEB scripts in <head> |
| 324 | * |
| 325 | * @since 4.5.0 |
| 326 | */ |
| 327 | function insert_head_iponweb() { |
| 328 | if ( self::is_amp() ) { |
| 329 | return; |
| 330 | } |
| 331 | $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; |
| 332 | echo <<<HTML |
| 333 | <script$data_tags type="text/javascript"> |
| 334 | (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} |
| 335 | 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; |
| 336 | 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} |
| 337 | 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},"","."); |
| 338 | (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); |
| 339 | </script> |
| 340 | HTML; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Insert the ad onto the page |
| 345 | * |
| 346 | * @since 4.5.0 |
| 347 | */ |
| 348 | function insert_ad( $content ) { |
| 349 | // Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module). |
| 350 | if ( is_feed() || ! is_main_query() || ! in_the_loop() ) { |
| 351 | return $content; |
| 352 | } |
| 353 | /** |
| 354 | * Allow third-party tools to disable the display of in post ads. |
| 355 | * |
| 356 | * @module wordads |
| 357 | * |
| 358 | * @since 4.5.0 |
| 359 | * |
| 360 | * @param bool true Should the in post unit be disabled. Default to false. |
| 361 | */ |
| 362 | $disable = apply_filters( 'wordads_inpost_disable', false ); |
| 363 | if ( ! $this->params->should_show() || $disable ) { |
| 364 | return $content; |
| 365 | } |
| 366 | |
| 367 | $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; |
| 368 | return $content . $this->get_ad( 'belowpost', $ad_type ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Insert an inline ad into a post content |
| 373 | * Used for rendering the `wordads` shortcode. |
| 374 | * |
| 375 | * @since 6.1.0 |
| 376 | */ |
| 377 | function insert_inline_ad( $content ) { |
| 378 | // Ad JS won't work in XML feeds. |
| 379 | if ( is_feed() ) { |
| 380 | return $content; |
| 381 | } |
| 382 | /** |
| 383 | * Allow third-party tools to disable the display of in post ads. |
| 384 | * |
| 385 | * @module wordads |
| 386 | * |
| 387 | * @since 4.5.0 |
| 388 | * |
| 389 | * @param bool true Should the in post unit be disabled. Default to false. |
| 390 | */ |
| 391 | $disable = apply_filters( 'wordads_inpost_disable', false ); |
| 392 | if ( $disable ) { |
| 393 | return $content; |
| 394 | } |
| 395 | |
| 396 | $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; |
| 397 | $content .= $this->get_ad( 'inline', $ad_type ); |
| 398 | return $content; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Inserts ad into header |
| 403 | * |
| 404 | * @since 4.5.0 |
| 405 | */ |
| 406 | function insert_header_ad() { |
| 407 | /** |
| 408 | * Allow third-party tools to disable the display of header ads. |
| 409 | * |
| 410 | * @module wordads |
| 411 | * |
| 412 | * @since 4.5.0 |
| 413 | * |
| 414 | * @param bool true Should the header unit be disabled. Default to false. |
| 415 | */ |
| 416 | if ( apply_filters( 'wordads_header_disable', false ) ) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; |
| 421 | echo $this->get_ad( 'top', $ad_type ); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Special cases for inserting header unit via jQuery |
| 426 | * |
| 427 | * @since 4.5.0 |
| 428 | */ |
| 429 | function insert_header_ad_special() { |
| 430 | /** |
| 431 | * Allow third-party tools to disable the display of header ads. |
| 432 | * |
| 433 | * @module wordads |
| 434 | * |
| 435 | * @since 4.5.0 |
| 436 | * |
| 437 | * @param bool true Should the header unit be disabled. Default to false. |
| 438 | */ |
| 439 | if ( apply_filters( 'wordads_header_disable', false ) ) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | $selector = '#content'; |
| 444 | switch ( get_stylesheet() ) { |
| 445 | case 'twentyseventeen': |
| 446 | $selector = '#content'; |
| 447 | break; |
| 448 | case 'twentyfifteen': |
| 449 | $selector = '#main'; |
| 450 | break; |
| 451 | case 'twentyfourteen': |
| 452 | $selector = 'article:first'; |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; |
| 457 | echo $this->get_ad( 'top', $ad_type ); |
| 458 | if ( ! self::is_amp() ) { |
| 459 | echo <<<HTML |
| 460 | <script type="text/javascript"> |
| 461 | jQuery('.wpcnt-header').insertBefore('$selector'); |
| 462 | </script> |
| 463 | HTML; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Header unit for AMP |
| 469 | * |
| 470 | * @param string $content Content of the page. |
| 471 | * |
| 472 | * @since 7.5.0 |
| 473 | */ |
| 474 | public function insert_header_ad_amp( $content ) { |
| 475 | |
| 476 | $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; |
| 477 | if ( 'house' === $ad_type ) { |
| 478 | return $content; |
| 479 | } |
| 480 | return $this->get_ad( 'top_amp', $ad_type ) . $content; |
| 481 | |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace. |
| 486 | * |
| 487 | * @param string $adstxt The ads.txt being filtered |
| 488 | * @return string Filtered ads.txt with custom entries, if applicable |
| 489 | * |
| 490 | * @since 6.5.0 |
| 491 | */ |
| 492 | function insert_custom_adstxt( $adstxt ) { |
| 493 | $custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) ); |
| 494 | if ( $custom_adstxt ) { |
| 495 | $adstxt .= "\n\n#Jetpack - User Custom Entries\n"; |
| 496 | $adstxt .= $custom_adstxt . "\n"; |
| 497 | } |
| 498 | |
| 499 | return $adstxt; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Get the ad for the spot and type. |
| 504 | * |
| 505 | * @param string $spot top, side, inline, or belowpost |
| 506 | * @param string $type iponweb or adsense |
| 507 | */ |
| 508 | function get_ad( $spot, $type = 'iponweb' ) { |
| 509 | $snippet = ''; |
| 510 | if ( 'iponweb' == $type ) { |
| 511 | // Default to mrec |
| 512 | $width = 300; |
| 513 | $height = 250; |
| 514 | |
| 515 | $section_id = WORDADS_API_TEST_ID; |
| 516 | $second_belowpost = ''; |
| 517 | $snippet = ''; |
| 518 | if ( 'top' == $spot ) { |
| 519 | // mrec for mobile, leaderboard for desktop |
| 520 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2'; |
| 521 | $width = $this->params->mobile_device ? 300 : 728; |
| 522 | $height = $this->params->mobile_device ? 250 : 90; |
| 523 | $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot ); |
| 524 | } elseif ( 'belowpost' == $spot ) { |
| 525 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1'; |
| 526 | $width = 300; |
| 527 | $height = 250; |
| 528 | |
| 529 | $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS ); |
| 530 | if ( $this->option( 'wordads_second_belowpost', true ) ) { |
| 531 | $section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4'; |
| 532 | $snippet .= $this->get_ad_snippet( $section_id2, $height, $width, $spot . '2', 'float:left;margin-top:0px;' ); |
| 533 | } |
| 534 | } elseif ( 'inline' === $spot ) { |
| 535 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5'; |
| 536 | $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS ); |
| 537 | } elseif ( 'top_amp' === $spot ) { |
| 538 | // 320x50 unit which can safely be inserted below title, above content in a variety of themes. |
| 539 | $width = 320; |
| 540 | $height = 50; |
| 541 | $snippet = $this->get_ad_snippet( null, $height, $width ); |
| 542 | } |
| 543 | } elseif ( 'house' == $type ) { |
| 544 | $leaderboard = 'top' == $spot && ! $this->params->mobile_device; |
| 545 | $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
| 546 | if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) { |
| 547 | $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return $this->get_ad_div( $spot, $snippet ); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | /** |
| 556 | * Returns the snippet to be inserted into the ad unit |
| 557 | * |
| 558 | * @param int $section_id |
| 559 | * @param int $height |
| 560 | * @param int $width |
| 561 | * @param int $location |
| 562 | * @param string $css |
| 563 | * @return string |
| 564 | * |
| 565 | * @since 5.7 |
| 566 | */ |
| 567 | public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) { |
| 568 | $this->ads[] = array( |
| 569 | 'location' => $location, |
| 570 | 'width' => $width, |
| 571 | 'height' => $height, |
| 572 | ); |
| 573 | |
| 574 | if ( self::is_amp() ) { |
| 575 | $height = esc_attr( $height + 15 ); // this will ensure enough padding for "Report this ad" |
| 576 | $width = esc_attr( $width ); |
| 577 | $amp_section_id = esc_attr( self::get_amp_section_id() ); |
| 578 | $site_id = esc_attr( $this->params->blog_id ); |
| 579 | return <<<HTML |
| 580 | <amp-ad width="$width" height="$height" |
| 581 | type="pubmine" |
| 582 | data-siteid="$site_id" |
| 583 | data-section="$amp_section_id"> |
| 584 | </amp-ad> |
| 585 | HTML; |
| 586 | } |
| 587 | |
| 588 | $ad_number = count( $this->ads ) . '-' . uniqid(); |
| 589 | $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : ''; |
| 590 | $css = esc_attr( $css ); |
| 591 | |
| 592 | $loc_id = 100; |
| 593 | if ( ! empty( self::$ad_location_ids[ $location ] ) ) { |
| 594 | $loc_id = self::$ad_location_ids[ $location ]; |
| 595 | } |
| 596 | |
| 597 | return <<<HTML |
| 598 | <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css"> |
| 599 | <div id="atatags-{$ad_number}"> |
| 600 | <script$data_tags type="text/javascript"> |
| 601 | __ATA.cmd.push(function() { |
| 602 | __ATA.initSlot('atatags-{$ad_number}', { |
| 603 | collapseEmpty: 'before', |
| 604 | sectionId: '{$section_id}', |
| 605 | location: {$loc_id}, |
| 606 | width: {$width}, |
| 607 | height: {$height} |
| 608 | }); |
| 609 | }); |
| 610 | </script> |
| 611 | </div> |
| 612 | </div> |
| 613 | HTML; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Returns the complete ad div with snippet to be inserted into the page |
| 618 | * |
| 619 | * @param string $spot top, side, inline, or belowpost |
| 620 | * @param string $snippet The snippet to insert into the div |
| 621 | * @param array $css_classes |
| 622 | * @return string The supporting ad unit div |
| 623 | * |
| 624 | * @since 7.1 |
| 625 | */ |
| 626 | function get_ad_div( $spot, $snippet, array $css_classes = array() ) { |
| 627 | if ( empty( $css_classes ) ) { |
| 628 | $css_classes = array(); |
| 629 | } |
| 630 | |
| 631 | $css_classes[] = 'wpcnt'; |
| 632 | if ( 'top' == $spot ) { |
| 633 | $css_classes[] = 'wpcnt-header'; |
| 634 | } |
| 635 | |
| 636 | $spot = esc_attr( $spot ); |
| 637 | $classes = esc_attr( implode( ' ', $css_classes ) ); |
| 638 | $about = esc_html__( 'Advertisements', 'jetpack' ); |
| 639 | return <<<HTML |
| 640 | <div class="$classes"> |
| 641 | <div class="wpa"> |
| 642 | <span class="wpa-about">$about</span> |
| 643 | <div class="u $spot"> |
| 644 | $snippet |
| 645 | </div> |
| 646 | </div> |
| 647 | </div> |
| 648 | HTML; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Check the reasons to bail before we attempt to insert ads. |
| 653 | * |
| 654 | * @return true if we should bail (don't insert ads) |
| 655 | * |
| 656 | * @since 4.5.0 |
| 657 | */ |
| 658 | public function should_bail() { |
| 659 | return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' ); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Returns markup for HTML5 house ad base on unit |
| 664 | * |
| 665 | * @param string $unit mrec, widesky, or leaderboard |
| 666 | * @return string markup for HTML5 house ad |
| 667 | * |
| 668 | * @since 4.7.0 |
| 669 | */ |
| 670 | public function get_house_ad( $unit = 'mrec' ) { |
| 671 | |
| 672 | switch ( $unit ) { |
| 673 | case 'widesky': |
| 674 | $width = 160; |
| 675 | $height = 600; |
| 676 | break; |
| 677 | case 'leaderboard': |
| 678 | $width = 728; |
| 679 | $height = 90; |
| 680 | break; |
| 681 | case 'mrec': |
| 682 | default: |
| 683 | $width = 300; |
| 684 | $height = 250; |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | return <<<HTML |
| 689 | <iframe |
| 690 | src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html" |
| 691 | width="$width" |
| 692 | height="$height" |
| 693 | frameborder="0" |
| 694 | scrolling="no" |
| 695 | marginheight="0" |
| 696 | marginwidth="0"> |
| 697 | </iframe> |
| 698 | HTML; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Activation hook actions |
| 703 | * |
| 704 | * @since 4.5.0 |
| 705 | */ |
| 706 | public static function activate() { |
| 707 | WordAds_API::update_wordads_status_from_api(); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) ); |
| 712 | add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) ); |
| 713 | add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) ); |
| 714 | |
| 715 | global $wordads; |
| 716 | $wordads = new WordAds(); |
| 717 |