|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Pods\Fields |
|
4
|
|
|
*/ |
|
5
|
|
|
class PodsField_Password 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 = 'password'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Field Type Label |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
* @since 2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
public static $label = 'Password'; |
|
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
|
|
|
self::$label = __( 'Password', 'pods' ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add options and set defaults to |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
* |
|
53
|
|
|
* @since 2.0 |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function options () { |
|
57
|
|
|
$options = array( |
|
58
|
|
|
self::$type . '_max_length' => array( |
|
59
|
|
|
'label' => __( 'Maximum Length', 'pods' ), |
|
60
|
|
|
'default' => 255, |
|
61
|
|
|
'type' => 'number', |
|
62
|
|
|
'help' => __( 'Set to -1 for no limit', 'pods' ) |
|
63
|
|
|
), |
|
64
|
|
|
self::$type . '_placeholder' => array( |
|
65
|
|
|
'label' => __( 'HTML Placeholder', 'pods' ), |
|
66
|
|
|
'default' => '', |
|
67
|
|
|
'type' => 'text', |
|
68
|
|
|
'help' => array( |
|
69
|
|
|
__( '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' ), |
|
70
|
|
|
'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text', |
|
71
|
|
|
), |
|
72
|
|
|
),/*, |
|
|
|
|
|
|
73
|
|
|
self::$type . '_size' => array( |
|
74
|
|
|
'label' => __( 'Field Size', 'pods' ), |
|
75
|
|
|
'default' => 'medium', |
|
76
|
|
|
'type' => 'pick', |
|
77
|
|
|
'data' => array( |
|
78
|
|
|
'small' => __( 'Small', 'pods' ), |
|
79
|
|
|
'medium' => __( 'Medium', 'pods' ), |
|
80
|
|
|
'large' => __( 'Large', 'pods' ) |
|
81
|
|
|
) |
|
82
|
|
|
)*/ |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $options; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Define the current field's schema for DB table storage |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $options |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
* @since 2.0 |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function schema ( $options = null ) { |
|
|
|
|
|
|
97
|
|
|
$length = (int) pods_var( self::$type . '_max_length', $options, 255 ); |
|
98
|
|
|
|
|
99
|
|
|
$schema = 'VARCHAR(' . $length . ')'; |
|
100
|
|
|
|
|
101
|
|
|
if ( 255 < $length || $length < 1 ) |
|
102
|
|
|
$schema = 'LONGTEXT'; |
|
103
|
|
|
|
|
104
|
|
|
return $schema; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Customize output of the form field |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* @param mixed $value |
|
112
|
|
|
* @param array $options |
|
113
|
|
|
* @param array $pod |
|
114
|
|
|
* @param int $id |
|
115
|
|
|
* |
|
116
|
|
|
* @since 2.0 |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
|
|
|
|
|
119
|
|
|
$options = (array) $options; |
|
120
|
|
|
$form_field_type = PodsForm::$field_type; |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if ( is_array( $value ) ) |
|
123
|
|
|
$value = implode( ' ', $value ); |
|
124
|
|
|
|
|
125
|
|
|
if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { |
|
126
|
|
|
if ( pods_var( 'read_only', $options, false ) ) |
|
127
|
|
|
$options[ 'readonly' ] = true; |
|
128
|
|
|
else |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) |
|
132
|
|
|
$options[ 'readonly' ] = true; |
|
133
|
|
|
|
|
134
|
|
|
pods_view( PODS_DIR . 'ui/fields/password.php', compact( array_keys( get_defined_vars() ) ) ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Validate a value before it's saved |
|
139
|
|
|
* |
|
140
|
|
|
* @param mixed $value |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @param array $options |
|
143
|
|
|
* @param array $fields |
|
144
|
|
|
* @param array $pod |
|
145
|
|
|
* @param int $id |
|
146
|
|
|
* @param null $params |
|
147
|
|
|
* |
|
148
|
|
|
* @return array|bool |
|
149
|
|
|
* @since 2.0 |
|
150
|
|
|
*/ |
|
151
|
|
View Code Duplication |
public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
|
|
|
|
|
152
|
|
|
$errors = array(); |
|
153
|
|
|
|
|
154
|
|
|
$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
|
155
|
|
|
|
|
156
|
|
|
if ( is_array( $check ) ) |
|
157
|
|
|
$errors = $check; |
|
158
|
|
|
else { |
|
159
|
|
|
if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) { |
|
160
|
|
|
if ( 1 == pods_var( 'required', $options ) ) |
|
161
|
|
|
$errors[] = __( 'This field is required.', 'pods' ); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if ( !empty( $errors ) ) |
|
166
|
|
|
return $errors; |
|
167
|
|
|
|
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Change the value or perform actions after validation but before saving to the DB |
|
173
|
|
|
* |
|
174
|
|
|
* @param mixed $value |
|
175
|
|
|
* @param int $id |
|
176
|
|
|
* @param string $name |
|
177
|
|
|
* @param array $options |
|
178
|
|
|
* @param array $fields |
|
179
|
|
|
* @param array $pod |
|
180
|
|
|
* @param object $params |
|
181
|
|
|
* |
|
182
|
|
|
* @return mixed|string |
|
183
|
|
|
* @since 2.0 |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
|
|
|
|
|
186
|
|
|
$length = (int) pods_var( self::$type . '_max_length', $options, 255 ); |
|
187
|
|
|
|
|
188
|
|
|
if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
|
189
|
|
|
$value = pods_mb_substr( $value, 0, $length ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $value; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
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.