wordads.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. define( 'WORDADS_ROOT', dirname( __FILE__ ) );
  3. define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
  4. define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
  5. define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
  6. define( 'WORDADS_API_TEST_ID', '26942' );
  7. define( 'WORDADS_API_TEST_ID2', '114160' );
  8. require_once( WORDADS_ROOT . '/php/widgets.php' );
  9. require_once( WORDADS_ROOT . '/php/api.php' );
  10. require_once( WORDADS_ROOT . '/php/cron.php' );
  11. class WordAds {
  12. public $params = null;
  13. public $ads = array();
  14. /**
  15. * Array of supported ad types.
  16. *
  17. * @var array
  18. */
  19. public static $ad_tag_ids = array(
  20. 'mrec' => array(
  21. 'tag' => '300x250_mediumrectangle',
  22. 'height' => '250',
  23. 'width' => '300',
  24. ),
  25. 'lrec' => array(
  26. 'tag' => '336x280_largerectangle',
  27. 'height' => '280',
  28. 'width' => '336',
  29. ),
  30. 'leaderboard' => array(
  31. 'tag' => '728x90_leaderboard',
  32. 'height' => '90',
  33. 'width' => '728',
  34. ),
  35. 'wideskyscraper' => array(
  36. 'tag' => '160x600_wideskyscraper',
  37. 'height' => '600',
  38. 'width' => '160',
  39. ),
  40. );
  41. /**
  42. * Convenience function for grabbing options from params->options
  43. *
  44. * @param string $option the option to grab
  45. * @param mixed $default (optional)
  46. * @return option or $default if not set
  47. *
  48. * @since 4.5.0
  49. */
  50. function option( $option, $default = false ) {
  51. if ( ! isset( $this->params->options[ $option ] ) ) {
  52. return $default;
  53. }
  54. return $this->params->options[ $option ];
  55. }
  56. /**
  57. * Instantiate the plugin
  58. *
  59. * @since 4.5.0
  60. */
  61. function __construct() {
  62. add_action( 'init', array( $this, 'init' ) );
  63. }
  64. /**
  65. * Code to run on WordPress 'init' hook
  66. *
  67. * @since 4.5.0
  68. */
  69. function init() {
  70. // bail on infinite scroll
  71. if ( self::is_infinite_scroll() ) {
  72. return;
  73. }
  74. require_once( WORDADS_ROOT . '/php/params.php' );
  75. $this->params = new WordAds_Params();
  76. if ( is_admin() ) {
  77. require_once( WORDADS_ROOT . '/php/admin.php' );
  78. return;
  79. }
  80. if ( $this->should_bail() ) {
  81. return;
  82. }
  83. $this->insert_adcode();
  84. if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) {
  85. if ( false === ( $ads_txt_transient = get_transient( 'jetpack_ads_txt' ) ) ) {
  86. $ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : '';
  87. set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
  88. }
  89. /**
  90. * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
  91. *
  92. * @module wordads
  93. *
  94. * @since 6.1.0
  95. *
  96. * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
  97. */
  98. $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
  99. header( 'Content-Type: text/plain; charset=utf-8' );
  100. echo esc_html( $ads_txt_content );
  101. die();
  102. }
  103. }
  104. /**
  105. * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
  106. *
  107. * @return boolean true if load came from infinite scroll
  108. *
  109. * @since 4.5.0
  110. */
  111. public static function is_infinite_scroll() {
  112. return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
  113. }
  114. /**
  115. * Add the actions/filters to insert the ads. Checks for mobile or desktop.
  116. *
  117. * @since 4.5.0
  118. */
  119. private function insert_adcode() {
  120. add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
  121. add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
  122. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  123. add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
  124. /**
  125. * Filters enabling ads in `the_content` filter
  126. *
  127. * @see https://jetpack.com/support/ads/
  128. *
  129. * @module wordads
  130. *
  131. * @since 5.8.0
  132. *
  133. * @param bool True to disable ads in `the_content`
  134. */
  135. if ( ! apply_filters( 'wordads_content_disable', false ) ) {
  136. add_filter( 'the_content', array( $this, 'insert_ad' ) );
  137. }
  138. /**
  139. * Filters enabling ads in `the_excerpt` filter
  140. *
  141. * @see https://jetpack.com/support/ads/
  142. *
  143. * @module wordads
  144. *
  145. * @since 5.8.0
  146. *
  147. * @param bool True to disable ads in `the_excerpt`
  148. */
  149. if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
  150. add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
  151. }
  152. if ( $this->option( 'enable_header_ad', true ) ) {
  153. switch ( get_stylesheet() ) {
  154. case 'twentyseventeen':
  155. case 'twentyfifteen':
  156. case 'twentyfourteen':
  157. add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
  158. break;
  159. default:
  160. add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
  161. break;
  162. }
  163. }
  164. }
  165. /**
  166. * Register desktop scripts and styles
  167. *
  168. * @since 4.5.0
  169. */
  170. function enqueue_scripts() {
  171. wp_enqueue_style(
  172. 'wordads',
  173. WORDADS_URL . 'css/style.css',
  174. array(),
  175. '2015-12-18'
  176. );
  177. }
  178. /**
  179. * IPONWEB metadata used by the various scripts
  180. * @return [type] [description]
  181. */
  182. function insert_head_meta() {
  183. $themename = esc_js( get_stylesheet() );
  184. $pagetype = intval( $this->params->get_page_type_ipw() );
  185. $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
  186. $site_id = $this->params->blog_id;
  187. $consent = intval( isset( $_COOKIE['personalized-ads-consent'] ) );
  188. echo <<<HTML
  189. <script$data_tags type="text/javascript">
  190. var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id, consent: $consent };
  191. var __ATA = __ATA || {};
  192. __ATA.cmd = __ATA.cmd || [];
  193. __ATA.criteo = __ATA.criteo || {};
  194. __ATA.criteo.cmd = __ATA.criteo.cmd || [];
  195. </script>
  196. HTML;
  197. }
  198. /**
  199. * IPONWEB scripts in <head>
  200. *
  201. * @since 4.5.0
  202. */
  203. function insert_head_iponweb() {
  204. $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
  205. echo <<<HTML
  206. <link rel='dns-prefetch' href='//s.pubmine.com' />
  207. <link rel='dns-prefetch' href='//x.bidswitch.net' />
  208. <link rel='dns-prefetch' href='//static.criteo.net' />
  209. <link rel='dns-prefetch' href='//ib.adnxs.com' />
  210. <link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
  211. <link rel='dns-prefetch' href='//bidder.criteo.com' />
  212. <link rel='dns-prefetch' href='//cas.criteo.com' />
  213. <link rel='dns-prefetch' href='//gum.criteo.com' />
  214. <link rel='dns-prefetch' href='//ads.pubmatic.com' />
  215. <link rel='dns-prefetch' href='//gads.pubmatic.com' />
  216. <link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
  217. <link rel='dns-prefetch' href='//ad.doubleclick.net' />
  218. <link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
  219. <link rel='dns-prefetch' href='//www.googletagservices.com' />
  220. <script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
  221. HTML;
  222. }
  223. /**
  224. * Insert the ad onto the page
  225. *
  226. * @since 4.5.0
  227. */
  228. function insert_ad( $content ) {
  229. // Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
  230. if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
  231. return $content;
  232. }
  233. /**
  234. * Allow third-party tools to disable the display of in post ads.
  235. *
  236. * @module wordads
  237. *
  238. * @since 4.5.0
  239. *
  240. * @param bool true Should the in post unit be disabled. Default to false.
  241. */
  242. $disable = apply_filters( 'wordads_inpost_disable', false );
  243. if ( ! $this->params->should_show() || $disable ) {
  244. return $content;
  245. }
  246. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
  247. return $content . $this->get_ad( 'belowpost', $ad_type );
  248. }
  249. /**
  250. * Insert an inline ad into a post content
  251. * Used for rendering the `wordads` shortcode.
  252. *
  253. * @since 6.1.0
  254. */
  255. function insert_inline_ad( $content ) {
  256. // Ad JS won't work in XML feeds.
  257. if ( is_feed() ) {
  258. return $content;
  259. }
  260. /**
  261. * Allow third-party tools to disable the display of in post ads.
  262. *
  263. * @module wordads
  264. *
  265. * @since 4.5.0
  266. *
  267. * @param bool true Should the in post unit be disabled. Default to false.
  268. */
  269. $disable = apply_filters( 'wordads_inpost_disable', false );
  270. if ( $disable ) {
  271. return $content;
  272. }
  273. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
  274. $content .= $this->get_ad( 'inline', $ad_type );
  275. return $content;
  276. }
  277. /**
  278. * Inserts ad into header
  279. *
  280. * @since 4.5.0
  281. */
  282. function insert_header_ad() {
  283. /**
  284. * Allow third-party tools to disable the display of header ads.
  285. *
  286. * @module wordads
  287. *
  288. * @since 4.5.0
  289. *
  290. * @param bool true Should the header unit be disabled. Default to false.
  291. */
  292. if ( apply_filters( 'wordads_header_disable', false ) ) {
  293. return;
  294. }
  295. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
  296. echo $this->get_ad( 'top', $ad_type );
  297. }
  298. /**
  299. * Special cases for inserting header unit via jQuery
  300. *
  301. * @since 4.5.0
  302. */
  303. function insert_header_ad_special() {
  304. /**
  305. * Allow third-party tools to disable the display of header ads.
  306. *
  307. * @module wordads
  308. *
  309. * @since 4.5.0
  310. *
  311. * @param bool true Should the header unit be disabled. Default to false.
  312. */
  313. if ( apply_filters( 'wordads_header_disable', false ) ) {
  314. return;
  315. }
  316. $selector = '#content';
  317. switch ( get_stylesheet() ) {
  318. case 'twentyseventeen':
  319. $selector = '#content';
  320. break;
  321. case 'twentyfifteen':
  322. $selector = '#main';
  323. break;
  324. case 'twentyfourteen':
  325. $selector = 'article:first';
  326. break;
  327. }
  328. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
  329. echo $this->get_ad( 'top', $ad_type );
  330. echo <<<HTML
  331. <script type="text/javascript">
  332. jQuery('.wpcnt-header').insertBefore('$selector');
  333. </script>
  334. HTML;
  335. }
  336. /**
  337. * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
  338. * @param string $adstxt The ads.txt being filtered
  339. * @return string Filtered ads.txt with custom entries, if applicable
  340. *
  341. * @since 6.5.0
  342. */
  343. function insert_custom_adstxt( $adstxt ) {
  344. $custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
  345. if ( $custom_adstxt ) {
  346. $adstxt .= "\n\n#Jetpack - User Custom Entries\n";
  347. $adstxt .= $custom_adstxt . "\n";
  348. }
  349. return $adstxt;
  350. }
  351. /**
  352. * Get the ad for the spot and type.
  353. * @param string $spot top, side, inline, or belowpost
  354. * @param string $type iponweb or adsense
  355. */
  356. function get_ad( $spot, $type = 'iponweb' ) {
  357. $snippet = '';
  358. if ( 'iponweb' == $type ) {
  359. // Default to mrec
  360. $width = 300;
  361. $height = 250;
  362. $section_id = WORDADS_API_TEST_ID;
  363. $second_belowpost = '';
  364. $snippet = '';
  365. if ( 'top' == $spot ) {
  366. // mrec for mobile, leaderboard for desktop
  367. $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
  368. $width = $this->params->mobile_device ? 300 : 728;
  369. $height = $this->params->mobile_device ? 250 : 90;
  370. $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot );
  371. } else if ( 'belowpost' == $spot ) {
  372. $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
  373. $width = 300;
  374. $height = 250;
  375. $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, 'float:left;margin-right:5px;margin-top:0px;' );
  376. if ( $this->option( 'wordads_second_belowpost', true ) ) {
  377. $section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
  378. $snippet .= $this->get_ad_snippet( $section_id2, $height, $width, $spot, 'float:left;margin-top:0px;' );
  379. }
  380. } else if ( 'inline' === $spot ) {
  381. $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
  382. $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, 'mrec', 'float:left;margin-right:5px;margin-top:0px;' );
  383. }
  384. } else if ( 'house' == $type ) {
  385. $leaderboard = 'top' == $spot && ! $this->params->mobile_device;
  386. $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
  387. if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
  388. $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
  389. }
  390. }
  391. $header = 'top' == $spot ? 'wpcnt-header' : '';
  392. $about = __( 'Advertisements', 'jetpack' );
  393. return <<<HTML
  394. <div class="wpcnt $header">
  395. <div class="wpa">
  396. <span class="wpa-about">$about</span>
  397. <div class="u $spot">
  398. $snippet
  399. </div>
  400. </div>
  401. </div>
  402. HTML;
  403. }
  404. /**
  405. * Returns the snippet to be inserted into the ad unit
  406. * @param int $section_id
  407. * @param int $height
  408. * @param int $width
  409. * @param int $location
  410. * @param string $css
  411. * @return string
  412. *
  413. * @since 5.7
  414. */
  415. function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
  416. $this->ads[] = array( 'location' => $location, 'width' => $width, 'height' => $height );
  417. $ad_number = count( $this->ads );
  418. // Max 6 ads per page.
  419. if ( $ad_number > 5 && 'top' !== $location ) {
  420. return;
  421. }
  422. $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
  423. return <<<HTML
  424. <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
  425. <div id="atatags-{$ad_number}">
  426. <script$data_tags type="text/javascript">
  427. __ATA.cmd.push(function() {
  428. __ATA.initSlot('atatags-{$ad_number}', {
  429. collapseEmpty: 'before',
  430. sectionId: '{$section_id}',
  431. location: '{$location}',
  432. width: {$width},
  433. height: {$height}
  434. });
  435. });
  436. </script>
  437. </div>
  438. </div>
  439. HTML;
  440. }
  441. /**
  442. * Check the reasons to bail before we attempt to insert ads.
  443. * @return true if we should bail (don't insert ads)
  444. *
  445. * @since 4.5.0
  446. */
  447. public function should_bail() {
  448. return ! $this->option( 'wordads_approved' ) || !! $this->option( 'wordads_unsafe' );
  449. }
  450. /**
  451. * Returns markup for HTML5 house ad base on unit
  452. * @param string $unit mrec, widesky, or leaderboard
  453. * @return string markup for HTML5 house ad
  454. *
  455. * @since 4.7.0
  456. */
  457. public function get_house_ad( $unit = 'mrec' ) {
  458. switch ( $unit ) {
  459. case 'widesky':
  460. $width = 160;
  461. $height = 600;
  462. break;
  463. case 'leaderboard':
  464. $width = 728;
  465. $height = 90;
  466. break;
  467. case 'mrec':
  468. default:
  469. $width = 300;
  470. $height = 250;
  471. break;
  472. }
  473. return <<<HTML
  474. <iframe
  475. src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
  476. width="$width"
  477. height="$height"
  478. frameborder="0"
  479. scrolling="no"
  480. marginheight="0"
  481. marginwidth="0">
  482. </iframe>
  483. HTML;
  484. }
  485. /**
  486. * Activation hook actions
  487. *
  488. * @since 4.5.0
  489. */
  490. public static function activate() {
  491. WordAds_API::update_wordads_status_from_api();
  492. }
  493. }
  494. add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
  495. add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
  496. add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
  497. global $wordads;
  498. $wordads = new WordAds();