providers = $providers; $this->settings = $settings; $this->resolver = $resolver; $this->log = $log; } /** * Registers the hook. * * @return void */ public function register(): void { add_filter( 'pre_http_request', array( $this, 'inspect' ), 1, 3 ); } /** * Inspects one outbound request. * * @param false|array|WP_Error $preempt Short-circuit value from an earlier filter. * @param array $args Request arguments. * @param string $url Request URL. * @return false|array|WP_Error The original value, or a WP_Error when blocking. */ public function inspect( $preempt, $args, $url ) { // Another handler has already decided; do not override it. if ( false !== $preempt ) { return $preempt; } $mode = $this->get_mode(); if ( Settings::OUTBOUND_OFF === $mode ) { return $preempt; } $provider = $this->providers->match_url( (string) $url, $this->get_enabled_provider_ids() ); if ( null === $provider ) { return $preempt; } $host = $this->get_host( (string) $url ); $source = $this->resolver->resolve(); $blocked = Settings::OUTBOUND_BLOCK === $mode; $this->log->record( $host, $source, $provider['id'], $blocked ? BlockedCalls::BLOCKED : BlockedCalls::OBSERVED ); /** * Fires when an outgoing request to an AI provider is seen. * * @since 1.0.0 * * @param string $host The host contacted. * @param string $source Slug of the plugin, theme or component responsible. * @param string $provider Provider identifier. * @param bool $blocked Whether the request was refused. */ do_action( 'aiblackout_outbound_call_seen', $host, $source, $provider['id'], $blocked ); if ( ! $blocked ) { return $preempt; } return new WP_Error( self::ERROR_CODE, sprintf( /* translators: %s: Host name, for example api.openai.com. */ __( 'Blocked by AI Blackout: this site does not permit outgoing requests to %s.', 'ai-blackout' ), $host ), array( 'status' => 403, 'host' => $host, 'source' => $source, 'provider' => $provider['id'], ) ); } /** * Returns the active mode, accounting for the global kill switch. * * @return string One of the Settings::OUTBOUND_* constants. */ public function get_mode(): string { if ( aiblackout_is_disabled() ) { return Settings::OUTBOUND_OFF; } if ( ! $this->settings->is_blackout_enabled( Settings::BLACKOUT_OUTBOUND ) ) { return Settings::OUTBOUND_OFF; } return $this->settings->get_outbound_mode(); } /** * Returns the provider ids currently being watched. * * @return string[] Provider identifiers. */ public function get_enabled_provider_ids(): array { $stored = $this->settings->get_blackout( 'providers', null ); if ( ! is_array( $stored ) ) { return $this->providers->get_default_ids(); } $valid = array(); foreach ( $this->providers->get_providers() as $provider ) { if ( in_array( $provider['id'], $stored, true ) ) { $valid[] = $provider['id']; } } return $valid; } /** * Extracts the host from a URL. * * @param string $url Request URL. * @return string Host, or an empty string. */ private function get_host( string $url ): string { $host = wp_parse_url( $url, PHP_URL_HOST ); return is_string( $host ) ? strtolower( $host ) : ''; } }