Completed
Push — add/related-posts-customize ( 4a306a...1c5144 )
by
unknown
43:30 queued 35:07
created

WordAds_Params::should_show()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 7
nc 3
nop 0
dl 0
loc 13
rs 7.7777
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
		$this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['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...
19
		if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
20
			$this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
21
		}
22
		$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...
23
		$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...
24
		$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...
25
		$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...
26
			'WordAds'   => 1,
27
			'BlogId'    => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
28
			'Domain'    => esc_js( parse_url( home_url(), PHP_URL_HOST ) ),
29
			'PageURL'   => esc_js( $this->url ),
30
			'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...
31
			'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...
32
		);
33
	}
34
35
	/**
36
	 * @return boolean true if the user is browsing on a mobile device (iPad not included)
37
	 *
38
	 * @since 4.5.0
39
	 */
40
	public function is_mobile() {
41
		return ! empty( $this->mobile_device );
42
	}
43
44
	/**
45
	 * @return boolean true if site is being served via CloudFlare
46
	 *
47
	 * @since 4.5.0
48
	 */
49
	public static function is_cloudflare() {
50
		if ( defined( 'WORDADS_CLOUDFLARE' ) ) {
51
			return true;
52
		}
53
		if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
54
			return true;
55
		}
56
		if ( isset( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) {
57
			return true;
58
		}
59
		if ( isset( $_SERVER['HTTP_CF_VISITOR'] ) ) {
60
			return true;
61
		}
62
63
		return false;
64
	}
65
66
	/**
67
	 * @return boolean true if user is browsing in iOS device
68
	 *
69
	 * @since 4.5.0
70
	 */
71
	public function is_ios() {
72
		return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
73
	}
74
75
	/**
76
	 * Returns the user's device (see user-agent.php) or 'desktop'
77
	 * @return string user device
78
	 *
79
	 * @since 4.5.0
80
	 */
81
	public function get_device() {
82
		global $agent_info;
83
84
		if ( ! empty( $this->mobile_device ) ) {
85
			return $this->mobile_device;
86
		}
87
88
		if ( $agent_info->is_ipad() ) {
89
			return 'ipad';
90
		}
91
92
		return 'desktop';
93
	}
94
95
	/**
96
	 * @return string The type of page that is being loaded
97
	 *
98
	 * @since 4.5.0
99
	 */
100
	public function get_page_type() {
101
		if ( ! empty( $this->page_type ) ) {
102
			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...
103
		}
104
105
		if ( self::is_static_home() ) {
106
			$this->page_type = 'static_home';
107
		} else if ( is_home() ) {
108
			$this->page_type = 'home';
109
		} else if ( is_page() ) {
110
			$this->page_type = 'page';
111
		} else if ( is_single() ) {
112
			$this->page_type = 'post';
113
		} else if ( is_search() ) {
114
			$this->page_type = 'search';
115
		} else if ( is_category() ) {
116
			$this->page_type = 'category';
117
		} else if ( is_archive() ) {
118
			$this->page_type = 'archive';
119
		} else {
120
			$this->page_type = 'wtf';
121
		}
122
123
		return $this->page_type;
124
	}
125
126
	/**
127
	 * Returns true if page is static home
128
	 * @return boolean true if page is static home
129
	 *
130
	 * @since 4.5.0
131
	 */
132
	public static function is_static_home() {
133
		return is_front_page() &&
134
			'page' == get_option( 'show_on_front' ) &&
135
			get_option( 'page_on_front' );
136
	}
137
138
	/**
139
	 * Logic for if we should show an ad
140
	 *
141
	 * @since 4.5.0
142
	 */
143
	public static function should_show() {
144
		global $wp_query;
145
		if ( is_single() || ( is_page() && ! is_home() ) ) {
146
			return true;
147
		}
148
149
		// 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...
150
		if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
151
			return true;
152
		}
153
154
		return false;
155
	}
156
157
	/**
158
	 * Logic for if we should show a mobile ad
159
	 *
160
	 * @since 4.5.0
161
	 */
162
	public static function should_show_mobile() {
163
		global $wp_query;
164
165
		if ( ! in_the_loop() || ! did_action( 'wp_head' ) ) {
166
			return false;
167
		}
168
169
		if ( is_single() || ( is_page() && ! is_home() ) ) {
170
			return true;
171
		}
172
173
		if ( ( is_home() || is_archive() ) && 0 == $wp_query->current_post ) {
174
			return true;
175
		}
176
177
		return false;
178
	}
179
}
180