class-evf-field-address.php
7 years ago
class-evf-field-checkbox.php
6 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
7 years ago
class-evf-field-date-time.php
6 years ago
class-evf-field-email.php
6 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
6 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
6 years ago
class-evf-field-likert.php
7 years ago
class-evf-field-number.php
6 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-quantity.php
6 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-radio.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-scale-rating.php
7 years ago
class-evf-field-select.php
6 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
6 years ago
class-evf-field-textarea.php
6 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
6 years ago
class-evf-field-textarea.php
206 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', |
| 33 | ), |
| 34 | ), |
| 35 | 'advanced-options' => array( |
| 36 | 'field_options' => array( |
| 37 | 'size', |
| 38 | 'placeholder', |
| 39 | 'label_hide', |
| 40 | 'limit_length', |
| 41 | 'default_value', |
| 42 | 'css', |
| 43 | ), |
| 44 | ), |
| 45 | ); |
| 46 | |
| 47 | parent::__construct(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Hook in tabs. |
| 52 | */ |
| 53 | public function init_hooks() { |
| 54 | add_action( 'everest_forms_shortcode_scripts', array( $this, 'load_assets' ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Limit length field option. |
| 59 | * |
| 60 | * @param array $field Field settings. |
| 61 | */ |
| 62 | public function limit_length( $field ) { |
| 63 | // Limit length. |
| 64 | $args = array( |
| 65 | 'slug' => 'limit_enabled', |
| 66 | 'content' => $this->field_element( |
| 67 | 'checkbox', |
| 68 | $field, |
| 69 | array( |
| 70 | 'slug' => 'limit_enabled', |
| 71 | 'value' => isset( $field['limit_enabled'] ), |
| 72 | 'desc' => esc_html__( 'Limit Length', 'everest-forms' ), |
| 73 | 'tooltip' => esc_html__( 'Check this option to limit text length by characters or words count.', 'everest-forms' ), |
| 74 | ), |
| 75 | false |
| 76 | ), |
| 77 | ); |
| 78 | $this->field_element( 'row', $field, $args ); |
| 79 | |
| 80 | // Limit controls. |
| 81 | $count = $this->field_element( |
| 82 | 'text', |
| 83 | $field, |
| 84 | array( |
| 85 | 'type' => 'number', |
| 86 | 'class' => 'small-text', |
| 87 | 'slug' => 'limit_count', |
| 88 | 'attrs' => array( |
| 89 | 'min' => 1, |
| 90 | 'step' => 1, |
| 91 | 'pattern' => '[0-9]', |
| 92 | ), |
| 93 | 'value' => ! empty( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 1, |
| 94 | ), |
| 95 | false |
| 96 | ); |
| 97 | |
| 98 | $mode = $this->field_element( |
| 99 | 'select', |
| 100 | $field, |
| 101 | array( |
| 102 | 'slug' => 'limit_mode', |
| 103 | 'class' => 'limit-select', |
| 104 | 'value' => ! empty( $field['limit_mode'] ) ? esc_attr( $field['limit_mode'] ) : 'characters', |
| 105 | 'options' => array( |
| 106 | 'characters' => esc_html__( 'Characters', 'everest-forms' ), |
| 107 | 'words' => esc_html__( 'Words Count', 'everest-forms' ), |
| 108 | ), |
| 109 | ), |
| 110 | false |
| 111 | ); |
| 112 | $args = array( |
| 113 | 'slug' => 'limit_controls', |
| 114 | 'class' => ! isset( $field['limit_enabled'] ) ? 'everest-forms-hidden' : '', |
| 115 | 'content' => $count . $mode, |
| 116 | ); |
| 117 | $this->field_element( 'row', $field, $args ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Enqueue shortcode scripts. |
| 122 | * |
| 123 | * @param array $atts Shortcode Attributes. |
| 124 | */ |
| 125 | public function load_assets( $atts ) { |
| 126 | $form_id = isset( $atts['id'] ) ? wp_unslash( $atts['id'] ) : ''; // WPCS: CSRF ok, input var ok, sanitization ok. |
| 127 | $form_obj = evf()->form->get( $form_id ); |
| 128 | $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : ''; |
| 129 | |
| 130 | // Leave only fields with limit. |
| 131 | $form_fields = array_filter( $form_data['form_fields'], array( $this, 'field_is_limit' ) ); |
| 132 | |
| 133 | if ( count( $form_fields ) ) { |
| 134 | wp_enqueue_script( 'everest-forms-text-limit' ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Field preview inside the builder. |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | * |
| 143 | * @param array $field Field data and settings. |
| 144 | */ |
| 145 | public function field_preview( $field ) { |
| 146 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 147 | |
| 148 | // Label. |
| 149 | $this->field_preview_option( 'label', $field ); |
| 150 | |
| 151 | // Primary input. |
| 152 | echo '<textarea placeholder="' . esc_attr( $placeholder ) . '" class="widefat" disabled></textarea>'; |
| 153 | |
| 154 | // Description. |
| 155 | $this->field_preview_option( 'description', $field ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Field display on the form front-end. |
| 160 | * |
| 161 | * @since 1.0.0 |
| 162 | * |
| 163 | * @param array $field Field data and settings. |
| 164 | * @param array $deprecated Deprecated. |
| 165 | * @param array $form_data Form data and settings. |
| 166 | */ |
| 167 | public function field_display( $field, $deprecated, $form_data ) { |
| 168 | // Define data. |
| 169 | $value = ''; |
| 170 | $primary = $field['properties']['inputs']['primary']; |
| 171 | |
| 172 | if ( ! empty( $primary['attr']['value'] ) ) { |
| 173 | $value = $primary['attr']['value']; |
| 174 | unset( $primary['attr']['value'] ); |
| 175 | |
| 176 | $value = evf_sanitize_textarea_field( $value ); |
| 177 | } |
| 178 | |
| 179 | // Limit length. |
| 180 | if ( isset( $field['limit_enabled'] ) ) { |
| 181 | $limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0; |
| 182 | $limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters'; |
| 183 | |
| 184 | $primary['data']['form-id'] = $form_data['id']; |
| 185 | $primary['data']['field-id'] = $field['id']; |
| 186 | |
| 187 | if ( 'characters' === $limit_mode ) { |
| 188 | $primary['class'][] = 'everest-forms-limit-characters-enabled'; |
| 189 | $primary['attr']['maxlength'] = $limit_count; |
| 190 | $primary['data']['text-limit'] = $limit_count; |
| 191 | } else { |
| 192 | $primary['class'][] = 'everest-forms-limit-words-enabled'; |
| 193 | $primary['data']['text-limit'] = $limit_count; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Primary field. |
| 198 | printf( |
| 199 | '<textarea %s %s>%s</textarea>', |
| 200 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 201 | esc_attr( $primary['required'] ), |
| 202 | $value // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 203 | ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 204 | } |
| 205 | } |
| 206 |