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