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