Settings > Acrislip Order Receipts. * * @package VerifiablePaymentReceipts */ defined( 'ABSPATH' ) || exit; /** * Settings. */ class VPRWC_Settings { const TAB = 'vprwc'; /** * Default values for every option. * * @return array */ public static function defaults() { return array( 'vprwc_logo' => '', 'vprwc_legal_name' => '', 'vprwc_business_address' => '', 'vprwc_business_phone' => '', 'vprwc_business_email' => '', 'vprwc_tax_number' => '', 'vprwc_brand_color' => '#1B365D', 'vprwc_layout' => 'classic', 'vprwc_paper' => 'A4', 'vprwc_prefix' => 'RCPT-', 'vprwc_invoice_prefix' => 'INV-', 'vprwc_padding' => 6, 'vprwc_next_number' => 1, 'vprwc_invoice_next_number' => 1, 'vprwc_footer_note' => 'Thank you for your payment. Please keep this receipt for your records.', 'vprwc_terms' => '', 'vprwc_attach_emails' => 'yes', 'vprwc_attach_invoice_emails' => 'yes', 'vprwc_show_logo' => 'yes', 'vprwc_show_shop_address' => 'yes', 'vprwc_show_billing' => 'yes', 'vprwc_show_shipping' => 'yes', 'vprwc_show_items' => 'yes', 'vprwc_show_payment' => 'yes', 'vprwc_show_card' => 'yes', 'vprwc_show_bank' => 'yes', 'vprwc_show_channel' => 'yes', 'vprwc_show_gateway_ref' => 'yes', 'vprwc_show_qr' => 'yes', 'vprwc_show_customer_note' => 'no', ); } /** * Read one option with its default. * * @param string $key Option name. * @return mixed */ public static function get( $key ) { $defaults = self::defaults(); $value = get_option( $key, isset( $defaults[ $key ] ) ? $defaults[ $key ] : '' ); return apply_filters( 'vprwc_setting', $value, $key ); } /** * Whether a yes/no option is on. * * @param string $key Option name. * @return bool */ public static function on( $key ) { return 'yes' === self::get( $key ); } /** * Hooks. */ public static function init() { add_filter( 'woocommerce_settings_tabs_array', array( __CLASS__, 'add_tab' ), 60 ); add_action( 'woocommerce_settings_' . self::TAB, array( __CLASS__, 'output' ) ); add_action( 'woocommerce_update_options_' . self::TAB, array( __CLASS__, 'save' ) ); add_action( 'admin_menu', array( __CLASS__, 'menu' ), 60 ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_next_number', array( __CLASS__, 'sanitize_positive_int' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_invoice_next_number', array( __CLASS__, 'sanitize_positive_int' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_padding', array( __CLASS__, 'sanitize_padding' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_brand_color', array( __CLASS__, 'sanitize_color' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_logo', 'esc_url_raw' ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_legal_name', 'sanitize_text_field' ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_business_address', array( __CLASS__, 'sanitize_address' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_business_phone', 'sanitize_text_field' ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_business_email', array( __CLASS__, 'sanitize_email' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_tax_number', 'sanitize_text_field' ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_prefix', array( __CLASS__, 'sanitize_prefix' ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_vprwc_invoice_prefix', array( __CLASS__, 'sanitize_prefix' ) ); } /** * Add a WooCommerce submenu that opens the settings tab. */ public static function menu() { add_submenu_page( 'woocommerce', __( 'Acrislip Order Receipts', 'acrislip-order-receipts' ), __( 'Acrislip Order Receipts', 'acrislip-order-receipts' ), 'manage_woocommerce', 'admin.php?page=wc-settings&tab=' . self::TAB ); } /** * Register the tab. * * @param array $tabs Tabs. * @return array */ public static function add_tab( $tabs ) { $tabs[ self::TAB ] = __( 'Acrislip Order Receipts', 'acrislip-order-receipts' ); return $tabs; } /** * Field definitions. * * @return array */ public static function fields() { $d = self::defaults(); $yes = static function ( $id, $title, $desc = '' ) use ( $d ) { $fields = array( 'id' => $id, 'title' => $title, 'desc' => $desc, 'type' => 'checkbox', 'default' => $d[ $id ], ); }; return array( array( 'title' => __( 'Acrislip Order Receipts', 'acrislip-order-receipts' ), 'type' => 'title', 'desc' => __( 'Settings for invoices and payment receipts.', 'acrislip-order-receipts' ), 'id' => 'vprwc_page', ), array( 'type' => 'sectionend', 'id' => 'vprwc_page', ), array( 'title' => __( 'Branding', 'acrislip-order-receipts' ), 'type' => 'title', 'desc' => __( 'Legal name, address, phone and email printed on invoices and receipts can be set here. Leave a field empty to use the matching WooCommerce store or email setting. These fields do not change tax or shipping.', 'acrislip-order-receipts' ), 'id' => 'vprwc_branding', ), array( 'id' => 'vprwc_logo', 'title' => __( 'Logo URL', 'acrislip-order-receipts' ), 'desc' => __( 'URL of an image in your Media Library (PNG or JPG). Leave empty to use the site logo, or the shop name if there is none.', 'acrislip-order-receipts' ), 'type' => 'url', 'default' => $d['vprwc_logo'], 'css' => 'min-width:400px;', 'placeholder' => 'https://example.com/wp-content/uploads/logo.png', ), array( 'id' => 'vprwc_legal_name', 'title' => __( 'Legal business name', 'acrislip-order-receipts' ), 'desc' => __( 'Printed on invoices and receipts. Leave empty to use the shop name from WooCommerce email settings.', 'acrislip-order-receipts' ), 'type' => 'text', 'default' => $d['vprwc_legal_name'], 'css' => 'min-width:400px;', ), array( 'id' => 'vprwc_business_address', 'title' => __( 'Business address', 'acrislip-order-receipts' ), 'desc' => __( 'Printed on invoices and receipts. One line per row. Leave empty to use the store address from WooCommerce general settings (this field does not change tax or shipping).', 'acrislip-order-receipts' ), 'type' => 'textarea', 'default' => $d['vprwc_business_address'], 'css' => 'min-width:400px;height:80px;', ), array( 'id' => 'vprwc_business_phone', 'title' => __( 'Business phone', 'acrislip-order-receipts' ), 'desc' => __( 'Printed on invoices and receipts. Leave empty to omit a phone number.', 'acrislip-order-receipts' ), 'type' => 'text', 'default' => $d['vprwc_business_phone'], 'css' => 'min-width:400px;', ), array( 'id' => 'vprwc_business_email', 'title' => __( 'Business email', 'acrislip-order-receipts' ), 'desc' => __( 'Printed on invoices and receipts. Leave empty to use the address from WooCommerce email settings.', 'acrislip-order-receipts' ), 'type' => 'email', 'default' => $d['vprwc_business_email'], 'css' => 'min-width:400px;', ), array( 'id' => 'vprwc_tax_number', 'title' => __( 'Tax or registration number', 'acrislip-order-receipts' ), 'desc' => __( 'VAT, EIN, company or tax registration number, printed under the address.', 'acrislip-order-receipts' ), 'type' => 'text', 'default' => $d['vprwc_tax_number'], 'css' => 'min-width:400px;', ), array( 'id' => 'vprwc_brand_color', 'title' => __( 'Accent color', 'acrislip-order-receipts' ), 'desc' => __( 'Used sparingly for rules, headings and the header band. Default is a neutral navy.', 'acrislip-order-receipts' ), 'type' => 'color', 'default' => $d['vprwc_brand_color'], 'css' => 'width:6em;', ), array( 'id' => 'vprwc_layout', 'title' => __( 'Layout', 'acrislip-order-receipts' ), 'type' => 'select', 'default' => $d['vprwc_layout'], 'options' => array( 'classic' => __( 'Classic — private-bank letterhead', 'acrislip-order-receipts' ), 'modern' => __( 'Modern — bold header band', 'acrislip-order-receipts' ), 'minimal' => __( 'Minimal — typographic, extra whitespace', 'acrislip-order-receipts' ), ), ), array( 'id' => 'vprwc_paper', 'title' => __( 'Paper size', 'acrislip-order-receipts' ), 'type' => 'select', 'default' => $d['vprwc_paper'], 'options' => array( 'A4' => 'A4', 'letter' => __( 'US Letter', 'acrislip-order-receipts' ), ), ), array( 'id' => 'vprwc_footer_note', 'title' => __( 'Footer note', 'acrislip-order-receipts' ), 'type' => 'textarea', 'default' => $d['vprwc_footer_note'], 'css' => 'min-width:400px;height:60px;', ), array( 'id' => 'vprwc_terms', 'title' => __( 'Terms or legal text', 'acrislip-order-receipts' ), 'desc' => __( 'Optional. Printed in small type at the bottom of the receipt.', 'acrislip-order-receipts' ), 'type' => 'textarea', 'default' => $d['vprwc_terms'], 'css' => 'min-width:400px;height:60px;', ), array( 'type' => 'sectionend', 'id' => 'vprwc_branding', ), array( 'title' => __( 'Numbering', 'acrislip-order-receipts' ), 'type' => 'title', 'desc' => __( 'Each paid order gets one receipt number the first time a receipt is needed. Each order gets an invoice number the first time an invoice is needed. Numbers are never reused.', 'acrislip-order-receipts' ), 'id' => 'vprwc_numbering', ), array( 'id' => 'vprwc_prefix', 'title' => __( 'Receipt prefix', 'acrislip-order-receipts' ), 'type' => 'text', 'default' => $d['vprwc_prefix'], ), array( 'id' => 'vprwc_invoice_prefix', 'title' => __( 'Invoice prefix', 'acrislip-order-receipts' ), 'type' => 'text', 'default' => $d['vprwc_invoice_prefix'], ), array( 'id' => 'vprwc_padding', 'title' => __( 'Digits', 'acrislip-order-receipts' ), 'desc' => __( 'Minimum number of digits, padded with zeros. 6 gives RCPT-000123.', 'acrislip-order-receipts' ), 'type' => 'number', 'default' => $d['vprwc_padding'], 'custom_attributes' => array( 'min' => 1, 'max' => 12, ), ), array( 'id' => 'vprwc_next_number', 'title' => __( 'Next receipt number', 'acrislip-order-receipts' ), 'desc' => __( 'The number the next new receipt will use. Lowering it can create duplicate numbers.', 'acrislip-order-receipts' ), 'type' => 'number', 'default' => $d['vprwc_next_number'], 'custom_attributes' => array( 'min' => 1 ), ), array( 'id' => 'vprwc_invoice_next_number', 'title' => __( 'Next invoice number', 'acrislip-order-receipts' ), 'desc' => __( 'The number the next new invoice will use. Lowering it can create duplicate numbers.', 'acrislip-order-receipts' ), 'type' => 'number', 'default' => $d['vprwc_invoice_next_number'], 'custom_attributes' => array( 'min' => 1 ), ), array( 'type' => 'sectionend', 'id' => 'vprwc_numbering', ), array( 'title' => __( 'What to show', 'acrislip-order-receipts' ), 'type' => 'title', 'id' => 'vprwc_sections', ), $yes( 'vprwc_show_logo', __( 'Logo', 'acrislip-order-receipts' ), __( 'Show the logo', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_shop_address', __( 'Shop address', 'acrislip-order-receipts' ), __( 'Show the shop address and email', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_billing', __( 'Billing address', 'acrislip-order-receipts' ), __( 'Show the billing address', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_shipping', __( 'Shipping', 'acrislip-order-receipts' ), __( 'Show the shipping address and method', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_items', __( 'Items', 'acrislip-order-receipts' ), __( 'Show line items and totals', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_payment', __( 'Payment details', 'acrislip-order-receipts' ), __( 'Show the payment details block', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_card', __( 'Card', 'acrislip-order-receipts' ), __( 'Show the masked card number when the gateway provides it', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_bank', __( 'Bank transfer', 'acrislip-order-receipts' ), __( 'Show bank name, masked account (last 4), account name and transfer reference for bank payments', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_channel', __( 'Channel', 'acrislip-order-receipts' ), __( 'Show the payment channel when the gateway provides it', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_gateway_ref', __( 'Payment reference', 'acrislip-order-receipts' ), __( 'Show the payment reference used with the gateway', 'acrislip-order-receipts' ) ), $yes( 'vprwc_show_customer_note', __( 'Customer note', 'acrislip-order-receipts' ), __( 'Show the note the customer left at checkout', 'acrislip-order-receipts' ) ), array( 'type' => 'sectionend', 'id' => 'vprwc_sections', ), array( 'title' => __( 'Emails', 'acrislip-order-receipts' ), 'type' => 'title', 'id' => 'vprwc_emails', ), $yes( 'vprwc_attach_emails', __( 'Attach receipt to order emails', 'acrislip-order-receipts' ), __( 'Attach the receipt PDF to the Processing and Completed order emails sent to customers', 'acrislip-order-receipts' ) ), $yes( 'vprwc_attach_invoice_emails', __( 'Attach invoice to invoice email', 'acrislip-order-receipts' ), __( 'Attach the invoice PDF to the Customer invoice / Order details email', 'acrislip-order-receipts' ) ), array( 'type' => 'sectionend', 'id' => 'vprwc_emails', ), ); return apply_filters( 'vprwc_settings_fields', $fields ); } public static function output() { WC_Admin_Settings::output_fields( self::fields() ); } /** * Save the tab. */ public static function save() { WC_Admin_Settings::save_fields( self::fields() ); } /** * Sanitize a positive integer. * * @param mixed $value Value. * @return int */ public static function sanitize_positive_int( $value ) { return max( 1, absint( $value ) ); } /** * Sanitize padding. * * @param mixed $value Value. * @return int */ public static function sanitize_padding( $value ) { return min( 12, max( 1, absint( $value ) ) ); } /** * Sanitize a hex color. * * @param mixed $value Value. * @return string */ public static function sanitize_color( $value ) { $color = sanitize_hex_color( (string) $value ); return $color ? $color : self::defaults()['vprwc_brand_color']; } /** * Sanitize the prefix (letters, digits, dash, slash, underscore, dot). * * @param mixed $value Value. * @return string */ public static function sanitize_prefix( $value ) { return substr( preg_replace( '/[^A-Za-z0-9\-\/_.]/', '', (string) $value ), 0, 20 ); } /** * Sanitize the document business address (plain text, line breaks kept). * * @param mixed $value Value. * @return string */ public static function sanitize_address( $value ) { return sanitize_textarea_field( (string) $value ); } /** * Sanitize the document business email. Invalid values are stored empty so the WooCommerce email is used. * * @param mixed $value Value. * @return string */ public static function sanitize_email( $value ) { $email = sanitize_email( (string) $value ); return is_email( $email ) ? $email : ''; } }