sanitize_key( $method['id'] ), 'label' => sanitize_text_field( $method['label'] ), 'type' => sanitize_key( $method['type'] ), 'fields' => array(), 'instructions' => wp_kses_post( $method['instructions'] ?? '' ), 'qr_attachment_id' => isset( $method['qr_attachment_id'] ) ? absint( $method['qr_attachment_id'] ) : 0, 'copy_fields' => isset( $method['copy_fields'] ) && is_array( $method['copy_fields'] ) ? array_map( 'sanitize_key', $method['copy_fields'] ) : array(), ); // Sanitize fields based on type. if ( isset( $method['fields'] ) && is_array( $method['fields'] ) ) { $sanitized['fields'] = self::sanitize_fields( $method['type'], $method['fields'] ); } return $sanitized; } /** * Sanitize fields based on type. * * @param string $type Method type. * @param array $fields Fields data. * @return array */ private static function sanitize_fields( $type, $fields ) { $sanitized = array(); if ( 'custom' === $type ) { // Custom fields as key-value pairs. if ( is_array( $fields ) ) { foreach ( $fields as $key => $value ) { $sanitized[ sanitize_key( $key ) ] = sanitize_text_field( $value ); } } return $sanitized; } $definitions = self::get_definitions(); if ( ! isset( $definitions[ $type ] ) ) { return $sanitized; } foreach ( $definitions[ $type ]['fields'] as $key => $field ) { $value = isset( $fields[ $key ] ) ? $fields[ $key ] : ''; $sanitized[ $key ] = 'email' === $field['type'] ? sanitize_email( $value ) : sanitize_text_field( $value ); } return $sanitized; } /** * Payment method types and their fields. * * This is the single source of truth: the type list, the admin form, the * sanitising and the labels shown to customers are all built from it. * * @return array Type => array( 'label' => string, 'fields' => array( key => array( 'label', 'type' ) ) ). */ public static function get_definitions() { return array( 'bank' => array( 'label' => __( 'Bank Transfer', 'afridirect-payment' ), 'fields' => array( 'bank_name' => array( 'label' => __( 'Bank Name', 'afridirect-payment' ), 'type' => 'text', ), 'account_name' => array( 'label' => __( 'Account Name', 'afridirect-payment' ), 'type' => 'text', ), 'account_number' => array( 'label' => __( 'Account Number', 'afridirect-payment' ), 'type' => 'text', ), 'sort_code' => array( 'label' => __( 'Sort Code', 'afridirect-payment' ), 'type' => 'text', ), 'iban' => array( 'label' => __( 'IBAN', 'afridirect-payment' ), 'type' => 'text', ), 'swift' => array( 'label' => __( 'SWIFT/BIC', 'afridirect-payment' ), 'type' => 'text', ), 'routing_number' => array( 'label' => __( 'Routing Number', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'paypal' => array( 'label' => __( 'PayPal', 'afridirect-payment' ), 'fields' => array( 'email' => array( 'label' => __( 'PayPal Email', 'afridirect-payment' ), 'type' => 'email', ), ), ), 'zelle' => array( 'label' => __( 'Zelle', 'afridirect-payment' ), 'fields' => array( 'email' => array( 'label' => __( 'Email', 'afridirect-payment' ), 'type' => 'email', ), 'phone' => array( 'label' => __( 'Phone Number', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'venmo' => array( 'label' => __( 'Venmo', 'afridirect-payment' ), 'fields' => array( 'username' => array( 'label' => __( 'Venmo Username', 'afridirect-payment' ), 'type' => 'text', ), 'phone' => array( 'label' => __( 'Phone Number', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'cashapp' => array( 'label' => __( 'Cash App', 'afridirect-payment' ), 'fields' => array( 'cashtag' => array( 'label' => __( 'Cashtag', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'wise' => array( 'label' => __( 'Wise', 'afridirect-payment' ), 'fields' => array( 'email' => array( 'label' => __( 'Wise Email', 'afridirect-payment' ), 'type' => 'email', ), 'wisetag' => array( 'label' => __( 'Wisetag', 'afridirect-payment' ), 'type' => 'text', ), 'account_name' => array( 'label' => __( 'Account Name', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'payoneer' => array( 'label' => __( 'Payoneer', 'afridirect-payment' ), 'fields' => array( 'email' => array( 'label' => __( 'Payoneer Email', 'afridirect-payment' ), 'type' => 'email', ), 'payoneer_id' => array( 'label' => __( 'Payoneer ID', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'skrill' => array( 'label' => __( 'Skrill', 'afridirect-payment' ), 'fields' => array( 'email' => array( 'label' => __( 'Skrill Email', 'afridirect-payment' ), 'type' => 'email', ), ), ), 'mobile_money' => array( 'label' => __( 'Mobile Money', 'afridirect-payment' ), 'fields' => array( 'provider' => array( 'label' => __( 'Mobile Money Provider', 'afridirect-payment' ), 'type' => 'text', ), 'phone_number' => array( 'label' => __( 'Mobile Number', 'afridirect-payment' ), 'type' => 'text', ), 'account_name' => array( 'label' => __( 'Account Name', 'afridirect-payment' ), 'type' => 'text', ), 'country' => array( 'label' => __( 'Country', 'afridirect-payment' ), 'type' => 'text', ), ), ), 'crypto' => array( 'label' => __( 'Cryptocurrency', 'afridirect-payment' ), 'fields' => array( 'wallet_address' => array( 'label' => __( 'Wallet Address', 'afridirect-payment' ), 'type' => 'text', ), 'network' => array( 'label' => __( 'Network', 'afridirect-payment' ), 'type' => 'text', ), 'currency' => array( 'label' => __( 'Currency', 'afridirect-payment' ), 'type' => 'text', ), ), ), ); } /** * Get method types. * * @return array Type => label. */ public static function get_types() { $types = array(); foreach ( self::get_definitions() as $type => $definition ) { $types[ $type ] = $definition['label']; } $types['custom'] = __( 'Custom', 'afridirect-payment' ); return $types; } /** * Get field labels for a type. * * @param string $type Method type. * @return array Field key => label. */ public static function get_field_labels( $type ) { $definitions = self::get_definitions(); if ( ! isset( $definitions[ $type ] ) ) { return array(); } $labels = array(); foreach ( $definitions[ $type ]['fields'] as $key => $field ) { $labels[ $key ] = $field['label']; } return $labels; } }