Completed
Push — update/publicize-settings-ui ( 6f3aeb...ae05fe )
by
unknown
07:30
created

WordAds_Params::is_mobile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
			'enable_header_ad'           => true,
17
			'wordads_second_belowpost'   => true,
18
			'wordads_display_front_page' => true,
19
			'wordads_display_post'       => true,
20
			'wordads_display_page'       => true,
21
			'wordads_display_archive'    => true,
22
		);
23
24
		// grab settings, or set as default if it doesn't exist
25
		$this->options = array();
0 ignored issues
show
Bug introduced by
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
		foreach ( $settings as $setting => $default ) {
27
			$option = get_option( $setting, null );
28
			if ( is_null( $option ) ) {
29
				update_option( $setting, $default, true );
30
				$option = $default;
31
			}
32
33
			$this->options[$setting] = (bool) $option;
34
		}
35
36
		$host = 'localhost';
37
		if ( isset( $_SERVER['HTTP_HOST'] ) ) {
38
			$host = $_SERVER['HTTP_HOST'];
39
		}
40
41
		$this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI'];
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
		if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
43
			$this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
44
		}
45
46
		$this->cloudflare = self::is_cloudflare();
0 ignored issues
show
Bug introduced by
The property cloudflare does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
		$this->blog_id = Jetpack::get_option( 'id', 0 );
0 ignored issues
show
Bug introduced by
The property blog_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
		$this->mobile_device = jetpack_is_mobile( 'any', true );
0 ignored issues
show
Bug introduced by
The property mobile_device does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
		$this->targeting_tags = array(
0 ignored issues
show
Bug introduced by
The property targeting_tags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
			'WordAds'   => 1,
51
			'BlogId'    => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
52
			'Domain'    => esc_js( parse_url( home_url(), PHP_URL_HOST ) ),
53
			'PageURL'   => esc_js( $this->url ),
54
			'LangId'    => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else?
55
			'AdSafe'    => 1, // TODO
56
		);
57
	}
58
59
	/**
60
	 * @return boolean true if the user is browsing on a mobile device (iPad not included)
61
	 *
62
	 * @since 4.5.0
63
	 */
64
	public function is_mobile() {
65
		return ! empty( $this->mobile_device );
66
	}
67
68
	/**
69
	 * @return boolean true if site is being served via CloudFlare
70
	 *
71
	 * @since 4.5.0
72
	 */
73
	public static function is_cloudflare() {
74
		if (
75
			defined( 'WORDADS_CLOUDFLARE' )
76
			|| isset( $_SERVER['HTTP_CF_CONNECTING_IP'] )
77
			|| isset( $_SERVER['HTTP_CF_IPCOUNTRY'] )
78
			|| isset( $_SERVER['HTTP_CF_VISITOR'] )
79
		) {
80
			return true;
81
		}
82
83
		return false;
84
	}
85
86
	/**
87
	 * @return boolean true if user is browsing in iOS device
88
	 *
89
	 * @since 4.5.0
90
	 */
91
	public function is_ios() {
92
		return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
93
	}
94
95
	/**
96
	 * Returns the user's device (see user-agent.php) or 'desktop'
97
	 * @return string user device
98
	 *
99
	 * @since 4.5.0
100
	 */
101
	public function get_device() {
102
		global $agent_info;
103
104
		if ( ! empty( $this->mobile_device ) ) {
105
			return $this->mobile_device;
106
		}
107
108
		if ( $agent_info->is_ipad() ) {
109
			return 'ipad';
110
		}
111
112
		return 'desktop';
113
	}
114
115
	/**
116
	 * @return string The type of page that is being loaded
117
	 *
118
	 * @since 4.5.0
119
	 */
120
	public function get_page_type() {
121
		if ( ! empty( $this->page_type ) ) {
122
			return $this->page_type;
0 ignored issues
show
Bug introduced by
The property page_type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
		}
124
125
		if ( self::is_static_home() ) {
126
			$this->page_type = 'static_home';
127
		} else if ( is_home() ) {
128
			$this->page_type = 'home';
129
		} else if ( is_page() ) {
130
			$this->page_type = 'page';
131
		} else if ( is_single() ) {
132
			$this->page_type = 'post';
133
		} else if ( is_search() ) {
134
			$this->page_type = 'search';
135
		} else if ( is_category() ) {
136
			$this->page_type = 'category';
137
		} else if ( is_archive() ) {
138
			$this->page_type = 'archive';
139
		} else {
140
			$this->page_type = 'wtf';
141
		}
142
143
		return $this->page_type;
144
	}
145
146
	/**
147
	 * @return int The page type code for ipw config
148
	 *
149
	 * @since 5.6.0
150
	 */
151
	public function get_page_type_ipw() {
152
		if ( ! empty( $this->page_type_ipw ) ) {
0 ignored issues
show
Bug introduced by
The property page_type_ipw does not seem to exist. Did you mean page_type?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
153
			return $this->page_type_ipw;
0 ignored issues
show
Bug introduced by
The property page_type_ipw does not seem to exist. Did you mean page_type?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
154
		}
155
156
		$page_type_ipw = 6;
157
		if ( self::is_static_home() || is_home() || is_front_page() ) {
158
			$page_type_ipw = 0;
159
		} else if ( is_page() ) {
160
			$page_type_ipw = 2;
161
		} else if ( is_singular() ) {
162
			$page_type_ipw = 1;
163
		} else if ( is_search() ) {
164
			$page_type_ipw = 4;
165
		} else if ( is_category() || is_tag() || is_archive() || is_author() ) {
166
			$page_type_ipw = 3;
167
		} else if ( is_404() ) {
168
			$page_type_ipw = 5;
169
		}
170
171
		$this->page_type_ipw = $page_type_ipw;
0 ignored issues
show
Bug introduced by
The property page_type_ipw does not seem to exist. Did you mean page_type?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
172
		return $page_type_ipw;
173
	}
174
175
	/**
176
	 * Returns true if page is static home
177
	 * @return boolean true if page is static home
178
	 *
179
	 * @since 4.5.0
180
	 */
181
	public static function is_static_home() {
182
		return is_front_page() &&
183
			'page' == get_option( 'show_on_front' ) &&
184
			get_option( 'page_on_front' );
185
	}
186
187
	/**
188
	 * Logic for if we should show an ad
189
	 *
190
	 * @since 4.5.0
191
	 */
192
	public function should_show() {
193
		global $wp_query;
194
		if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) {
195
			return false;
196
		}
197
198
		if ( is_single() && ! $this->options['wordads_display_post'] ) {
199
			return false;
200
		}
201
202
		if ( is_page() && ! $this->options['wordads_display_page'] ) {
203
			return false;
204
		}
205
206
		if ( is_archive() && ! $this->options['wordads_display_archive'] ) {
207
			return false;
208
		}
209
210
		if ( is_single() || ( is_page() && ! is_home() ) ) {
211
			return true;
212
		}
213
214
		// TODO this would be a good place for allowing the user to specify
215
		if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
216
			return true;
217
		}
218
219
		return false;
220
	}
221
}
222