Completed
Push — update/activity-log-links ( 92fe50...3b9362 )
by
unknown
12:21
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
			'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();
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...
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'];
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...
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();
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...
50
		$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...
51
		$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...
52
		$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...
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
	 * @return string user device
101
	 *
102
	 * @since 4.5.0
103
	 */
104
	public function get_device() {
105
		global $agent_info;
106
107
		if ( ! empty( $this->mobile_device ) ) {
108
			return $this->mobile_device;
109
		}
110
111
		if ( $agent_info->is_ipad() ) {
112
			return 'ipad';
113
		}
114
115
		return 'desktop';
116
	}
117
118
	/**
119
	 * @return string The type of page that is being loaded
120
	 *
121
	 * @since 4.5.0
122
	 */
123
	public function get_page_type() {
124
		if ( ! empty( $this->page_type ) ) {
125
			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...
126
		}
127
128
		if ( self::is_static_home() ) {
129
			$this->page_type = 'static_home';
130
		} else if ( is_home() ) {
131
			$this->page_type = 'home';
132
		} else if ( is_page() ) {
133
			$this->page_type = 'page';
134
		} else if ( is_single() ) {
135
			$this->page_type = 'post';
136
		} else if ( is_search() ) {
137
			$this->page_type = 'search';
138
		} else if ( is_category() ) {
139
			$this->page_type = 'category';
140
		} else if ( is_archive() ) {
141
			$this->page_type = 'archive';
142
		} else {
143
			$this->page_type = 'wtf';
144
		}
145
146
		return $this->page_type;
147
	}
148
149
	/**
150
	 * @return int The page type code for ipw config
151
	 *
152
	 * @since 5.6.0
153
	 */
154
	public function get_page_type_ipw() {
155
		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...
156
			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...
157
		}
158
159
		$page_type_ipw = 6;
160
		if ( self::is_static_home() || is_home() || is_front_page() ) {
161
			$page_type_ipw = 0;
162
		} else if ( is_page() ) {
163
			$page_type_ipw = 2;
164
		} else if ( is_singular() ) {
165
			$page_type_ipw = 1;
166
		} else if ( is_search() ) {
167
			$page_type_ipw = 4;
168
		} else if ( is_category() || is_tag() || is_archive() || is_author() ) {
169
			$page_type_ipw = 3;
170
		} else if ( is_404() ) {
171
			$page_type_ipw = 5;
172
		}
173
174
		$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...
175
		return $page_type_ipw;
176
	}
177
178
	/**
179
	 * Returns true if page is static home
180
	 * @return boolean true if page is static home
181
	 *
182
	 * @since 4.5.0
183
	 */
184
	public static function is_static_home() {
185
		return is_front_page() &&
186
			'page' == get_option( 'show_on_front' ) &&
187
			get_option( 'page_on_front' );
188
	}
189
190
	/**
191
	 * Logic for if we should show an ad
192
	 *
193
	 * @since 4.5.0
194
	 */
195
	public function should_show() {
196
		global $wp_query;
197
		if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) {
198
			return false;
199
		}
200
201
		if ( is_single() && ! $this->options['wordads_display_post'] ) {
202
			return false;
203
		}
204
205
		if ( is_page() && ! $this->options['wordads_display_page'] ) {
206
			return false;
207
		}
208
209
		if ( is_archive() && ! $this->options['wordads_display_archive'] ) {
210
			return false;
211
		}
212
213
		if ( is_single() || ( is_page() && ! is_home() ) ) {
214
			return true;
215
		}
216
217
		// TODO this would be a good place for allowing the user to specify
218
		if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
219
			return true;
220
		}
221
222
		return false;
223
	}
224
}
225