token_validator = $token_validator; $this->init_hooks(); } /** * Initialize WordPress hooks */ private function init_hooks() { add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'admin_init', array( $this, 'settings_init' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); add_action( 'update_option_fivechat_website_token', array( $this, 'clear_token_cache' ), 10, 2 ); add_action( 'admin_init', array( $this, 'capture_settings_errors' ), 999 ); } /** * Add the settings page to the WordPress admin menu */ public function add_admin_menu() { add_options_page( '5chat Settings', '5chat', 'manage_options', 'fivechat-settings', array( $this, 'render_options_page' ) ); } /** * Register settings, sections, and fields for the settings page */ public function settings_init() { // Register the setting with proper sanitization callback register_setting( 'fivechat_settings_group', 'fivechat_website_token', array( 'sanitize_callback' => array( $this, 'sanitize_token' ), 'show_in_rest' => false, ) ); // Add a settings section add_settings_section( 'fivechat_main_section', null, // No title needed for a single section null, // No description callback needed 'fivechat-settings' ); // Add the Website Token field add_settings_field( 'fivechat_website_token_field', __( 'Website Token', '5chat-blazing-fast-live-chat' ), array( $this, 'render_token_field' ), 'fivechat-settings', 'fivechat_main_section', array( 'label_for' => 'fivechat_website_token_id' ) ); } /** * Sanitization callback for the website token * * @param string $input The input token value. * @return string Sanitized token value. */ public function sanitize_token( $input ) { // Trim whitespace and sanitize as a simple text field $sanitized = sanitize_text_field( trim( $input ) ); // If empty, allow it (user can clear the token) if ( empty( $sanitized ) ) { return $sanitized; } // Basic format validation - token should be alphanumeric with possible hyphens/underscores if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $sanitized ) ) { add_settings_error( 'fivechat_website_token', 'invalid_token_format', __( 'Invalid Website Token format. Please use only letters, numbers, hyphens, and underscores.', '5chat-blazing-fast-live-chat' ), 'error' ); // Return the previous value if validation fails return get_option( 'fivechat_website_token' ); } // Validate token with 5chat API $validation_result = $this->token_validator->validate_token( $sanitized ); if ( ! $validation_result['valid'] ) { add_settings_error( 'fivechat_website_token', 'invalid_token_api', sprintf( /* translators: %s: Error message from the token validation API */ __( 'Token validation failed: %s', '5chat-blazing-fast-live-chat' ), $validation_result['error'] ), 'error' ); // Return the previous value if API validation fails return get_option( 'fivechat_website_token' ); } // Token is valid - add success message only if this is a different token $old_token = get_option( 'fivechat_website_token' ); if ( $old_token !== $sanitized ) { add_settings_error( 'fivechat_website_token', 'token_validated', __( 'Website Token validated successfully! Your 5chat widget is now active.', '5chat-blazing-fast-live-chat' ), 'success' ); } return $sanitized; } /** * Render the HTML for the Website Token input field */ public function render_token_field() { $option_value = get_option( 'fivechat_website_token' ); ?>

should_suppress_default_notices(); ?>

display_custom_settings_errors( $suppress_default ); ?>

1
2

3

token_validator->clear_token_cache( $old_value ); } // Clear cache for new token if ( ! empty( $new_value ) ) { $this->token_validator->clear_token_cache( $new_value ); } } } /** * Render the logo image */ private function render_logo_image() { // WordPress-compliant image display using proper attribute building $image_url = FIVECHAT_PLUGIN_URL . 'assets/icon-128x128.png'; $image_attributes = array( 'src' => esc_url( $image_url ), 'width' => '64', 'height' => '64', 'alt' => esc_attr__( '5chat', '5chat-blazing-fast-live-chat' ), 'class' => 'fivechat-logo-icon', 'loading' => 'lazy', ); // Build image tag with proper escaping printf( '', wp_kses_post( $this->build_image_attributes( $image_attributes ) ) ); } /** * Build image attributes string * * @param array $attributes Image attributes. * @return string Properly formatted attributes string. */ private function build_image_attributes( $attributes ) { $attribute_strings = array(); foreach ( $attributes as $key => $value ) { $attribute_strings[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( $value ) ); } return implode( ' ', $attribute_strings ); } /** * Display custom settings errors * * @param bool $suppress_default Whether to suppress WordPress default messages. */ private function display_custom_settings_errors( $suppress_default = false ) { // Use captured errors instead of getting them again $settings_errors = $this->captured_errors; if ( empty( $settings_errors ) ) { return; } // Check if we have any error messages (not success) $has_errors = false; foreach ( $settings_errors as $error ) { if ( 'error' === $error['type'] ) { $has_errors = true; break; } } // If we have errors or are suppressing defaults, only show error messages if ( $has_errors || $suppress_default ) { foreach ( $settings_errors as $error ) { if ( 'error' === $error['type'] ) { printf( '

%s

', wp_kses_post( $error['message'] ) ); } } } else { // No errors, show all messages (including success) foreach ( $settings_errors as $error ) { $notice_class = 'error' === $error['type'] ? 'notice-error' : 'notice-success'; printf( '

%s

', esc_attr( $notice_class ), wp_kses_post( $error['message'] ) ); } } } /** * Check if we should suppress WordPress default success message * * @return bool True if we should suppress default notices, false otherwise. */ private function should_suppress_default_notices() { // Use captured errors instead of getting them again foreach ( $this->captured_errors as $error ) { if ( 'error' === $error['type'] ) { return true; } } return false; } /** * Capture settings errors early to prevent WordPress from showing them */ public function capture_settings_errors() { // Only capture on our settings page - use screen detection instead of $_GET $screen = get_current_screen(); if ( ! $screen || 'settings_page_fivechat-settings' !== $screen->id ) { return; } // Capture our settings errors $this->captured_errors = get_settings_errors( 'fivechat_website_token' ); // Clear them from WordPress to prevent default display if ( ! empty( $this->captured_errors ) ) { global $wp_settings_errors; unset( $wp_settings_errors['fivechat_website_token'] ); } } }