array(), 'purpose' => '', 'links' => array(), ); } function setting( string $key ) { $saved = (array) get_option( OPTION, array() ); return $saved[ $key ] ?? setting_defaults()[ $key ] ?? ''; } /** * Accounts that can be shown, in their saved order: a valid IBAN, and a BIC only when it is valid. * * @param string[] $only IDs to keep; empty, or only IDs no longer saved, keeps every account. * @return array */ function accounts( array $only = array() ): array { $only = known_ids( 'accounts', $only ); $accounts = array(); foreach ( (array) setting( 'accounts' ) as $account ) { $account = wp_parse_args( (array) $account, array( 'id' => '', 'label' => '', 'recipient' => '', 'iban' => '', 'bic' => '', ) ); if ( ! iban_valid( (string) $account['iban'] ) || ( $only && ! in_array( $account['id'], $only, true ) ) ) { continue; } $account['bic'] = bic_valid( (string) $account['bic'] ) ? $account['bic'] : ''; $accounts[] = array_map( 'strval', $account ); } return $accounts; } /** * @param string[] $only IDs to keep; empty, or only IDs no longer saved, keeps every link. * @return array */ function links( array $only = array() ): array { $only = known_ids( 'links', $only ); return array_values( array_filter( array_map( static fn( $link ) => array( 'id' => (string) ( $link['id'] ?? '' ), 'url' => (string) ( $link['url'] ?? '' ), 'label' => (string) ( $link['label'] ?? '' ), ), (array) setting( 'links' ) ), static fn( $link ) => '' !== $link['url'] && ( ! $only || in_array( $link['id'], $only, true ) ) ) ); } /** * The IDs of $ids that a saved row still has. A block whose picks were all removed from the * settings then shows everything, as its editor does when nothing is ticked. * * @param string[] $ids * @return string[] */ function known_ids( string $list, array $ids ): array { return $ids ? array_values( array_intersect( $ids, array_column( (array) setting( $list ), 'id' ) ) ) : array(); } /** * A comma or space separated list of IDs, as the block and the shortcode store them. * * @param mixed $value An array of IDs or a string. * @return string[] */ function parse_ids( $value ): array { $ids = is_array( $value ) ? $value : preg_split( '/[\s,]+/', (string) $value ); return array_values( array_unique( array_filter( array_map( static fn( $id ) => strtolower( trim( (string) $id ) ), $ids ), static fn( $id ) => (bool) preg_match( '/^[a-z0-9]{6}$/', $id ) ) ) ); } /** * Gives each row an ID that blocks and shortcodes refer to it by: a posted ID is kept when it is * well formed and not taken by an earlier row, otherwise a new one is made. * * @param array[] $rows * @return array[] */ function with_ids( array $rows ): array { $taken = array(); foreach ( $rows as &$row ) { $id = strtolower( (string) ( $row['id'] ?? '' ) ); while ( ! preg_match( '/^[a-z0-9]{6}$/', $id ) || isset( $taken[ $id ] ) ) { $id = strtolower( wp_generate_password( 6, false ) ); } $taken[ $id ] = true; $row = array( 'id' => $id ) + $row; } return $rows; } /** * Settings saved before accounts and links had IDs get them once, so blocks can refer to them. */ add_action( 'init', static function () { $saved = get_option( OPTION ); if ( ! is_array( $saved ) ) { return; } $changed = false; foreach ( array( 'accounts', 'links' ) as $list ) { $rows = array_values( (array) ( $saved[ $list ] ?? array() ) ); if ( array_filter( $rows, static fn( $row ) => empty( $row['id'] ) ) ) { $saved[ $list ] = with_ids( $rows ); $changed = true; } } if ( $changed ) { update_option( OPTION, $saved ); } } ); /** * Donation sites a link is named after when it has no label of its own: host => name. */ function platforms(): array { /** * Filters the donation sites links are recognised as. * * @param array $platforms Host name (without "www.") => site name. */ return (array) apply_filters( 'ibanbox_platforms', array( 'buymeacoffee.com' => 'Buy Me a Coffee', 'ko-fi.com' => 'Ko-fi', 'paypal.com' => 'PayPal', 'paypal.me' => 'PayPal', 'patreon.com' => 'Patreon', 'revolut.me' => 'Revolut', 'donate.stripe.com' => 'Stripe', 'buy.stripe.com' => 'Stripe', 'liberapay.com' => 'Liberapay', 'opencollective.com' => 'Open Collective', 'gofundme.com' => 'GoFundMe', 'github.com' => 'GitHub Sponsors', ) ); } function platform_for( string $url ): string { $host = preg_replace( '/^www\./', '', strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) ) ); return (string) ( platforms()[ $host ] ?? '' ); } /** * "de89 3704 0044..." to "DE8937040044...". */ function normalize_iban( string $iban ): string { return strtoupper( (string) preg_replace( '/[^A-Za-z0-9]/', '', $iban ) ); } /** * Checks an IBAN's structure and its mod-97 check digits. */ function iban_valid( string $iban ): bool { if ( ! preg_match( '/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/', $iban ) ) { return false; } return 1 === mod97( substr( $iban, 4 ) . substr( $iban, 0, 4 ) ); } function bic_valid( string $bic ): bool { return (bool) preg_match( '/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/', $bic ); } /** * "DE89370400440532013000" to "DE89 3704 0044 0532 0130 00". */ function format_iban( string $iban ): string { return trim( chunk_split( $iban, 4, ' ' ) ); } add_action( 'admin_init', static function () { register_setting( 'ibanbox', OPTION, array( 'type' => 'array', 'sanitize_callback' => 'CommerceConsulting\IbanBox\sanitize_settings', 'default' => setting_defaults(), ) ); } ); /** * Rows keep the order they were posted in. An account with a mistyped IBAN or BIC is saved as * typed, so nothing is lost, and left off the page until it is corrected. */ function sanitize_settings( $input ): array { $input = is_array( $input ) ? $input : array(); $accounts = array(); foreach ( (array) ( $input['accounts'] ?? array() ) as $row ) { $account = array( 'id' => sanitize_key( $row['id'] ?? '' ), 'label' => mb_substr( sanitize_text_field( $row['label'] ?? '' ), 0, 40 ), 'recipient' => mb_substr( sanitize_text_field( $row['recipient'] ?? '' ), 0, 70 ), 'iban' => normalize_iban( sanitize_text_field( $row['iban'] ?? '' ) ), 'bic' => normalize_iban( sanitize_text_field( $row['bic'] ?? '' ) ), ); if ( '' === $account['iban'] && '' === $account['label'] && '' === $account['recipient'] ) { continue; } $name = '' !== $account['label'] ? $account['label'] : format_iban( $account['iban'] ); if ( ! iban_valid( $account['iban'] ) ) { /* translators: %s: the account's bank or number */ add_settings_error( OPTION, 'iban-' . count( $accounts ), sprintf( __( '%s: the account number is not a valid IBAN. The account is saved but not shown until it is corrected.', 'ibanbox' ), $name ) ); } elseif ( '' !== $account['bic'] && ! bic_valid( $account['bic'] ) ) { /* translators: %s: the account's bank or number */ add_settings_error( OPTION, 'bic-' . count( $accounts ), sprintf( __( '%s: the BIC should be 8 or 11 letters and digits. It is left out until it is corrected.', 'ibanbox' ), $name ) ); } $accounts[] = $account; } $links = array(); foreach ( (array) ( $input['links'] ?? array() ) as $row ) { $url = esc_url_raw( (string) ( $row['url'] ?? '' ), array( 'https', 'http' ) ); if ( '' !== $url ) { $links[] = array( 'id' => sanitize_key( $row['id'] ?? '' ), 'url' => $url, 'label' => mb_substr( sanitize_text_field( $row['label'] ?? '' ), 0, 40 ), ); } } return array( 'accounts' => with_ids( $accounts ), 'purpose' => mb_substr( sanitize_text_field( $input['purpose'] ?? '' ), 0, 140 ), 'links' => with_ids( $links ), ); } add_action( 'admin_menu', static function () { add_options_page( __( 'IbanBox', 'ibanbox' ), __( 'IbanBox', 'ibanbox' ), 'manage_options', 'ibanbox', __NAMESPACE__ . '\render_settings_page' ); } ); add_action( 'admin_enqueue_scripts', static function ( string $hook ) { if ( 'settings_page_ibanbox' !== $hook ) { return; } wp_enqueue_style( 'btdon-admin', BTDON_URL . 'admin/admin.css', array(), asset_version( 'admin/admin.css' ) ); wp_enqueue_script( 'btdon-admin', BTDON_URL . 'admin/admin.js', array( 'jquery-ui-sortable', 'btdon-qrcode' ), asset_version( 'admin/admin.js' ), array( 'in_footer' => true ) ); } ); /** * One row of a sortable list. $index is a number for saved rows and "__i__" in the template * the Add button copies. * * @param int|string $index Row index in the field names. * @param array $fields key => [ label, type, value, help, extra class ]. */ function render_row( string $list, $index, array $fields, string $title, string $id = '' ): void { ?>
  • [ $label, $type, $value, $help, $class ] ) : $id = 'btdon-' . $list . '-' . $index . '-' . $key; ?>

  • array( __( 'Bank', 'ibanbox' ), 'text', (string) ( $account['label'] ?? '' ), __( 'Shown to donors when there is more than one account, such as "Swedbank".', 'ibanbox' ), '' ), 'recipient' => array( __( 'Recipient', 'ibanbox' ), 'text', (string) ( $account['recipient'] ?? '' ), __( 'The account holder\'s name, as this bank has it.', 'ibanbox' ), '' ), 'iban' => array( __( 'Account number (IBAN)', 'ibanbox' ), 'text', format_iban( $iban ), '' !== $iban && ! iban_valid( $iban ) ? __( 'Not a valid IBAN. This account is not shown.', 'ibanbox' ) : '', 'code' ), 'bic' => array( __( 'BIC (SWIFT)', 'ibanbox' ), 'text', $bic, '' !== $bic && ! bic_valid( $bic ) ? __( 'Not a valid BIC. It is left out.', 'ibanbox' ) : __( 'Optional.', 'ibanbox' ), 'code' ), ); } function link_fields( array $link ): array { return array( 'url' => array( __( 'Address', 'ibanbox' ), 'url', (string) ( $link['url'] ?? '' ), '', '' ), 'label' => array( __( 'Button text', 'ibanbox' ), 'text', (string) ( $link['label'] ?? '' ), __( 'Optional. Known sites are named automatically.', 'ibanbox' ), '' ), ); } function render_settings_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $lists = array( 'accounts' => array( __( 'Bank accounts', 'ibanbox' ), __( 'IBAN accounts in the SEPA area. With more than one, donors pick their bank and the details and QR code switch to it. Drag to change the order; the first is shown first.', 'ibanbox' ), __( 'Add account', 'ibanbox' ), __( 'Account', 'ibanbox' ), __NAMESPACE__ . '\account_fields', ), 'links' => array( __( 'Online donation links', 'ibanbox' ), __( 'Pages where people can give by card, such as Buy Me a Coffee, Ko-fi or PayPal. Each is a button above the bank details, in this order.', 'ibanbox' ), __( 'Add link', 'ibanbox' ), __( 'Link', 'ibanbox' ), __NAMESPACE__ . '\link_fields', ), ); // Which tab to show; it only picks a view, so there is no form to check a nonce for. $tab = sanitize_key( wp_unslash( $_GET['tab'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended $url = admin_url( 'options-general.php?page=ibanbox' ); ?>

    [ $heading, $intro, $add, $noun, $fields ] ) : ?>

      $row ) { $title = (string) ( $row['label'] ?? '' ); render_row( $list, $index, $fields( (array) $row ), '' !== $title ? $title : $noun, (string) ( $row['id'] ?? '' ) ); } ?>

    $account ) : $payload = epc_payload( $account, $purpose ); $name = '' !== $account['label'] ? $account['label'] : format_iban( $account['iban'] ); ?>

    __( 'Bank accounts', 'ibanbox' ), 'links' => __( 'Donation links', 'ibanbox' ), ); ?>

    $label ) : if ( 'links' === $key && $choices['links'] ) : ?>