Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

WordAds_Params::is_static_home()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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();
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...
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'];
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...
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();
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...
52
		$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...
53
		$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...
54
		$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...
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 ) ),
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_HOST.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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;
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...
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 ) ) {
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...
159
			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...
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;
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...
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