Completed
Push — renovate/wp-coding-standards-w... ( 227465...8587d4 )
by
unknown
35:46 queued 27:50
created

WordAds_Params::__construct()   F

Complexity

Conditions 12
Paths 448

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 448
nop 0
dl 0
loc 60
rs 3.726
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			'wordads_custom_adstxt_enabled'   => false,
27
			'wordads_ccpa_enabled'            => false,
28
			'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '',
29
		);
30
31
		// grab settings, or set as default if it doesn't exist
32
		$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...
33
		foreach ( $settings as $setting => $default ) {
34
			$option = get_option( $setting, null );
35
36
			if ( is_null( $option ) ) {
37
38
				// Handle retroactively setting wordads_custom_adstxt_enabled to true if custom ads.txt content is already entered.
39
				if ( 'wordads_custom_adstxt_enabled' === $setting ) {
40
					$default = get_option( 'wordads_custom_adstxt' ) !== '';
41
				}
42
43
				update_option( $setting, $default, true );
44
				$option = $default;
45
			}
46
47
			$this->options[ $setting ] = is_bool( $default ) ? (bool) $option : $option;
48
		}
49
50
		$host = 'localhost';
51
		if ( isset( $_SERVER['HTTP_HOST'] ) ) {
52
			$host = $_SERVER['HTTP_HOST'];
53
		}
54
55
		$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...
56
		if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
57
			$this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
58
		}
59
60
		$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...
61
		$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...
62
		$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...
63
		$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...
64
			'WordAds' => 1,
65
			'BlogId'  => ( new Status() )->is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
66
			'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...
67
			'PageURL' => esc_js( $this->url ),
68
			'LangId'  => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else?
69
			'AdSafe'  => 1, // TODO
70
		);
71
	}
72
73
	/**
74
	 * @return boolean true if the user is browsing on a mobile device (iPad not included)
75
	 *
76
	 * @since 4.5.0
77
	 */
78
	public function is_mobile() {
79
		return ! empty( $this->mobile_device );
80
	}
81
82
	/**
83
	 * @return boolean true if site is being served via CloudFlare
84
	 *
85
	 * @since 4.5.0
86
	 */
87
	public static function is_cloudflare() {
88
		if (
89
			defined( 'WORDADS_CLOUDFLARE' )
90
			|| isset( $_SERVER['HTTP_CF_CONNECTING_IP'] )
91
			|| isset( $_SERVER['HTTP_CF_IPCOUNTRY'] )
92
			|| isset( $_SERVER['HTTP_CF_VISITOR'] )
93
		) {
94
			return true;
95
		}
96
97
		return false;
98
	}
99
100
	/**
101
	 * @return boolean true if user is browsing in iOS device
102
	 *
103
	 * @since 4.5.0
104
	 */
105
	public function is_ios() {
106
		return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
107
	}
108
109
	/**
110
	 * Returns the user's device (see user-agent.php) or 'desktop'
111
	 *
112
	 * @return string user device
113
	 *
114
	 * @since 4.5.0
115
	 */
116
	public function get_device() {
117
		global $agent_info;
118
119
		if ( ! empty( $this->mobile_device ) ) {
120
			return $this->mobile_device;
121
		}
122
123
		if ( $agent_info->is_ipad() ) {
124
			return 'ipad';
125
		}
126
127
		return 'desktop';
128
	}
129
130
	/**
131
	 * @return string The type of page that is being loaded
132
	 *
133
	 * @since 4.5.0
134
	 */
135
	public function get_page_type() {
136
		if ( ! empty( $this->page_type ) ) {
137
			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...
138
		}
139
140
		if ( self::is_static_home() ) {
141
			$this->page_type = 'static_home';
142
		} elseif ( is_home() ) {
143
			$this->page_type = 'home';
144
		} elseif ( is_page() ) {
145
			$this->page_type = 'page';
146
		} elseif ( is_single() ) {
147
			$this->page_type = 'post';
148
		} elseif ( is_search() ) {
149
			$this->page_type = 'search';
150
		} elseif ( is_category() ) {
151
			$this->page_type = 'category';
152
		} elseif ( is_archive() ) {
153
			$this->page_type = 'archive';
154
		} else {
155
			$this->page_type = 'wtf';
156
		}
157
158
		return $this->page_type;
159
	}
160
161
	/**
162
	 * @return int The page type code for ipw config
163
	 *
164
	 * @since 5.6.0
165
	 */
166
	public function get_page_type_ipw() {
167
		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...
168
			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...
169
		}
170
171
		$page_type_ipw = 6;
172
		if ( self::is_static_home() || is_home() || is_front_page() ) {
173
			$page_type_ipw = 0;
174
		} elseif ( is_page() ) {
175
			$page_type_ipw = 2;
176
		} elseif ( is_singular() ) {
177
			$page_type_ipw = 1;
178
		} elseif ( is_search() ) {
179
			$page_type_ipw = 4;
180
		} elseif ( is_category() || is_tag() || is_archive() || is_author() ) {
181
			$page_type_ipw = 3;
182
		} elseif ( is_404() ) {
183
			$page_type_ipw = 5;
184
		}
185
186
		$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...
187
		return $page_type_ipw;
188
	}
189
190
	/**
191
	 * Returns true if page is static home
192
	 *
193
	 * @return boolean true if page is static home
194
	 *
195
	 * @since 4.5.0
196
	 */
197
	public static function is_static_home() {
198
		return is_front_page() &&
199
			'page' == get_option( 'show_on_front' ) &&
200
			get_option( 'page_on_front' );
201
	}
202
203
	/**
204
	 * Logic for if we should show an ad
205
	 *
206
	 * @since 4.5.0
207
	 */
208
	public function should_show() {
209
		global $wp_query;
210
		if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) {
211
			return false;
212
		}
213
214
		if ( is_single() && ! $this->options['wordads_display_post'] ) {
215
			return false;
216
		}
217
218
		if ( is_page() && ! $this->options['wordads_display_page'] ) {
219
			return false;
220
		}
221
222
		if ( ( is_archive() || is_search() ) && ! $this->options['wordads_display_archive'] ) {
223
			return false;
224
		}
225
226
		if ( is_single() || ( is_page() && ! is_home() ) ) {
227
			return true;
228
		}
229
230
		// TODO this would be a good place for allowing the user to specify
231
		if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
232
			return true;
233
		}
234
235
		return false;
236
	}
237
}
238