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