'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => $this->default_settings(), 'show_in_rest' => false, ) ); } /** * Sanitize settings to a known schema. * * @param mixed $input Raw settings value. * @return array */ public function sanitize_settings( $input ) { $defaults = $this->default_settings(); $input = is_array( $input ) ? $input : array(); $phone = $this->sanitize_phone_number( isset( $input['phone'] ) ? $input['phone'] : '' ); $label = isset( $input['button_label'] ) ? sanitize_text_field( (string) $input['button_label'] ) : ''; $intro = isset( $input['intro'] ) ? sanitize_textarea_field( (string) $input['intro'] ) : ''; if ( '' !== $phone && ! $this->is_valid_phone_number( $phone ) ) { add_settings_error( self::OPTION_NAME, 'aetos_qoc_invalid_phone', __( 'Enter an international WhatsApp number containing 7 to 15 digits, including the country code.', 'aetos-quick-order-chat' ), 'error' ); } return array( 'phone' => $phone, 'button_label' => '' !== $label ? $label : $defaults['button_label'], 'intro' => '' !== $intro ? $intro : $defaults['intro'], 'product_enabled' => ! empty( $input['product_enabled'] ), 'cart_enabled' => ! empty( $input['cart_enabled'] ), 'include_sku' => ! empty( $input['include_sku'] ), 'include_price' => ! empty( $input['include_price'] ), 'include_link' => ! empty( $input['include_link'] ), 'new_tab' => ! empty( $input['new_tab'] ), ); } /** * Keep digits only and convert an international 00 prefix. * * @param mixed $phone Raw phone value. * @return string */ public function sanitize_phone_number( $phone ) { $phone = preg_replace( '/\D+/', '', (string) $phone ); $phone = is_string( $phone ) ? $phone : ''; if ( 0 === strpos( $phone, '00' ) ) { $phone = substr( $phone, 2 ); } return $phone; } /** * Validate the destination against the E.164 maximum length. * * @param string $phone Digits-only number. * @return bool */ public function is_valid_phone_number( $phone ) { return 1 === preg_match( '/^\d{7,15}$/', $phone ); } /** * Return settings merged into a fixed, typed shape. * * @return array */ public function get_settings() { $settings = get_option( self::OPTION_NAME, array() ); $settings = is_array( $settings ) ? $settings : array(); $settings = wp_parse_args( $settings, $this->default_settings() ); return array( 'phone' => (string) $settings['phone'], 'button_label' => (string) $settings['button_label'], 'intro' => (string) $settings['intro'], 'product_enabled' => (bool) $settings['product_enabled'], 'cart_enabled' => (bool) $settings['cart_enabled'], 'include_sku' => (bool) $settings['include_sku'], 'include_price' => (bool) $settings['include_price'], 'include_link' => (bool) $settings['include_link'], 'new_tab' => (bool) $settings['new_tab'], ); } /** * Register the settings page under Settings. * * @return void */ public function register_settings_page() { add_options_page( __( 'Aetos Quick Order Chat for WooCommerce', 'aetos-quick-order-chat' ), __( 'Aetos Quick Order Chat', 'aetos-quick-order-chat' ), 'manage_options', self::SETTINGS_PAGE_SLUG, array( $this, 'render_settings_page' ) ); } /** * Render the plugin settings screen. * * @return void */ public function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = $this->get_settings(); ?>

render_settings_rows( $settings ); ?>
render_external_service_notice(); ?>
$links Existing action links. * @return array */ public function add_settings_link( $links ) { $link = sprintf( '%2$s', esc_url( admin_url( 'options-general.php?page=' . self::SETTINGS_PAGE_SLUG ) ), esc_html__( 'Settings', 'aetos-quick-order-chat' ) ); array_unshift( $links, $link ); return $links; } /** * Return conservative initial settings. * * @return array */ private function default_settings() { return array( 'phone' => '', 'button_label' => __( 'Order via WhatsApp', 'aetos-quick-order-chat' ), 'intro' => __( 'Hello, I would like to order:', 'aetos-quick-order-chat' ), 'product_enabled' => true, 'cart_enabled' => true, 'include_sku' => true, 'include_price' => true, 'include_link' => true, 'new_tab' => true, ); } /** * Render all settings table rows. * * @param array $settings Current settings. * @return void */ private function render_settings_rows( $settings ) { $this->render_phone_row( $settings ); $this->render_text_row( $settings, 'button_label', __( 'Button label', 'aetos-quick-order-chat' ), __( 'Visible text used for every order-chat button.', 'aetos-quick-order-chat' ) ); $this->render_intro_row( $settings ); foreach ( $this->checkbox_fields() as $key => $field ) { $this->render_checkbox_row( $settings, $key, $field['label'], $field['description'] ); } $this->render_status_row( $settings ); $this->render_preview_row( $settings ); } /** * Define checkbox labels and descriptions. * * @return array> */ private function checkbox_fields() { return array( 'product_enabled' => array( 'label' => __( 'Single product button', 'aetos-quick-order-chat' ), 'description' => __( 'Show the order-chat button after the WooCommerce add-to-cart form.', 'aetos-quick-order-chat' ), ), 'cart_enabled' => array( 'label' => __( 'Cart button', 'aetos-quick-order-chat' ), 'description' => __( 'Show one button on classic carts and WooCommerce Cart Blocks.', 'aetos-quick-order-chat' ), ), 'include_sku' => array( 'label' => __( 'Include SKU', 'aetos-quick-order-chat' ), 'description' => __( 'Add a product SKU when one exists.', 'aetos-quick-order-chat' ), ), 'include_price' => array( 'label' => __( 'Include prices', 'aetos-quick-order-chat' ), 'description' => __( 'Add the visible product price or cart totals.', 'aetos-quick-order-chat' ), ), 'include_link' => array( 'label' => __( 'Include links', 'aetos-quick-order-chat' ), 'description' => __( 'Add the product URL or current cart URL.', 'aetos-quick-order-chat' ), ), 'new_tab' => array( 'label' => __( 'Open in a new tab', 'aetos-quick-order-chat' ), 'description' => __( 'Keep the store page open while WhatsApp opens separately.', 'aetos-quick-order-chat' ), ), ); } /** * Render the destination phone row. * * @param array $settings Current settings. * @return void */ private function render_phone_row( $settings ) { $field_id = 'aetos-qoc-phone'; ?>

$settings Current settings. * @param string $key Setting key. * @param string $label Visible label. * @param string $description Visible description. * @return void */ private function render_text_row( $settings, $key, $label, $description ) { $field_id = 'aetos-qoc-' . $key; ?>

$settings Current settings. * @return void */ private function render_intro_row( $settings ) { $field_id = 'aetos-qoc-intro'; ?>

$settings Current settings. * @param string $key Setting key. * @param string $label Visible label. * @param string $description Visible description. * @return void */ private function render_checkbox_row( $settings, $key, $label, $description ) { $field_id = 'aetos-qoc-' . $key; $description_id = $field_id . '-description'; ?> />

$settings Current settings. * @return void */ private function render_status_row( $settings ) { $is_ready = $this->is_valid_phone_number( $settings['phone'] ); ?> $settings Current settings. * @return void */ private function render_preview_row( $settings ) { $field_id = 'aetos-qoc-message-preview'; $preview = $settings['intro'] . "\n\n"; $preview .= __( 'Product', 'aetos-quick-order-chat' ) . ': ' . __( 'Example product', 'aetos-quick-order-chat' ) . "\n"; $preview .= __( 'Quantity', 'aetos-quick-order-chat' ) . ': 1'; ?>

wa.me with the visible product or cart message. WhatsApp then processes that request under its Terms and Privacy Policy.', 'aetos-quick-order-chat' ), array( 'code' => array(), 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ) ); ?>