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