[label, type, default, tab, description]. * * @return array */ function klbq_settings_fields() { $labels = klbq_country_tax_labels( klbq_base_country() ); $fields = array( 'prefix' => array( __( 'Quote number prefix', 'koalab-quote' ), 'text', _x( 'QT-', 'default quote number prefix', 'koalab-quote' ), 'general', __( 'Quotes are numbered PREFIX0001, PREFIX0002…', 'koalab-quote' ) ), 'validity' => array( __( 'Default validity (days)', 'koalab-quote' ), 'number', 15, 'general', '' ), 'company_tax_label' => array( __( 'Company tax ID label', 'koalab-quote' ), 'text', $labels[0], 'general', __( 'Printed before your tax ID, e.g. NIT, RFC, CUIT, RUT or RUC.', 'koalab-quote' ) ), 'client_tax_label' => array( __( 'Customer tax ID label', 'koalab-quote' ), 'text', $labels[1], 'general', '' ), 'show_images' => array( __( 'Show product images on new quotes', 'koalab-quote' ), 'checkbox', 1, 'general', '' ), 'show_regular_price' => array( __( 'Show the store price struck through and the savings on new quotes', 'koalab-quote' ), 'checkbox', 1, 'general', __( 'Only when the quoted price is lower than the store price.', 'koalab-quote' ) ), 'delete_data' => array( __( 'Delete all quotes and settings when the plugin is deleted', 'koalab-quote' ), 'checkbox', 0, 'general', '' ), 'doc_title' => array( __( 'Document title', 'koalab-quote' ), 'text', __( 'Quote', 'koalab-quote' ), 'document', '' ), 'terms' => array( __( 'Default terms and conditions', 'koalab-quote' ), 'textarea', __( 'Prices and availability may change without notice until payment is confirmed.', 'koalab-quote' ), 'document', __( 'One item per line. Editable on each quote.', 'koalab-quote' ) ), 'extra_title' => array( __( 'Additional block title', 'koalab-quote' ), 'text', __( 'Warranty', 'koalab-quote' ), 'document', __( 'A fixed block printed on every quote, e.g. warranty or payment details.', 'koalab-quote' ) ), 'extra_text' => array( __( 'Additional block text', 'koalab-quote' ), 'textarea', '', 'document', __( 'One item per line. Leave empty to hide the block.', 'koalab-quote' ) ), 'thanks' => array( __( 'Footer message', 'koalab-quote' ), 'text', __( 'Thank you for your business.', 'koalab-quote' ), 'document', '' ), ); /** * Filters the settings fields. * * @param array $fields key => [label, type, default, tab, description]. */ return (array) apply_filters( 'klbq_settings_fields', $fields ); } /** * Settings merged with defaults. * * @return array */ function klbq_settings() { $saved = get_option( 'klbq_settings', array() ); $saved = is_array( $saved ) ? $saved : array(); $out = array(); foreach ( klbq_settings_fields() as $key => $field ) { // Saved textareas may be empty on purpose; other empty values take the default. $out[ $key ] = array_key_exists( $key, $saved ) && ( '' !== $saved[ $key ] || 'textarea' === $field[1] ) ? $saved[ $key ] : $field[2]; } return $out; } /** * One setting. * * @param string $key Key. * @return mixed */ function klbq_setting( $key ) { $settings = klbq_settings(); return isset( $settings[ $key ] ) ? $settings[ $key ] : null; } /** * Settings tabs. * * @return array Slug => label. */ function klbq_settings_tabs() { return (array) apply_filters( 'klbq_settings_tabs', array( 'general' => __( 'General', 'koalab-quote' ), 'brands' => __( 'Company', 'koalab-quote' ), 'document' => __( 'Document', 'koalab-quote' ), ) ); } add_action( 'admin_menu', function () { add_submenu_page( 'edit.php?post_type=' . KLBQ_CPT, __( 'Koalab Quote settings', 'koalab-quote' ), __( 'Settings', 'koalab-quote' ), klbq_cap(), 'klbq-settings', 'klbq_render_settings_page' ); } ); add_action( 'admin_init', function () { register_setting( 'klbq', 'klbq_settings', array( 'type' => 'array', 'sanitize_callback' => 'klbq_sanitize_settings', 'default' => array(), ) ); register_setting( 'klbq_company', 'klbq_company', array( 'type' => 'array', 'sanitize_callback' => 'klbq_sanitize_company', 'default' => array(), ) ); } ); // Shop managers may save the settings (options.php checks these caps). add_filter( 'option_page_capability_klbq', 'klbq_cap' ); add_filter( 'option_page_capability_klbq_company', 'klbq_cap' ); /** * Sanitize the posted tab and merge it with the stored settings. * * @param mixed $value Raw value. * @return array */ function klbq_sanitize_settings( $value ) { $value = is_array( $value ) ? $value : array(); $saved = get_option( 'klbq_settings', array() ); $out = is_array( $saved ) ? $saved : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- options.php verified the nonce. $tab = isset( $_POST['klbq_tab'] ) ? sanitize_key( wp_unslash( $_POST['klbq_tab'] ) ) : ''; foreach ( klbq_settings_fields() as $key => $field ) { $in_tab = '' === $tab ? array_key_exists( $key, $value ) : $field[3] === $tab; if ( ! $in_tab ) { continue; } $raw = isset( $value[ $key ] ) ? $value[ $key ] : ''; $raw = is_array( $raw ) ? array_map( 'sanitize_text_field', wp_unslash( $raw ) ) : wp_unslash( $raw ); switch ( $field[1] ) { case 'textarea': $out[ $key ] = sanitize_textarea_field( $raw ); break; case 'checkbox': $out[ $key ] = empty( $raw ) ? 0 : 1; break; case 'number': $out[ $key ] = '' === $raw ? '' : max( 0, (float) $raw ); break; case 'email': $out[ $key ] = sanitize_email( $raw ); break; case 'roles': $roles = array_keys( wp_roles()->get_names() ); $out[ $key ] = array_values( array_intersect( (array) $raw, $roles ) ); break; case 'select': $options = isset( $field[5] ) ? array_keys( (array) $field[5] ) : array(); $out[ $key ] = in_array( $raw, $options, true ) ? $raw : $field[2]; break; default: $out[ $key ] = sanitize_text_field( $raw ); } } return $out; } /** * Sanitize the company form. * * @param mixed $value Raw company data. * @return array */ function klbq_sanitize_company( $value ) { $company = klbq_sanitize_brand( is_array( $value ) ? $value : array() ); if ( empty( $value['id'] ) ) { $company['id'] = klbq_company()['id']; } return $company; } add_action( 'admin_enqueue_scripts', function ( $hook ) { if ( false === strpos( (string) $hook, 'klbq-settings' ) ) { return; } wp_enqueue_media(); wp_enqueue_style( 'klbq-admin', KLBQ_URL . 'assets/css/admin.css', array(), KLBQ_VERSION ); wp_enqueue_script( 'klbq-admin', KLBQ_URL . 'assets/js/admin.js', array( 'jquery' ), KLBQ_VERSION, true ); wp_localize_script( 'klbq-admin', 'klbqAdmin', array( 'chooseLogo' => __( 'Choose logo', 'koalab-quote' ), 'useLogo' => __( 'Use this logo', 'koalab-quote' ), ) ); } ); /** * Current settings tab. * * @return string */ function klbq_current_tab() { $tabs = klbq_settings_tabs(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; return isset( $tabs[ $tab ] ) ? $tab : 'general'; } /** * Render the settings page. */ function klbq_render_settings_page() { if ( ! current_user_can( klbq_cap() ) ) { return; } $tab = klbq_current_tab(); $base = admin_url( 'edit.php?post_type=' . KLBQ_CPT . '&page=klbq-settings' ); ?>

$field ) { if ( $field[3] === $tab ) { klbq_render_field( $key, $field, $values[ $key ] ); } } ?>
get_names() as $role => $label ) : ?>