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