class-evf-field-address.php
7 years ago
class-evf-field-ai.php
2 years ago
class-evf-field-captcha.php
2 years ago
class-evf-field-checkbox.php
3 years ago
class-evf-field-color.php
3 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
2 years ago
class-evf-field-date-time.php
2 years ago
class-evf-field-divider.php
4 years ago
class-evf-field-email.php
2 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
2 years ago
class-evf-field-hidden.php
7 years ago
class-evf-field-html.php
7 years ago
class-evf-field-image-upload.php
7 years ago
class-evf-field-last-name.php
2 years ago
class-evf-field-likert.php
2 years ago
class-evf-field-lookup.php
2 years ago
class-evf-field-number.php
2 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-authorize-net.php
2 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-coupon.php
2 years ago
class-evf-field-payment-quantity.php
5 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-subtotal.php
3 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-privacy-policy.php
4 years ago
class-evf-field-progress.php
3 years ago
class-evf-field-radio.php
3 years ago
class-evf-field-range-slider.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-repeater.php
2 years ago
class-evf-field-reset.php
3 years ago
class-evf-field-scale-rating.php
2 years ago
class-evf-field-select.php
3 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
2 years ago
class-evf-field-textarea.php
3 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
2 years ago
class-evf-field-wysiwyg.php
4 years ago
class-evf-field-yes-no.php
2 years ago
class-evf-field-textarea.php
290 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Textarea field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Textarea class. |
| 13 | */ |
| 14 | class EVF_Field_Textarea extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Paragraph Text', 'everest-forms' ); |
| 21 | $this->type = 'textarea'; |
| 22 | $this->icon = 'evf-icon evf-icon-paragraph'; |
| 23 | $this->order = 40; |
| 24 | $this->group = 'general'; |
| 25 | $this->settings = array( |
| 26 | 'basic-options' => array( |
| 27 | 'field_options' => array( |
| 28 | 'label', |
| 29 | 'meta', |
| 30 | 'description', |
| 31 | 'required', |
| 32 | 'required_field_message_setting', |
| 33 | 'required_field_message', |
| 34 | 'readonly', |
| 35 | ), |
| 36 | ), |
| 37 | 'advanced-options' => array( |
| 38 | 'field_options' => array( |
| 39 | 'size', |
| 40 | 'placeholder', |
| 41 | 'label_hide', |
| 42 | 'limit_length', |
| 43 | 'min_length', |
| 44 | 'default_value', |
| 45 | 'css', |
| 46 | ), |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | parent::__construct(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Hook in tabs. |
| 55 | */ |
| 56 | public function init_hooks() { |
| 57 | add_action( 'everest_forms_shortcode_scripts', array( $this, 'load_assets' ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Limit length field option. |
| 62 | * |
| 63 | * @param array $field Field settings. |
| 64 | */ |
| 65 | public function limit_length( $field ) { |
| 66 | // Limit length. |
| 67 | $args = array( |
| 68 | 'slug' => 'limit_enabled', |
| 69 | 'content' => $this->field_element( |
| 70 | 'checkbox', |
| 71 | $field, |
| 72 | array( |
| 73 | 'slug' => 'limit_enabled', |
| 74 | 'value' => isset( $field['limit_enabled'] ), |
| 75 | 'desc' => esc_html__( 'Limit Length', 'everest-forms' ), |
| 76 | 'tooltip' => esc_html__( 'Check this option to specify maximum text length by characters or word count.', 'everest-forms' ), |
| 77 | ), |
| 78 | false |
| 79 | ), |
| 80 | ); |
| 81 | $this->field_element( 'row', $field, $args ); |
| 82 | |
| 83 | // Limit controls. |
| 84 | $count = $this->field_element( |
| 85 | 'text', |
| 86 | $field, |
| 87 | array( |
| 88 | 'type' => 'number', |
| 89 | 'class' => 'small-text', |
| 90 | 'slug' => 'limit_count', |
| 91 | 'attrs' => array( |
| 92 | 'min' => 1, |
| 93 | 'step' => 1, |
| 94 | 'pattern' => '[0-9]', |
| 95 | ), |
| 96 | 'value' => ! empty( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 1, |
| 97 | ), |
| 98 | false |
| 99 | ); |
| 100 | |
| 101 | $mode = $this->field_element( |
| 102 | 'select', |
| 103 | $field, |
| 104 | array( |
| 105 | 'slug' => 'limit_mode', |
| 106 | 'class' => 'limit-select', |
| 107 | 'value' => ! empty( $field['limit_mode'] ) ? esc_attr( $field['limit_mode'] ) : 'characters', |
| 108 | 'options' => array( |
| 109 | 'characters' => esc_html__( 'Characters', 'everest-forms' ), |
| 110 | 'words' => esc_html__( 'Words Count', 'everest-forms' ), |
| 111 | ), |
| 112 | ), |
| 113 | false |
| 114 | ); |
| 115 | $args = array( |
| 116 | 'slug' => 'limit_controls', |
| 117 | 'class' => ! isset( $field['limit_enabled'] ) ? 'everest-forms-hidden' : '', |
| 118 | 'content' => $count . $mode, |
| 119 | ); |
| 120 | $this->field_element( 'row', $field, $args ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Minimum Length length field option. |
| 125 | * |
| 126 | * @param array $field Field settings. |
| 127 | */ |
| 128 | public function min_length( $field ) { |
| 129 | // Minimum length. |
| 130 | $args = array( |
| 131 | 'slug' => 'min_length_enabled', |
| 132 | 'content' => $this->field_element( |
| 133 | 'checkbox', |
| 134 | $field, |
| 135 | array( |
| 136 | 'slug' => 'min_length_enabled', |
| 137 | 'value' => isset( $field['min_length_enabled'] ), |
| 138 | 'desc' => esc_html__( 'Minimum Length', 'everest-forms' ), |
| 139 | 'tooltip' => esc_html__( 'Check this option to specify minimum text length by characters or word count.', 'everest-forms' ), |
| 140 | ), |
| 141 | false |
| 142 | ), |
| 143 | ); |
| 144 | $this->field_element( 'row', $field, $args ); |
| 145 | |
| 146 | // Minimum length controls. |
| 147 | $count = $this->field_element( |
| 148 | 'text', |
| 149 | $field, |
| 150 | array( |
| 151 | 'type' => 'number', |
| 152 | 'class' => 'small-text', |
| 153 | 'slug' => 'min_length_count', |
| 154 | 'attrs' => array( |
| 155 | 'min' => 1, |
| 156 | 'step' => 1, |
| 157 | 'pattern' => '[0-9]', |
| 158 | ), |
| 159 | 'value' => ! empty( $field['min_length_count'] ) ? absint( $field['min_length_count'] ) : 1, |
| 160 | ), |
| 161 | false |
| 162 | ); |
| 163 | |
| 164 | $mode = $this->field_element( |
| 165 | 'select', |
| 166 | $field, |
| 167 | array( |
| 168 | 'slug' => 'min_length_mode', |
| 169 | 'class' => 'min-length-select', |
| 170 | 'value' => ! empty( $field['min_length_mode'] ) ? esc_attr( $field['min_length_mode'] ) : 'characters', |
| 171 | 'options' => array( |
| 172 | 'characters' => esc_html__( 'Characters', 'everest-forms' ), |
| 173 | 'words' => esc_html__( 'Words Count', 'everest-forms' ), |
| 174 | ), |
| 175 | ), |
| 176 | false |
| 177 | ); |
| 178 | $args = array( |
| 179 | 'slug' => 'min_length_controls', |
| 180 | 'class' => ! isset( $field['min_length_enabled'] ) ? 'everest-forms-hidden' : '', |
| 181 | 'content' => $count . $mode, |
| 182 | ); |
| 183 | $this->field_element( 'row', $field, $args ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Enqueue shortcode scripts. |
| 188 | * |
| 189 | * @param array $atts Shortcode Attributes. |
| 190 | */ |
| 191 | public function load_assets( $atts ) { |
| 192 | $form_id = isset( $atts['id'] ) ? wp_unslash( $atts['id'] ) : ''; // WPCS: CSRF ok, input var ok, sanitization ok. |
| 193 | $form_obj = evf()->form->get( $form_id ); |
| 194 | $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : ''; |
| 195 | |
| 196 | // Leave only fields with limit. |
| 197 | if ( ! empty( $form_data['form_fields'] ) ) { |
| 198 | $form_fields = array_filter( $form_data['form_fields'], array( $this, 'field_is_limit' ) ); |
| 199 | |
| 200 | if ( count( $form_fields ) ) { |
| 201 | wp_enqueue_script( 'everest-forms-text-limit' ); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Field preview inside the builder. |
| 208 | * |
| 209 | * @since 1.0.0 |
| 210 | * |
| 211 | * @param array $field Field data and settings. |
| 212 | */ |
| 213 | public function field_preview( $field ) { |
| 214 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 215 | |
| 216 | // Label. |
| 217 | $this->field_preview_option( 'label', $field ); |
| 218 | |
| 219 | // Primary input. |
| 220 | echo '<textarea placeholder="' . esc_attr( $placeholder ) . '" class="widefat" disabled></textarea>'; |
| 221 | |
| 222 | // Description. |
| 223 | $this->field_preview_option( 'description', $field ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Field display on the form front-end. |
| 228 | * |
| 229 | * @since 1.0.0 |
| 230 | * |
| 231 | * @param array $field Field Data. |
| 232 | * @param array $field_atts Field attributes. |
| 233 | * @param array $form_data All Form Data. |
| 234 | */ |
| 235 | public function field_display( $field, $field_atts, $form_data ) { |
| 236 | // Define data. |
| 237 | $value = ''; |
| 238 | $primary = $field['properties']['inputs']['primary']; |
| 239 | |
| 240 | if ( isset( $primary['attr']['value'] ) ) { |
| 241 | $value = evf_sanitize_textarea_field( $primary['attr']['value'] ); |
| 242 | unset( $primary['attr']['value'] ); |
| 243 | } |
| 244 | |
| 245 | // Limit length. |
| 246 | if ( isset( $field['limit_enabled'] ) ) { |
| 247 | $limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0; |
| 248 | $limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters'; |
| 249 | |
| 250 | $primary['data']['form-id'] = $form_data['id']; |
| 251 | $primary['data']['field-id'] = $field['id']; |
| 252 | |
| 253 | if ( 'characters' === $limit_mode ) { |
| 254 | $primary['class'][] = 'everest-forms-limit-characters-enabled'; |
| 255 | $primary['attr']['maxlength'] = $limit_count; |
| 256 | $primary['data']['text-limit'] = $limit_count; |
| 257 | } else { |
| 258 | $primary['class'][] = 'everest-forms-limit-words-enabled'; |
| 259 | $primary['data']['text-limit'] = $limit_count; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Minimum length. |
| 264 | if ( isset( $field['min_length_enabled'] ) ) { |
| 265 | $min_length_count = isset( $field['min_length_count'] ) ? absint( $field['min_length_count'] ) : 0; |
| 266 | $min_length_mode = isset( $field['min_length_mode'] ) ? sanitize_key( $field['min_length_mode'] ) : 'characters'; |
| 267 | |
| 268 | $primary['data']['form-id'] = $form_data['id']; |
| 269 | $primary['data']['field-id'] = $field['id']; |
| 270 | |
| 271 | if ( 'characters' === $min_length_mode ) { |
| 272 | $primary['class'][] = 'everest-forms-min-characters-length-enabled'; |
| 273 | $primary['attr']['minlength'] = $min_length_count; |
| 274 | $primary['data']['text-min-length'] = $min_length_count; |
| 275 | } else { |
| 276 | $primary['class'][] = 'everest-forms-min-words-length-enabled'; |
| 277 | $primary['data']['text-min-length'] = $min_length_count; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // Primary field. |
| 282 | printf( |
| 283 | '<textarea %s %s >%s</textarea>', |
| 284 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 285 | esc_attr( $primary['required'] ), |
| 286 | esc_html( $value ) |
| 287 | ); |
| 288 | } |
| 289 | } |
| 290 |