PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.3
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / Admin / Elementor / Form_Actions / Payment_Amount_Field.php

Payment_Amount_Field.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 1.0.3, at includes/Admin/Elementor/Form_Actions/Payment_Amount_Field.php

384 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Admin\Elementor\Form_Actions;
4
5 use Better_Payment\Lite\Admin\DB;
6 use Better_Payment\Lite\Classes\Handler;
7 use Elementor\Widget_Base;
8 use ElementorPro\Modules\Forms\Classes;
9 use Elementor\Controls_Manager;
10 use ElementorPro\Modules\Forms\Fields\Field_Base;
11 use ElementorPro\Plugin;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Payment Amount field class
19 *
20 * @since 0.0.1
21 */
22 class Payment_Amount_Field extends Field_Base {
23
24 public function get_type() {
25 return 'payment_amount';
26 }
27
28 public function get_name() {
29 return __( 'Payment Amount', 'better-payment' );
30 }
31
32 public function render( $item, $item_index, $form ) {
33 $settings = $form->get_settings_for_display();
34
35 $show_input_field = $this->is_bp_input_field_enable( $settings, $item );
36 $show_amount_list = $this->is_bp_show_amount_list_enable( $settings, $item );
37
38 $item['custom_id'] = $this->get_type();
39
40 $form->add_render_attribute( 'input' . $item['custom_id'], [
41 'name' => $this->get_attribute_name( $item ),
42 'id' => $this->get_attribute_id( $item ),
43 ] );
44
45 if ( $item['bp_placeholder'] ) {
46 $form->add_render_attribute( 'input' . $item['custom_id'], 'placeholder', $item['bp_placeholder'] );
47 }
48
49 $form->add_render_attribute( 'input' . $item['custom_id'], 'required', 'required' );
50 $form->add_render_attribute( 'input' . $item['custom_id'], 'aria-required', 'true' );
51
52 if ( isset( $item['bp_field_min'] ) ) {
53 $form->add_render_attribute( 'input' . $item['custom_id'], 'min', esc_attr( $item['bp_field_min'] ) );
54 }
55
56 if ( isset( $item['bp_field_max'] ) ) {
57 $form->add_render_attribute( 'input' . $item['custom_id'], 'max', esc_attr( $item['bp_field_max'] ) );
58 }
59
60 if ( isset( $item['bp_field_default'] ) ) {
61 $form->add_render_attribute( 'input' . $item['custom_id'], 'value', esc_attr( $item['bp_field_default'] ) );
62 }
63
64 $better_payment_amount_readonly = ! empty( $item['bp_field_default_fixed'] ) ? 'readonly' : '';
65 $better_payment_form_currency = $this->get_better_payment_form_currency($form);
66 $better_payment_form_currency_symbol = Handler::get_currency_symbols( $better_payment_form_currency );
67
68 $bp_payment_amount_input = '<input type="number" class="elementor-field elementor-size-sm elementor-field-textual bp-elementor-field-textual-amount" ' . $form->get_render_attribute_string( 'input' . $item['custom_id'] ) . '>';
69
70 $bp_payment_amount_html = "<div class='bp-input-group'>
71 <div class='bp-input-group-prepend'>
72 <div class='bp-input-group-text' title='$better_payment_form_currency'>$better_payment_form_currency_symbol</div>
73 </div>
74 $bp_payment_amount_input
75 </div>";
76
77 $bp_payment_amount_html_v2 = "<div class='better-payment is-full-width'>";
78
79 if( $show_amount_list ){
80 $bp_payment_amount_html_v2 .= $this->render_amount_element( $settings );
81 }
82
83 if( $show_input_field ){
84 $bp_payment_amount_html_v2 .= "
85 <div class='better-payment-field-advanced-layout field-primary_payment_amount elementor-repeater-item-".$this->get_attribute_id( $item )."'>
86 <div class='control has-icons-left'>
87 <input class='input is-medium required bp-custom-payment-amount bp-custom-payment-amount-el-integration' type='number' placeholder='" . esc_attr( $item['bp_placeholder'] ) . "' name='" . esc_attr( $this->get_attribute_name( $item ) ) . "' required='required' min='" . esc_attr( $item['bp_field_min'] ) . "' max='" . esc_attr( $item['bp_field_max'] ) . "' value='" . esc_attr( $item['bp_field_default'] ) . "' " . esc_attr( $better_payment_amount_readonly ) . " >
88 <span class='icon is-medium is-left'>
89 <span class='bp-currency-symbol'>" . esc_html( $better_payment_form_currency_symbol ) . "</span>
90 </span>
91 </div>
92 </div>
93 ";
94 }
95
96 $bp_payment_amount_html_v2 .= "</div>";
97
98
99 $allowed_html = array(
100 'a' => array(
101 'id' => array(),
102 'class' => array(),
103 'href' => array(),
104 'title' => array(),
105 ),
106 'br' => array(),
107 'em' => array(),
108 'strong' => array(),
109 'div' => array(
110 'class' => array(),
111 'id' => array(),
112 'title' => array(),
113 ),
114 'input' => array(
115 'id' => array(),
116 'class' => array(),
117 'name' => array(),
118 'type' => array(),
119 'placeholder' => array(),
120 'aria-required' => array(),
121 'required' => array(),
122 'min' => array(),
123 'max' => array(),
124 'value' => array(),
125 'readonly' => array(),
126 ),
127 'span' => array(
128 'class' => array(),
129 'id' => array(),
130 'title' => array(),
131 ),
132 'label' => array(
133 'for' => array(),
134 ),
135
136 );
137
138 echo wp_kses($bp_payment_amount_html_v2, $allowed_html) ;
139 }
140
141 /**
142 * Check if payment amount field is enabled
143 *
144 * @since 1.0.0
145 */
146 public function is_bp_fields_enable( $settings ){
147 return ! empty( $settings['better_payment_payment_amount_enable'] ) && 'yes' === $settings['better_payment_payment_amount_enable'];
148 }
149
150 /**
151 * Check item field type (input field / amount list / both)
152 *
153 * @since 1.0.0
154 */
155 public function is_bp_input_field_enable( $settings, $item ){
156 if( ! $this->is_bp_fields_enable( $settings ) ){
157 return false;
158 }
159
160 return ! empty( $item['bp_field_type'] ) && ( 'input-field' === $item['bp_field_type'] || 'both' === $item['bp_field_type'] );
161 }
162
163 /**
164 * Check item field type (input field / amount list / both)
165 *
166 * @since 1.0.0
167 */
168 public function is_bp_show_amount_list_enable( $settings, $item ){
169 if( ! $this->is_bp_fields_enable( $settings ) ){
170 return false;
171 }
172
173 $is_show_amount_list_enable = ! empty( $settings['better_payment_show_amount_list_enable'] ) && 'yes' === $settings['better_payment_show_amount_list_enable'];
174
175 return $is_show_amount_list_enable && ( ! empty( $item['bp_field_type'] ) ) && ( 'amount-list' === $item['bp_field_type'] || 'both' === $item['bp_field_type'] );
176 }
177
178 /**
179 * Render amount element
180 *
181 * @since 1.0.0
182 */
183 public function render_amount_element( $settings ) {
184 if( empty($settings[ 'better_payment_show_amount_list_items' ] ) ){
185 return;
186 }
187
188 $global_settings = DB::get_settings();
189 $currency = $global_settings['better_payment_settings_general_general_currency'];
190 $currency_alignment = 'left';
191
192 if ( in_array( 'paypal', $settings['submit_actions'] ) ) {
193 $currency = ! empty( $settings['better_payment_form_paypal_currency'] ) ? $settings['better_payment_form_paypal_currency'] : $currency;
194 $currency_alignment = ! empty( $settings['better_payment_form_currency_alignment_paypal'] ) ? $settings['better_payment_form_currency_alignment_paypal'] : $currency_alignment;
195 } elseif ( in_array( 'stripe', $settings['submit_actions'] ) ) {
196 $currency = ! empty( $settings['better_payment_form_stripe_currency'] ) ? $settings['better_payment_form_stripe_currency'] : $currency;
197 $currency_alignment = ! empty( $settings['better_payment_form_currency_alignment_stripe'] ) ? $settings['better_payment_form_currency_alignment_stripe'] : $currency_alignment;
198 }
199
200 $currency_symbol = Handler::get_currency_symbols( esc_html( $currency ) );
201 $currency_symbol_left = 'left' === $currency_alignment ? $currency_symbol : '';
202 $currency_symbol_right = 'right' === $currency_alignment ? $currency_symbol : '' ;
203
204 ob_start();
205 ?>
206 <div class="payment-form-layout">
207 <div class="bp-payment-amount-wrap">
208 <?php
209 foreach ( $settings[ 'better_payment_show_amount_list_items' ] as $item ) {
210 $uid = uniqid();
211
212 ?>
213 <div class="bp-form__group pb-3">
214 <input type="radio" value="<?php echo floatval( $item[ 'better_payment_amount_val' ] ); ?>"
215 id="bp_payment_amount-<?php echo esc_attr($uid); ?>" class="bp-form__control bp-form_pay-radio "
216 name="form_fields[primary_payment_amount_radio]">
217 <label for="bp_payment_amount-<?php echo esc_attr($uid); ?>"><?php printf( "%s%s%s", esc_html( $currency_symbol_left ), floatval( $item['better_payment_amount_val'] ), esc_html( $currency_symbol_right ) ); ?></label>
218 </div>
219 <?php
220 }
221 ?>
222 </div>
223 </div>
224 <?php
225 return ob_get_clean();
226 }
227
228 /**
229 * @param Widget_Base $widget
230 */
231 public function update_controls( $widget ) {
232 $elementor = Plugin::elementor();
233
234 $control_data = $elementor->controls_manager->get_control_from_stack( $widget->get_unique_name(), 'form_fields' );
235
236 if ( is_wp_error( $control_data ) ) {
237 return;
238 }
239
240 $field_controls = [
241 'bp_field_type' => [
242 'name' => 'bp_field_type',
243 'label' => __( 'Field Type', 'better-payment' ),
244 'type' => Controls_Manager::SELECT,
245 'options' => [
246 'both' => esc_html__( 'Both', 'better-payment' ),
247 'input-field' => esc_html__( 'Input Field', 'better-payment' ),
248 'amount-list' => esc_html__( 'Amount List', 'better-payment' ),
249 ],
250 'default' => 'both',
251 'condition' => [
252 'field_type' => $this->get_type(),
253 ],
254 'tab' => 'content',
255 'inner_tab' => 'form_fields_content_tab',
256 'tabs_wrapper' => 'form_fields_tabs',
257 'separator' => 'before',
258 ],
259 'bp_payment_field_usage_notice' => [
260 'name' => 'bp_payment_field_usage_notice',
261 'raw' => __( 'Don\'t forget to enable the <strong>Payment Amount</strong> (& Show Amount List) field from Better Payment Section below', 'better-payment' ),
262 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
263 'type' => Controls_Manager::RAW_HTML,
264 'condition' => [
265 'field_type' => $this->get_type(),
266 ],
267 'tab' => 'content',
268 'inner_tab' => 'form_fields_content_tab',
269 'tabs_wrapper' => 'form_fields_tabs',
270 ],
271 'bp_placeholder' => [
272 'name' => 'bp_placeholder',
273 'label' => __( 'Placeholder', 'better-payment' ),
274 'type' => Controls_Manager::TEXT,
275 'condition' => [
276 'field_type' => $this->get_type(),
277 ],
278 'tab' => 'content',
279 'inner_tab' => 'form_fields_content_tab',
280 'tabs_wrapper' => 'form_fields_tabs',
281 ],
282 'bp_field_min' => [
283 'name' => 'bp_field_min',
284 'label' => __( 'Min. Value', 'better-payment' ),
285 'type' => Controls_Manager::NUMBER,
286 'condition' => [
287 'field_type' => $this->get_type(),
288 ],
289 'tab' => 'content',
290 'inner_tab' => 'form_fields_content_tab',
291 'tabs_wrapper' => 'form_fields_tabs',
292 ],
293 'bp_field_max' => [
294 'name' => 'bp_field_max',
295 'label' => __( 'Max. Value', 'better-payment' ),
296 'type' => Controls_Manager::NUMBER,
297 'condition' => [
298 'field_type' => $this->get_type(),
299 ],
300 'tab' => 'content',
301 'inner_tab' => 'form_fields_content_tab',
302 'tabs_wrapper' => 'form_fields_tabs',
303 ],
304 'bp_field_default' => [
305 'name' => 'bp_field_default',
306 'label' => __( 'Default Value', 'better-payment' ),
307 'type' => Controls_Manager::NUMBER,
308 'condition' => [
309 'field_type' => $this->get_type(),
310 ],
311 'tab' => 'content',
312 'inner_tab' => 'form_fields_content_tab',
313 'tabs_wrapper' => 'form_fields_tabs',
314 ],
315 'bp_field_default_fixed' => [
316 'name' => 'bp_field_default_fixed',
317 'label' => __( 'Readonly', 'better-payment' ),
318 'type' => Controls_Manager::SWITCHER,
319 'condition' => [
320 'field_type' => $this->get_type(),
321 ],
322 'tab' => 'content',
323 'inner_tab' => 'form_fields_content_tab',
324 'tabs_wrapper' => 'form_fields_tabs',
325 ],
326 ];
327
328 foreach ( $control_data['fields'] as $index => $field ) {
329 if ( 'required' === $field['name'] ) {
330 $control_data['fields'][ $index ]['conditions']['terms'][] = [
331 'name' => 'field_type',
332 'operator' => '!in',
333 'value' => [
334 'payment_amount',
335 ],
336 ];
337 }
338 }
339
340 $control_data['fields'] = $this->inject_field_controls( $control_data['fields'], $field_controls );
341
342 $widget->update_control( 'form_fields', $control_data );
343 }
344
345 public function validation( $field, Classes\Form_Record $record, Classes\Ajax_Handler $ajax_handler ) {
346
347 if ( ! empty( $field['bp_field_max'] ) && $field['bp_field_max'] < (int) $field['value'] ) {
348 $ajax_handler->add_error( $field['id'], sprintf( __( 'The value must be less than or equal to %s', 'better-payment' ), esc_html( $field['bp_field_max'] ) ) );
349 }
350
351 if ( ! empty( $field['bp_field_min'] ) && $field['bp_field_min'] > (int) $field['value'] ) {
352 $ajax_handler->add_error( $field['id'], sprintf( __( 'The value must be greater than or equal %s', 'better-payment' ), esc_html( $field['bp_field_min'] ) ) );
353 }
354 }
355
356 public function sanitize_field( $value, $field ) {
357 return intval( $value );
358 }
359
360 public function get_attribute_name( $item ) {
361 return "form_fields[{$item['custom_id']}]";
362 }
363
364 public function get_attribute_id( $item ) {
365 return 'form-field-' . $item['custom_id'];
366 }
367
368 public function get_better_payment_form_currency($form){
369 $instance = $form->get_settings_for_display();
370 $submit_actions = $instance['submit_actions'];
371
372 $better_payment_global_currency = DB::get_settings('better_payment_settings_general_general_currency'); //USD
373 $better_payment_global_currency = !empty($better_payment_global_currency) ? esc_html( $better_payment_global_currency ) : 'USD';
374
375 if(in_array('paypal', $submit_actions)){
376 $better_payment_form_currency = isset($instance['better_payment_form_paypal_currency']) ? esc_html($instance['better_payment_form_paypal_currency']) : esc_html($better_payment_global_currency);
377 }else {
378 $better_payment_form_currency = isset($instance['better_payment_form_stripe_currency']) ? esc_html($instance['better_payment_form_stripe_currency']) : esc_html($better_payment_global_currency);
379 }
380
381 return $better_payment_form_currency;
382 }
383 }
384