| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\App\Services\FormBuilder\RatingIcon; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class Rating extends BaseComponent |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Compile and echo the html element |
| 13 |
* |
| 14 |
* @param array $data [element data] |
| 15 |
* @param \stdClass $form [Form Object] |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function compile($data, $form) |
| 20 |
{ |
| 21 |
$elementName = $data['element']; |
| 22 |
|
| 23 |
$data = apply_filters_deprecated( |
| 24 |
'fluentform_rendering_field_data_' . $elementName, |
| 25 |
[ |
| 26 |
$data, |
| 27 |
$form |
| 28 |
], |
| 29 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 30 |
'fluentform/rendering_field_data_' . $elementName, |
| 31 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 32 |
); |
| 33 |
|
| 34 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 35 |
|
| 36 |
$data['attributes']['type'] = 'radio'; |
| 37 |
|
| 38 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 39 |
|
| 40 |
$iconSettings = RatingIcon::resolveSettings($data); |
| 41 |
$iconSvg = RatingIcon::getResolvedIconSvg($data, [ |
| 42 |
'class' => 'ff-rating-icon-svg jss-ff-svg ff-svg', |
| 43 |
]); |
| 44 |
$ratingStyles = sprintf( |
| 45 |
'--ff-rating-inactive-color: %s; --ff-rating-active-color: %s;', |
| 46 |
esc_attr($iconSettings['inactive_color']), |
| 47 |
esc_attr($iconSettings['active_color']) |
| 48 |
); |
| 49 |
|
| 50 |
$firstTabIndex = Helper::getNextTabIndex(); |
| 51 |
$elMarkup = "<div class='ff-el-ratings jss-ff-el-ratings' role='radiogroup' style='" . $ratingStyles . "' data-rating-icon-source='" . esc_attr($iconSettings['icon_source']) . "' data-rating-icon-type='" . esc_attr($iconSettings['icon_type']) . "' data-base-tabindex='" . esc_attr($firstTabIndex ?: 0) . "'>"; |
| 52 |
$ratingText = ''; |
| 53 |
$selectedValues = array_filter(array_map('strval', $defaultValues), function ($value) { |
| 54 |
return $value !== ''; |
| 55 |
}); |
| 56 |
$hasSelectedValue = !empty($selectedValues); |
| 57 |
$optionIndex = 0; |
| 58 |
|
| 59 |
foreach ((array) ArrayHelper::get($data, 'options', []) as $value => $label) { |
| 60 |
$starred = ''; |
| 61 |
$isSelected = in_array((string) $value, $selectedValues, true); |
| 62 |
|
| 63 |
if ($isSelected) { |
| 64 |
$data['attributes']['checked'] = true; |
| 65 |
$starred = 'active'; |
| 66 |
} else { |
| 67 |
$data['attributes']['checked'] = false; |
| 68 |
} |
| 69 |
|
| 70 |
$data['attributes']['tabindex'] = -1; |
| 71 |
$atts = $this->buildAttributes($data['attributes']); |
| 72 |
$id = esc_attr($this->getUniqueId(str_replace(['[', ']'], ['', ''], $data['attributes']['name']))); |
| 73 |
|
| 74 |
$ariaRequired = 'false'; |
| 75 |
if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) { |
| 76 |
$ariaRequired = 'true'; |
| 77 |
} |
| 78 |
|
| 79 |
$labelTabIndex = -1; |
| 80 |
if ($isSelected || (!$hasSelectedValue && $optionIndex === 0)) { |
| 81 |
$labelTabIndex = $firstTabIndex ?: 0; |
| 82 |
} |
| 83 |
|
| 84 |
$checked = $isSelected ? 'true' : 'false'; |
| 85 |
$elMarkup .= "<label for={$id} class='{$starred}' role='radio' aria-checked='{$checked}' aria-label='" . esc_attr(wp_strip_all_tags($label)) . "' tabindex='" . esc_attr($labelTabIndex) . "'><input {$atts} id={$id} aria-valuenow='" . esc_attr($value) . "' value='" . esc_attr($value) . "' aria-required={$ariaRequired} aria-invalid='false'>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $starred, $atts, $id are escaped before being passed in. |
| 86 |
$elMarkup .= "<span class='ff-rating-icon' aria-hidden='true'>{$iconSvg}</span>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG is sanitized before output. |
| 87 |
$elMarkup .= '</label>'; |
| 88 |
|
| 89 |
if ('yes' == ArrayHelper::get($data, 'settings.show_text')) { |
| 90 |
$displayDefaultText = $isSelected ? 'display: inline-block' : 'display: none'; |
| 91 |
$ratingText .= "<span style='{$displayDefaultText}' class='ff-el-rating-text' data-id='{$id}'>" . fluentform_sanitize_html($label) . '</span>'; |
| 92 |
} |
| 93 |
|
| 94 |
$optionIndex++; |
| 95 |
}; |
| 96 |
|
| 97 |
$elMarkup .= '</div>' . $ratingText; |
| 98 |
|
| 99 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 100 |
|
| 101 |
$html = apply_filters_deprecated( |
| 102 |
'fluentform_rendering_field_html_' . $elementName, |
| 103 |
[ |
| 104 |
$html, |
| 105 |
$data, |
| 106 |
$form |
| 107 |
], |
| 108 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 109 |
'fluentform/rendering_field_html_' . $elementName, |
| 110 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 111 |
); |
| 112 |
|
| 113 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 114 |
} |
| 115 |
} |
| 116 |
|