$id, 'embed_token' => $token, ); } } $id = (string) get_option( self::OPTION_ID, '' ); $token = (string) get_option( self::OPTION_TOKEN, '' ); if ( '' !== $id && '' !== $token ) { return array( 'chatbot_id' => $id, 'embed_token' => $token, ); } // Last resort: read options table directly (bypasses a broken object cache). return self::get_credentials_from_db(); } /** * @return array{chatbot_id: string, embed_token: string} */ private static function get_credentials_from_db() { global $wpdb; if ( ! $wpdb ) { return array( 'chatbot_id' => '', 'embed_token' => '', ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $wpdb->get_results( $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name IN (%s, %s, %s)", self::OPTION_BUNDLE, self::OPTION_ID, self::OPTION_TOKEN ), OBJECT_K ); if ( empty( $rows ) ) { return array( 'chatbot_id' => '', 'embed_token' => '', ); } if ( isset( $rows[ self::OPTION_BUNDLE ] ) ) { $bundle = maybe_unserialize( $rows[ self::OPTION_BUNDLE ]->option_value ); if ( is_array( $bundle ) ) { $id = isset( $bundle['chatbot_id'] ) ? (string) $bundle['chatbot_id'] : ''; $token = isset( $bundle['embed_token'] ) ? (string) $bundle['embed_token'] : ''; if ( '' !== $id && '' !== $token ) { return array( 'chatbot_id' => $id, 'embed_token' => $token, ); } } } $id = isset( $rows[ self::OPTION_ID ] ) ? (string) $rows[ self::OPTION_ID ]->option_value : ''; $token = isset( $rows[ self::OPTION_TOKEN ] ) ? (string) $rows[ self::OPTION_TOKEN ]->option_value : ''; return array( 'chatbot_id' => $id, 'embed_token' => $token, ); } /** * Persist credentials (autoload on for reliable admin-ajax reads). * * @param string $chatbot_id Chatbot id. * @param string $embed_token Embed token. * @return bool True when values are readable after save. */ public static function save( $chatbot_id, $embed_token ) { $chatbot_id = sanitize_text_field( (string) $chatbot_id ); $embed_token = sanitize_text_field( (string) $embed_token ); if ( '' === $chatbot_id || '' === $embed_token ) { return false; } $bundle = array( 'chatbot_id' => $chatbot_id, 'embed_token' => $embed_token, ); update_option( self::OPTION_BUNDLE, $bundle, true ); update_option( self::OPTION_ID, $chatbot_id, true ); update_option( self::OPTION_TOKEN, $embed_token, true ); // Bust common object-cache staleness after write. wp_cache_delete( self::OPTION_BUNDLE, 'options' ); wp_cache_delete( self::OPTION_ID, 'options' ); wp_cache_delete( self::OPTION_TOKEN, 'options' ); wp_cache_delete( 'alloptions', 'options' ); $check = self::get_credentials(); return ( $check['chatbot_id'] === $chatbot_id && $check['embed_token'] === $embed_token ); } /** * Remove stored credentials. */ public static function clear() { delete_option( self::OPTION_BUNDLE ); delete_option( self::OPTION_ID ); delete_option( self::OPTION_TOKEN ); } /** * Preferred site_url for studio API (must match connected host). * * @return string */ public static function site_url() { $home = home_url( '/' ); $site = site_url( '/' ); // Prefer home_url; fall back if empty. return $home ? $home : $site; } }