Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

WordAds_Params::__construct()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 32
nop 0
dl 0
loc 30
rs 6.7272
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
		$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...
12
			'wordads_approved' => (bool) get_option( 'wordads_approved', false ),
13
			'wordads_active'   => (bool) get_option( 'wordads_active',   false ),
14
			'wordads_house'    => (bool) get_option( 'wordads_house',    true ),
15
			'enable_header_ad' => (bool) get_option( 'enable_header_ad', false )
16
		);
17
18
		$host = 'localhost';
19
		if ( isset( $_SERVER['HTTP_HOST'] ) ) {
20
			$host = $_SERVER['HTTP_HOST'];
21
		}
22
23
		$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...
24
		if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
25
			$this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
26
		}
27
28
		$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...
29
		$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...
30
		$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...
31
		$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...
32
			'WordAds'   => 1,
33
			'BlogId'    => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
34
			'Domain'    => esc_js( parse_url( home_url(), PHP_URL_HOST ) ),
35
			'PageURL'   => esc_js( $this->url ),
36
			'LangId'    => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
37
			'AdSafe'    => 1, // TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
38
		);
39
	}
40
41
	/**
42
	 * @return boolean true if the user is browsing on a mobile device (iPad not included)
43
	 *
44
	 * @since 4.5.0
45
	 */
46
	public function is_mobile() {
47
		return ! empty( $this->mobile_device );
48
	}
49
50
	/**
51
	 * @return boolean true if site is being served via CloudFlare
52
	 *
53
	 * @since 4.5.0
54
	 */
55
	public static function is_cloudflare() {
56
		if ( defined( 'WORDADS_CLOUDFLARE' ) ) {
57
			return true;
58
		}
59
		if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
60
			return true;
61
		}
62
		if ( isset( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) {
63
			return true;
64
		}
65
		if ( isset( $_SERVER['HTTP_CF_VISITOR'] ) ) {
66
			return true;
67
		}
68
69
		return false;
70
	}
71
72
	/**
73
	 * @return boolean true if user is browsing in iOS device
74
	 *
75
	 * @since 4.5.0
76
	 */
77
	public function is_ios() {
78
		return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
79
	}
80
81
	/**
82
	 * Returns the user's device (see user-agent.php) or 'desktop'
83
	 * @return string user device
84
	 *
85
	 * @since 4.5.0
86
	 */
87
	public function get_device() {
88
		global $agent_info;
89
90
		if ( ! empty( $this->mobile_device ) ) {
91
			return $this->mobile_device;
92
		}
93
94
		if ( $agent_info->is_ipad() ) {
95
			return 'ipad';
96
		}
97
98
		return 'desktop';
99
	}
100
101
	/**
102
	 * @return string The type of page that is being loaded
103
	 *
104
	 * @since 4.5.0
105
	 */
106
	public function get_page_type() {
107
		if ( ! empty( $this->page_type ) ) {
108
			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...
109
		}
110
111
		if ( self::is_static_home() ) {
112
			$this->page_type = 'static_home';
113
		} else if ( is_home() ) {
114
			$this->page_type = 'home';
115
		} else if ( is_page() ) {
116
			$this->page_type = 'page';
117
		} else if ( is_single() ) {
118
			$this->page_type = 'post';
119
		} else if ( is_search() ) {
120
			$this->page_type = 'search';
121
		} else if ( is_category() ) {
122
			$this->page_type = 'category';
123
		} else if ( is_archive() ) {
124
			$this->page_type = 'archive';
125
		} else {
126
			$this->page_type = 'wtf';
127
		}
128
129
		return $this->page_type;
130
	}
131
132
	/**
133
	 * Returns true if page is static home
134
	 * @return boolean true if page is static home
135
	 *
136
	 * @since 4.5.0
137
	 */
138
	public static function is_static_home() {
139
		return is_front_page() &&
140
			'page' == get_option( 'show_on_front' ) &&
141
			get_option( 'page_on_front' );
142
	}
143
144
	/**
145
	 * Logic for if we should show an ad
146
	 *
147
	 * @since 4.5.0
148
	 */
149
	public static function should_show() {
150
		global $wp_query;
151
		if ( is_single() || ( is_page() && ! is_home() ) ) {
152
			return true;
153
		}
154
155
		// TODO this would be a good place for allowing the user to specify
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
156
		if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
157
			return true;
158
		}
159
160
		return false;
161
	}
162
163
	/**
164
	 * Logic for if we should show a mobile ad
165
	 *
166
	 * @since 4.5.0
167
	 */
168
	public static function should_show_mobile() {
169
		global $wp_query;
170
171
		if ( ! in_the_loop() || ! did_action( 'wp_head' ) ) {
172
			return false;
173
		}
174
175
		if ( is_single() || ( is_page() && ! is_home() ) ) {
176
			return true;
177
		}
178
179
		if ( ( is_home() || is_archive() ) && 0 == $wp_query->current_post ) {
180
			return true;
181
		}
182
183
		return false;
184
	}
185
}
186