| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
use FluentForm\App\Modules\Component\Component; |
| 7 |
|
| 8 |
class BaseComponent |
| 9 |
{ |
| 10 |
public $app; |
| 11 |
|
| 12 |
public function __construct($key = '', $title = '', $tags = [], $position = 'advanced') |
| 13 |
{ |
| 14 |
$this->app = wpFluentForm(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Build unique ID concating form id and name attribute |
| 19 |
* |
| 20 |
* @param array $data $form |
| 21 |
* |
| 22 |
* @return string for id value |
| 23 |
*/ |
| 24 |
protected function makeElementId($data, $form) |
| 25 |
{ |
| 26 |
if (isset($data['attributes']['name'])) { |
| 27 |
if (! empty($data['attributes']['id'])) { |
| 28 |
return $data['attributes']['id']; |
| 29 |
} |
| 30 |
$elementName = $data['attributes']['name']; |
| 31 |
$elementName = str_replace(['[', ']', ' '], '_', $elementName); |
| 32 |
return 'ff_' . esc_attr($form->id) . '_' . esc_attr($elementName) . ''; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Build attributes for any html element |
| 38 |
* |
| 39 |
* @param array $attributes |
| 40 |
* |
| 41 |
* @return string [Compiled key='value' attributes] |
| 42 |
*/ |
| 43 |
protected function buildAttributes($attributes, $form = null) |
| 44 |
{ |
| 45 |
$atts = ''; |
| 46 |
|
| 47 |
foreach ($attributes as $key => $value) { |
| 48 |
if ($value || 0 === $value || '0' === $value) { |
| 49 |
$value = htmlspecialchars($value); |
| 50 |
$atts .= esc_attr($key) . '="' . $value . '" '; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return $atts; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Extract value attribute from attribute list |
| 59 |
* |
| 60 |
* @param array &$element |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
protected function extractValueFromAttributes(&$element) |
| 65 |
{ |
| 66 |
$value = ''; |
| 67 |
|
| 68 |
if (isset($element['attributes']['value'])) { |
| 69 |
$value = $element['attributes']['value']; |
| 70 |
unset($element['attributes']['value']); |
| 71 |
} |
| 72 |
|
| 73 |
return $value; |
| 74 |
} |
| 75 |
|
| 76 |
protected function extractDynamicValues($data, $form) |
| 77 |
{ |
| 78 |
$defaultValues = []; |
| 79 |
if ($dynamicDefaultValue = ArrayHelper::get($data, 'settings.dynamic_default_value')) { |
| 80 |
$parseValue = $this->parseEditorSmartCode($dynamicDefaultValue, $form); |
| 81 |
if ($parseValue) { |
| 82 |
$defaultValues = explode(',', $parseValue); |
| 83 |
$defaultValues = array_map('trim', $defaultValues); |
| 84 |
} |
| 85 |
} |
| 86 |
return $defaultValues; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Determine if the given element has conditions bound |
| 91 |
* |
| 92 |
* @param array $element [Html element being compiled] |
| 93 |
* |
| 94 |
* @return boolean |
| 95 |
*/ |
| 96 |
protected function hasConditions($element) |
| 97 |
{ |
| 98 |
$conditionals = ArrayHelper::get($element, 'settings.conditional_logics'); |
| 99 |
|
| 100 |
if (isset($conditionals['status']) && $conditionals['status']) { |
| 101 |
return array_filter($conditionals['conditions'], function ($item) { |
| 102 |
return $item['field'] && $item['operator']; |
| 103 |
}); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Generate a unique id for an element |
| 109 |
* |
| 110 |
* @param string $str [preix] |
| 111 |
* |
| 112 |
* @return string [Unique id] |
| 113 |
*/ |
| 114 |
protected function getUniqueId($str) |
| 115 |
{ |
| 116 |
return $str . '_' . md5(uniqid(mt_rand(), true)); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get a default class for each form element wrapper |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
protected function getDefaultContainerClass() |
| 125 |
{ |
| 126 |
return 'ff-el-group '; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get required class for form element wrapper |
| 131 |
* |
| 132 |
* @param array $rules [Validation rules] |
| 133 |
* |
| 134 |
* @return mixed |
| 135 |
*/ |
| 136 |
protected function getRequiredClass($rules) |
| 137 |
{ |
| 138 |
if (isset($rules['required'])) { |
| 139 |
return $rules['required']['value'] ? 'ff-el-is-required ' : ''; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get asterisk placement for the required form elements |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
protected function getAsteriskPlacement($form) |
| 149 |
{ |
| 150 |
// for older version compatibility |
| 151 |
$asteriskPlacement = 'asterisk-right'; |
| 152 |
|
| 153 |
if (isset($form->settings['layout']['asteriskPlacement'])) { |
| 154 |
$asteriskPlacement = $form->settings['layout']['asteriskPlacement']; |
| 155 |
} |
| 156 |
|
| 157 |
return $asteriskPlacement . ' '; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Generate a label for any element |
| 162 |
* |
| 163 |
* @param array $data |
| 164 |
* |
| 165 |
* @return string [label Html element] |
| 166 |
*/ |
| 167 |
protected function buildElementLabel($data, $form) |
| 168 |
{ |
| 169 |
$helpMessage = ''; |
| 170 |
if ('with_label' == $form->settings['layout']['helpMessagePlacement']) { |
| 171 |
$helpMessage = $this->getLabelHelpMessage($data); |
| 172 |
} |
| 173 |
|
| 174 |
$id = isset($data['attributes']['id']) ? $data['attributes']['id'] : ''; |
| 175 |
$label = isset($data['settings']['label']) ? $data['settings']['label'] : ''; |
| 176 |
$requiredClass = $this->getRequiredClass(ArrayHelper::get($data, 'settings.validation_rules', [])); |
| 177 |
$classes = trim('ff-el-input--label ' . $requiredClass . $this->getAsteriskPlacement($form)); |
| 178 |
|
| 179 |
return "<div class='" . esc_attr($classes) . "'><label for='" . esc_attr($id) . "'>" . fluentform_sanitize_html($label, false) . '</label>' . $helpMessage . '</div>'; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Generate html/markup for any element |
| 184 |
* |
| 185 |
* @param string $elMarkup [Predifined partial markup] |
| 186 |
* @param array $data |
| 187 |
* @param \stdClass $form [Form object] |
| 188 |
* |
| 189 |
* @return string [Compiled markup] |
| 190 |
*/ |
| 191 |
protected function buildElementMarkup($elMarkup, $data, $form) |
| 192 |
{ |
| 193 |
$hasConditions = $this->hasConditions($data) ? 'has-conditions ' : ''; |
| 194 |
|
| 195 |
$labelPlacement = ArrayHelper::get($data, 'settings.label_placement'); |
| 196 |
|
| 197 |
$labelPlacementClass = $labelPlacement ? 'ff-el-form-' . $labelPlacement . ' ' : ''; |
| 198 |
|
| 199 |
$validationRules = ArrayHelper::get($data, 'settings.validation_rules'); |
| 200 |
|
| 201 |
$labelClass = trim( |
| 202 |
'ff-el-input--label ' . |
| 203 |
$this->getRequiredClass($validationRules) . |
| 204 |
$this->getAsteriskPlacement($form) |
| 205 |
); |
| 206 |
|
| 207 |
$formGroupClass = trim( |
| 208 |
$this->getDefaultContainerClass() . |
| 209 |
$labelPlacementClass . |
| 210 |
$hasConditions . |
| 211 |
ArrayHelper::get($data, 'settings.container_class') |
| 212 |
); |
| 213 |
|
| 214 |
$labelHelpText = $inputHelpText = ''; |
| 215 |
|
| 216 |
$labelPlacement = $form->settings['layout']['helpMessagePlacement']; |
| 217 |
if ('with_label' == $labelPlacement) { |
| 218 |
$labelHelpText = $this->getLabelHelpMessage($data); |
| 219 |
} elseif ('on_focus' == $labelPlacement) { |
| 220 |
$inputHelpText = $this->getInputHelpMessage($data, 'ff-hidden'); |
| 221 |
} elseif ('after_label' == $labelPlacement) { |
| 222 |
$inputHelpText = $this->getInputHelpMessage($data, 'ff_ahm'); |
| 223 |
} else { |
| 224 |
$inputHelpText = $this->getInputHelpMessage($data); |
| 225 |
} |
| 226 |
|
| 227 |
$forStr = ''; |
| 228 |
if (isset($data['attributes']['id'])) { |
| 229 |
$forStr = "for='{$data['attributes']['id']}'"; |
| 230 |
} |
| 231 |
|
| 232 |
$labelMarkup = ''; |
| 233 |
|
| 234 |
if (! empty($data['settings']['label'])) { |
| 235 |
$label = ArrayHelper::get($data, 'settings.label'); |
| 236 |
|
| 237 |
$labelMarkup = sprintf( |
| 238 |
"<div class='%s'><label %s>%s</label> %s</div>", |
| 239 |
esc_attr($labelClass), |
| 240 |
esc_attr($forStr), |
| 241 |
fluentform_sanitize_html($label, false), |
| 242 |
fluentform_sanitize_html($labelHelpText, false) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
$inputHelpText = fluentform_sanitize_html($inputHelpText, false); |
| 247 |
|
| 248 |
if ('after_label' == $labelPlacement) { |
| 249 |
$elMarkup = $inputHelpText . $elMarkup; |
| 250 |
$inputHelpText = ''; |
| 251 |
} |
| 252 |
|
| 253 |
return sprintf( |
| 254 |
"<div class='%s'>%s<div class='ff-el-input--content'>%s%s</div></div>", |
| 255 |
esc_attr($formGroupClass), |
| 256 |
$labelMarkup, |
| 257 |
$elMarkup, |
| 258 |
$inputHelpText |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Generate a help message for any element beside label |
| 264 |
* |
| 265 |
* @param array $data |
| 266 |
* |
| 267 |
* @return string [Html] |
| 268 |
*/ |
| 269 |
protected function getLabelHelpMessage($data) |
| 270 |
{ |
| 271 |
if (isset($data['settings']['help_message']) && '' != $data['settings']['help_message']) { |
| 272 |
$text = htmlspecialchars($data['settings']['help_message']); |
| 273 |
$icon = '<svg width="16" height="16" viewBox="0 0 25 25"><path d="m329 393l0-46c0-2-1-4-2-6-2-2-4-3-7-3l-27 0 0-146c0-3-1-5-3-7-2-1-4-2-7-2l-91 0c-3 0-5 1-7 2-1 2-2 4-2 7l0 46c0 2 1 5 2 6 2 2 4 3 7 3l27 0 0 91-27 0c-3 0-5 1-7 3-1 2-2 4-2 6l0 46c0 3 1 5 2 7 2 1 4 2 7 2l128 0c3 0 5-1 7-2 1-2 2-4 2-7z m-36-256l0-46c0-2-1-4-3-6-2-2-4-3-7-3l-54 0c-3 0-5 1-7 3-2 2-3 4-3 6l0 46c0 3 1 5 3 7 2 1 4 2 7 2l54 0c3 0 5-1 7-2 2-2 3-4 3-7z m182 119c0 40-9 77-29 110-20 34-46 60-80 80-33 20-70 29-110 29-40 0-77-9-110-29-34-20-60-46-80-80-20-33-29-70-29-110 0-40 9-77 29-110 20-34 46-60 80-80 33-20 70-29 110-29 40 0 77 9 110 29 34 20 60 46 80 80 20 33 29 70 29 110z" transform="scale(0.046875 0.046875)"></path></svg>'; |
| 274 |
return sprintf('<div class="ff-el-tooltip" data-content="%s">%s</div>', $text, $icon); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Generate a help message for any element beside form element |
| 280 |
* |
| 281 |
* @param array $data |
| 282 |
* |
| 283 |
* @return string [Html] |
| 284 |
*/ |
| 285 |
protected function getInputHelpMessage($data, $hideClass = '') |
| 286 |
{ |
| 287 |
$class = trim('ff-el-help-message ' . $hideClass); |
| 288 |
|
| 289 |
if (isset($data['settings']['help_message']) && ! empty($data['settings']['help_message'])) { |
| 290 |
return "<div class='" . esc_attr($class) . "'>" . fluentform_sanitize_html($data['settings']['help_message'], false) . '</div>'; |
| 291 |
} |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
protected function parseEditorSmartCode($text, $form) |
| 296 |
{ |
| 297 |
return (new Component($this->app))->replaceEditorSmartCodes($text, $form); |
| 298 |
} |
| 299 |
|
| 300 |
protected function printContent($hook, $html, $data, $form) |
| 301 |
{ |
| 302 |
echo apply_filters($hook, $html, $data, $form); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is escaped before being passed in. |
| 303 |
} |
| 304 |
} |
| 305 |
|