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
email.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Pods\Fields |
| 5 | */ |
| 6 | class PodsField_Email extends PodsField { |
| 7 | |
| 8 | /** |
| 9 | * {@inheritdoc} |
| 10 | */ |
| 11 | public static $group = 'Text'; |
| 12 | |
| 13 | /** |
| 14 | * {@inheritdoc} |
| 15 | */ |
| 16 | public static $type = 'email'; |
| 17 | |
| 18 | /** |
| 19 | * {@inheritdoc} |
| 20 | */ |
| 21 | public static $label = 'E-mail'; |
| 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 = __( 'E-mail', 'pods' ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public function options() { |
| 41 | |
| 42 | $options = array( |
| 43 | static::$type . '_max_length' => array( |
| 44 | 'label' => __( 'Maximum Length', 'pods' ), |
| 45 | 'default' => 255, |
| 46 | 'type' => 'number', |
| 47 | 'help' => __( 'Set to -1 for no limit', 'pods' ), |
| 48 | ), |
| 49 | static::$type . '_html5' => array( |
| 50 | 'label' => __( 'Enable HTML5 Input Field', 'pods' ), |
| 51 | 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ), |
| 52 | 'type' => 'boolean', |
| 53 | ), |
| 54 | static::$type . '_placeholder' => array( |
| 55 | 'label' => __( 'HTML Placeholder', 'pods' ), |
| 56 | 'default' => '', |
| 57 | 'type' => 'text', |
| 58 | 'help' => array( |
| 59 | __( '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' ), |
| 60 | 'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text', |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | |
| 65 | return $options; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * {@inheritdoc} |
| 70 | */ |
| 71 | public function schema( $options = null ) { |
| 72 | |
| 73 | $length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
| 74 | |
| 75 | $schema = 'VARCHAR(' . $length . ')'; |
| 76 | |
| 77 | if ( 255 < $length || $length < 1 ) { |
| 78 | $schema = 'LONGTEXT'; |
| 79 | } |
| 80 | |
| 81 | return $schema; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 88 | |
| 89 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 90 | $form_field_type = PodsForm::$field_type; |
| 91 | |
| 92 | $value = $this->normalize_value_for_input( $value, $options ); |
| 93 | |
| 94 | $field_type = 'email'; |
| 95 | |
| 96 | if ( isset( $options['name'] ) && ! pods_permission( $options ) ) { |
| 97 | if ( pods_v( 'read_only', $options, false ) ) { |
| 98 | $options['readonly'] = true; |
| 99 | |
| 100 | $field_type = 'text'; |
| 101 | } else { |
| 102 | return; |
| 103 | } |
| 104 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
| 105 | $options['readonly'] = true; |
| 106 | |
| 107 | $field_type = 'text'; |
| 108 | } |
| 109 | |
| 110 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 111 | return pods_view( PODS_DIR . 'ui/fields/email.php', compact( array_keys( get_defined_vars() ) ) ); |
| 112 | } |
| 113 | |
| 114 | $type = pods_v( 'type', $options, static::$type ); |
| 115 | |
| 116 | $args = compact( array_keys( get_defined_vars() ) ); |
| 117 | $args = (object) $args; |
| 118 | |
| 119 | $this->render_input_script( $args ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 126 | $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); |
| 127 | |
| 128 | $errors = array(); |
| 129 | |
| 130 | if ( is_array( $validate ) ) { |
| 131 | $errors = $validate; |
| 132 | } |
| 133 | |
| 134 | $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 135 | |
| 136 | if ( is_array( $check ) ) { |
| 137 | $errors = $check; |
| 138 | } else { |
| 139 | if ( 0 < strlen( (string) $value ) && '' === $check ) { |
| 140 | $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); |
| 141 | |
| 142 | if ( $this->is_required( $options ) ) { |
| 143 | $errors[] = sprintf( __( '%s is required', 'pods' ), $label ); |
| 144 | } else { |
| 145 | $errors[] = sprintf( __( 'Invalid e-mail provided for %s', 'pods' ), $label ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ( ! empty( $errors ) ) { |
| 151 | return $errors; |
| 152 | } |
| 153 | |
| 154 | return $validate; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * {@inheritdoc} |
| 159 | */ |
| 160 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 161 | |
| 162 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 163 | |
| 164 | if ( ! is_email( $value ) ) { |
| 165 | $value = ''; |
| 166 | } |
| 167 | |
| 168 | $length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
| 169 | |
| 170 | if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
| 171 | $value = pods_mb_substr( $value, 0, $length ); |
| 172 | } |
| 173 | |
| 174 | return $value; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * {@inheritdoc} |
| 179 | */ |
| 180 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
| 181 | |
| 182 | return '<a href="mailto:' . esc_attr( $value ) . '">' . $value . '</a>'; |
| 183 | } |
| 184 | } |
| 185 |