PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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 3 days ago boolean.php 3 days ago code.php 3 days ago color.php 3 days ago comment.php 3 days ago currency.php 3 days ago date.php 3 days ago datetime.php 3 days ago email.php 3 days ago file.php 3 days ago html.php 3 days ago link.php 3 days ago number.php 3 days ago oembed.php 3 days ago paragraph.php 3 days ago password.php 3 days ago phone.php 3 days ago pick.php 3 days ago slug.php 3 days ago taxonomy.php 3 days ago text.php 3 days ago time.php 3 days ago website.php 3 days ago wysiwyg.php 3 days ago
phone.php
258 lines
1 <?php
2
3 /**
4 * @package Pods\Fields
5 */
6 class PodsField_Phone extends PodsField {
7
8 /**
9 * {@inheritdoc}
10 */
11 public static $group = 'Text';
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $type = 'phone';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $label = 'Phone';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $prepare = '%s';
27
28 /**
29 * {@inheritdoc}
30 */
31 public function setup() {
32
33 self::$label = __( 'Phone', 'pods' );
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function options() {
40
41 $options = array(
42 static::$type . '_repeatable' => array(
43 'label' => __( 'Repeatable Field', 'pods' ),
44 'default' => 0,
45 'type' => 'boolean',
46 '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' ),
47 'boolean_yes_label' => '',
48 'dependency' => true,
49 'developer_mode' => true,
50 ),
51 static::$type . '_format' => array(
52 'label' => __( 'Format', 'pods' ),
53 'default' => '999-999-9999 x999',
54 'type' => 'pick',
55 'data' => array(
56 __( 'US', 'pods' ) => array(
57 '999-999-9999 x999' => '123-456-7890 x123',
58 '(999) 999-9999 x999' => '(123) 456-7890 x123',
59 '999.999.9999 x999' => '123.456.7890 x123',
60 ),
61 __( 'International', 'pods' ) => array(
62 'international' => __( 'Any (no validation available)', 'pods' ),
63 ),
64 ),
65 ),
66 static::$type . '_options' => array(
67 'label' => __( 'Phone Options', 'pods' ),
68 'group' => array(
69 static::$type . '_enable_phone_extension' => array(
70 'label' => __( 'Enable Phone Extension?', 'pods' ),
71 'default' => 1,
72 'type' => 'boolean',
73 ),
74 ),
75 ),
76 static::$type . '_max_length' => array(
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' => array(
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' => array(
88 'label' => __( 'HTML Placeholder', 'pods' ),
89 'default' => '',
90 'type' => 'text',
91 'help' => array(
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 = (array) $options;
123 $form_field_type = PodsForm::$field_type;
124
125 if ( is_array( $value ) ) {
126 $value = implode( ' ', $value );
127 }
128
129 $field_type = 'phone';
130
131 if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
132 if ( pods_v( 'read_only', $options, false ) ) {
133 $options['readonly'] = true;
134
135 $field_type = 'text';
136 } else {
137 return;
138 }
139 } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
140 $options['readonly'] = true;
141
142 $field_type = 'text';
143 }
144
145 pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
146 }
147
148 /**
149 * {@inheritdoc}
150 */
151 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
152 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
153
154 $errors = array();
155
156 if ( is_array( $validate ) ) {
157 $errors = $validate;
158 }
159
160 $label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
161
162 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
163
164 if ( is_array( $check ) ) {
165 $errors = $check;
166 } else {
167 if ( 0 < strlen( $value ) && '' === $check ) {
168 if ( $this->is_required( $options ) ) {
169 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
170 } else {
171 $errors[] = sprintf( __( 'Invalid phone number provided for the field %s.', 'pods' ), $label );
172 }
173 }
174 }
175
176 if ( ! empty( $errors ) ) {
177 return $errors;
178 }
179
180 return $validate;
181 }
182
183 /**
184 * {@inheritdoc}
185 */
186 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
187
188 $options = (array) $options;
189
190 if ( 'international' !== pods_v( static::$type . '_format', $options ) ) {
191 // Clean input
192 $number = preg_replace( '/([^0-9ext])/', '', $value );
193
194 $number = str_replace(
195 array( '-', '.', 'ext', 'x', 't', 'e', '(', ')' ), array(
196 '',
197 '',
198 '|',
199 '|',
200 '',
201 '',
202 '',
203 '',
204 ), $number
205 );
206
207 // Get extension
208 $extension = explode( '|', $number );
209 if ( 1 < count( $extension ) ) {
210 $number = preg_replace( '/([^0-9])/', '', $extension[0] );
211 $extension = preg_replace( '/([^0-9])/', '', $extension[1] );
212 } else {
213 $extension = '';
214 }
215
216 // Build number array
217 $numbers = str_split( $number, 3 );
218
219 if ( isset( $numbers[3] ) ) {
220 $numbers[2] .= $numbers[3];
221 $numbers = array( $numbers[0], $numbers[1], $numbers[2] );
222 } elseif ( isset( $numbers[1] ) ) {
223 $numbers = array( $numbers[0], $numbers[1] );
224 }
225
226 // Format number
227 if ( '(999) 999-9999 x999' === pods_v( static::$type . '_format', $options ) ) {
228 $number_count = count( $numbers );
229
230 if ( 1 === $number_count ) {
231 $value = '';
232 } elseif ( 2 === $number_count ) {
233 $value = implode( '-', $numbers );
234 } else {
235 $value = '(' . $numbers[0] . ') ' . $numbers[1] . '-' . $numbers[2];
236 }
237 } elseif ( '999.999.9999 x999' === pods_v( static::$type . '_format', $options ) ) {
238 $value = implode( '.', $numbers );
239 } else {
240 $value = implode( '-', $numbers );
241 }
242
243 // Add extension
244 if ( 1 === (int) pods_v( static::$type . '_enable_phone_extension', $options ) && 0 < strlen( $extension ) ) {
245 $value .= ' x' . $extension;
246 }
247 }//end if
248
249 $length = (int) pods_v( static::$type . '_max_length', $options, 25 );
250
251 if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
252 $value = pods_mb_substr( $value, 0, $length );
253 }
254
255 return $value;
256 }
257 }
258