Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:52
created

PodsField_Email::regex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 5
1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Email extends PodsField {
7
8
	/**
9
	 * Field Type Group
10
	 *
11
	 * @var string
12
	 * @since 2.0
13
	 */
14
	public static $group = 'Text';
15
16
	/**
17
	 * Field Type Identifier
18
	 *
19
	 * @var string
20
	 * @since 2.0
21
	 */
22
	public static $type = 'email';
23
24
	/**
25
	 * Field Type Label
26
	 *
27
	 * @var string
28
	 * @since 2.0
29
	 */
30
	public static $label = 'E-mail';
31
32
	/**
33
	 * Field Type Preparation
34
	 *
35
	 * @var string
36
	 * @since 2.0
37
	 */
38
	public static $prepare = '%s';
39
40
	/**
41
	 * Do things like register/enqueue scripts and stylesheets
42
	 *
43
	 * @since 2.0
44
	 */
45
	public function __construct() {
46
47
		self::$label = __( 'E-mail', 'pods' );
48
	}
49
50
	/**
51
	 * Add options and set defaults to
52
	 *
53
	 * @return array
54
	 *
55
	 * @since 2.0
56
	 */
57
	public function options() {
58
59
		$options = array(
60
			self::$type . '_repeatable'  => array(
61
				'label'             => __( 'Repeatable Field', 'pods' ),
62
				'default'           => 0,
63
				'type'              => 'boolean',
64
				'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' ),
65
				'boolean_yes_label' => '',
66
				'dependency'        => true,
67
				'developer_mode'    => true,
68
			),
69
			self::$type . '_max_length'  => array(
70
				'label'   => __( 'Maximum Length', 'pods' ),
71
				'default' => 255,
72
				'type'    => 'number',
73
				'help'    => __( 'Set to -1 for no limit', 'pods' ),
74
			),
75
			self::$type . '_html5'       => array(
76
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
77
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
78
				'type'    => 'boolean',
79
			),
80
			self::$type . '_placeholder' => array(
81
				'label'   => __( 'HTML Placeholder', 'pods' ),
82
				'default' => '',
83
				'type'    => 'text',
84
				'help'    => array(
85
					__( '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' ),
86
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
87
				),
88
			), /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
89
		,
90
            self::$type . '_size' => array(
91
                'label' => __( 'Field Size', 'pods' ),
92
                'default' => 'medium',
93
                'type' => 'pick',
94
                'data' => array(
95
                    'small' => __( 'Small', 'pods' ),
96
                    'medium' => __( 'Medium', 'pods' ),
97
                    'large' => __( 'Large', 'pods' )
98
                )
99
            )*/
100
		);
101
102
		return $options;
103
	}
104
105
	/**
106
	 * Define the current field's schema for DB table storage
107
	 *
108
	 * @param array $options
109
	 *
110
	 * @return array
111
	 * @since 2.0
112
	 */
113 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...
114
115
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
116
117
		$schema = 'VARCHAR(' . $length . ')';
118
119
		if ( 255 < $length || $length < 1 ) {
120
			$schema = 'LONGTEXT';
121
		}
122
123
		return $schema;
124
	}
125
126
	/**
127
	 * Customize output of the form field
128
	 *
129
	 * @param string $name
130
	 * @param mixed  $value
131
	 * @param array  $options
132
	 * @param array  $pod
133
	 * @param int    $id
134
	 *
135
	 * @since 2.0
136
	 */
137 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...
138
139
		$options         = (array) $options;
140
		$form_field_type = PodsForm::$field_type;
141
142
		if ( is_array( $value ) ) {
143
			$value = implode( ' ', $value );
144
		}
145
146
		$field_type = 'email';
147
148
		if ( isset( $options['name'] ) && false === PodsForm::permission( self::$type, $options['name'], $options, null, $pod, $id ) ) {
149
			if ( pods_var( 'read_only', $options, false ) ) {
150
				$options['readonly'] = true;
151
152
				$field_type = 'text';
153
			} else {
154
				return;
155
			}
156
		} elseif ( ! pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
157
			$options['readonly'] = true;
158
159
			$field_type = 'text';
160
		}
161
162
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
163
	}
164
165
	/**
166
	 * Build regex necessary for JS validation
167
	 *
168
	 * @param mixed  $value
169
	 * @param string $name
170
	 * @param array  $options
171
	 * @param string $pod
172
	 * @param int    $id
173
	 *
174
	 * @return bool
175
	 * @since 2.0
176
	 */
177
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
178
179
		return false;
180
	}
181
182
	/**
183
	 * Validate a value before it's saved
184
	 *
185
	 * @param mixed  $value
186
	 * @param string $name
187
	 * @param array  $options
188
	 * @param array  $fields
189
	 * @param array  $pod
190
	 * @param int    $id
191
	 * @param null   $params
192
	 *
193
	 * @return array|bool
194
	 * @since 2.0
195
	 */
196 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...
197
198
		$errors = array();
199
200
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
201
202
		if ( is_array( $check ) ) {
203
			$errors = $check;
204
		} else {
205
			if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
206
				$label = pods_var( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
207
208
				if ( 1 == pods_var( 'required', $options ) ) {
209
					$errors[] = sprintf( __( '%s is required', 'pods' ), $label );
210
				} else {
211
					$errors[] = sprintf( __( 'Invalid e-mail provided for %s', 'pods' ), $label );
212
				}
213
			}
214
		}
215
216
		if ( ! empty( $errors ) ) {
217
			return $errors;
218
		}
219
220
		return true;
221
	}
222
223
	/**
224
	 * Change the value or perform actions after validation but before saving to the DB
225
	 *
226
	 * @param mixed  $value
227
	 * @param int    $id
228
	 * @param string $name
229
	 * @param array  $options
230
	 * @param array  $fields
231
	 * @param array  $pod
232
	 * @param object $params
233
	 *
234
	 * @return mixed|string
235
	 * @since 2.0
236
	 */
237
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
238
239
		$options = (array) $options;
240
241
		if ( ! is_email( $value ) ) {
242
			$value = '';
243
		}
244
245
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
246
247
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
248
			$value = pods_mb_substr( $value, 0, $length );
249
		}
250
251
		return $value;
252
	}
253
254
	/**
255
	 * Customize the Pods UI manage table column output
256
	 *
257
	 * @param int    $id
258
	 * @param mixed  $value
259
	 * @param string $name
260
	 * @param array  $options
261
	 * @param array  $fields
262
	 * @param array  $pod
263
	 *
264
	 * @return mixed|string
265
	 * @since 2.0
266
	 */
267
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
268
269
		return '<a href="mailto:' . esc_attr( $value ) . '">' . $value . '</a>';
270
	}
271
}
272