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