Completed
Push — master ( 1a7371...4a1bf4 )
by Stephanie
22s
created

FrmFieldUrl   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 8.79 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 91
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A field_settings_for_type() 8 8 1
A extra_field_opts() 0 5 1
A get_field_name() 0 3 1
A fill_default_atts() 0 11 2
B validate() 0 22 8
A prepare_display_value() 0 17 4
A sanitize_value() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @since 3.0
5
 */
6
class FrmFieldUrl extends FrmFieldType {
7
8
	/**
9
	 * @var string
10
	 * @since 3.0
11
	 */
12
	protected $type = 'url';
13
	protected $display_type = 'text';
14
15 View Code Duplication
	protected function field_settings_for_type() {
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...
16
		return array(
17
			'size'           => true,
18
			'clear_on_focus' => true,
19
			'invalid'        => true,
20
			'show_image'     => true,
21
		);
22
	}
23
24
	/**
25
	 * @return array
26
	 */
27
	protected function extra_field_opts() {
28
		return array(
29
			'show_image' => 0,
30
		);
31
	}
32
33
	protected function get_field_name() {
34
		return __( 'Website', 'formidable' );
35
	}
36
37
	protected function fill_default_atts( &$atts ) {
38
		$defaults = array(
39
			'sep'  => ', ',
40
			'html' => false,
41
		);
42
		$atts     = wp_parse_args( $atts, $defaults );
43
44
		if ( $atts['html'] ) {
45
			$atts['sep'] = ' ';
46
		}
47
	}
48
49
	public function validate( $args ) {
50
		$value = $args['value'];
51
		if ( trim( $value ) == 'http://' || empty( $value ) ) {
52
			$value = '';
53
		} else {
54
			$value = esc_url_raw( $value );
55
			$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value;
56
		}
57
58
		FrmEntriesHelper::set_posted_value( $this->field, $value, $args );
59
60
		$errors = array();
61
62
		// validate the url format
63
		if ( ! empty( $value ) && ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) {
64
			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' );
65
		} elseif ( $this->field->required == '1' && empty( $value ) ) {
66
			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'blank' );
67
		}
68
69
		return $errors;
70
	}
71
72
	protected function prepare_display_value( $value, $atts ) {
73
		if ( $atts['html'] ) {
74
			$images = '';
75
			foreach ( (array) $value as $url ) {
76
				$image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/';
77
				$is_image    = preg_match( $image_regex, $url );
78
				if ( $is_image ) {
79
					$images .= '<img src="' . esc_attr( $url ) . '" class="frm_image_from_url" alt="" /> ';
80
				} else {
81
					$images .= strip_tags( $url );
82
				}
83
			}
84
			$value = $images;
85
		}
86
87
		return $value;
88
	}
89
90
	/**
91
	 * @since 4.0
92
	 */
93
	public function sanitize_value( &$value ) {
94
		FrmAppHelper::sanitize_value( 'esc_url_raw', $value );
95
	}
96
}
97