| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\Components; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\App\Helpers\Helper; |
| 10 |
use FluentForm\App\Services\FormBuilder\BaseFieldManager; |
| 11 |
use FluentForm\App\Services\FormBuilder\Components\Text; |
| 12 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 13 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 14 |
|
| 15 |
class MultiPaymentComponent extends BaseFieldManager |
| 16 |
{ |
| 17 |
public function __construct( |
| 18 |
$key = 'multi_payment_component', |
| 19 |
$title = 'Multi Payment Item', |
| 20 |
$tags = ['custom', 'payment', 'donation'], |
| 21 |
$position = 'payments' |
| 22 |
) |
| 23 |
{ |
| 24 |
parent::__construct( |
| 25 |
$key, |
| 26 |
$title, |
| 27 |
$tags, |
| 28 |
$position |
| 29 |
); |
| 30 |
|
| 31 |
add_filter('fluentform/editor_init_element_multi_payment_component', function ($element) { |
| 32 |
if(!isset($element['settings']['layout_class'])) { |
| 33 |
$element['settings']['layout_class'] = ''; |
| 34 |
} |
| 35 |
return $element; |
| 36 |
}); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
function getComponent() |
| 41 |
{ |
| 42 |
return array( |
| 43 |
'index' => 8, |
| 44 |
'element' => $this->key, |
| 45 |
'attributes' => array( |
| 46 |
'type' => 'single', // single|radio|checkbox|select |
| 47 |
'name' => 'payment_input', |
| 48 |
'value' => '10', |
| 49 |
), |
| 50 |
'settings' => array( |
| 51 |
'container_class' => '', |
| 52 |
'label' => __('Payment Item', 'fluentform'), |
| 53 |
'admin_field_label' => '', |
| 54 |
'label_placement' => '', |
| 55 |
'display_type' => '', |
| 56 |
'help_message' => '', |
| 57 |
'is_payment_field' => 'yes', |
| 58 |
'pricing_options' => array( |
| 59 |
[ |
| 60 |
'label' => 'Payment Item 1', |
| 61 |
'value' => 10, |
| 62 |
'image' => '' |
| 63 |
] |
| 64 |
), |
| 65 |
'dynamic_default_value' => '', |
| 66 |
'price_label' => __('Price:', 'fluentform'), |
| 67 |
'enable_quantity' => false, |
| 68 |
'enable_image_input' => false, |
| 69 |
'is_element_lock' => false, |
| 70 |
'layout_class' => '', |
| 71 |
'validation_rules' => array( |
| 72 |
'required' => array( |
| 73 |
'value' => false, |
| 74 |
'global' => true, |
| 75 |
'message' => Helper::getGlobalDefaultMessage('required'), |
| 76 |
'global_message' => Helper::getGlobalDefaultMessage('required'), |
| 77 |
), |
| 78 |
), |
| 79 |
'conditional_logics' => array(), |
| 80 |
), |
| 81 |
'editor_options' => array( |
| 82 |
'title' => __('Payment Item', 'fluentform'), |
| 83 |
'icon_class' => 'ff-edit-shopping-cart', |
| 84 |
'element' => 'input-radio', |
| 85 |
'template' => 'inputMultiPayment' |
| 86 |
) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
public function getGeneralEditorElements() |
| 91 |
{ |
| 92 |
return [ |
| 93 |
'label', |
| 94 |
'label_placement', |
| 95 |
'admin_field_label', |
| 96 |
'placeholder', |
| 97 |
'pricing_options', |
| 98 |
'validation_rules', |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
public function getAdvancedEditorElements() |
| 103 |
{ |
| 104 |
return [ |
| 105 |
'container_class', |
| 106 |
'help_message', |
| 107 |
'name', |
| 108 |
'layout_class', |
| 109 |
'dynamic_default_value', |
| 110 |
'conditional_logics' |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
function render($data, $form) |
| 115 |
{ |
| 116 |
$type = ArrayHelper::get($data, 'attributes.type'); |
| 117 |
|
| 118 |
if(!$this->app){ |
| 119 |
$this->app = wpFluentForm(); |
| 120 |
} |
| 121 |
if ($type == 'single') { |
| 122 |
$this->renderSingleItem($data, $form); |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
$this->renderMultiProduct($data, $form); |
| 127 |
} |
| 128 |
|
| 129 |
public function renderSingleItem($data, $form) |
| 130 |
{ |
| 131 |
$elementName = $data['element']; |
| 132 |
$label = ArrayHelper::get($data, 'settings.label'); |
| 133 |
$data = apply_filters_deprecated( |
| 134 |
'fluentform_rendering_field_data_' . $elementName, |
| 135 |
[ |
| 136 |
$data, |
| 137 |
$form |
| 138 |
], |
| 139 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 140 |
'fluentform/rendering_field_data_' . $elementName, |
| 141 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 142 |
); |
| 143 |
|
| 144 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 145 |
|
| 146 |
$priceLabel = ArrayHelper::get($data, 'settings.price_label'); |
| 147 |
$productPrice = ArrayHelper::get($data, 'attributes.value'); |
| 148 |
|
| 149 |
if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 150 |
$productPrice = ArrayHelper::get($dynamicValues, '0'); |
| 151 |
} |
| 152 |
if (!$productPrice || !is_numeric($productPrice)) { |
| 153 |
$productPrice = apply_filters('fluentform/single_payment_item_fallback_value', 0, $data, $form); |
| 154 |
} |
| 155 |
$inputAttributes = [ |
| 156 |
'type' => 'hidden', |
| 157 |
'id' => $this->makeElementId($data, $form), |
| 158 |
'name' => $data['attributes']['name'], |
| 159 |
'value' => $productPrice, |
| 160 |
'class' => 'ff_payment_item', |
| 161 |
'data-payment_item' => 'yes', |
| 162 |
'data-payment_value' => $productPrice, |
| 163 |
'data-payment_label' => $label, |
| 164 |
'data-quantity_remaining' => ArrayHelper::get($data, 'settings.quantity_remaining', false) |
| 165 |
]; |
| 166 |
$money = PaymentHelper::formatMoney( |
| 167 |
$productPrice * 100, PaymentHelper::getFormCurrency($form->id) |
| 168 |
); |
| 169 |
|
| 170 |
$elMarkup = $input = "<input " . $this->buildAttributes($inputAttributes, $form) . ">"; |
| 171 |
|
| 172 |
$elMarkup .= '<span class="ff_item_price_wrapper"><span class="ff_product_price_label">' . $priceLabel . '</span>'; |
| 173 |
$elMarkup .= ' <span class="ff_product_price">' . $money . '</span></span>'; |
| 174 |
|
| 175 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 176 |
|
| 177 |
$html = apply_filters_deprecated( |
| 178 |
'fluentform_rendering_field_html_' . $elementName, |
| 179 |
[ |
| 180 |
$html, |
| 181 |
$data, |
| 182 |
$form |
| 183 |
], |
| 184 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 185 |
'fluentform/rendering_field_html_' . $elementName, |
| 186 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 187 |
); |
| 188 |
|
| 189 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Content is escaped by the filter |
| 190 |
echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 191 |
} |
| 192 |
|
| 193 |
public function renderMultiProduct($data, $form) |
| 194 |
{ |
| 195 |
$elementName = $data['element']; |
| 196 |
|
| 197 |
$data = apply_filters_deprecated( |
| 198 |
'fluentform_rendering_field_data_' . $elementName, |
| 199 |
[ |
| 200 |
$data, |
| 201 |
$form |
| 202 |
], |
| 203 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 204 |
'fluentform/rendering_field_data_' . $elementName, |
| 205 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 206 |
); |
| 207 |
|
| 208 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 209 |
|
| 210 |
$data['attributes']['class'] = trim( |
| 211 |
'ff-el-form-check-input ' . |
| 212 |
'ff-el-form-check-'.$data['attributes']['type'].' '. |
| 213 |
ArrayHelper::get($data, 'attributes.class') |
| 214 |
); |
| 215 |
|
| 216 |
if ($data['attributes']['type'] == 'checkbox') { |
| 217 |
$data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 218 |
} |
| 219 |
|
| 220 |
$defaultValues = (array)$this->extractValueFromAttributes($data); |
| 221 |
if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 222 |
$defaultValues = $dynamicValues; |
| 223 |
} |
| 224 |
|
| 225 |
$elMarkup = ''; |
| 226 |
|
| 227 |
$firstTabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex(); |
| 228 |
|
| 229 |
$formattedOptions = ArrayHelper::get($data, 'settings.pricing_options'); |
| 230 |
|
| 231 |
$formattedOptions = apply_filters_deprecated( |
| 232 |
'fluentform_payment_field_' . $elementName . '_pricing_options', |
| 233 |
[ |
| 234 |
$formattedOptions, |
| 235 |
$data, |
| 236 |
$form |
| 237 |
], |
| 238 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 239 |
'fluentform/payment_field_' . $elementName . '_pricing_options', |
| 240 |
'Use fluentform/payment_field_' . $elementName . '_pricing_options instead of fluentform_payment_field_' . $elementName . '_pricing_options' |
| 241 |
); |
| 242 |
|
| 243 |
$formattedOptions = apply_filters('fluentform/payment_field_' . $elementName . '_pricing_options', $formattedOptions, $data, $form); |
| 244 |
|
| 245 |
$hasImageOption = ArrayHelper::get($data, 'settings.enable_image_input'); |
| 246 |
|
| 247 |
$type = ArrayHelper::get($data, 'attributes.type'); |
| 248 |
|
| 249 |
|
| 250 |
if ($type == 'select') { |
| 251 |
$selectAtts = [ |
| 252 |
'name' => $data['attributes']['name'], |
| 253 |
'class' => 'ff-el-form-control ff_payment_item', |
| 254 |
'data-payment_item' => 'yes', |
| 255 |
'data-name' => $data['attributes']['name'], |
| 256 |
'id' => $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name'])) |
| 257 |
]; |
| 258 |
|
| 259 |
if ($firstTabIndex) { |
| 260 |
$selectAtts['tabindex'] = $firstTabIndex; |
| 261 |
} |
| 262 |
|
| 263 |
$elMarkup .= '<select ' . $this->buildAttributes($selectAtts, $form) . '>'; |
| 264 |
|
| 265 |
if($placeholder = ArrayHelper::get($data, 'settings.placeholder')) { |
| 266 |
$optionAtts = ''; |
| 267 |
foreach ([ |
| 268 |
'value' => "", |
| 269 |
'data-payment_value' => '', |
| 270 |
'data-calc_value' => '' |
| 271 |
] as $key => $value) { |
| 272 |
$optionAtts .= $key . '="' . $value . '" '; |
| 273 |
} |
| 274 |
$elMarkup .= '<option '.$optionAtts.'>'.$placeholder.'</option>'; |
| 275 |
} |
| 276 |
|
| 277 |
} else if ($hasImageOption) { |
| 278 |
$data['settings']['container_class'] .= ''; |
| 279 |
$elMarkup .= '<div class="ff_el_checkable_photo_holders">'; |
| 280 |
} |
| 281 |
$groupId = $this->makeElementId($data, $form); |
| 282 |
|
| 283 |
foreach ($formattedOptions as $index => $option) { |
| 284 |
$quantityLabel = ArrayHelper::get($option,'quantiy_label'); |
| 285 |
if ($type == 'select') { |
| 286 |
if (!$defaultValues && $index == 0) { |
| 287 |
$checked = true; |
| 288 |
} else { |
| 289 |
$checked = in_array($option['value'], $defaultValues); |
| 290 |
} |
| 291 |
$optionAtts = $this->buildAttributes([ |
| 292 |
'value' => $option['label'], |
| 293 |
'data-payment_value' => $option['value'], |
| 294 |
'data-calc_value' => $option['value'], |
| 295 |
'selected' => $checked, |
| 296 |
'disabled' => ArrayHelper::get($option, 'disabled') ? 'disabled' : '', |
| 297 |
'data-quantity_remaining' => ArrayHelper::get($option, 'quantity_remaining', false) |
| 298 |
], $form); |
| 299 |
$elMarkup .= '<option ' . $optionAtts . '>' . $option['label'] . $quantityLabel.'</option>'; |
| 300 |
continue; |
| 301 |
} |
| 302 |
|
| 303 |
$displayType = isset($data['settings']['display_type']) ? ' ff-el-form-check-' . $data['settings']['display_type'] : ''; |
| 304 |
$parentClass = "ff-el-form-check{$displayType}"; |
| 305 |
|
| 306 |
if (in_array($option['value'], $defaultValues)) { |
| 307 |
$data['attributes']['checked'] = true; |
| 308 |
$parentClass .= ' ff_item_selected'; |
| 309 |
} else { |
| 310 |
$data['attributes']['checked'] = false; |
| 311 |
} |
| 312 |
|
| 313 |
if ($firstTabIndex) { |
| 314 |
$data['attributes']['tabindex'] = $firstTabIndex; |
| 315 |
$firstTabIndex = '-1'; |
| 316 |
} |
| 317 |
|
| 318 |
$data['attributes']['class'] .= ' ff_payment_item'; |
| 319 |
|
| 320 |
$data['attributes']['value'] = $option['label']; |
| 321 |
$data['attributes']['data-payment_value'] = $option['value']; |
| 322 |
$data['attributes']['data-calc_value'] = ArrayHelper::get($option, 'value'); |
| 323 |
$data['attributes']['data-group_id'] = $groupId; |
| 324 |
$data['attributes']['disabled'] = ArrayHelper::get($option, 'disabled') ? 'disabled' : ''; |
| 325 |
$data['attributes']['data-quantity_remaining'] = ArrayHelper::get($option, 'quantity_remaining', false); |
| 326 |
|
| 327 |
|
| 328 |
$atts = $this->buildAttributes($data['attributes'], $form); |
| 329 |
$id = $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name'])); |
| 330 |
|
| 331 |
if ($hasImageOption && !empty($option['image'])) { |
| 332 |
$parentClass .= ' ff-el-image-holder'; |
| 333 |
} |
| 334 |
|
| 335 |
$elMarkup .= "<div class='{$parentClass}'>"; |
| 336 |
// Here we can push the visual items |
| 337 |
if ($hasImageOption && !empty($option['image'])) { |
| 338 |
$elMarkup .= "<label style='background-image: url({$option['image']})' class='ff-el-image-input-src' for={$id}></label>"; |
| 339 |
} |
| 340 |
|
| 341 |
$elMarkup .= "<label class='ff-el-form-check-label' for={$id}><input {$atts} id='{$id}'> <span class='ff_plan_title'>{$option['label']}{$quantityLabel}</span></label>"; |
| 342 |
$elMarkup .= "</div>"; |
| 343 |
} |
| 344 |
|
| 345 |
if ($type == 'select') { |
| 346 |
$elMarkup .= '</select>'; |
| 347 |
} else if ($hasImageOption) { |
| 348 |
$elMarkup .= '</div> '; |
| 349 |
} |
| 350 |
|
| 351 |
if($layoutClass = ArrayHelper::get($data, 'settings.layout_class')) { |
| 352 |
$data['settings']['container_class'] = $data['settings']['container_class'].' '.$layoutClass; |
| 353 |
} |
| 354 |
|
| 355 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 356 |
|
| 357 |
$html = apply_filters_deprecated( |
| 358 |
'fluentform_rendering_field_html_' . $elementName, |
| 359 |
[ |
| 360 |
$html, |
| 361 |
$data, |
| 362 |
$form |
| 363 |
], |
| 364 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 365 |
'fluentform/rendering_field_html_' . $elementName, |
| 366 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 367 |
); |
| 368 |
|
| 369 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Content is escaped by the filter |
| 370 |
echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 371 |
} |
| 372 |
} |
| 373 |
|