| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SRFM\Inc\Fields; |
| 10 | 10 | |
| 11 | +use SRFM\Inc\Helper; | |
| 12 | + | |
| 11 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 14 | exit; // Exit if accessed directly. |
| 13 | 15 | } |
| 14 | 16 | |
| @@ -17,10 +19,25 @@ | ||
| 17 | 19 | * |
| 18 | 20 | * @since 0.0.1 |
| 19 | 21 | */ |
| 20 | 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; | |
| 21 | 30 | |
| 22 | 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 | + /** | |
| 23 | 40 | * Maximum length of text allowed for the textarea. |
| 24 | 41 | * |
| 25 | 42 | * @var string |
| 26 | 43 | * @since 0.0.2 |
| @@ -59,24 +76,32 @@ | ||
| 59 | 76 | */ |
| 60 | 77 | protected $rows_attr; |
| 61 | 78 | |
| 62 | 79 | /** |
| 63 | - * Number of columns for the textarea. | |
| 80 | + * Indicates whether the textarea is a rich text editor. | |
| 64 | 81 | * |
| 65 | - * @var string | |
| 66 | - * @since 0.0.2 | |
| 82 | + * @var bool | |
| 83 | + * @since 1.7.1 | |
| 67 | 84 | */ |
| 68 | - protected $cols; | |
| 85 | + protected $is_richtext; | |
| 69 | 86 | |
| 70 | 87 | /** |
| 71 | - * HTML attribute string for the number of columns. | |
| 88 | + * Read-only attribute for the textarea field. | |
| 72 | 89 | * |
| 73 | - * @var string | |
| 74 | - * @since 0.0.2 | |
| 90 | + * @var bool | |
| 91 | + * @since 1.7.2 | |
| 75 | 92 | */ |
| 76 | - protected $cols_attr; | |
| 93 | + protected $read_only; | |
| 77 | 94 | |
| 78 | 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 | + /** | |
| 79 | 104 | * Initialize the properties based on block attributes. |
| 80 | 105 | * |
| 81 | 106 | * @param array<mixed> $attributes Block attributes. |
| 82 | 107 | * @since 0.0.2 |
| @@ -84,20 +109,33 @@ | ||
| 84 | 109 | public function __construct( $attributes ) { |
| 85 | 110 | $this->set_properties( $attributes ); |
| 86 | 111 | $this->set_input_label( __( 'Textarea', 'sureforms' ) ); |
| 87 | 112 | $this->set_error_msg( $attributes, 'srfm_textarea_block_required_text' ); |
| 88 | - $this->slug = 'textarea'; | |
| 89 | - $this->max_length = isset( $attributes['maxLength'] ) ? $attributes['maxLength'] : ''; | |
| 90 | - $this->rows = isset( $attributes['rows'] ) ? $attributes['rows'] : ''; | |
| 91 | - $this->cols = isset( $attributes['cols'] ) ? $attributes['cols'] : ''; | |
| 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']; | |
| 92 | 129 | // html attributes. |
| 93 | - $this->max_length_attr = $this->max_length ? ' maxLength="' . $this->max_length . '" ' : ''; | |
| 94 | - $this->rows_attr = $this->rows ? ' rows="' . $this->rows . '" ' : ''; | |
| 95 | - $this->cols_attr = $this->cols ? ' cols="' . $this->cols . '" ' : ''; | |
| 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 ) . '" ' : ''; | |
| 96 | 133 | $this->max_length_html = '' !== $this->max_length ? '0/' . $this->max_length : ''; |
| 134 | + $this->random_id = wp_rand( 1000, 9999 ); | |
| 97 | 135 | $this->set_unique_slug(); |
| 98 | 136 | $this->set_field_name( $this->unique_slug ); |
| 99 | - $this->set_markup_properties( $this->input_label ); | |
| 137 | + $this->set_markup_properties( $this->input_label . '-' . $this->random_id, ! empty( $this->min_length ) ); | |
| 100 | 138 | $this->set_aria_described_by(); |
| 101 | 139 | $this->set_label_as_placeholder( $this->input_label ); |
| 102 | 140 | } |
| 103 | 141 | |
| @@ -104,26 +142,64 @@ | ||
| 104 | 142 | /** |
| 105 | 143 | * Render the sureforms textarea classic styling |
| 106 | 144 | * |
| 107 | 145 | * @since 0.0.2 |
| 108 | - * @return string|boolean | |
| 146 | + * @return string|bool | |
| 109 | 147 | */ |
| 110 | 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 | + | |
| 111 | 166 | ob_start(); ?> |
| 112 | - <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="srfm-block-single srfm-block srfm-<?php echo esc_attr( $this->slug ); ?>-block srf-<?php echo esc_attr( $this->slug ); ?>-<?php echo esc_attr( $this->block_id ); ?>-block<?php echo esc_attr( $this->block_width ); ?><?php echo esc_attr( $this->class_name ); ?> <?php echo esc_attr( $this->conditional_class ); ?>"> | |
| 167 | + <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> | |
| 113 | 168 | <?php echo wp_kses_post( $this->label_markup ); ?> |
| 114 | 169 | <?php echo wp_kses_post( $this->help_markup ); ?> |
| 115 | 170 | <div class="srfm-block-wrap"> |
| 116 | - <textarea class="srfm-input-common srfm-input-<?php echo esc_attr( $this->slug ); ?>" name="<?php echo esc_attr( $this->field_name ); ?>" id="<?php echo esc_attr( $this->unique_slug ); ?>" | |
| 117 | - <?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> | |
| 118 | - aria-required="<?php echo esc_attr( $this->aria_require_attr ); ?>" <?php echo wp_kses_post( $this->max_length_attr . '' . $this->cols_attr . '' . $this->rows_attr ); ?> <?php echo wp_kses_post( $this->placeholder_attr ); ?>><?php echo esc_html( $this->default ); ?></textarea> | |
| 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 } ?> | |
| 119 | 185 | </div> |
| 120 | 186 | <div class="srfm-error-wrap"> |
| 121 | 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 } ?> | |
| 122 | 199 | </div> |
| 123 | 200 | </div> |
| 124 | 201 | |
| 125 | 202 | <?php |
| 126 | 203 | return ob_get_clean(); |
| 127 | - | |
| 128 | 204 | } |
| 129 | 205 | } |