Completed
Pull Request — 2.x (#3632)
by Scott Kingsley
05:59
created

PodsField_Website::validate_url()   D

Complexity

Conditions 24
Paths 104

Size

Total Lines 80
Code Lines 55

Duplication

Lines 8
Ratio 10 %

Importance

Changes 0
Metric Value
cc 24
eloc 55
nc 104
nop 2
dl 8
loc 80
rs 4.9177
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
 * @package Pods\Fields
4
 */
5
class PodsField_Website extends PodsField {
6
7
	/**
8
	 * Field Type Group
9
	 *
10
	 * @var string
11
	 * @since 2.0
12
	 */
13
	public static $group = 'Text';
14
15
	/**
16
	 * Field Type Identifier
17
	 *
18
	 * @var string
19
	 * @since 2.0
20
	 */
21
	public static $type = 'website';
22
23
	/**
24
	 * Field Type Label
25
	 *
26
	 * @var string
27
	 * @since 2.0
28
	 */
29
	public static $label = 'Website';
30
31
	/**
32
	 * Field Type Preparation
33
	 *
34
	 * @var string
35
	 * @since 2.0
36
	 */
37
	public static $prepare = '%s';
38
39
	/**
40
	 * Do things like register/enqueue scripts and stylesheets
41
	 *
42
	 * @since 2.0
43
	 */
44
	public function __construct () {
45
46
	}
47
48
	/**
49
	 * Add options and set defaults to
50
	 *
51
	 * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
	 *
53
	 * @since 2.0
54
	 */
55
	public function options () {
56
		$options = array(
57
			self::$type . '_repeatable' => array(
58
				'label' => __( 'Repeatable Field', 'pods' ),
59
				'default' => 0,
60
				'type' => 'boolean',
61
				'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
62
				'boolean_yes_label' => '',
63
				'dependency' => true,
64
				'developer_mode' => true
65
			),
66
			self::$type . '_format' => array(
67
				'label' => __( 'Format', 'pods' ),
68
				'default' => 'normal',
69
				'type' => 'pick',
70
				'data' => array(
71
					'normal' => __( 'http://example.com/', 'pods' ),
72
					'no-www' => __( 'http://example.com/ (remove www)', 'pods' ),
73
					'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
74
					'no-http' => __( 'example.com', 'pods' ),
75
					'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),
76
					'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' )
77
				)
78
			),
79
			self::$type . '_clickable' => array(
80
				'label' => __( 'Output as a link?', 'pods' ),
81
				'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, self::$type ),
82
				'type' => 'boolean',
83
				'dependency' => true,
84
			),
85
			self::$type . '_new_window' => array(
86
				'label' => __( 'Open link in new window?', 'pods' ),
87
				'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, self::$type ),
88
				'type' => 'boolean',
89
				'depends-on' => array( self::$type . '_clickable' => true ),
90
			),
91
			self::$type . '_max_length' => array(
92
				'label' => __( 'Maximum Length', 'pods' ),
93
				'default' => 255,
94
				'type' => 'number',
95
				'help' => __( 'Set to -1 for no limit', 'pods' )
96
			),
97
			self::$type . '_html5' => array(
98
				'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
99
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
100
				'type' => 'boolean'
101
			)/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
			self::$type . '_size' => array(
103
				'label' => __( 'Field Size', 'pods' ),
104
				'default' => 'medium',
105
				'type' => 'pick',
106
				'data' => array(
107
					'small' => __( 'Small', 'pods' ),
108
					'medium' => __( 'Medium', 'pods' ),
109
					'large' => __( 'Large', 'pods' )
110
				)
111
			)*/
112
		);
113
		return $options;
114
	}
115
116
	/**
117
	 * Define the current field's schema for DB table storage
118
	 *
119
	 * @param array $options
120
	 *
121
	 * @return array
122
	 * @since 2.0
123
	 */
124 View Code Duplication
	public function schema ( $options = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
126
127
		$schema = 'VARCHAR(' . $length . ')';
128
129
		if ( 255 < $length || $length < 1 )
130
			$schema = 'LONGTEXT';
131
132
		return $schema;
133
	}
134
135
	/**
136
	 * Change the way the value of the field is displayed with Pods::get
137
	 *
138
	 * @param mixed $value
139
	 * @param string $name
140
	 * @param array $options
141
	 * @param array $pod
142
	 * @param int $id
143
	 *
144
	 * @return mixed|null
145
	 * @since 2.0
146
	 */
147
	public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
148
		// Ensure proper format
149
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
150
151
		if ( 1 == pods_v( self::$type . '_clickable', $options ) && 0 < strlen( $value ) ) {
152
			$link = '<a href="%s"%s>%s</a>';
153
154
			$atts = '';
155
156
			if ( 1 == pods_v( self::$type . '_new_window', $options ) ) {
157
				$atts .= ' target="_blank"';
158
			}
159
160
			$value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) );
161
		}
162
163
		return $value;
164
	}
165
166
	/**
167
	 * Customize output of the form field
168
	 *
169
	 * @param string $name
170
	 * @param mixed $value
171
	 * @param array $options
172
	 * @param array $pod
173
	 * @param int $id
174
	 *
175
	 * @since 2.0
176
	 */
177 View Code Duplication
	public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
		$options = (array) $options;
179
		$form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
180
181
		if ( is_array( $value ) )
182
			$value = implode( ' ', $value );
183
184
		$field_type = 'website';
185
186
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
187
			if ( pods_var( 'read_only', $options, false ) ) {
188
				$options[ 'readonly' ] = true;
189
190
				$field_type = 'text';
191
			}
192
			else
193
				return;
194
		}
195
		elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
196
			$options[ 'readonly' ] = true;
197
198
			$field_type = 'text';
199
		}
200
201
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
202
	}
203
204
	/**
205
	 * Validate a value before it's saved
206
	 *
207
	 * @param mixed $value
208
	 * @param string $name
209
	 * @param array $options
210
	 * @param array $fields
211
	 * @param array $pod
212
	 * @param int $id
213
	 *
214
	 * @since 2.0
215
	 */
216 View Code Duplication
	public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
		$errors = array();
218
219
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
220
221
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
222
223
		if ( is_array( $check ) )
224
			$errors = $check;
225
		else {
226
			if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
227
				if ( 1 == pods_var( 'required', $options ) )
228
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
229
				else
230
					$errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
231
			}
232
		}
233
234
		if ( !empty( $errors ) )
235
			return $errors;
236
237
		return true;
238
	}
239
240
	/**
241
	 * Change the value or perform actions after validation but before saving to the DB
242
	 *
243
	 * @param mixed $value
244
	 * @param int $id
245
	 * @param string $name
246
	 * @param array $options
247
	 * @param array $fields
248
	 * @param array $pod
249
	 * @param object $params
250
	 *
251
	 * @since 2.0
252
	 */
253 View Code Duplication
	public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
		$options = (array) $options;
255
256
		$value = $this->validate_url( $value, $options );
257
258
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
259
260
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
261
			$value = pods_mb_substr( $value, 0, $length );
262
		}
263
264
		return $value;
265
	}
266
267
	/**
268
	 * Customize the Pods UI manage table column output
269
	 *
270
	 * @param int $id
271
	 * @param mixed $value
272
	 * @param string $name
273
	 * @param array $options
274
	 * @param array $fields
275
	 * @param array $pod
276
	 *
277
	 * @since 2.0
278
	 */
279
	public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
280
		$value = $this->display( $value, $name, $options, $pod, $id );
281
282
		return $value;
283
	}
284
285
	/**
286
	 * Validate an URL with the options
287
	 *
288
	 * @param string $value
289
	 * @param array $options
290
	 *
291
	 * @since 2.7
292
	 */
293
	public function validate_url( $value, $options = null ) {
294
		if ( empty( $value ) )
295
			return $value;
296
297
		if ( 'none' != pods_var( self::$type . '_format', $options ) ) {
298
			if ( is_array( $value ) ) {
299
				if ( isset( $value[ 'scheme' ] ) )
300
					$value = $this->build_url( $value );
301
				else
302
					$value = implode( '', $value );
303
			}
304
305
			if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) )
306
				$value = 'http://' . $value;
307
308
			$url = @parse_url( $value );
309
310
			if ( empty( $url ) || count( $url ) < 2 )
311
				$value = '';
312
			else {
313
				$defaults = array(
314
					'scheme' => 'http',
315
					'host' => '',
316
					'path' => '/',
317
					'query' => '',
318
					'fragment' => ''
319
				);
320
321
				$url = array_merge( $defaults, $url );
322
323
				if ( 'normal' == pods_var( self::$type . '_format', $options ) )
324
					$value = $this->build_url( $url );
325
				elseif ( 'no-www' == pods_var( self::$type . '_format', $options ) ) {
326 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
327
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
328
329
					$value = $this->build_url( $url );
330
				}
331
				elseif ( 'force-www' == pods_var( self::$type . '_format', $options ) ) {
332 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
333
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
334
335
					$value = $this->build_url( $url );
336
				}
337
				elseif ( 'no-http' == pods_var( self::$type . '_format', $options ) ) {
338
					$value = $this->build_url( $url );
339
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
340
341
					if ( '/' == $url[ 'path' ] )
342
						$value = trim( $value, '/' );
343
				}
344
				elseif ( 'no-http-no-www' == pods_var( self::$type . '_format', $options ) ) {
345 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
346
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
347
348
					$value = $this->build_url( $url );
349
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
350
351
					if ( '/' == $url[ 'path' ] )
352
						$value = trim( $value, '/' );
353
				}
354
				elseif ( 'no-http-force-www' == pods_var( self::$type . '_format', $options ) ) {
355 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
356
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
357
358
					$value = $this->build_url( $url );
359
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
360
361
					if ( '/' == $url[ 'path' ] )
362
						$value = trim( $value, '/' );
363
				}
364
			}
365
		} else {
366
			$value = $this->strip_html( $value, $options );
367
		}
368
369
		$value = esc_url( $value );
370
371
		return $value;
372
	}
373
374
	/**
375
	 * Strip HTML based on options
376
	 *
377
	 * @param string $value
378
	 * @param array $options
379
	 *
380
	 * @return string
381
	 *
382
	 * @since 2.7
383
	 */
384 View Code Duplication
	public function strip_html ( $value, $options = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
385
		if ( is_array( $value ) )
386
			$value = @implode( ' ', $value );
387
388
		$value = trim( $value );
389
390
		if ( empty( $value ) )
391
			return $value;
392
393
		$options = (array) $options;
394
395
		if ( 1 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) ) {
396
			$allowed_html_tags = '';
397
398
			if ( 0 < strlen( pods_var( self::$type . '_allowed_html_tags', $options ) ) ) {
399
				$allowed_html_tags = explode( ' ', trim( pods_var( self::$type . '_allowed_html_tags', $options ) ) );
400
				$allowed_html_tags = '<' . implode( '><', $allowed_html_tags ) . '>';
401
			}
402
403
			if ( !empty( $allowed_html_tags ) && '<>' != $allowed_html_tags )
404
				$value = strip_tags( $value, $allowed_html_tags );
405
		}
406
		else
407
			$value = strip_tags( $value );
408
409
		return $value;
410
	}
411
412
	/**
413
	 * Validate an target attribute with the options
414
	 *
415
	 * @param string $value
416
	 * @param array $options
417
	 *
418
	 * @since 2.7
419
	 */
420
	public function validate_target( $value, $options = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
421
		if ( ! empty( $value ) && $value == '_blank' ) {
422
			$value = '_blank';
423
		} else {
424
			$value = '';
425
		}
426
		return $value;
427
	}
428
429
	/**
430
	 * Build an url
431
	 *
432
	 * @param array|string $url
433
	 *
434
	 * @return string
435
	 */
436
	public function build_url ( $url ) {
437
		if ( function_exists( 'http_build_url' ) )
438
			return http_build_url( $url );
439
440
		$defaults = array(
441
			'scheme' => 'http',
442
			'host' => '',
443
			'path' => '/',
444
			'query' => '',
445
			'fragment' => ''
446
		);
447
448
		$url = array_merge( $defaults, (array) $url );
449
450
		$new_url = trim( $url[ 'scheme' ] . '://', ':' ) . $url[ 'host' ] . '/' . ltrim( $url[ 'path' ], '/' );
451
452
		if ( !empty( $url[ 'query' ] ) )
453
			$new_url .= '?' . ltrim( $url[ 'query' ], '?' );
454
455
		if ( !empty( $url[ 'fragment' ] ) )
456
			$new_url .= '#' . ltrim( $url[ 'fragment' ], '#' );
457
458
		return $new_url;
459
	}
460
461
}