WordAds

The WordPress Core WordAds class.

Defined (1)

The class is defined in the following location(s).

/modules/wordads/wordads.php  
  1. class WordAds { 
  2.  
  3. public $params = null; 
  4.  
  5. /** 
  6. * The different supported ad types. 
  7. * v0.1 - mrec only for now 
  8. * @var array 
  9. */ 
  10. public static $ad_tag_ids = array( 
  11. 'mrec' => array( 
  12. 'tag' => '300x250_mediumrectangle',  
  13. 'height' => '250',  
  14. 'width' => '300',  
  15. ),  
  16. 'lrec' => array( 
  17. 'tag' => '336x280_largerectangle',  
  18. 'height' => '280',  
  19. 'width' => '336',  
  20. ),  
  21. 'leaderboard' => array( 
  22. 'tag' => '728x90_leaderboard',  
  23. 'height' => '90',  
  24. 'width' => '728',  
  25. ),  
  26. 'wideskyscraper' => array( 
  27. 'tag' => '160x600_wideskyscraper',  
  28. 'height' => '600',  
  29. 'width' => '160',  
  30. ),  
  31. ); 
  32.  
  33. /** 
  34. * Convenience function for grabbing options from params->options 
  35. * @param string $option the option to grab 
  36. * @param mixed $default (optional) 
  37. * @return option or $default if not set 
  38. * @since 4.5.0 
  39. */ 
  40. function option( $option, $default = false ) { 
  41. if ( ! isset( $this->params->options[ $option ] ) ) { 
  42. return $default; 
  43.  
  44. return $this->params->options[ $option ]; 
  45.  
  46. /** 
  47. * Instantiate the plugin 
  48. * @since 4.5.0 
  49. */ 
  50. function __construct() { 
  51. add_action( 'init', array( $this, 'init' ) ); 
  52.  
  53. /** 
  54. * Code to run on WordPress 'init' hook 
  55. * @since 4.5.0 
  56. */ 
  57. function init() { 
  58. // bail on infinite scroll 
  59. if ( self::is_infinite_scroll() ) { 
  60. return; 
  61.  
  62. require_once( WORDADS_ROOT . '/php/params.php' ); 
  63. $this->params = new WordAds_Params(); 
  64.  
  65. if ( is_admin() ) { 
  66. require_once( WORDADS_ROOT . '/php/admin.php' ); 
  67. return; 
  68.  
  69. if ( $this->should_bail() ) { 
  70. return; 
  71.  
  72. $this->insert_adcode(); 
  73. $this->insert_extras(); 
  74.  
  75. /** 
  76. * Check for Jetpack's The_Neverending_Home_Page and use got_infinity 
  77. * @return boolean true if load came from infinite scroll 
  78. * @since 4.5.0 
  79. */ 
  80. public static function is_infinite_scroll() { 
  81. return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity(); 
  82.  
  83. /** 
  84. * Add the actions/filters to insert the ads. Checks for mobile or desktop. 
  85. * @since 4.5.0 
  86. */ 
  87. private function insert_adcode() { 
  88. add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 ); 
  89. add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 ); 
  90. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  91. add_filter( 'the_content', array( $this, 'insert_ad' ) ); 
  92. add_filter( 'the_excerpt', array( $this, 'insert_ad' ) ); 
  93.  
  94. if ( $this->option( 'enable_header_ad' ) ) { 
  95. switch ( get_stylesheet() ) { 
  96. case 'twentyseventeen': 
  97. case 'twentyfifteen': 
  98. case 'twentyfourteen': 
  99. add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) ); 
  100. break; 
  101. default: 
  102. add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 ); 
  103. break; 
  104.  
  105. /** 
  106. * Add the actions/filters to insert extra-network features. 
  107. * @since 4.5.0 
  108. */ 
  109. private function insert_extras() { 
  110. require_once( WORDADS_ROOT . '/php/networks/amazon.php' ); 
  111.  
  112. /** 
  113. * Register desktop scripts and styles 
  114. * @since 4.5.0 
  115. */ 
  116. function enqueue_scripts() { 
  117. wp_enqueue_style( 
  118. 'wordads',  
  119. WORDADS_URL . 'css/style.css',  
  120. array(),  
  121. '2015-12-18' 
  122. ); 
  123.  
  124. /** 
  125. * IPONWEB metadata used by the various scripts 
  126. * @return [type] [description] 
  127. */ 
  128. function insert_head_meta() { 
  129. $domain = $this->params->targeting_tags['Domain']; 
  130. $pageURL = $this->params->targeting_tags['PageURL']; 
  131. $adsafe = $this->params->targeting_tags['AdSafe']; 
  132. $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; 
  133. echo <<<HTML 
  134. <script$data_tags type="text/javascript"> 
  135. var _ipw_custom = { 
  136. wordAds: '1',  
  137. domain: '$domain',  
  138. pageURL: '$pageURL',  
  139. adSafe: '$adsafe' 
  140. }; 
  141. </script> 
  142. HTML; 
  143.  
  144. /** 
  145. * IPONWEB scripts in <head> 
  146. * @since 4.5.0 
  147. */ 
  148. function insert_head_iponweb() { 
  149. $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; 
  150. echo "<script$data_tags type='text/javascript' src='//s.pubmine.com/head.js'></script>"; 
  151.  
  152. /** 
  153. * Insert the ad onto the page 
  154. * @since 4.5.0 
  155. */ 
  156. function insert_ad( $content ) { 
  157. /** 
  158. * Allow third-party tools to disable the display of in post ads. 
  159. * @module wordads 
  160. * @since 4.5.0 
  161. * @param bool true Should the in post unit be disabled. Default to false. 
  162. */ 
  163. $disable = apply_filters( 'wordads_inpost_disable', false ); 
  164. if ( ! $this->params->should_show() || $disable ) { 
  165. return $content; 
  166.  
  167. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; 
  168. return $content . $this->get_ad( 'belowpost', $ad_type ); 
  169.  
  170. /** 
  171. * Inserts ad into header 
  172. * @since 4.5.0 
  173. */ 
  174. function insert_header_ad() { 
  175. /** 
  176. * Allow third-party tools to disable the display of header ads. 
  177. * @module wordads 
  178. * @since 4.5.0 
  179. * @param bool true Should the header unit be disabled. Default to false. 
  180. */ 
  181. if ( apply_filters( 'wordads_header_disable', false ) ) { 
  182. return; 
  183.  
  184. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; 
  185. echo $this->get_ad( 'top', $ad_type ); 
  186.  
  187. /** 
  188. * Special cases for inserting header unit via jQuery 
  189. * @since 4.5.0 
  190. */ 
  191. function insert_header_ad_special() { 
  192. /** 
  193. * Allow third-party tools to disable the display of header ads. 
  194. * @module wordads 
  195. * @since 4.5.0 
  196. * @param bool true Should the header unit be disabled. Default to false. 
  197. */ 
  198. if ( apply_filters( 'wordads_header_disable', false ) ) { 
  199. return; 
  200.  
  201. $selector = '#content'; 
  202. switch ( get_stylesheet() ) { 
  203. case 'twentyseventeen': 
  204. $selector = '#content'; 
  205. break; 
  206. case 'twentyfifteen': 
  207. $selector = '#main'; 
  208. break; 
  209. case 'twentyfourteen': 
  210. $selector = 'article:first'; 
  211. break; 
  212.  
  213. $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb'; 
  214. echo $this->get_ad( 'top', $ad_type ); 
  215. echo <<<HTML 
  216. <script type="text/javascript"> 
  217. jQuery('.wpcnt-header').insertBefore('$selector'); 
  218. </script> 
  219. HTML; 
  220.  
  221. /** 
  222. * Get the ad for the spot and type. 
  223. * @param string $spot top, side, or belowpost 
  224. * @param string $type iponweb or adsense 
  225. */ 
  226. function get_ad( $spot, $type = 'iponweb' ) { 
  227. $snippet = ''; 
  228. if ( 'iponweb' == $type ) { 
  229. $section_id = WORDADS_API_TEST_ID; 
  230. $width = 300; 
  231. $height = 250; 
  232. if ( 'top' == $spot ) { 
  233. // mrec for mobile, leaderboard for desktop 
  234. $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2'; 
  235. $width = $this->params->mobile_device ? 300 : 728; 
  236. $height = $this->params->mobile_device ? 250 : 90; 
  237. } else if ( 'belowpost' ) { 
  238. $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1'; 
  239. $width = 300; 
  240. $height = 250; 
  241. $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : ''; 
  242. $snippet = <<<HTML 
  243. <script$data_tags type='text/javascript'> 
  244. (function(g) {g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window); 
  245. </script> 
  246. HTML; 
  247. } else if ( 'house' == $type ) { 
  248. $leaderboard = 'top' == $spot && ! $this->params->mobile_device; 
  249. $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); 
  250.  
  251. $header = 'top' == $spot ? 'wpcnt-header' : ''; 
  252. $about = __( 'Advertisements', 'jetpack' ); 
  253. return <<<HTML 
  254. <div class="wpcnt $header"> 
  255. <div class="wpa"> 
  256. <span class="wpa-about">$about</span> 
  257. <div class="u $spot"> 
  258. $snippet 
  259. </div> 
  260. </div> 
  261. </div> 
  262. HTML; 
  263.  
  264. /** 
  265. * Check the reasons to bail before we attempt to insert ads. 
  266. * @return true if we should bail (don't insert ads) 
  267. * @since 4.5.0 
  268. */ 
  269. public function should_bail() { 
  270. return ! $this->option( 'wordads_approved' ); 
  271.  
  272. /** 
  273. * Returns markup for HTML5 house ad base on unit 
  274. * @param string $unit mrec, widesky, or leaderboard 
  275. * @return string markup for HTML5 house ad 
  276. * @since 4.7.0 
  277. */ 
  278. public function get_house_ad( $unit = 'mrec' ) { 
  279. if ( ! in_array( $unit, array( 'mrec', 'widesky', 'leaderboard' ) ) ) { 
  280. $unit = 'mrec'; 
  281.  
  282. $width = 300; 
  283. $height = 250; 
  284. if ( 'widesky' == $unit ) { 
  285. $width = 160; 
  286. $height = 600; 
  287. } else if ( 'leaderboard' == $unit ) { 
  288. $width = 728; 
  289. $height = 90; 
  290.  
  291. return <<<HTML 
  292. <iframe 
  293. src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html" 
  294. width="$width" 
  295. height="$height" 
  296. frameborder="0" 
  297. scrolling="no" 
  298. marginheight="0" 
  299. marginwidth="0"> 
  300. </iframe> 
  301. HTML; 
  302.  
  303. /** 
  304. * Activation hook actions 
  305. * @since 4.5.0 
  306. */ 
  307. public static function activate() { 
  308. WordAds_API::update_wordads_status_from_api();