self::MAX_RETRIES ) { return $response; } self::backoff( $attempt ); continue; } $code = wp_remote_retrieve_response_code( $response ); if ( 429 === $code ) { ++$attempt; if ( ! $retry || $attempt > self::MAX_RETRIES ) { return $response; } self::backoff( $attempt ); continue; } // 401/403 oder jeder andere Code (inkl. 200 mit "keine Credits" im Body): // sofort zurück, kein Retry. return $response; } while ( true ); } /** * Wartet zwischen zwei Wiederholungsversuchen. Die Verzögerung ist über den Filter * `image_ai_seo_retry_delay_ms` überschreibbar (Tests setzen ihn auf 0). * * @param int $attempt Nummer des aktuellen Versuchs (ab 1). */ private static function backoff( $attempt ) { $delay_ms = apply_filters( 'image_ai_seo_retry_delay_ms', self::RETRY_BASE_DELAY_MS * $attempt, $attempt ); usleep( $delay_ms * 1000 ); } /** * Resolves the currently active API key per D-01: a site-level override * (this site's own `image_ai_seo_api_key` option) always wins over the * network-wide key (`image_ai_seo_network_api_key`, a network option). * * D-01/Pitfall 4 (05-RESEARCH.md): this is the ONLY place in the codebase * permitted to resolve this precedence. Every caller that needs the active * key -- for an outbound HTTP request or purely for display -- MUST go * through this method instead of reading either option directly. A second, * independent implementation of this fallback (however trivial it looks) * is the exact failure mode Pitfall 4 warns about. * * @return array{key: string, scope: string} `scope` is one of 'site', * 'network', or 'none'. */ public static function get_active_api_key() { $site_key = (string) get_option( 'image_ai_seo_api_key', '' ); if ( '' !== $site_key ) { return array( 'key' => $site_key, 'scope' => 'site', ); } $network_key = (string) get_site_option( 'image_ai_seo_network_api_key', '' ); if ( '' !== $network_key ) { return array( 'key' => $network_key, 'scope' => 'network', ); } return array( 'key' => '', 'scope' => 'none', ); } }