class. */ private const CLASSES = array( 'google' => Providers\Google::class, 'deepl' => Providers\Deepl::class, 'gemini' => Providers\Gemini::class, 'claude' => Providers\Claude::class, 'wordpress' => Providers\WordPressAi::class, 'pseudo' => Providers\Pseudo::class, ); public function __construct( private readonly SettingsRepository $settings, private readonly Defaults $defaults, private readonly HttpClient $http = new HttpClient() ) {} /** * The provider with this id, or the one saved in the settings. * Filter blocklingo_pre_provider supplies a custom Provider (own engine, tests). * $key and $model: typed in the settings form but not saved yet ("Load * models", "Test connection"); empty means the saved ones. */ public function make( ?string $id = null, string $key = '', string $model = '' ): Provider|WP_Error { $settings = $this->settings->get(); $id = $id ? $id : $settings->provider(); /** * Supply your own machine translation engine. * * @param Provider|null $provider Return a Provider to use it. * @param string $id Provider id from the settings. */ $custom = apply_filters( 'blocklingo_pre_provider', null, $id ); if ( $custom instanceof Provider ) { return $custom; } if ( '' === $id ) { return new WP_Error( 'blocklingo_mt_off', __( 'Machine translation is not configured. Choose a provider in Settings > Blocklingo.', 'blocklingo' ) ); } $available = $this->defaults->providers(); if ( ! isset( $available[ $id ], self::CLASSES[ $id ] ) ) { return new WP_Error( 'blocklingo_mt_unknown', __( 'Unknown translation provider.', 'blocklingo' ) ); } $key = '' !== trim( $key ) ? trim( $key ) : $settings->api_key( $id ); if ( '' === $key ) { return new WP_Error( 'blocklingo_mt_nokey', sprintf( /* translators: %s: provider name. */ __( 'API key for %s is missing.', 'blocklingo' ), $available[ $id ]['label'] ) ); } $model = trim( $model ); $config = new ProviderConfig( $key, '' !== $model ? $model : $settings->model( $id ), (array) preg_split( '/\r\n|\r|\n/', $settings->glossary() ), $settings->instructions() ); $class = self::CLASSES[ $id ]; return new $class( $config, $this->http ); } }