暂无描述

class-wordads.php 25KB

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