Settings (native WC Settings API, no framework). * All settings stored in ONE option: bbquote_settings. * * @package BoringQuote */ namespace BoringQuote; defined( 'ABSPATH' ) || exit; /** * Class Admin */ class Admin { const TAB = 'boring_quote'; /** * Constructor. */ public function __construct() { add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_tab' ), 50 ); add_action( 'woocommerce_settings_' . self::TAB, array( $this, 'output' ) ); add_action( 'woocommerce_settings_save_' . self::TAB, array( $this, 'save' ) ); add_filter( 'plugin_action_links_' . plugin_basename( BBQUOTE_FILE ), array( $this, 'action_links' ) ); // Per-product "hide quote button" checkbox. add_action( 'woocommerce_product_options_general_product_data', array( $this, 'product_field' ) ); add_action( 'woocommerce_admin_process_product_object', array( $this, 'save_product_field' ) ); } /** * Add settings tab. * * @param array $tabs Tabs. * @return array */ public function add_tab( $tabs ) { $tabs[ self::TAB ] = __( 'Boring Quote', 'boring-quote' ); return $tabs; } /** * Settings definition. Each id is "bbquote_settings[key]" so WC stores them in one array option. * * @return array */ protected function settings() { return apply_filters( 'bbquote_settings_fields', array( array( 'title' => __( 'Boring Quote', 'boring-quote' ), 'type' => 'title', 'id' => 'bbquote_section', ), array( 'title' => __( 'Show button on product page', 'boring-quote' ), 'desc' => __( 'Show the "Request a quote" button under the add to cart area of every product.', 'boring-quote' ), 'id' => 'bbquote_settings[show_on_single]', 'type' => 'checkbox', 'default' => 'yes', ), array( 'title' => __( 'Button label', 'boring-quote' ), 'desc_tip' => __( 'Text on the button, for example "Ask for a price".', 'boring-quote' ), 'id' => 'bbquote_settings[button_label]', 'type' => 'text', 'default' => __( 'Request a quote', 'boring-quote' ), ), array( 'title' => __( 'Hide prices', 'boring-quote' ), 'desc' => __( 'Catalog mode: prices are hidden everywhere in the shop.', 'boring-quote' ), 'id' => 'bbquote_settings[hide_price]', 'type' => 'checkbox', 'default' => 'no', ), array( 'title' => __( 'Hide "Add to cart" buttons', 'boring-quote' ), 'desc' => __( 'Catalog mode: customers can only ask for a quote, not buy online.', 'boring-quote' ), 'id' => 'bbquote_settings[hide_add_to_cart]', 'type' => 'checkbox', 'default' => 'no', ), array( 'title' => __( 'Consent text', 'boring-quote' ), 'desc' => __( 'Label of the required GDPR consent checkbox. A link to the privacy policy page (Settings > Privacy) is added automatically.', 'boring-quote' ), 'id' => 'bbquote_settings[consent_text]', 'type' => 'textarea', 'default' => '', 'placeholder' => __( 'I agree to the processing of my personal data in order to prepare a quote.', 'boring-quote' ), 'css' => 'min-width:400px;height:70px;', ), array( 'title' => __( 'Quote request page', 'boring-quote' ), 'id' => 'bbquote_page_id', 'type' => 'single_select_page', 'class' => 'wc-enhanced-select-nostd', 'css' => 'min-width:300px;', ), array( 'type' => 'sectionend', 'id' => 'bbquote_section', ), ) ); } /** * Render settings. */ public function output() { printf( '

%s

%s %s

', esc_html__( 'Customers collect products into a quote list and send it with a short form. Requests land in Quote Requests and in your inbox.', 'boring-quote' ), esc_url( GettingStarted::url() ), esc_html__( 'Getting started', 'boring-quote' ), esc_url( admin_url( 'edit.php?post_type=' . QuoteCPT::POST_TYPE ) ), esc_html__( 'Quote Requests', 'boring-quote' ) ); \WC_Admin_Settings::output_fields( $this->settings() ); /** * Whether to show the single, discreet note about the Pro version on this screen. * Pro returns false. * * @param bool $show Show note. */ if ( apply_filters( 'bbquote_show_pro_note', true ) ) { printf( '

%s %s

', esc_html__( 'Need to send priced offers that customers accept and pay online? That is what Boring Quote Pro adds.', 'boring-quote' ), esc_url( 'https://boringdev.pl' ), esc_html__( 'Learn more', 'boring-quote' ) ); } } /** * Save settings. */ public function save() { \WC_Admin_Settings::save_fields( $this->settings() ); } /** * Settings link on plugins screen. * * @param array $links Links. * @return array */ public function action_links( $links ) { array_unshift( $links, sprintf( '%s', esc_url( admin_url( 'admin.php?page=wc-settings&tab=' . self::TAB ) ), esc_html__( 'Settings', 'boring-quote' ) ) ); return $links; } /** * Product edit: hide button checkbox. */ public function product_field() { woocommerce_wp_checkbox( array( 'id' => '_bbquote_hide_button', 'label' => __( 'Hide quote button', 'boring-quote' ), 'description' => __( 'Do not show "Request a quote" for this product.', 'boring-quote' ), ) ); } /** * Save product checkbox. * * @param \WC_Product $product Product. */ public function save_product_field( $product ) { $nonce = isset( $_POST['woocommerce_meta_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_meta_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'woocommerce_save_data' ) || ! current_user_can( 'edit_product', $product->get_id() ) ) { return; } if ( isset( $_POST['_bbquote_hide_button'] ) ) { $product->update_meta_data( '_bbquote_hide_button', 'yes' ); } else { $product->delete_meta_data( '_bbquote_hide_button' ); } } }