account.php
5 days ago
header.php
5 days ago
preferences.php
5 days ago
reset-password-modal.php
5 days ago
security.php
5 days ago
social-accounts.php
5 days ago
withdraw.php
5 days ago
withdraw.php
187 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Withdrawal Method Settings |
| 4 | * |
| 5 | * @package Tutor\Templates |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | use Tutor\Models\WithdrawModel; |
| 14 | use Tutor\Components\EmptyState; |
| 15 | use Tutor\Components\InputField; |
| 16 | use Tutor\Components\Constants\InputType; |
| 17 | |
| 18 | $get_field_key = fn( $method_id, $field_name ) => $method_id . '_' . $field_name; |
| 19 | |
| 20 | $map_field_type = function ( $field_type ) { |
| 21 | $types = array( |
| 22 | 'email' => InputType::EMAIL, |
| 23 | 'number' => InputType::NUMBER, |
| 24 | 'textarea' => InputType::TEXTAREA, |
| 25 | ); |
| 26 | return isset( $types[ $field_type ] ) ? $types[ $field_type ] : InputType::TEXT; |
| 27 | }; |
| 28 | |
| 29 | $get_saved_method_values = function ( $method_id, $user_id ) { |
| 30 | $meta_key = '_tutor_withdraw_method_data_' . $method_id; |
| 31 | $values = get_user_meta( $user_id, $meta_key, true ); |
| 32 | $values = maybe_unserialize( $values ); |
| 33 | return is_array( $values ) ? $values : array(); |
| 34 | }; |
| 35 | |
| 36 | $get_field_value = function ( $method_id, $field_name, $old_method_key, $saved_account, $method_values ) { |
| 37 | $saved_value = ''; |
| 38 | |
| 39 | if ( $old_method_key === $method_id ) { |
| 40 | $saved_value = tutor_utils()->avalue_dot( $field_name . '.value', $saved_account ); |
| 41 | } |
| 42 | |
| 43 | if ( ! $saved_value && isset( $method_values[ $field_name ]['value'] ) ) { |
| 44 | $saved_value = $method_values[ $field_name ]['value']; |
| 45 | } |
| 46 | |
| 47 | return $saved_value ? $saved_value : ''; |
| 48 | }; |
| 49 | |
| 50 | $user_id = get_current_user_id(); |
| 51 | $withdrawal_methods = apply_filters( 'tutor_withdrawal_methods_available', array() ); |
| 52 | $saved_account = WithdrawModel::get_user_withdraw_method(); |
| 53 | |
| 54 | if ( empty( $withdrawal_methods ) ) { |
| 55 | ?> |
| 56 | <section class="tutor-flex tutor-flex-column tutor-gap-4"> |
| 57 | <h5 class="tutor-h5 tutor-md-hidden tutor-my-none"><?php echo esc_html__( 'Withdrawal Method', 'tutor' ); ?></h5> |
| 58 | <?php |
| 59 | EmptyState::make() |
| 60 | ->title( __( "There's no Withdrawal method selected yet! To select a Withdraw method, please contact the Site Admin.", 'tutor' ) ) |
| 61 | ->render(); |
| 62 | ?> |
| 63 | </section> |
| 64 | <?php |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $method_options = array_map( |
| 69 | fn( $method_id, $method ) => array( |
| 70 | 'label' => tutor_utils()->avalue_dot( 'method_name', $method ), |
| 71 | 'value' => $method_id, |
| 72 | ), |
| 73 | array_keys( $withdrawal_methods ), |
| 74 | $withdrawal_methods |
| 75 | ); |
| 76 | |
| 77 | $old_method_key = tutor_utils()->avalue_dot( 'withdraw_method_key', $saved_account ); |
| 78 | $default_values = array( 'withdraw_method' => $old_method_key ? $old_method_key : '' ); |
| 79 | |
| 80 | do_action( 'tutor_withdraw_set_account_form_before' ); |
| 81 | |
| 82 | foreach ( $withdrawal_methods as $method_id => $method ) { |
| 83 | $method_values = $get_saved_method_values( $method_id, $user_id ); |
| 84 | $form_fields = tutor_utils()->avalue_dot( 'form_fields', $method ); |
| 85 | |
| 86 | if ( is_array( $form_fields ) ) { |
| 87 | foreach ( $form_fields as $field_name => $field ) { |
| 88 | $field_key = $get_field_key( $method_id, $field_name ); |
| 89 | $default_values[ $field_key ] = $get_field_value( |
| 90 | $method_id, |
| 91 | $field_name, |
| 92 | $old_method_key, |
| 93 | $saved_account, |
| 94 | $method_values |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ?> |
| 101 | |
| 102 | <section class="tutor-flex tutor-flex-column tutor-gap-4"> |
| 103 | <h5 class="tutor-h5 tutor-md-hidden tutor-my-none"><?php echo esc_html__( 'Withdrawal Method', 'tutor' ); ?></h5> |
| 104 | |
| 105 | <form |
| 106 | id="<?php echo esc_attr( $form_id ); ?>" |
| 107 | x-data='tutorForm({ |
| 108 | id: "<?php echo esc_attr( $form_id ); ?>", |
| 109 | mode: "onChange", |
| 110 | defaultValues: <?php echo wp_json_encode( $default_values ); ?>, |
| 111 | })' |
| 112 | x-bind="getFormBindings()" |
| 113 | @submit="handleSubmit((data) => handleSaveWithdrawMethod(data, '<?php echo esc_attr( $form_id ); ?>'))($event)" |
| 114 | class="tutor-card tutor-card-rounded-2xl tutor-flex tutor-flex-column tutor-gap-5" |
| 115 | > |
| 116 | <?php |
| 117 | $min_withdraw_amount = tutor_utils()->get_option( 'min_withdraw_amount' ); |
| 118 | $formatted_min = tutor_utils()->tutor_price( $min_withdraw_amount ); |
| 119 | |
| 120 | InputField::make() |
| 121 | ->type( InputType::SELECT ) |
| 122 | ->label( __( 'Select Method', 'tutor' ) ) |
| 123 | ->name( 'withdraw_method' ) |
| 124 | ->id( 'withdraw_method' ) |
| 125 | ->options( $method_options ) |
| 126 | ->placeholder( __( 'Select a withdrawal method', 'tutor' ) ) |
| 127 | ->required() |
| 128 | ->attr( 'x-bind', "register('withdraw_method', { required: true })" ) |
| 129 | ->help_text( |
| 130 | sprintf( |
| 131 | /* translators: %s: minimum withdraw amount */ |
| 132 | __( 'Minimum withdraw amount is %s', 'tutor' ), |
| 133 | $formatted_min |
| 134 | ), |
| 135 | tutor_price_allowed_html() |
| 136 | ) |
| 137 | ->render(); |
| 138 | ?> |
| 139 | |
| 140 | <?php foreach ( $withdrawal_methods as $method_id => $method ) : ?> |
| 141 | <?php |
| 142 | do_action( "tutor_withdraw_set_account_{$method_id}_before" ); |
| 143 | |
| 144 | $form_fields = tutor_utils()->avalue_dot( 'form_fields', $method ); |
| 145 | if ( ! is_array( $form_fields ) || empty( $form_fields ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | ?> |
| 149 | <div |
| 150 | x-show="values.withdraw_method === '<?php echo esc_js( $method_id ); ?>'" |
| 151 | x-cloak |
| 152 | class="tutor-flex tutor-flex-column tutor-gap-5" |
| 153 | > |
| 154 | <?php foreach ( $form_fields as $field_name => $field ) : ?> |
| 155 | <?php |
| 156 | $field_key = $get_field_key( $method_id, $field_name ); |
| 157 | $input_type = $map_field_type( $field['type'] ?? 'text' ); |
| 158 | $register_options = 'number' === $input_type |
| 159 | ? '{ numberOnly: true }' |
| 160 | : '{}'; |
| 161 | |
| 162 | $input_field = InputField::make() |
| 163 | ->type( $input_type ) |
| 164 | ->name( $field_key ) |
| 165 | ->id( $field_key ) |
| 166 | ->attr( 'x-bind', "register('{$field_key}', {$register_options})" ); |
| 167 | |
| 168 | if ( ! empty( $field['label'] ) ) { |
| 169 | $input_field->label( $field['label'] ); |
| 170 | } |
| 171 | |
| 172 | if ( ! empty( $field['desc'] ) ) { |
| 173 | $input_field->help_text( $field['desc'] ); |
| 174 | } |
| 175 | |
| 176 | $input_field->render(); |
| 177 | ?> |
| 178 | <?php endforeach; ?> |
| 179 | </div> |
| 180 | |
| 181 | <?php do_action( "tutor_withdraw_set_account_{$method_id}_after" ); ?> |
| 182 | <?php endforeach; ?> |
| 183 | |
| 184 | <?php do_action( 'tutor_withdraw_set_account_form_after' ); ?> |
| 185 | </form> |
| 186 | </section> |
| 187 |