Completed
Push — 2.x ( 971a6c...befcfa )
by Scott Kingsley
08:24 queued 03:02
created

PodsField_Website::pre_save()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 7
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
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 . '_clickable' => array(
82
				'label' => __( 'Output as a link?', 'pods' ),
83
				'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, self::$type ),
84
				'type' => 'boolean',
85
				'dependency' => true,
86
			),
87
			self::$type . '_new_window' => array(
88
				'label' => __( 'Open link in new window?', 'pods' ),
89
				'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, self::$type ),
90
				'type' => 'boolean',
91
				'depends-on' => array( self::$type . '_clickable' => true ),
92
			),
93
			self::$type . '_max_length' => array(
94
				'label' => __( 'Maximum Length', 'pods' ),
95
				'default' => 255,
96
				'type' => 'number',
97
				'help' => __( 'Set to -1 for no limit', 'pods' )
98
			),
99
			self::$type . '_html5' => array(
100
				'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
101
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
102
				'type' => 'boolean'
103
			)/*,
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...
104
			self::$type . '_size' => array(
105
				'label' => __( 'Field Size', 'pods' ),
106
				'default' => 'medium',
107
				'type' => 'pick',
108
				'data' => array(
109
					'small' => __( 'Small', 'pods' ),
110
					'medium' => __( 'Medium', 'pods' ),
111
					'large' => __( 'Large', 'pods' )
112
				)
113
			)*/
114
		);
115
		return $options;
116
	}
117
118
	/**
119
	 * Define the current field's schema for DB table storage
120
	 *
121
	 * @param array $options
122
	 *
123
	 * @return array
124
	 * @since 2.0
125
	 */
126 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...
127
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
128
129
		$schema = 'VARCHAR(' . $length . ')';
130
131
		if ( 255 < $length || $length < 1 )
132
			$schema = 'LONGTEXT';
133
134
		return $schema;
135
	}
136
137
	/**
138
	 * Change the way the value of the field is displayed with Pods::get
139
	 *
140
	 * @param mixed $value
141
	 * @param string $name
142
	 * @param array $options
143
	 * @param array $pod
144
	 * @param int $id
145
	 *
146
	 * @return mixed|null
147
	 * @since 2.0
148
	 */
149
	public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
150
		// Ensure proper format
151
		$value = $this->pre_save( $value, $id, $name, $options, null, $pod );
152
153
		if ( 1 == pods_v( self::$type . '_clickable', $options ) && 0 < strlen( $value ) ) {
154
			$link = '<a href="%s"%s>%s</a>';
155
156
			$atts = '';
157
158
			if ( 1 == pods_v( self::$type . '_new_window', $options ) ) {
159
				$atts .= ' target="_blank"';
160
			}
161
162
			$value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) );
163
		}
164
165
		return $value;
166
	}
167
168
	/**
169
	 * Customize output of the form field
170
	 *
171
	 * @param string $name
172
	 * @param mixed $value
173
	 * @param array $options
174
	 * @param array $pod
175
	 * @param int $id
176
	 *
177
	 * @since 2.0
178
	 */
179 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...
180
		$options = (array) $options;
181
		$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...
182
183
		if ( is_array( $value ) )
184
			$value = implode( ' ', $value );
185
186
		$field_type = 'website';
187
188
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
189
			if ( pods_var( 'read_only', $options, false ) ) {
190
				$options[ 'readonly' ] = true;
191
192
				$field_type = 'text';
193
			}
194
			else
195
				return;
196
		}
197
		elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
198
			$options[ 'readonly' ] = true;
199
200
			$field_type = 'text';
201
		}
202
203
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
204
	}
205
206
	/**
207
	 * Validate a value before it's saved
208
	 *
209
	 * @param mixed $value
210
	 * @param string $name
211
	 * @param array $options
212
	 * @param array $fields
213
	 * @param array $pod
214
	 * @param int $id
215
	 *
216
	 * @since 2.0
217
	 */
218 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...
219
		$errors = array();
220
221
		$label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
222
223
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
224
225
		if ( is_array( $check ) )
226
			$errors = $check;
227
		else {
228
			if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
229
				if ( 1 == pods_var( 'required', $options ) )
230
					$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
231
				else
232
					$errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
233
			}
234
		}
235
236
		if ( !empty( $errors ) )
237
			return $errors;
238
239
		return true;
240
	}
241
242
	/**
243
	 * Change the value or perform actions after validation but before saving to the DB
244
	 *
245
	 * @param mixed $value
246
	 * @param int $id
247
	 * @param string $name
248
	 * @param array $options
249
	 * @param array $fields
250
	 * @param array $pod
251
	 * @param object $params
252
	 *
253
	 * @since 2.0
254
	 */
255 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...
256
		$options = (array) $options;
257
258
		$value = $this->validate_url( $value, $options );
259
260
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
261
262
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
263
			$value = pods_mb_substr( $value, 0, $length );
264
		}
265
266
		return $value;
267
	}
268
269
	/**
270
	 * Customize the Pods UI manage table column output
271
	 *
272
	 * @param int $id
273
	 * @param mixed $value
274
	 * @param string $name
275
	 * @param array $options
276
	 * @param array $fields
277
	 * @param array $pod
278
	 *
279
	 * @since 2.0
280
	 */
281
	public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
282
		$value = $this->display( $value, $name, $options, $pod, $id );
283
284
		return $value;
285
	}
286
287
	/**
288
	 * Validate an URL with the options
289
	 *
290
	 * @param string $value
291
	 * @param array $options
292
	 *
293
	 * @since 2.7
294
	 */
295
	public function validate_url( $value, $options = null ) {
296
		if ( empty( $value ) )
297
			return $value;
298
299
		if ( 'none' != pods_var( self::$type . '_format', $options ) ) {
300
			if ( is_array( $value ) ) {
301
				if ( isset( $value[ 'scheme' ] ) )
302
					$value = $this->build_url( $value );
303
				else
304
					$value = implode( '', $value );
305
			}
306
307
			if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) )
308
				$value = 'http://' . $value;
309
310
			$url = @parse_url( $value );
311
312
			if ( empty( $url ) || count( $url ) < 2 )
313
				$value = '';
314
			else {
315
				$defaults = array(
316
					'scheme' => 'http',
317
					'host' => '',
318
					'path' => '/',
319
					'query' => '',
320
					'fragment' => ''
321
				);
322
323
				$url = array_merge( $defaults, $url );
324
325
				if ( 'normal' == pods_var( self::$type . '_format', $options ) )
326
					$value = $this->build_url( $url );
327
				elseif ( 'no-www' == pods_var( self::$type . '_format', $options ) ) {
328 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
329
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
330
331
					$value = $this->build_url( $url );
332
				}
333
				elseif ( 'force-www' == pods_var( self::$type . '_format', $options ) ) {
334 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
335
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
336
337
					$value = $this->build_url( $url );
338
				}
339
				elseif ( 'no-http' == pods_var( self::$type . '_format', $options ) ) {
340
					$value = $this->build_url( $url );
341
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
342
343
					if ( '/' == $url[ 'path' ] )
344
						$value = trim( $value, '/' );
345
				}
346
				elseif ( 'no-http-no-www' == pods_var( self::$type . '_format', $options ) ) {
347 View Code Duplication
					if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
348
						$url[ 'host' ] = substr( $url[ 'host' ], 4 );
349
350
					$value = $this->build_url( $url );
351
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
352
353
					if ( '/' == $url[ 'path' ] )
354
						$value = trim( $value, '/' );
355
				}
356
				elseif ( 'no-http-force-www' == pods_var( self::$type . '_format', $options ) ) {
357 View Code Duplication
					if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
358
						$url[ 'host' ] = 'www.' . $url[ 'host' ];
359
360
					$value = $this->build_url( $url );
361
					$value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
362
363
					if ( '/' == $url[ 'path' ] )
364
						$value = trim( $value, '/' );
365
				}
366
			}
367
		} else {
368
			$value = $this->strip_html( $value, $options );
369
		}
370
371
		$value = esc_url( $value );
372
373
		return $value;
374
	}
375
376
	/**
377
	 * Strip HTML based on options
378
	 *
379
	 * @param string $value
380
	 * @param array $options
381
	 *
382
	 * @return string
383
	 *
384
	 * @since 2.7
385
	 */
386 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...
387
		if ( is_array( $value ) )
388
			$value = @implode( ' ', $value );
389
390
		$value = trim( $value );
391
392
		if ( empty( $value ) )
393
			return $value;
394
395
		$options = (array) $options;
396
397
		if ( 1 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) ) {
398
			$allowed_html_tags = '';
399
400
			if ( 0 < strlen( pods_var( self::$type . '_allowed_html_tags', $options ) ) ) {
401
				$allowed_html_tags = explode( ' ', trim( pods_var( self::$type . '_allowed_html_tags', $options ) ) );
402
				$allowed_html_tags = '<' . implode( '><', $allowed_html_tags ) . '>';
403
			}
404
405
			if ( !empty( $allowed_html_tags ) && '<>' != $allowed_html_tags )
406
				$value = strip_tags( $value, $allowed_html_tags );
407
		}
408
		else
409
			$value = strip_tags( $value );
410
411
		return $value;
412
	}
413
414
	/**
415
	 * Validate an target attribute with the options
416
	 *
417
	 * @param string $value
418
	 * @param array $options
419
	 *
420
	 * @since 2.7
421
	 */
422
	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...
423
		if ( ! empty( $value ) && $value == '_blank' ) {
424
			$value = '_blank';
425
		} else {
426
			$value = '';
427
		}
428
		return $value;
429
	}
430
431
	/**
432
	 * Build an url
433
	 *
434
	 * @param array|string $url
435
	 *
436
	 * @return string
437
	 */
438
	public function build_url ( $url ) {
439
		if ( function_exists( 'http_build_url' ) )
440
			return http_build_url( $url );
441
442
		$defaults = array(
443
			'scheme' => 'http',
444
			'host' => '',
445
			'path' => '/',
446
			'query' => '',
447
			'fragment' => ''
448
		);
449
450
		$url = array_merge( $defaults, (array) $url );
451
452
		$new_url = trim( $url[ 'scheme' ] . '://', ':' ) . $url[ 'host' ] . '/' . ltrim( $url[ 'path' ], '/' );
453
454
		if ( !empty( $url[ 'query' ] ) )
455
			$new_url .= '?' . ltrim( $url[ 'query' ], '?' );
456
457
		if ( !empty( $url[ 'fragment' ] ) )
458
			$new_url .= '#' . ltrim( $url[ 'fragment' ], '#' );
459
460
		return $new_url;
461
	}
462
463
}