| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extra Form Builder field types. |
| 4 |
* |
| 5 |
* Range, rating, signature and acceptance live here rather than in |
| 6 |
* Form_Builder.php so the widget file stays readable. |
| 7 |
* |
| 8 |
* @package King_Addons |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace King_Addons; |
| 12 |
|
| 13 |
use Elementor\Controls_Manager; |
| 14 |
use Elementor\Repeater; |
| 15 |
|
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Controls and markup for the field types added on top of the original set. |
| 22 |
*/ |
| 23 |
class Form_Extra_Fields |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Field types this class renders. |
| 27 |
* |
| 28 |
* @return array<string,string> |
| 29 |
*/ |
| 30 |
public static function field_types(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'range' => esc_html__('Range Slider', 'king-addons'), |
| 34 |
'rating' => esc_html__('Rating', 'king-addons'), |
| 35 |
'signature' => esc_html__('Signature', 'king-addons'), |
| 36 |
'acceptance' => esc_html__('Acceptance', 'king-addons'), |
| 37 |
'calculation' => esc_html__('Calculation', 'king-addons'), |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Types that carry their own markup instead of a plain input. |
| 43 |
* |
| 44 |
* @return array<int,string> |
| 45 |
*/ |
| 46 |
public static function custom_markup_types(): array |
| 47 |
{ |
| 48 |
return ['rating', 'signature', 'acceptance', 'calculation']; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Add the per-field controls to the fields repeater. |
| 53 |
* |
| 54 |
* @param Repeater $repeater Fields repeater. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function register_controls(Repeater $repeater): void |
| 59 |
{ |
| 60 |
$repeater->add_control( |
| 61 |
'range_min', |
| 62 |
[ |
| 63 |
'label' => esc_html__('Minimum', 'king-addons'), |
| 64 |
'type' => Controls_Manager::NUMBER, |
| 65 |
'default' => 0, |
| 66 |
'condition' => ['field_type' => 'range'], |
| 67 |
] |
| 68 |
); |
| 69 |
|
| 70 |
$repeater->add_control( |
| 71 |
'range_max', |
| 72 |
[ |
| 73 |
'label' => esc_html__('Maximum', 'king-addons'), |
| 74 |
'type' => Controls_Manager::NUMBER, |
| 75 |
'default' => 100, |
| 76 |
'condition' => ['field_type' => 'range'], |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
$repeater->add_control( |
| 81 |
'range_step', |
| 82 |
[ |
| 83 |
'label' => esc_html__('Step', 'king-addons'), |
| 84 |
'type' => Controls_Manager::NUMBER, |
| 85 |
'min' => 0, |
| 86 |
'default' => 1, |
| 87 |
'condition' => ['field_type' => 'range'], |
| 88 |
] |
| 89 |
); |
| 90 |
|
| 91 |
$repeater->add_control( |
| 92 |
'range_show_value', |
| 93 |
[ |
| 94 |
'label' => esc_html__('Show the value', 'king-addons'), |
| 95 |
'type' => Controls_Manager::SWITCHER, |
| 96 |
'return_value' => 'yes', |
| 97 |
'default' => 'yes', |
| 98 |
'condition' => ['field_type' => 'range'], |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$repeater->add_control( |
| 103 |
'rating_max', |
| 104 |
[ |
| 105 |
'label' => esc_html__('How many icons', 'king-addons'), |
| 106 |
'type' => Controls_Manager::NUMBER, |
| 107 |
'min' => 2, |
| 108 |
'max' => 10, |
| 109 |
'default' => 5, |
| 110 |
'condition' => ['field_type' => 'rating'], |
| 111 |
] |
| 112 |
); |
| 113 |
|
| 114 |
$repeater->add_control( |
| 115 |
'rating_icon', |
| 116 |
[ |
| 117 |
'label' => esc_html__('Icon', 'king-addons'), |
| 118 |
'type' => Controls_Manager::SELECT, |
| 119 |
'options' => [ |
| 120 |
'star' => esc_html__('Star', 'king-addons'), |
| 121 |
'heart' => esc_html__('Heart', 'king-addons'), |
| 122 |
'circle' => esc_html__('Circle', 'king-addons'), |
| 123 |
], |
| 124 |
'default' => 'star', |
| 125 |
'condition' => ['field_type' => 'rating'], |
| 126 |
] |
| 127 |
); |
| 128 |
|
| 129 |
$repeater->add_control( |
| 130 |
'signature_height', |
| 131 |
[ |
| 132 |
'label' => esc_html__('Height (px)', 'king-addons'), |
| 133 |
'type' => Controls_Manager::NUMBER, |
| 134 |
'min' => 80, |
| 135 |
'max' => 600, |
| 136 |
'default' => 160, |
| 137 |
'condition' => ['field_type' => 'signature'], |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
$repeater->add_control( |
| 142 |
'signature_clear_text', |
| 143 |
[ |
| 144 |
'label' => esc_html__('Clear button', 'king-addons'), |
| 145 |
'type' => Controls_Manager::TEXT, |
| 146 |
'default' => esc_html__('Clear', 'king-addons'), |
| 147 |
'dynamic' => ['active' => true], |
| 148 |
'condition' => ['field_type' => 'signature'], |
| 149 |
] |
| 150 |
); |
| 151 |
|
| 152 |
$repeater->add_control( |
| 153 |
'acceptance_text', |
| 154 |
[ |
| 155 |
'label' => esc_html__('Consent text', 'king-addons'), |
| 156 |
'type' => Controls_Manager::TEXTAREA, |
| 157 |
'default' => esc_html__('I agree to the terms.', 'king-addons'), |
| 158 |
'dynamic' => ['active' => true], |
| 159 |
'description' => esc_html__('Links and basic formatting are allowed.', 'king-addons'), |
| 160 |
'condition' => ['field_type' => 'acceptance'], |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
$repeater->add_control( |
| 165 |
'acceptance_checked', |
| 166 |
[ |
| 167 |
'label' => esc_html__('Checked by default', 'king-addons'), |
| 168 |
'type' => Controls_Manager::SWITCHER, |
| 169 |
'return_value' => 'yes', |
| 170 |
'condition' => ['field_type' => 'acceptance'], |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$repeater->add_control( |
| 175 |
'calc_formula', |
| 176 |
[ |
| 177 |
'label' => esc_html__('Formula', 'king-addons'), |
| 178 |
'type' => Controls_Manager::TEXT, |
| 179 |
'placeholder' => '[seats] * 25 + [extras]', |
| 180 |
'description' => esc_html__('Reference other fields by their ID in square brackets. Supported: + - * / ( ) and numbers.', 'king-addons'), |
| 181 |
'condition' => ['field_type' => 'calculation'], |
| 182 |
] |
| 183 |
); |
| 184 |
|
| 185 |
$repeater->add_control( |
| 186 |
'calc_decimals', |
| 187 |
[ |
| 188 |
'label' => esc_html__('Decimals', 'king-addons'), |
| 189 |
'type' => Controls_Manager::NUMBER, |
| 190 |
'min' => 0, |
| 191 |
'max' => 6, |
| 192 |
'default' => 2, |
| 193 |
'condition' => ['field_type' => 'calculation'], |
| 194 |
] |
| 195 |
); |
| 196 |
|
| 197 |
$repeater->add_control( |
| 198 |
'calc_prefix', |
| 199 |
[ |
| 200 |
'label' => esc_html__('Prefix', 'king-addons'), |
| 201 |
'type' => Controls_Manager::TEXT, |
| 202 |
'dynamic' => ['active' => true], |
| 203 |
'condition' => ['field_type' => 'calculation'], |
| 204 |
] |
| 205 |
); |
| 206 |
|
| 207 |
$repeater->add_control( |
| 208 |
'calc_suffix', |
| 209 |
[ |
| 210 |
'label' => esc_html__('Suffix', 'king-addons'), |
| 211 |
'type' => Controls_Manager::TEXT, |
| 212 |
'dynamic' => ['active' => true], |
| 213 |
'condition' => ['field_type' => 'calculation'], |
| 214 |
] |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Markup for one of the added field types. |
| 220 |
* |
| 221 |
* @param array<string,mixed> $item Repeater item. |
| 222 |
* @param string $name Input name attribute. |
| 223 |
* @param string $id Input id attribute. |
| 224 |
* |
| 225 |
* @return string |
| 226 |
*/ |
| 227 |
public static function render(array $item, string $name, string $id): string |
| 228 |
{ |
| 229 |
switch ($item['field_type']) { |
| 230 |
case 'rating': |
| 231 |
return self::render_rating($item, $name, $id); |
| 232 |
case 'signature': |
| 233 |
return self::render_signature($item, $name, $id); |
| 234 |
case 'acceptance': |
| 235 |
return self::render_acceptance($item, $name, $id); |
| 236 |
case 'calculation': |
| 237 |
return self::render_calculation($item, $name, $id); |
| 238 |
} |
| 239 |
|
| 240 |
return ''; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Star (or heart, or circle) rating. |
| 245 |
* |
| 246 |
* Built from radio inputs so it works without JavaScript and reads |
| 247 |
* correctly to a screen reader; the hidden input carries the value the |
| 248 |
* form collector picks up. |
| 249 |
* |
| 250 |
* @param array<string,mixed> $item Repeater item. |
| 251 |
* @param string $name Input name. |
| 252 |
* @param string $id Input id. |
| 253 |
* |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
private static function render_rating(array $item, string $name, string $id): string |
| 257 |
{ |
| 258 |
$max = isset($item['rating_max']) ? (int) $item['rating_max'] : 5; |
| 259 |
if ($max < 2 || $max > 10) { |
| 260 |
$max = 5; |
| 261 |
} |
| 262 |
|
| 263 |
$icons = ['star' => '� |
| 264 |
', 'heart' => '♥', 'circle' => '●']; |
| 265 |
$icon_key = isset($item['rating_icon']) ? (string) $item['rating_icon'] : 'star'; |
| 266 |
$icon = $icons[$icon_key] ?? $icons['star']; |
| 267 |
|
| 268 |
$required = self::is_required($item) ? ' data-ka-required="1"' : ''; |
| 269 |
|
| 270 |
$html = '<div class="king-addons-form-rating" data-ka-rating="1" data-max="' . esc_attr((string) $max) . '"' . $required . '>'; |
| 271 |
|
| 272 |
for ($i = 1; $i <= $max; $i++) { |
| 273 |
$html .= '<button type="button" class="king-addons-form-rating__item" data-value="' . esc_attr((string) $i) . '"' |
| 274 |
. ' aria-label="' . esc_attr(sprintf( |
| 275 |
/* translators: 1: chosen value, 2: maximum. */ |
| 276 |
esc_html__('%1$d out of %2$d', 'king-addons'), |
| 277 |
$i, |
| 278 |
$max |
| 279 |
)) . '">' |
| 280 |
. '<span aria-hidden="true">' . esc_html($icon) . '</span></button>'; |
| 281 |
} |
| 282 |
|
| 283 |
$html .= '<input type="hidden" class="king-addons-form-field" name="' . esc_attr($name) . '" id="' . esc_attr($id) . '" value="">'; |
| 284 |
$html .= '</div>'; |
| 285 |
|
| 286 |
return $html; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Signature pad. |
| 291 |
* |
| 292 |
* @param array<string,mixed> $item Repeater item. |
| 293 |
* @param string $name Input name. |
| 294 |
* @param string $id Input id. |
| 295 |
* |
| 296 |
* @return string |
| 297 |
*/ |
| 298 |
private static function render_signature(array $item, string $name, string $id): string |
| 299 |
{ |
| 300 |
$height = isset($item['signature_height']) ? (int) $item['signature_height'] : 160; |
| 301 |
if ($height < 80 || $height > 600) { |
| 302 |
$height = 160; |
| 303 |
} |
| 304 |
|
| 305 |
$clear = isset($item['signature_clear_text']) && '' !== $item['signature_clear_text'] |
| 306 |
? (string) $item['signature_clear_text'] |
| 307 |
: esc_html__('Clear', 'king-addons'); |
| 308 |
|
| 309 |
$required = self::is_required($item) ? ' data-ka-required="1"' : ''; |
| 310 |
|
| 311 |
return '<div class="king-addons-form-signature" data-ka-signature="1"' . $required . '>' |
| 312 |
. '<canvas class="king-addons-form-signature__pad" height="' . esc_attr((string) $height) . '"' |
| 313 |
. ' style="height:' . esc_attr((string) $height) . 'px"></canvas>' |
| 314 |
. '<button type="button" class="king-addons-form-signature__clear">' . esc_html($clear) . '</button>' |
| 315 |
. '<input type="hidden" class="king-addons-form-field" name="' . esc_attr($name) . '" id="' . esc_attr($id) . '" value="">' |
| 316 |
. '</div>'; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Consent checkbox with its own inline text. |
| 321 |
* |
| 322 |
* @param array<string,mixed> $item Repeater item. |
| 323 |
* @param string $name Input name. |
| 324 |
* @param string $id Input id. |
| 325 |
* |
| 326 |
* @return string |
| 327 |
*/ |
| 328 |
private static function render_acceptance(array $item, string $name, string $id): string |
| 329 |
{ |
| 330 |
$text = isset($item['acceptance_text']) ? (string) $item['acceptance_text'] : ''; |
| 331 |
$checked = 'yes' === ($item['acceptance_checked'] ?? '') ? ' checked' : ''; |
| 332 |
$required = self::is_required($item) ? ' required="required"' : ''; |
| 333 |
|
| 334 |
return '<div class="king-addons-form-acceptance">' |
| 335 |
. '<input type="checkbox" class="king-addons-form-field" name="' . esc_attr($name) . '"' |
| 336 |
. ' id="' . esc_attr($id) . '" value="1"' . $checked . $required . '>' |
| 337 |
. '<label class="king-addons-form-acceptance__text" for="' . esc_attr($id) . '">' |
| 338 |
. wp_kses_post($text) |
| 339 |
. '</label>' |
| 340 |
. '</div>'; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* A read-only field whose value is worked out from the others. |
| 345 |
* |
| 346 |
* The formula is evaluated in the browser by a small parser - never by |
| 347 |
* eval() - and the result lands in a read-only input so it travels with the |
| 348 |
* submission like any other value. |
| 349 |
* |
| 350 |
* @param array<string,mixed> $item Repeater item. |
| 351 |
* @param string $name Input name. |
| 352 |
* @param string $id Input id. |
| 353 |
* |
| 354 |
* @return string |
| 355 |
*/ |
| 356 |
private static function render_calculation(array $item, string $name, string $id): string |
| 357 |
{ |
| 358 |
$formula = isset($item['calc_formula']) ? (string) $item['calc_formula'] : ''; |
| 359 |
$decimals = isset($item['calc_decimals']) && '' !== $item['calc_decimals'] ? (int) $item['calc_decimals'] : 2; |
| 360 |
if ($decimals < 0 || $decimals > 6) { |
| 361 |
$decimals = 2; |
| 362 |
} |
| 363 |
|
| 364 |
return '<div class="king-addons-form-calculation" data-ka-calc="1"' |
| 365 |
. ' data-formula="' . esc_attr($formula) . '"' |
| 366 |
. ' data-decimals="' . esc_attr((string) $decimals) . '"' |
| 367 |
. ' data-prefix="' . esc_attr((string) ($item['calc_prefix'] ?? '')) . '"' |
| 368 |
. ' data-suffix="' . esc_attr((string) ($item['calc_suffix'] ?? '')) . '">' |
| 369 |
. '<input type="text" class="king-addons-form-field king-addons-form-field-textual" readonly' |
| 370 |
. ' name="' . esc_attr($name) . '" id="' . esc_attr($id) . '" value="">' |
| 371 |
. '</div>'; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Whether the author marked the field required. |
| 376 |
* |
| 377 |
* @param array<string,mixed> $item Repeater item. |
| 378 |
* |
| 379 |
* @return bool |
| 380 |
*/ |
| 381 |
private static function is_required(array $item): bool |
| 382 |
{ |
| 383 |
return !empty($item['required']) && 'true' === (string) $item['required']; |
| 384 |
} |
| 385 |
} |
| 386 |
|