networks
5 years ago
class-wordads-admin.php
5 years ago
class-wordads-api.php
4 years ago
class-wordads-california-privacy.php
3 years ago
class-wordads-ccpa-do-not-sell-link-widget.php
3 years ago
class-wordads-cron.php
5 years ago
class-wordads-params.php
3 years ago
class-wordads-sidebar-widget.php
2 years ago
class-wordads-params.php
314 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WordAds Param Class file. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Status; |
| 9 | |
| 10 | /** |
| 11 | * Class WordAds_Params |
| 12 | * |
| 13 | * Sets parameters for WordAds. |
| 14 | */ |
| 15 | class WordAds_Params { |
| 16 | /** |
| 17 | * WordAds options |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | public $options; |
| 22 | |
| 23 | /** |
| 24 | * Current URL |
| 25 | * |
| 26 | * @access public |
| 27 | * @var string |
| 28 | */ |
| 29 | public $url; |
| 30 | |
| 31 | /** |
| 32 | * Is this site served by CloudFlare? |
| 33 | * |
| 34 | * @access public |
| 35 | * @var bool |
| 36 | */ |
| 37 | public $cloudflare; |
| 38 | |
| 39 | /** |
| 40 | * Jetpack Blog ID |
| 41 | * |
| 42 | * @var mixed |
| 43 | */ |
| 44 | public $blog_id; |
| 45 | |
| 46 | /** |
| 47 | * Determine if the current User Agent is a mobile device |
| 48 | * |
| 49 | * @var bool |
| 50 | */ |
| 51 | public $mobile_device; |
| 52 | |
| 53 | /** |
| 54 | * WordAds targeting tags |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | public $targeting_tags; |
| 59 | |
| 60 | /** |
| 61 | * Type of page that is being loaded |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | public $page_type; |
| 66 | |
| 67 | /** |
| 68 | * Page type code for IPW config |
| 69 | * |
| 70 | * @var int |
| 71 | */ |
| 72 | public $page_type_ipw; |
| 73 | |
| 74 | /** |
| 75 | * Setup parameters for serving the ads |
| 76 | * |
| 77 | * @since 4.5.0 |
| 78 | */ |
| 79 | public function __construct() { |
| 80 | // WordAds setting => default. |
| 81 | $settings = array( |
| 82 | 'wordads_approved' => false, |
| 83 | 'wordads_active' => false, |
| 84 | 'wordads_house' => true, |
| 85 | 'wordads_unsafe' => false, |
| 86 | 'enable_header_ad' => true, |
| 87 | 'wordads_second_belowpost' => true, |
| 88 | 'wordads_display_front_page' => true, |
| 89 | 'wordads_display_post' => true, |
| 90 | 'wordads_display_page' => true, |
| 91 | 'wordads_display_archive' => true, |
| 92 | 'wordads_custom_adstxt' => '', |
| 93 | 'wordads_custom_adstxt_enabled' => false, |
| 94 | 'wordads_ccpa_enabled' => false, |
| 95 | 'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', |
| 96 | ); |
| 97 | |
| 98 | // Grab settings, or set as default if it doesn't exist. |
| 99 | $this->options = array(); |
| 100 | |
| 101 | foreach ( $settings as $setting => $default ) { |
| 102 | $option = get_option( $setting, null ); |
| 103 | |
| 104 | if ( $option === null ) { |
| 105 | |
| 106 | // Handle retroactively setting wordads_custom_adstxt_enabled to true if custom ads.txt content is already entered. |
| 107 | if ( 'wordads_custom_adstxt_enabled' === $setting ) { |
| 108 | $default = get_option( 'wordads_custom_adstxt' ) !== ''; |
| 109 | } |
| 110 | |
| 111 | // Convert boolean options to string first to work around update_option not setting the option if the value is false. |
| 112 | // This sets the option to either '1' if true or '' if false. |
| 113 | update_option( $setting, (string) $default, true ); |
| 114 | |
| 115 | $option = $default; |
| 116 | } |
| 117 | |
| 118 | $this->options[ $setting ] = is_bool( $default ) ? (bool) $option : $option; |
| 119 | } |
| 120 | |
| 121 | $this->url = esc_url_raw( ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : 'localhost' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ); |
| 122 | if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 123 | $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) ); |
| 124 | } |
| 125 | |
| 126 | $this->cloudflare = self::is_cloudflare(); |
| 127 | $this->blog_id = Jetpack::get_option( 'id', 0 ); |
| 128 | $this->mobile_device = jetpack_is_mobile( 'any', true ); |
| 129 | $this->targeting_tags = array( |
| 130 | 'WordAds' => 1, |
| 131 | 'BlogId' => ( new Status() )->is_offline_mode() ? 0 : Jetpack_Options::get_option( 'id' ), |
| 132 | 'Domain' => esc_js( wp_parse_url( home_url(), PHP_URL_HOST ) ), |
| 133 | 'PageURL' => esc_js( $this->url ), |
| 134 | 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else? |
| 135 | 'AdSafe' => 1, // TODO. |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Is this a mobile device? |
| 141 | * |
| 142 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
| 143 | * |
| 144 | * @since 4.5.0 |
| 145 | */ |
| 146 | public function is_mobile() { |
| 147 | return ! empty( $this->mobile_device ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Is this site served by CloudFlare? |
| 152 | * |
| 153 | * @return boolean true if site is being served via CloudFlare |
| 154 | * |
| 155 | * @since 4.5.0 |
| 156 | */ |
| 157 | public static function is_cloudflare() { |
| 158 | if ( |
| 159 | defined( 'WORDADS_CLOUDFLARE' ) |
| 160 | || isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) |
| 161 | || isset( $_SERVER['HTTP_CF_IPCOUNTRY'] ) |
| 162 | || isset( $_SERVER['HTTP_CF_VISITOR'] ) |
| 163 | ) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Is this an iOS device? |
| 172 | * |
| 173 | * @return boolean true if user is browsing in iOS device |
| 174 | * |
| 175 | * @since 4.5.0 |
| 176 | */ |
| 177 | public function is_ios() { |
| 178 | return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ), true ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns the user's device (see user-agent.php) or 'desktop' |
| 183 | * |
| 184 | * @return string user device |
| 185 | * |
| 186 | * @since 4.5.0 |
| 187 | */ |
| 188 | public function get_device() { |
| 189 | global $agent_info; |
| 190 | |
| 191 | if ( ! empty( $this->mobile_device ) ) { |
| 192 | return $this->mobile_device; |
| 193 | } |
| 194 | |
| 195 | if ( $agent_info->is_ipad() ) { |
| 196 | return 'ipad'; |
| 197 | } |
| 198 | |
| 199 | return 'desktop'; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get page type. |
| 204 | * |
| 205 | * @return string The type of page that is being loaded |
| 206 | * |
| 207 | * @since 4.5.0 |
| 208 | */ |
| 209 | public function get_page_type() { |
| 210 | if ( ! empty( $this->page_type ) ) { |
| 211 | return $this->page_type; |
| 212 | } |
| 213 | |
| 214 | if ( self::is_static_home() ) { |
| 215 | $this->page_type = 'static_home'; |
| 216 | } elseif ( is_home() ) { |
| 217 | $this->page_type = 'home'; |
| 218 | } elseif ( is_page() ) { |
| 219 | $this->page_type = 'page'; |
| 220 | } elseif ( is_single() ) { |
| 221 | $this->page_type = 'post'; |
| 222 | } elseif ( is_search() ) { |
| 223 | $this->page_type = 'search'; |
| 224 | } elseif ( is_category() ) { |
| 225 | $this->page_type = 'category'; |
| 226 | } elseif ( is_archive() ) { |
| 227 | $this->page_type = 'archive'; |
| 228 | } else { |
| 229 | $this->page_type = 'wtf'; |
| 230 | } |
| 231 | |
| 232 | return $this->page_type; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Get IPW code. |
| 237 | * |
| 238 | * @return int The page type code for ipw config |
| 239 | * |
| 240 | * @since 5.6.0 |
| 241 | */ |
| 242 | public function get_page_type_ipw() { |
| 243 | if ( ! empty( $this->page_type_ipw ) ) { |
| 244 | return $this->page_type_ipw; |
| 245 | } |
| 246 | |
| 247 | $page_type_ipw = 6; |
| 248 | if ( self::is_static_home() || is_home() || is_front_page() ) { |
| 249 | $page_type_ipw = 0; |
| 250 | } elseif ( is_page() ) { |
| 251 | $page_type_ipw = 2; |
| 252 | } elseif ( is_singular() ) { |
| 253 | $page_type_ipw = 1; |
| 254 | } elseif ( is_search() ) { |
| 255 | $page_type_ipw = 4; |
| 256 | } elseif ( is_category() || is_tag() || is_archive() || is_author() ) { |
| 257 | $page_type_ipw = 3; |
| 258 | } elseif ( is_404() ) { |
| 259 | $page_type_ipw = 5; |
| 260 | } |
| 261 | |
| 262 | $this->page_type_ipw = $page_type_ipw; |
| 263 | return $page_type_ipw; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns true if page is static home |
| 268 | * |
| 269 | * @return boolean true if page is static home |
| 270 | * |
| 271 | * @since 4.5.0 |
| 272 | */ |
| 273 | public static function is_static_home() { |
| 274 | return is_front_page() && |
| 275 | 'page' === get_option( 'show_on_front' ) && |
| 276 | get_option( 'page_on_front' ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Logic for if we should show an ad |
| 281 | * |
| 282 | * @since 4.5.0 |
| 283 | */ |
| 284 | public function should_show() { |
| 285 | global $wp_query; |
| 286 | if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if ( is_single() && ! $this->options['wordads_display_post'] ) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | if ( is_page() && ! $this->options['wordads_display_page'] ) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | if ( ( is_archive() || is_search() ) && ! $this->options['wordads_display_archive'] ) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | if ( is_single() || ( is_page() && ! is_home() ) ) { |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | // TODO this would be a good place for allowing the user to specify. |
| 307 | if ( ( is_home() || is_archive() || is_search() ) && 0 === $wp_query->current_post ) { |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | } |
| 314 |