service_client = new AI_Search_Service(); } /** * Get quota data (from cache or API) * * @param bool $force_refresh Force refresh from API * @return array|null Quota data or null if unavailable */ public function get_quota_data( $force_refresh = false ) { // Check if using AI Service provider $provider = get_option( 'ai_search_provider', 'ai_service' ); if ( $provider !== 'ai_service' ) { return null; // Not using AI Service } // Return cached data unless force refresh if ( ! $force_refresh ) { $cached_data = get_transient( self::QUOTA_CACHE_KEY ); if ( $cached_data !== false ) { return $cached_data; } } // Fetch fresh data from API $quota_data = $this->fetch_quota_from_api(); // Cache the data if valid if ( $quota_data && $this->validate_quota_data( $quota_data ) ) { set_transient( self::QUOTA_CACHE_KEY, $quota_data, self::CACHE_DURATION ); return $quota_data; } return null; } /** * Fetch quota data from API * * @return array|null Raw quota data from API */ private function fetch_quota_from_api() { $quota_data = $this->service_client->get_quota_info(); if ( $quota_data === false ) { return null; } return $quota_data; } /** * Validate quota data structure * * @param array $data Data to validate * @return bool True if valid */ private function validate_quota_data( $data ) { if ( ! is_array( $data ) ) { return false; } $required_keys = [ 'total_embeddings_used', 'total_embeddings_limit', 'daily_usage', 'daily_limit', 'daily_quota_exceeded', 'total_quota_exceeded', ]; foreach ( $required_keys as $key ) { if ( ! isset( $data[ $key ] ) ) { return false; } } return true; } /** * Get default/placeholder quota data * * @return array Default quota structure */ public function get_default_quota_data() { return [ 'total_embeddings_used' => 0, 'total_embeddings_limit' => 10000, 'daily_usage' => 0, 'daily_limit' => 1000, 'daily_quota_exceeded' => false, 'total_quota_exceeded' => false, ]; } /** * Calculate percentage for progress bars * * @param int $used Used amount * @param int $limit Total limit * @return float Percentage (0-100) */ public function calculate_percentage( $used, $limit ) { if ( $limit <= 0 ) { return 0; } return ( $used / $limit ) * 100; } /** * Get status class for quota item * * @param bool $quota_exceeded Whether quota is exceeded * @return string CSS class name */ public function get_status_class( $quota_exceeded ) { return $quota_exceeded ? 'quota-exceeded' : 'quota-ok'; } /** * Clear quota cache * * @return bool True on success */ public function clear_cache() { return delete_transient( self::QUOTA_CACHE_KEY ); } /** * Check if quota data is available * * @return bool True if data exists in cache */ public function has_cached_data() { return get_transient( self::QUOTA_CACHE_KEY ) !== false; } /** * Get last update timestamp * * @return int|null Timestamp or null if no cache */ public function get_last_update_time() { $timeout_option = '_transient_timeout_' . self::QUOTA_CACHE_KEY; $timeout = get_option( $timeout_option ); if ( $timeout === false ) { return null; } // Calculate when the cache was created return $timeout - self::CACHE_DURATION; } /** * Format last update time for display * * @return string Formatted time string */ public function get_last_update_display() { $last_update = $this->get_last_update_time(); if ( $last_update === null ) { return __( 'Never updated', 'ai-search' ); } return sprintf( /* translators: %s: human-readable time difference */ __( 'Last updated %s ago', 'ai-search' ), human_time_diff( $last_update, current_time( 'timestamp' ) ) ); } }