PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.6
Pods – Custom Content Types and Fields v3.3.6
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / phone.php
pods / classes / fields Last commit date
avatar.php 4 months ago boolean.php 4 months ago code.php 4 months ago color.php 4 months ago comment.php 4 months ago currency.php 4 months ago date.php 4 months ago datetime.php 4 months ago email.php 4 months ago file.php 4 months ago heading.php 4 months ago html.php 4 months ago link.php 4 months ago number.php 4 months ago oembed.php 4 months ago paragraph.php 4 months ago password.php 4 months ago phone.php 4 months ago pick.php 4 months ago slug.php 4 months ago taxonomy.php 4 months ago text.php 4 months ago time.php 4 months ago website.php 4 months ago wysiwyg.php 4 months ago
phone.php
277 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Fields
10 */
11 class PodsField_Phone extends PodsField {
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $group = 'Text';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $type = 'phone';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $label = 'Phone';
27
28 /**
29 * {@inheritdoc}
30 */
31 public static $prepare = '%s';
32
33 /**
34 * {@inheritdoc}
35 */
36 public function setup() {
37
38 static::$group = __( 'Text', 'pods' );
39 static::$label = __( 'Phone', 'pods' );
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function options() {
46
47 $options = [
48 static::$type . '_format' => [
49 'label' => __( 'Format', 'pods' ),
50 'default' => '999-999-9999 x999',
51 'type' => 'pick',
52 'data' => [
53 __( 'US', 'pods' ) => [
54 '999-999-9999 x999' => '123-456-7890 x123',
55 '(999) 999-9999 x999' => '(123) 456-7890 x123',
56 '999.999.9999 x999' => '123.456.7890 x123',
57 ],
58 __( 'International', 'pods' ) => [
59 'international' => __( 'Any (no validation available)', 'pods' ),
60 ],
61 ],
62 'pick_format_single' => 'dropdown',
63 'pick_show_select_text' => 0,
64 ],
65 static::$type . '_options' => [
66 'label' => __( 'Phone Options', 'pods' ),
67 'type' => 'boolean_group',
68 'boolean_group' => [
69 static::$type . '_enable_phone_extension' => [
70 'label' => __( 'Enable Phone Extension', 'pods' ),
71 'default' => 1,
72 'type' => 'boolean',
73 ],
74 ],
75 ],
76 static::$type . '_max_length' => [
77 'label' => __( 'Maximum Length', 'pods' ),
78 'default' => 25,
79 'type' => 'number',
80 'help' => __( 'Set to -1 for no limit', 'pods' ),
81 ],
82 static::$type . '_html5' => [
83 'label' => __( 'Enable HTML5 Input Field', 'pods' ),
84 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
85 'type' => 'boolean',
86 ],
87 static::$type . '_placeholder' => [
88 'label' => __( 'HTML Placeholder', 'pods' ),
89 'default' => '',
90 'type' => 'text',
91 'help' => [
92 __( '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' ),
93 'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
94 ],
95 ],
96 ];
97
98 return $options;
99 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function schema( $options = null ) {
105
106 $length = (int) pods_v( static::$type . '_max_length', $options, 25, true );
107
108 $schema = 'VARCHAR(' . $length . ')';
109
110 if ( 255 < $length || $length < 1 ) {
111 $schema = 'LONGTEXT';
112 }
113
114 return $schema;
115 }
116
117 /**
118 * {@inheritdoc}
119 */
120 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
121
122 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
123 $form_field_type = PodsForm::$field_type;
124
125 $value = $this->normalize_value_for_input( $value, $options );
126
127 $field_type = 'phone';
128
129 if ( isset( $options['name'] ) && ! pods_permission( $options ) ) {
130 if ( pods_v_bool( 'read_only_restricted', $options ) ) {
131 $options['readonly'] = true;
132
133 $field_type = 'text';
134 } else {
135 return;
136 }
137 } elseif ( ! pods_has_permissions( $options ) ) {
138 if ( pods_v_bool( 'read_only', $options ) ) {
139 $options['readonly'] = true;
140
141 $field_type = 'text';
142 }
143 }
144
145 if ( ! empty( $options['disable_dfv'] ) ) {
146 return pods_view( PODS_DIR . 'ui/fields/phone.php', compact( array_keys( get_defined_vars() ) ) );
147 }
148
149 $type = pods_v( 'type', $options, static::$type );
150
151 $args = compact( array_keys( get_defined_vars() ) );
152 $args = (object) $args;
153
154 $this->render_input_script( $args );
155 }
156
157 /**
158 * {@inheritdoc}
159 */
160 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
161 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
162
163 $errors = [];
164
165 if ( is_array( $validate ) ) {
166 $errors = $validate;
167 }
168
169 $label = wp_strip_all_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
170
171 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
172
173 if ( is_array( $check ) ) {
174 $errors = $check;
175 } elseif ( '' === $check && 0 < strlen( (string) $value ) ) {
176 if ( $this->is_required( $options ) ) {
177 // translators: %s is the field label.
178 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
179 } else {
180 // translators: %s is the field label.
181 $errors[] = sprintf( __( 'Invalid phone number provided for the field %s.', 'pods' ), $label );
182 }
183 }
184
185 if ( ! empty( $errors ) ) {
186 return $errors;
187 }
188
189 return $validate;
190 }
191
192 /**
193 * {@inheritdoc}
194 */
195 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
196 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
197
198 $phone_format = pods_v( static::$type . '_format', $options, '999-999-9999 x999', true );
199
200 if ( 'international' !== $phone_format ) {
201 // Clean input
202 $number = preg_replace( '/(\+\d+)/', '', $value );
203 $number = preg_replace( '/([^0-9ext])/', '', $number );
204
205 $number = str_replace(
206 [
207 'ext',
208 'x',
209 't',
210 'e',
211 ],
212 [
213 '|',
214 '|',
215 '',
216 '',
217 ],
218 $number
219 );
220
221 $extension = '';
222
223 // Get extension
224 $extension_data = explode( '|', $number );
225
226 if ( 1 < count( $extension_data ) ) {
227 $number = $extension_data[0];
228 $extension = $extension_data[1];
229 }
230
231 // Build number array
232 $numbers = str_split( $number, 3 );
233
234 // Split up the numbers: 123-456-7890: 123[0] 456[1] 789[2]0[3]
235 if ( isset( $numbers[3] ) ) {
236 $numbers[2] .= $numbers[3];
237 $numbers = [ $numbers[0], $numbers[1], $numbers[2] ];
238 } elseif ( isset( $numbers[1] ) ) {
239 $numbers = [ $numbers[0], $numbers[1] ];
240 }
241
242 // Format number
243 if ( '(999) 999-9999 x999' === $phone_format ) {
244 $number_count = count( $numbers );
245
246 if ( 1 === $number_count ) {
247 // Invalid number.
248 $value = '';
249 } elseif ( 2 === $number_count ) {
250 // Basic number, no area code!
251 $value = implode( '-', $numbers );
252 } else {
253 // Full number.
254 $value = '(' . $numbers[0] . ') ' . $numbers[1] . '-' . $numbers[2];
255 }
256 } elseif ( '999.999.9999 x999' === $phone_format ) {
257 $value = implode( '.', $numbers );
258 } else {
259 $value = implode( '-', $numbers );
260 }
261
262 // Add extension
263 if ( 1 === (int) pods_v( static::$type . '_enable_phone_extension', $options ) && 0 < strlen( (string) $extension ) ) {
264 $value .= ' x' . $extension;
265 }
266 }//end if
267
268 $length = (int) pods_v( static::$type . '_max_length', $options, 25 );
269
270 if ( 0 < $length && $length < pods_mb_strlen( (string) $value ) ) {
271 $value = pods_mb_substr( $value, 0, $length );
272 }
273
274 return $value;
275 }
276 }
277