api_key = '' !== $api_key ? $api_key : (string) get_option( $this->get_api_key_option() ); } /** * Option name that stores the API key. * * @since 2.3.0 * @return string */ abstract protected function get_api_key_option(): string; /** * Option name that stores the selected model. * * @since 2.3.0 * @return string */ abstract protected function get_model_option(): string; /** * Model used when no model option is saved. * * @since 2.3.0 * @return string */ abstract protected function get_fallback_model(): string; /** * Request headers carrying the provider credentials. * * @since 2.3.0 * @return array */ abstract protected function get_auth_headers(): array; /** * Whether the provider has an API key. * * @since 2.3.0 * @return bool */ public function is_configured(): bool { return '' !== $this->api_key; } /** * Get the default model, filterable per provider. * * @since 2.3.0 * @return string */ public function get_default_model(): string { $model = (string) get_option( $this->get_model_option() ); if ( '' === $model ) { $model = $this->get_fallback_model(); } $model = $this->replace_retired_model( $model ); /** * Filter the default model used by a provider. * * @param string $model The model ID. * @param string $provider The provider slug. * * @since 2.3.0 */ return (string) apply_filters( 'aicw_default_ai_model', $model, $this->get_slug() ); } /** * Models the provider has retired or deprecated, mapped to their replacement. * * A saved setting pointing at one of these keeps working: the model is * swapped for its replacement at request time. * * @since 2.5.0 * @return array Retired model ID => replacement model ID. */ public function get_retired_models(): array { return array(); } /** * Swap a retired model for its replacement. * * @param string $model The model ID. * * @since 2.5.0 * @return string The model ID, or its replacement when retired. */ public function replace_retired_model( string $model ): string { $retired = $this->get_retired_models(); return isset( $retired[ $model ] ) ? $retired[ $model ] : $model; } /** * Send a GET request to the provider API. * * @param string $endpoint The endpoint path (no leading slash). * @param int $timeout Request timeout in seconds. * * @since 2.3.0 * @return array Decoded response body, or array with an `error` key on failure. */ protected function get_request( string $endpoint, int $timeout = 30 ): array { $response = wp_remote_get( esc_url_raw( $this->api_url . $endpoint ), array( 'timeout' => $timeout, 'headers' => array_merge( array( 'Content-Type' => 'application/json' ), $this->get_auth_headers() ), ) ); return $this->parse_response( $response ); } /** * Send a POST request to the provider API. * * @param string $endpoint API endpoint. * @param array $data Request body. * @param int $timeout Request timeout in seconds. * * @since 2.3.0 * @return array Decoded response body, or array with an `error` key on failure. */ protected function post_request( string $endpoint, array $data, int $timeout = 300 ): array { // Allow long-running generation requests to finish (e.g. full articles). if ( $timeout > ini_get( 'max_execution_time' ) && function_exists( 'set_time_limit' ) ) { set_time_limit( $timeout ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Long-running AI generation runs in cron/AJAX context. } $response = wp_remote_post( esc_url_raw( $this->api_url . $endpoint ), array( 'timeout' => $timeout, 'headers' => array_merge( array( 'Content-Type' => 'application/json' ), $this->get_auth_headers() ), 'body' => wp_json_encode( $data ), ) ); return $this->parse_response( $response ); } /** * Normalize a wp_remote_* response into a decoded array. * * @param array|\WP_Error $response Raw response. * * @since 2.3.0 * @return array */ private function parse_response( $response ): array { if ( is_wp_error( $response ) ) { return array( 'error' => $response->get_error_message() ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $body ) ) { return array( 'error' => sprintf( /* translators: %s: Provider label. */ esc_html__( 'Invalid JSON response from the %s API.', 'ai-content-writer' ), $this->get_label() ), ); } return $body; } /** * Extract a readable error message from a decoded API response. * * @param array $response Decoded response. * @param string $fallback Fallback message. * * @since 2.3.0 * @return string */ protected function get_response_error( array $response, string $fallback = '' ): string { if ( isset( $response['error'] ) ) { if ( is_array( $response['error'] ) && isset( $response['error']['message'] ) ) { return (string) $response['error']['message']; } if ( is_string( $response['error'] ) ) { return $response['error']; } } return '' !== $fallback ? $fallback : esc_html__( 'An unexpected error occurred while generating the content.', 'ai-content-writer' ); } /** * WP_Error for a provider that has no API key configured. * * @since 2.3.0 * @return \WP_Error */ protected function not_configured_error(): \WP_Error { return new \WP_Error( 'aicw_api_key_not_set', sprintf( /* translators: %s: Provider label. */ esc_html__( 'Please configure the API settings. A valid %s API key is required to generate the content.', 'ai-content-writer' ), $this->get_label() ) ); } }