params.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. class WordAds_Params {
  3. /**
  4. * Setup parameters for serving the ads
  5. *
  6. * @since 4.5.0
  7. */
  8. public function __construct() {
  9. // WordAds setting => default
  10. $settings = array(
  11. 'wordads_approved' => false,
  12. 'wordads_active' => false,
  13. 'wordads_house' => true,
  14. 'wordads_unsafe' => false,
  15. 'enable_header_ad' => true,
  16. 'wordads_second_belowpost' => true,
  17. 'wordads_display_front_page' => true,
  18. 'wordads_display_post' => true,
  19. 'wordads_display_page' => true,
  20. 'wordads_display_archive' => true,
  21. 'wordads_custom_adstxt' => ''
  22. );
  23. // grab settings, or set as default if it doesn't exist
  24. $this->options = array();
  25. foreach ( $settings as $setting => $default ) {
  26. $option = get_option( $setting, null );
  27. if ( is_null( $option ) ) {
  28. update_option( $setting, $default, true );
  29. $option = $default;
  30. }
  31. $this->options[$setting] = 'wordads_custom_adstxt' !== $setting ? (bool) $option : $option;
  32. }
  33. $host = 'localhost';
  34. if ( isset( $_SERVER['HTTP_HOST'] ) ) {
  35. $host = $_SERVER['HTTP_HOST'];
  36. }
  37. $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI'];
  38. if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
  39. $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
  40. }
  41. $this->cloudflare = self::is_cloudflare();
  42. $this->blog_id = Jetpack::get_option( 'id', 0 );
  43. $this->mobile_device = jetpack_is_mobile( 'any', true );
  44. $this->targeting_tags = array(
  45. 'WordAds' => 1,
  46. 'BlogId' => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
  47. 'Domain' => esc_js( parse_url( home_url(), PHP_URL_HOST ) ),
  48. 'PageURL' => esc_js( $this->url ),
  49. 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else?
  50. 'AdSafe' => 1, // TODO
  51. );
  52. }
  53. /**
  54. * @return boolean true if the user is browsing on a mobile device (iPad not included)
  55. *
  56. * @since 4.5.0
  57. */
  58. public function is_mobile() {
  59. return ! empty( $this->mobile_device );
  60. }
  61. /**
  62. * @return boolean true if site is being served via CloudFlare
  63. *
  64. * @since 4.5.0
  65. */
  66. public static function is_cloudflare() {
  67. if (
  68. defined( 'WORDADS_CLOUDFLARE' )
  69. || isset( $_SERVER['HTTP_CF_CONNECTING_IP'] )
  70. || isset( $_SERVER['HTTP_CF_IPCOUNTRY'] )
  71. || isset( $_SERVER['HTTP_CF_VISITOR'] )
  72. ) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * @return boolean true if user is browsing in iOS device
  79. *
  80. * @since 4.5.0
  81. */
  82. public function is_ios() {
  83. return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
  84. }
  85. /**
  86. * Returns the user's device (see user-agent.php) or 'desktop'
  87. * @return string user device
  88. *
  89. * @since 4.5.0
  90. */
  91. public function get_device() {
  92. global $agent_info;
  93. if ( ! empty( $this->mobile_device ) ) {
  94. return $this->mobile_device;
  95. }
  96. if ( $agent_info->is_ipad() ) {
  97. return 'ipad';
  98. }
  99. return 'desktop';
  100. }
  101. /**
  102. * @return string The type of page that is being loaded
  103. *
  104. * @since 4.5.0
  105. */
  106. public function get_page_type() {
  107. if ( ! empty( $this->page_type ) ) {
  108. return $this->page_type;
  109. }
  110. if ( self::is_static_home() ) {
  111. $this->page_type = 'static_home';
  112. } else if ( is_home() ) {
  113. $this->page_type = 'home';
  114. } else if ( is_page() ) {
  115. $this->page_type = 'page';
  116. } else if ( is_single() ) {
  117. $this->page_type = 'post';
  118. } else if ( is_search() ) {
  119. $this->page_type = 'search';
  120. } else if ( is_category() ) {
  121. $this->page_type = 'category';
  122. } else if ( is_archive() ) {
  123. $this->page_type = 'archive';
  124. } else {
  125. $this->page_type = 'wtf';
  126. }
  127. return $this->page_type;
  128. }
  129. /**
  130. * @return int The page type code for ipw config
  131. *
  132. * @since 5.6.0
  133. */
  134. public function get_page_type_ipw() {
  135. if ( ! empty( $this->page_type_ipw ) ) {
  136. return $this->page_type_ipw;
  137. }
  138. $page_type_ipw = 6;
  139. if ( self::is_static_home() || is_home() || is_front_page() ) {
  140. $page_type_ipw = 0;
  141. } else if ( is_page() ) {
  142. $page_type_ipw = 2;
  143. } else if ( is_singular() ) {
  144. $page_type_ipw = 1;
  145. } else if ( is_search() ) {
  146. $page_type_ipw = 4;
  147. } else if ( is_category() || is_tag() || is_archive() || is_author() ) {
  148. $page_type_ipw = 3;
  149. } else if ( is_404() ) {
  150. $page_type_ipw = 5;
  151. }
  152. $this->page_type_ipw = $page_type_ipw;
  153. return $page_type_ipw;
  154. }
  155. /**
  156. * Returns true if page is static home
  157. * @return boolean true if page is static home
  158. *
  159. * @since 4.5.0
  160. */
  161. public static function is_static_home() {
  162. return is_front_page() &&
  163. 'page' == get_option( 'show_on_front' ) &&
  164. get_option( 'page_on_front' );
  165. }
  166. /**
  167. * Logic for if we should show an ad
  168. *
  169. * @since 4.5.0
  170. */
  171. public function should_show() {
  172. global $wp_query;
  173. if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) {
  174. return false;
  175. }
  176. if ( is_single() && ! $this->options['wordads_display_post'] ) {
  177. return false;
  178. }
  179. if ( is_page() && ! $this->options['wordads_display_page'] ) {
  180. return false;
  181. }
  182. if ( is_archive() && ! $this->options['wordads_display_archive'] ) {
  183. return false;
  184. }
  185. if ( is_single() || ( is_page() && ! is_home() ) ) {
  186. return true;
  187. }
  188. // TODO this would be a good place for allowing the user to specify
  189. if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. }