address-markup.php
1 month ago
base.php
2 months ago
checkbox-markup.php
1 year ago
countries.json
1 year ago
dropdown-markup.php
1 month ago
email-markup.php
2 months ago
gdpr-markup.php
4 months ago
inlinebutton-markup.php
3 weeks ago
input-markup.php
4 months ago
multichoice-markup.php
1 month ago
number-markup.php
4 months ago
payment-markup.php
2 months ago
phone-markup.php
1 month ago
textarea-markup.php
1 month ago
url-markup.php
4 months ago
textarea-markup.php
206 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sureforms Textarea Markup Class file. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Fields; |
| 10 | |
| 11 | use SRFM\Inc\Helper; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Sureforms Textarea Markup Class. |
| 19 | * |
| 20 | * @since 0.0.1 |
| 21 | */ |
| 22 | class Textarea_Markup extends Base { |
| 23 | /** |
| 24 | * Minimum length of text required for the textarea. |
| 25 | * |
| 26 | * @var string |
| 27 | * @since 2.8.2 |
| 28 | */ |
| 29 | protected $min_length; |
| 30 | |
| 31 | /** |
| 32 | * HTML attribute string for the minimum length. |
| 33 | * |
| 34 | * @var string |
| 35 | * @since 2.8.2 |
| 36 | */ |
| 37 | protected $min_length_attr; |
| 38 | |
| 39 | /** |
| 40 | * Maximum length of text allowed for the textarea. |
| 41 | * |
| 42 | * @var string |
| 43 | * @since 0.0.2 |
| 44 | */ |
| 45 | protected $max_length; |
| 46 | |
| 47 | /** |
| 48 | * HTML attribute string for the maximum length. |
| 49 | * |
| 50 | * @var string |
| 51 | * @since 0.0.2 |
| 52 | */ |
| 53 | protected $max_length_attr; |
| 54 | |
| 55 | /** |
| 56 | * HTML string for displaying the maximum length in the UI. |
| 57 | * |
| 58 | * @var string |
| 59 | * @since 0.0.2 |
| 60 | */ |
| 61 | protected $max_length_html; |
| 62 | |
| 63 | /** |
| 64 | * Number of rows for the textarea. |
| 65 | * |
| 66 | * @var string |
| 67 | * @since 0.0.2 |
| 68 | */ |
| 69 | protected $rows; |
| 70 | |
| 71 | /** |
| 72 | * HTML attribute string for the number of rows. |
| 73 | * |
| 74 | * @var string |
| 75 | * @since 0.0.2 |
| 76 | */ |
| 77 | protected $rows_attr; |
| 78 | |
| 79 | /** |
| 80 | * Indicates whether the textarea is a rich text editor. |
| 81 | * |
| 82 | * @var bool |
| 83 | * @since 1.7.1 |
| 84 | */ |
| 85 | protected $is_richtext; |
| 86 | |
| 87 | /** |
| 88 | * Read-only attribute for the textarea field. |
| 89 | * |
| 90 | * @var bool |
| 91 | * @since 1.7.2 |
| 92 | */ |
| 93 | protected $read_only; |
| 94 | |
| 95 | /** |
| 96 | * Append random ID to the textarea for uniqueness. |
| 97 | * |
| 98 | * @var int |
| 99 | * @since 1.7.3 |
| 100 | */ |
| 101 | protected $random_id; |
| 102 | |
| 103 | /** |
| 104 | * Initialize the properties based on block attributes. |
| 105 | * |
| 106 | * @param array<mixed> $attributes Block attributes. |
| 107 | * @since 0.0.2 |
| 108 | */ |
| 109 | public function __construct( $attributes ) { |
| 110 | $this->set_properties( $attributes ); |
| 111 | $this->set_input_label( __( 'Textarea', 'sureforms' ) ); |
| 112 | $this->set_error_msg( $attributes, 'srfm_textarea_block_required_text' ); |
| 113 | $this->slug = 'textarea'; |
| 114 | $this->is_richtext = $attributes['isRichText'] ?? false; |
| 115 | // Min-length only applies to plain textareas; the rich-text submission contains HTML markup. |
| 116 | $this->min_length = $this->is_richtext ? '' : ( $attributes['minLength'] ?? '' ); |
| 117 | $this->max_length = $attributes['maxLength'] ?? ''; |
| 118 | // Misconfiguration guard — if min exceeds max, drop min so the form stays submittable. |
| 119 | // The editor already warns the form-builder when this happens. |
| 120 | if ( |
| 121 | '' !== $this->min_length && |
| 122 | '' !== $this->max_length && |
| 123 | (int) $this->min_length > (int) $this->max_length |
| 124 | ) { |
| 125 | $this->min_length = ''; |
| 126 | } |
| 127 | $this->rows = $attributes['rows'] ?? ''; |
| 128 | $this->read_only = ! empty( trim( $this->default ) ) && $attributes['readOnly']; |
| 129 | // html attributes. |
| 130 | $this->min_length_attr = $this->min_length ? ' data-minlength="' . esc_attr( $this->min_length ) . '" ' : ''; |
| 131 | $this->max_length_attr = $this->max_length ? ' maxLength="' . esc_attr( $this->max_length ) . '" ' : ''; |
| 132 | $this->rows_attr = $this->rows ? ' rows="' . esc_attr( $this->rows ) . '" ' : ''; |
| 133 | $this->max_length_html = '' !== $this->max_length ? '0/' . $this->max_length : ''; |
| 134 | $this->random_id = wp_rand( 1000, 9999 ); |
| 135 | $this->set_unique_slug(); |
| 136 | $this->set_field_name( $this->unique_slug ); |
| 137 | $this->set_markup_properties( $this->input_label . '-' . $this->random_id, ! empty( $this->min_length ) ); |
| 138 | $this->set_aria_described_by(); |
| 139 | $this->set_label_as_placeholder( $this->input_label ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Render the sureforms textarea classic styling |
| 144 | * |
| 145 | * @since 0.0.2 |
| 146 | * @return string|bool |
| 147 | */ |
| 148 | public function markup() { |
| 149 | $classes = [ |
| 150 | 'srfm-block-single', |
| 151 | 'srfm-block', |
| 152 | 'srfm-' . $this->slug . '-block', |
| 153 | 'srf-' . $this->slug . '-' . $this->block_id . '-block', |
| 154 | $this->block_width, |
| 155 | $this->class_name, |
| 156 | $this->conditional_class, |
| 157 | $this->is_richtext ? 'srfm-richtext' : '', |
| 158 | $this->read_only ? 'srfm-read-only' : '', |
| 159 | ]; |
| 160 | |
| 161 | $classes = Helper::join_strings( $classes ); |
| 162 | $random_id = $this->unique_slug . '-' . $this->random_id; |
| 163 | $has_counter = ! $this->is_richtext && ( '' !== $this->min_length || '' !== $this->max_length ); |
| 164 | $display_limit = $has_counter ? ( '' !== $this->max_length ? $this->max_length : $this->min_length ) : ''; |
| 165 | |
| 166 | ob_start(); ?> |
| 167 | <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 168 | <?php echo wp_kses_post( $this->label_markup ); ?> |
| 169 | <?php echo wp_kses_post( $this->help_markup ); ?> |
| 170 | <div class="srfm-block-wrap"> |
| 171 | <textarea |
| 172 | class="srfm-input-common srfm-input-<?php echo esc_attr( $this->slug ); ?>" |
| 173 | name="<?php echo esc_attr( $this->field_name ); ?>" |
| 174 | id="<?php echo esc_attr( $random_id ); ?>" |
| 175 | <?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 176 | data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" <?php echo wp_kses_post( $this->min_length_attr . $this->max_length_attr . $this->rows_attr ); ?> <?php echo wp_kses_post( $this->placeholder_attr ); ?> |
| 177 | <?php echo $this->is_richtext ? 'data-is-richtext="true"' : ''; ?> |
| 178 | <?php echo $this->read_only ? 'readonly' : ''; ?> |
| 179 | ><?php echo esc_html( $this->default ); ?></textarea> |
| 180 | <?php if ( $this->is_richtext ) { ?> |
| 181 | <div class="quill-editor-container"> |
| 182 | <div id="quill-<?php echo esc_attr( $random_id ); ?>"></div> |
| 183 | </div> |
| 184 | <?php } ?> |
| 185 | </div> |
| 186 | <div class="srfm-error-wrap"> |
| 187 | <?php echo wp_kses_post( $this->error_msg_markup ); ?> |
| 188 | <?php if ( $has_counter ) { ?> |
| 189 | <div class="srfm-char-counter-wrap"> |
| 190 | <span class="srfm-char-counter" |
| 191 | data-counter-limit="<?php echo esc_attr( $display_limit ); ?>" |
| 192 | <?php if ( '' !== $this->min_length ) { ?> |
| 193 | data-counter-min="<?php echo esc_attr( $this->min_length ); ?>" |
| 194 | <?php } ?> |
| 195 | aria-live="polite" |
| 196 | >0/<?php echo esc_html( $display_limit ); ?></span> |
| 197 | </div> |
| 198 | <?php } ?> |
| 199 | </div> |
| 200 | </div> |
| 201 | |
| 202 | <?php |
| 203 | return ob_get_clean(); |
| 204 | } |
| 205 | } |
| 206 |