display_custom_settings_errors( $suppress_default ); ?>
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(); ?> 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( '
%s
%s