PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.14
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Payments / Components / PaymentMethods.php

PaymentMethods.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.14, at app/Modules/Payments/Components/PaymentMethods.php

388 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Framework\Helpers\ArrayHelper;
11 use FluentForm\App\Services\FormBuilder\BaseFieldManager;
12
13 class PaymentMethods extends BaseFieldManager
14 {
15
16 public function __construct()
17 {
18 parent::__construct(
19 'payment_method',
20 'Payment Method',
21 ['payment methods', 'payment', 'methods', 'stripe', 'paypal'],
22 'payments'
23 );
24
25 add_filter(
26 'fluentform/editor_init_element_payment_method',
27 [$this, 'recheckEditorComponent']
28 );
29
30 add_filter('fluentform/response_render_' . $this->key, function ($value) {
31 if ($value == 'test') {
32 return __('Offline', 'fluentform');
33 }
34 return $value;
35 });
36
37 }
38
39 function getComponent()
40 {
41 $available_methods = apply_filters_deprecated(
42 'fluentformpro_available_payment_methods',
43 [
44 []
45 ],
46 FLUENTFORM_FRAMEWORK_UPGRADE,
47 'fluentform/available_payment_methods',
48 'Use fluentform/available_payment_methods instead of fluentformpro_available_payment_methods.'
49 );
50 $available_methods = apply_filters('fluentform/available_payment_methods', $available_methods);
51
52 if (!$available_methods) {
53 return;
54 }
55
56 return [
57 'index' => 10,
58 'element' => $this->key,
59 'attributes' => [
60 'name' => $this->key,
61 'type' => 'radio',
62 'value' => ''
63 ],
64 'settings' => [
65 'container_class' => '',
66 'label' => $this->title,
67 'default_method' => '',
68 'label_placement' => '',
69 'help_message' => '',
70 'payment_methods' => $available_methods,
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 'admin_field_label' => '',
80 'conditional_logics' => []
81 ],
82 'editor_options' => [
83 'title' => $this->title,
84 'icon_class' => 'ff-edit-credit-card',
85 'template' => 'inputPaymentMethods'
86 ],
87 ];
88 }
89
90 public function recheckEditorComponent($component)
91 {
92 if (!isset($component['settings']['validation_rules'])) {
93 $component['settings']['validation_rules'] = array(
94 'required' => array(
95 'value' => false,
96 'global' => true,
97 'message' => Helper::getGlobalDefaultMessage('required'),
98 'global_message' => Helper::getGlobalDefaultMessage('required'),
99 ),
100 );
101 }
102 $available_methods = apply_filters_deprecated(
103 'fluentformpro_available_payment_methods',
104 [
105 []
106 ],
107 FLUENTFORM_FRAMEWORK_UPGRADE,
108 'fluentform/available_payment_methods',
109 'Use fluentform/available_payment_methods instead of fluentformpro_available_payment_methods.'
110 );
111
112 $available_methods = apply_filters('fluentform/available_payment_methods', $available_methods);
113
114 if (!$available_methods) {
115 $component['settings']['payment_methods'] = $available_methods;
116 return $component;
117 }
118
119 $existingMethods = ArrayHelper::get($component, 'settings.payment_methods', []);
120
121 $updatedMethods = [];
122
123 foreach ($available_methods as $methodName => $method) {
124 if (isset($existingMethods[$methodName])) {
125 $method['settings'] = wp_parse_args($existingMethods[$methodName]['settings'], $method['settings']);
126 $method['enabled'] = $existingMethods[$methodName]['enabled'];
127 } else {
128 $method['enabled'] = 'no';
129 }
130 $updatedMethods[$methodName] = $method;
131 }
132
133 $component['settings']['payment_methods'] = $updatedMethods;
134
135 return $component;
136 }
137
138 public function getEditorCustomizationSettings()
139 {
140 return [
141 'payment_methods' => array(
142 'template' => 'paymentMethodsConfig',
143 'label' => __('Payment Methods', 'fluentform'),
144 'help_text' => __('Please Select and Configure the payment methods for this form, At least 1 method is required. If you select only one it will not show on the form for selection but will process that payment with the selected method.', 'fluentform')
145 )
146 ];
147 }
148
149 public function getGeneralEditorElements()
150 {
151 return [
152 'label',
153 'admin_field_label',
154 'label_placement',
155 'payment_methods',
156 'value',
157 'validation_rules'
158 ];
159 }
160
161 function render($data, $form)
162 {
163 $elementName = $data['element'];
164
165 $data = apply_filters_deprecated(
166 'fluentform_rendering_field_data_' . $elementName,
167 [
168 $data,
169 $form
170 ],
171 FLUENTFORM_FRAMEWORK_UPGRADE,
172 'fluentform/rendering_field_data_' . $elementName,
173 'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName
174 );
175
176 $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form);
177
178 $data['attributes']['class'] = trim(
179 'ff-el-form-check-input ' .
180 ArrayHelper::get($data, 'attributes.class')
181 );
182
183 $paymentMethods = ArrayHelper::get($data, 'settings.payment_methods');
184
185 $available_methods = apply_filters_deprecated(
186 'fluentformpro_available_payment_methods',
187 [
188 []
189 ],
190 FLUENTFORM_FRAMEWORK_UPGRADE,
191 'fluentform/available_payment_methods',
192 'Use fluentform/available_payment_methods instead of fluentformpro_available_payment_methods.'
193 );
194
195 $available_methods = apply_filters('fluentform/available_payment_methods', $available_methods);
196 $activatedMethods = [];
197
198 foreach ($paymentMethods as $methodName => $paymentMethod) {
199 $enabled = ArrayHelper::get($paymentMethod, 'enabled');
200 if ($enabled == 'yes' && isset($available_methods[$methodName])) {
201 $activatedMethods[$methodName] = $paymentMethod;
202 }
203 }
204
205 if (!$activatedMethods) {
206 echo wp_sprintf(
207 '<p class="ff-error ff-payment-method-error">%s</p>',
208 esc_html__('No activated payment method found. If you are an admin please check the payment settings', 'fluentform')
209 );
210 return;
211 }
212
213 foreach ($activatedMethods as $methodName => $activatedMethod) {
214 do_action_deprecated(
215 'fluentform_rendering_payment_method_' . $methodName,
216 [
217 $activatedMethod,
218 $data,
219 $form
220 ],
221 FLUENTFORM_FRAMEWORK_UPGRADE,
222 'fluentform/rendering_payment_method_' . $methodName,
223 'Use fluentform/rendering_payment_method_' . $methodName . ' instead of fluentform_rendering_payment_method_' . $methodName
224 );
225 do_action('fluentform/rendering_payment_method_' . $methodName, $activatedMethod, $data, $form);
226 }
227
228
229 $data['attributes']['class'] = $data['attributes']['class'] . ' ff_payment_method';
230
231 if (count($activatedMethods) == 1) {
232 $methodKeys = array_keys($activatedMethods);
233 $methodName = $methodKeys[0];
234 $methodElement = $activatedMethods[$methodName];
235 $data['attributes']['type'] = 'hidden';
236 $data['attributes']['value'] = $methodName;
237 $data['attributes']['class'] .= ' ff_selected_payment_method';
238 $elMarkup = $html = "<input " . $this->buildAttributes($data['attributes'], $form) . ">";
239
240 $method = $activatedMethods[$methodName];
241 $method['is_default'] = true;
242 $selectedMarkups = apply_filters_deprecated(
243 'fluentform_payment_method_contents_' . $methodName,
244 [
245 '',
246 $method,
247 $data,
248 $form
249 ],
250 FLUENTFORM_FRAMEWORK_UPGRADE,
251 'fluentform/payment_method_contents_' . $methodName,
252 'Use fluentform/payment_method_contents_' . $methodName . ' instead of fluentform_payment_method_contents_' . $methodName
253 );
254 $selectedMarkups = apply_filters('fluentform/payment_method_contents_' . $methodName, $selectedMarkups, $method, $data, $form);
255
256 if ($selectedMarkups) {
257 $elMarkup .= $selectedMarkups;
258 $data['settings']['label'] = '';
259 $html = $this->buildElementMarkup($elMarkup, $data, $form);
260 }
261
262 $html = apply_filters_deprecated(
263 'fluentform_rendering_field_html_' . $elementName,
264 [
265 $html,
266 $data,
267 $form
268 ],
269 FLUENTFORM_FRAMEWORK_UPGRADE,
270 'fluentform/rendering_field_html_' . $elementName,
271 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName
272 );
273
274 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Content is escaped by the filter
275 echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
276
277 do_action_deprecated(
278 'fluentform_payment_method_render_' . $methodName,
279 [
280 $methodElement,
281 $form
282 ],
283 FLUENTFORM_FRAMEWORK_UPGRADE,
284 'fluentform/payment_method_render_' . $methodName,
285 'Use fluentform/payment_method_render_' . $methodName . ' instead of fluentform_payment_method_render_' . $methodName
286 );
287 do_action('fluentform/payment_method_render_' . $methodName, $methodElement, $form);
288 return;
289 }
290
291 $elMarkup = '';
292
293 $hasImageOption = false;
294
295 $defaultValue = $this->extractValueFromAttributes($data);
296
297 if ($defaultValue && !isset($activatedMethods[$defaultValue])) {
298 $defaultValue = key($activatedMethods);
299 }
300
301 if ($hasImageOption) {
302 $data['settings']['container_class'] .= '';
303 $elMarkup .= '<div class="ff_el_checkable_photo_holders">';
304 }
305
306 $firstTabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex();
307
308 $selectedMarkups = '';
309
310 foreach ($activatedMethods as $methodName => $method) {
311 $parentClass = "ff-el-form-check ff-el-payment-method-label";
312 if ($methodName == $defaultValue) {
313 $method['is_default'] = true;
314 $data['attributes']['checked'] = true;
315 $parentClass .= ' ff_item_selected';
316 } else {
317 $data['attributes']['checked'] = false;
318 $method['is_default'] = false;
319 }
320
321 if ($firstTabIndex) {
322 $data['attributes']['tabindex'] = $firstTabIndex;
323 $firstTabIndex = '-1';
324 }
325
326 $data['attributes']['value'] = $methodName;
327 $data['attributes']['type'] = 'radio';
328
329 $methodLabel = ArrayHelper::get($method, 'settings.option_label.value');
330
331 if ($methodLabel) {
332 $method['title'] = $methodLabel;
333 }
334
335 $atts = $this->buildAttributes($data['attributes']);
336
337 $id = $this->getUniqueid(str_replace(['[', ']'], ['', ''], $data['attributes']['name']));
338
339 if ($hasImageOption) {
340 $parentClass .= ' ff-el-image-holder';
341 }
342
343 $elMarkup .= "<div class='{$parentClass}'>";
344
345 $elMarkup .= "<label class='ff-el-form-check-label' for={$id}><input {$atts} id='{$id}'> <span>{$method['title']}</span></label>";
346 $elMarkup .= "</div>";
347
348 $selectedMarkups .= apply_filters_deprecated(
349 'fluentform_payment_method_contents_' . $methodName,
350 [
351 '',
352 $method,
353 $data,
354 $form
355 ],
356 FLUENTFORM_FRAMEWORK_UPGRADE,
357 'fluentform/payment_method_contents_' . $methodName,
358 'Use fluentform/payment_method_contents_' . $methodName . ' instead of fluentform_payment_method_contents_' . $methodName
359 );
360
361 $selectedMarkups = apply_filters('fluentform/payment_method_contents_' . $methodName, $selectedMarkups, $method, $data, $form);
362 }
363
364 if ($hasImageOption) {
365 $elMarkup .= '</div>';
366 }
367
368 $elMarkup .= $selectedMarkups;
369
370 $html = $this->buildElementMarkup($elMarkup, $data, $form);
371
372 $html = apply_filters_deprecated(
373 'fluentform_rendering_field_html_' . $elementName,
374 [
375 $html,
376 $data,
377 $form
378 ],
379 FLUENTFORM_FRAMEWORK_UPGRADE,
380 'fluentform/rendering_field_html_' . $elementName,
381 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName
382 );
383
384 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Content is escaped by the filter
385 echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
386 }
387 }
388