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