Admin::SLUG . '-payments', 'tab' => 'overview', 'marked' => 1, ], admin_url( 'admin.php' ) ) ); exit; } /** * Persist provider settings or coupons, depending on the active tab. */ public function handle_save() { if ( ! current_user_can( Admin::CAPABILITY ) ) { wp_die( esc_html__( 'Permission denied.', 'phemightforms' ) ); } check_admin_referer( 'phemightforms_payments' ); $settings = Payments::settings(); $tab = isset( $_POST['active_tab'] ) ? sanitize_key( wp_unslash( $_POST['active_tab'] ) ) : 'providers'; if ( 'coupons' === $tab ) { $settings['coupons'] = self::read_coupons(); } else { $currency = isset( $_POST['currency'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['currency'] ) ) ) : 'USD'; $settings['currency'] = isset( Payments::currencies()[ $currency ] ) ? $currency : 'USD'; foreach ( self::TOGGLE_KEYS as $key ) { $settings[ $key ] = ! empty( $_POST[ $key ] ) ? 1 : 0; } foreach ( self::TEXT_KEYS as $key ) { $settings[ $key ] = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : ''; } foreach ( self::TEXTAREA_KEYS as $key ) { $settings[ $key ] = isset( $_POST[ $key ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $key ] ) ) : ''; } $settings['paypal_email'] = isset( $_POST['paypal_email'] ) ? sanitize_email( wp_unslash( $_POST['paypal_email'] ) ) : ''; foreach ( self::SECRET_KEYS as $key ) { $value = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : ''; if ( '' !== $value ) { $settings[ $key ] = $value; } } } update_option( Payments::OPTION, $settings ); wp_safe_redirect( add_query_arg( [ 'page' => Admin::SLUG . '-payments', 'tab' => $tab, 'saved' => 1, ], admin_url( 'admin.php' ) ) ); exit; } /** * Read the coupon rows out of the request. * * @return array */ private static function read_coupons() { $coupons = []; if ( ! isset( $_POST['coupon_code'] ) || ! is_array( $_POST['coupon_code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing return $coupons; } $codes = array_map( 'sanitize_text_field', wp_unslash( $_POST['coupon_code'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput,WordPress.Security.NonceVerification.Missing $types = isset( $_POST['coupon_type'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_POST['coupon_type'] ) ) : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput,WordPress.Security.NonceVerification.Missing $amounts = isset( $_POST['coupon_amount'] ) ? array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['coupon_amount'] ) ) : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput,WordPress.Security.NonceVerification.Missing foreach ( $codes as $i => $code ) { $code = trim( $code ); if ( '' === $code ) { continue; } $coupons[] = [ 'code' => $code, 'type' => isset( $types[ $i ] ) && 'fixed' === $types[ $i ] ? 'fixed' : 'percent', 'amount' => Payments::to_amount( isset( $amounts[ $i ] ) ? $amounts[ $i ] : 0 ), ]; } return $coupons; } /** * Render the Payments page. */ public static function render_page() { if ( ! current_user_can( Admin::CAPABILITY ) ) { wp_die( esc_html__( 'Permission denied.', 'phemightforms' ) ); } $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'overview'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! in_array( $tab, [ 'overview', 'providers', 'coupons' ], true ) ) { $tab = 'overview'; } $settings = Payments::settings(); $base_url = add_query_arg( [ 'page' => Admin::SLUG . '-payments' ], admin_url( 'admin.php' ) ); ?>

'phemightforms_mark_paid', 'txn' => $txn['id'], ], admin_url( 'admin-post.php' ) ), 'phemightforms_mark_paid' ); ?>
exists() ? $form->title : '#' . (int) $txn['form_id'] ); ?> —

', __( 'Offline / Manual', 'phemightforms' ), __( 'Collect orders now and arrange payment separately (bank transfer, cash, invoice).', 'phemightforms' ), isset( $enabled['offline'] ) ); ?>

'', 'type' => 'percent', 'amount' => '', ], ]; } foreach ( $coupons as $coupon ) : ?>
( function () { var add = document.getElementById( 'phf-coupon-add' ); var rows = document.getElementById( 'phf-coupon-rows' ); if ( add && rows ) { add.addEventListener( 'click', function () { var first = rows.querySelector( 'tr' ); var clone = first.cloneNode( true ); clone.querySelectorAll( 'input' ).forEach( function ( el ) { el.value = ''; } ); clone.querySelector( 'select' ).value = 'percent'; rows.appendChild( clone ); } ); rows.addEventListener( 'click', function ( e ) { var btn = e.target.closest( '.phf-coupon-remove' ); if ( btn && rows.querySelectorAll( 'tr' ).length > 1 ) { btn.closest( 'tr' ).remove(); } } ); } } )();