Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsField_Website::validate_url()   D

Complexity

Conditions 24
Paths 104

Size

Total Lines 81
Code Lines 56

Duplication

Lines 14
Ratio 17.28 %

Importance

Changes 0
Metric Value
cc 24
eloc 56
nc 104
nop 2
dl 14
loc 81
rs 4.873
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
		self::$label = __( 'Website', 'pods' );
47
48
	}
49
50
	/**
51
	 * Add options and set defaults to
52
	 *
53
	 * @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...
54
	 *
55
	 * @since 2.0
56
	 */
57
	public function options () {
58
		$options = array(
59
			self::$type . '_repeatable' => array(
60
				'label' => __( 'Repeatable Field', 'pods' ),
61
				'default' => 0,
62
				'type' => 'boolean',
63
				'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' ),
64
				'boolean_yes_label' => '',
65
				'dependency' => true,
66
				'developer_mode' => true
67
			),
68
			self::$type . '_format' => array(
69
				'label' => __( 'Format', 'pods' ),
70
				'default' => 'normal',
71
				'type' => 'pick',
72
				'data' => array(
73
					'normal' => __( 'http://example.com/', 'pods' ),
74
					'no-www' => __( 'http://example.com/ (remove www)', 'pods' ),
75
					'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
76
					'no-http' => __( 'example.com', 'pods' ),
77
					'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),
78
					'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' )
79
				)
80
			),
81
			self::$type . '_allow_port' => array(
82
				'label' => __( 'Allow port in URL?', 'pods' ),
83
				'default' => apply_filters( 'pods_form_ui_field_website_port', 0, self::$type ),
84
				'type' => 'boolean',
85
				'dependency' => true,
86
			),
87
			self::$type . '_clickable' => array(
88
				'label' => __( 'Output as a link?', 'pods' ),
89
				'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, self::$type ),
90
				'type' => 'boolean',
91
				'dependency' => true,
92
			),
93
			self::$type . '_new_window' => array(
94
				'label' => __( 'Open link in new window?', 'pods' ),
95
				'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, self::$type ),
96
				'type' => 'boolean',
97
				'depends-on' => array( self::$type . '_clickable' => true ),
98
			),
99
			self::$type . '_max_length' => array(
100
				'label' => __( 'Maximum Length', 'pods' ),
101
				'default' => 255,
102
				'type' => 'number',
103
				'help' => __( 'Set to -1 for no limit', 'pods' )
104
			),
105
			self::$type . '_html5' => array(
106
				'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
107
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
108
				'type' => 'boolean'
109
			),
110
			self::$type . '_placeholder' => array(
111
				'label' => __( 'HTML Placeholder', 'pods' ),
112
				'default' => '',
113
				'type' => 'text',
114
				'help' => array(
115
					__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
116
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
117
				),
118
			),/*,
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...
119
			self::$type . '_size' => array(
120
				'label' => __( 'Field Size', 'pods' ),
121
				'default' => 'medium',
122
				'type' => 'pick',
123
				'data' => array(
124
					'small' => __( 'Small', 'pods' ),
125
					'medium' => __( 'Medium', 'pods' ),
126
					'large' => __( 'Large', 'pods' )
127
				)
128
			)*/
129
		);
130
		return $options;
131
	}
132
133
	/**
134
	 * Define the current field's schema for DB table storage
135
	 *
136
	 * @param array $options
137
	 *
138
	 * @return array
139
	 * @since 2.0
140
	 */
141 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...
142
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
143
144
		$schema = 'VARCHAR(' . $length . ')';
145
146
		if ( 255 < $length || $length < 1 )
147
			$schema = 'LONGTEXT';
148
149
		return $schema;
150
	}
151
152
	/**
153
	 * Change the way the value of the field is displayed with Pods::get
154
	 *
155
	 * @param mixed $value
156
	 * @param string $name
157
	 * @param array $options
158
	 * @param array $pod
159
	 * @param int $id
160
	 *
161
	 * @return mixed|null
162
	 * @since 2.0
163
	 */
164
	public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
165
		// Ensure proper format
166
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
167
168
		if ( 1 == pods_v( self::$type . '_clickable', $options ) && 0 < strlen( $value ) ) {
169
			$link = '<a href="%s"%s>%s</a>';
170
171
			$atts = '';
172
173
			if ( 1 == pods_v( self::$type . '_new_window', $options ) ) {
174
				$atts .= ' target="_blank"';
175
			}
176
177
			$value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) );
178
		}
179
180
		return $value;
181
	}
182
183
	/**
184
	 * Customize output of the form field
185
	 *
186
	 * @param string $name
187
	 * @param mixed $value
188
	 * @param array $options
189
	 * @param array $pod
190
	 * @param int $id
191
	 *
192
	 * @since 2.0
193
	 */
194
	public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
195
		$options = (array) $options;
196
		$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...
197
198
		// Ensure proper format
199
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
200
201
		$field_type = 'website';
202
203
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
204
			if ( pods_var( 'read_only', $options, false ) ) {
205
				$options[ 'readonly' ] = true;
206
207
				$field_type = 'text';
208
			}
209
			else
210
				return;
211
		}
212
		elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
213
			$options[ 'readonly' ] = true;
214
215
			$field_type = 'text';
216
		}
217
218
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
219
	}
220
221
	/**
222
	 * Validate a value before it's saved
223
	 *
224
	 * @param mixed $value
225
	 * @param string $name
226
	 * @param array $options
227
	 * @param array $fields
228
	 * @param array $pod
229
	 * @param int $id
230
	 *
231
	 * @return bool|array
232
	 *
233
	 * @since 2.0
234
	 */
235 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...
236
		$errors = array();
237
238
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
239
240
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
241
242
		if ( is_array( $check ) )
243
			$errors = $check;
244
		else {
245
			if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
246
				if ( 1 == pods_var( 'required', $options ) )
247
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
248
				else
249
					$errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
250
			}
251
		}
252
253
		if ( !empty( $errors ) )
254
			return $errors;
255
256
		return true;
257
	}
258
259
	/**
260
	 * Change the value or perform actions after validation but before saving to the DB
261
	 *
262
	 * @param mixed $value
263
	 * @param int $id
264
	 * @param string $name
265
	 * @param array $options
266
	 * @param array $fields
267
	 * @param array $pod
268
	 * @param object $params
269
	 *
270
	 * @return string
271
	 *
272
	 * @since 2.0
273
	 */
274
	public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
275
		$options = (array) $options;
276
277
		// Update from a array input field (like link) if the field updates
278 View Code Duplication
		if ( is_array( $value ) ) {
279
			if ( isset( $value['url'] ) ) {
280
				$value = $value['url'];
281
			} else {
282
				$value = implode( ' ', $value );
283
			}
284
		}
285
286
		$value = $this->validate_url( $value, $options );
287
288
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
289
290
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
291
			$value = pods_mb_substr( $value, 0, $length );
292
		}
293
294
		return $value;
295
	}
296
297
	/**
298
	 * Customize the Pods UI manage table column output
299
	 *
300
	 * @param int $id
301
	 * @param mixed $value
302
	 * @param string $name
303
	 * @param array $options
304
	 * @param array $fields
305
	 * @param array $pod
306
	 *
307
	 * @return string
308
	 *
309
	 * @since 2.0
310
	 */
311
	public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
312
		$value = $this->display( $value, $name, $options, $pod, $id );
313
314
		return $value;
315
	}
316
317
	/**
318
	 * Validate an URL with the options
319
	 *
320
	 * @param string $value
321
	 * @param array $options
322
	 *
323
	 * @return string
324
	 *
325
	 * @since 2.7
326
	 */
327
	public function validate_url( $value, $options = null ) {
328
		if ( empty( $value ) )
329
			return $value;
330
331
		if ( 'none' != pods_var( self::$type . '_format', $options ) ) {
332 View Code Duplication
			if ( is_array( $value ) ) {
333
				if ( isset( $value[ 'scheme' ] ) )
334
					$value = $this->build_url( $value, $options );
335
				else
336
					$value = implode( '', $value );
337
			}
338
339
			if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) )
340
				$value = 'http://' . $value;
341
342
			$url = @parse_url( $value );
343
344
			if ( empty( $url ) || count( $url ) < 2 )
345
				$value = '';
346
			else {
347
				$defaults = array(
348
					'scheme' => 'http',
349
					'host' => '',
350
					'port' => '',
351
					'path' => '/',
352
					'query' => '',
353
					'fragment' => ''
354
				);
355
356
				$url = array_merge( $defaults, $url );
357
358
				if ( 'normal' == pods_var( self::$type . '_format', $options ) )
359
					$value = $this->build_url( $url, $options );
360
				elseif ( 'no-www' == pods_var( self::$type . '_format', $options ) ) {
361 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
362
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
363
364
					$value = $this->build_url( $url, $options );
365
				}
366
				elseif ( 'force-www' == pods_var( self::$type . '_format', $options ) ) {
367 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
368
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
369
370
					$value = $this->build_url( $url, $options );
371
				}
372
				elseif ( 'no-http' == pods_var( self::$type . '_format', $options ) ) {
373
					$value = $this->build_url( $url, $options );
374
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
375
376
					if ( '/' == $url[ 'path' ] )
377
						$value = trim( $value, '/' );
378
				}
379
				elseif ( 'no-http-no-www' == pods_var( self::$type . '_format', $options ) ) {
380 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
381
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
382
383
					$value = $this->build_url( $url, $options );
384
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
385
386
					if ( '/' == $url[ 'path' ] )
387
						$value = trim( $value, '/' );
388
				}
389
				elseif ( 'no-http-force-www' == pods_var( self::$type . '_format', $options ) ) {
390 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
391
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
392
393
					$value = $this->build_url( $url, $options );
394
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
395
396
					if ( '/' == $url[ 'path' ] )
397
						$value = trim( $value, '/' );
398
				}
399
			}
400
		} else {
401
			$value = $this->strip_html( $value, $options );
402
		}
403
404
		$value = esc_url( $value );
405
406
		return $value;
407
	}
408
409
	/**
410
	 * Strip HTML based on options
411
	 *
412
	 * @param string $value
413
	 * @param array $options
414
	 *
415
	 * @return string
416
	 *
417
	 * @since 2.7
418
	 */
419 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...
420
		if ( is_array( $value ) )
421
			$value = @implode( ' ', $value );
422
423
		$value = trim( $value );
424
425
		if ( empty( $value ) )
426
			return $value;
427
428
		$options = (array) $options;
429
430
		if ( 1 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) ) {
431
			$allowed_html_tags = '';
432
433
			if ( 0 < strlen( pods_var( self::$type . '_allowed_html_tags', $options ) ) ) {
434
				$allowed_html_tags = explode( ' ', trim( pods_var( self::$type . '_allowed_html_tags', $options ) ) );
435
				$allowed_html_tags = '<' . implode( '><', $allowed_html_tags ) . '>';
436
			}
437
438
			if ( !empty( $allowed_html_tags ) && '<>' != $allowed_html_tags )
439
				$value = strip_tags( $value, $allowed_html_tags );
440
		}
441
		else
442
			$value = strip_tags( $value );
443
444
		return $value;
445
	}
446
447
	/**
448
	 * Validate an target attribute with the options
449
	 *
450
	 * @param string $value
451
	 *
452
	 * @return string
453
	 *
454
	 * @since 2.7
455
	 */
456
	public function validate_target( $value ) {
457
		if ( ! empty( $value ) && $value == '_blank' ) {
458
			$value = '_blank';
459
		} else {
460
			$value = '';
461
		}
462
		return $value;
463
	}
464
465
	/**
466
	 * Build a url from url parts
467
	 *
468
	 * @param array|string $url
469
	 * @param array        $options
470
	 *
471
	 * @return string
472
	 */
473
	public function build_url( $url, $options = array() ) {
474
475
		$url = (array) $url;
476
477
		$allow_port = (int) pods_v( self::$type . '_allow_port', $options, 0 );
478
479
		// If port is not allowed, always set to empty
480
		if ( 0 === $allow_port ) {
481
			$url['port'] = '';
482
		}
483
484
		if ( function_exists( 'http_build_url' ) ) {
485
			return http_build_url( $url );
486
		}
487
488
		$defaults = array(
489
			'scheme'   => 'http',
490
			'host'     => '',
491
			'port'     => '',
492
			'path'     => '/',
493
			'query'    => '',
494
			'fragment' => ''
495
		);
496
497
		$url = array_merge( $defaults, $url );
498
499
		$new_url = array();
500
501
		$new_url[] = trim( $url['scheme'] . '://', ':' );
502
		$new_url[] = $url['host'];
503
504
		if ( ! empty( $url['port'] ) ) {
505
			$new_url[] = ':' . $url['port'];
506
		}
507
508
		$new_url[] = '/' . ltrim( $url['path'], '/' );
509
510
		if ( ! empty( $url['query'] ) ) {
511
			$new_url[] = '?' . ltrim( $url['query'], '?' );
512
		}
513
514
		if ( ! empty( $url['fragment'] ) ) {
515
			$new_url[] = '#' . ltrim( $url['fragment'], '#' );
516
		}
517
518
		// Pull all of the parts back together
519
		$new_url = implode( '', $new_url );
520
521
		return $new_url;
522
523
	}
524
525
}
526