|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ElevenLabs shared functionality |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Classifai\Providers\ElevenLabs; |
| 7 | + |
| 8 | +use WP_Error; |
| 9 | + |
| 10 | +use function Classifai\safe_wp_remote_get; |
| 11 | +use function Classifai\safe_wp_remote_post; |
| 12 | + |
| 13 | +trait ElevenLabs { |
| 14 | + |
| 15 | + /** |
| 16 | + * ElevenLabs base API URL |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + protected $api_url = 'https://api.elevenlabs.io/v1'; |
| 21 | + |
| 22 | + /** |
| 23 | + * ElevenLabs model path |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $model_path = 'models'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Build the API URL. |
| 31 | + * |
| 32 | + * @param string $path The path to the API endpoint. |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + public function get_api_url( string $path = '' ): string { |
| 36 | + /** |
| 37 | + * Filter the ElevenLabs API URL. |
| 38 | + * |
| 39 | + * @since x.x.x |
| 40 | + * @hook classifai_elevenlabs_api_url |
| 41 | + * |
| 42 | + * @param string $url The default API URL. |
| 43 | + * @param string $path The path to the API endpoint. |
| 44 | + * |
| 45 | + * @return string The API URL. |
| 46 | + */ |
| 47 | + return apply_filters( 'classifai_elevenlabs_api_url', trailingslashit( $this->api_url ) . $path, $path ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Make a request to the ElevenLabs API. |
| 52 | + * |
| 53 | + * Note instead of adding a new APIRequest class like we do elsewhere, |
| 54 | + * doing a lightweight version of that here instead. The goal is to |
| 55 | + * replace this with a more robust APIRequest class in the future, |
| 56 | + * based on the PHP AI SDK. |
| 57 | + * |
| 58 | + * @param string $url The URL for the request. |
| 59 | + * @param string $api_key The API key. |
| 60 | + * @param string $type The type of request. |
| 61 | + * @param array $options The options for the request. |
| 62 | + * @return array|WP_Error |
| 63 | + */ |
| 64 | + public function request( string $url, string $api_key = '', string $type = 'post', array $options = [] ) { |
| 65 | + /** |
| 66 | + * Filter the URL for the request. |
| 67 | + * |
| 68 | + * @since x.x.x |
| 69 | + * @hook classifai_elevenlabs_api_request_url |
| 70 | + * |
| 71 | + * @param string $url The URL for the request. |
| 72 | + * @param array $options The options for the request. |
| 73 | + * |
| 74 | + * @return string The URL for the request. |
| 75 | + */ |
| 76 | + $url = apply_filters( 'classifai_elevenlabs_api_request_url', $url, $options ); |
| 77 | + |
| 78 | + // Set our default options. |
| 79 | + $options = wp_parse_args( |
| 80 | + $options, |
| 81 | + [ |
| 82 | + 'timeout' => 90, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 83 | + ] |
| 84 | + ); |
| 85 | + |
| 86 | + /** |
| 87 | + * Filter the options for the request. |
| 88 | + * |
| 89 | + * @since x.x.x |
| 90 | + * @hook classifai_elevenlabs_api_request_options |
| 91 | + * |
| 92 | + * @param array $options The options for the request. |
| 93 | + * @param string $url The URL for the request. |
| 94 | + * |
| 95 | + * @return array The options for the request. |
| 96 | + */ |
| 97 | + $options = apply_filters( 'classifai_elevenlabs_api_request_options', $options, $url ); |
| 98 | + |
| 99 | + // Set our default headers. |
| 100 | + if ( empty( $options['headers'] ) ) { |
| 101 | + $options['headers'] = []; |
| 102 | + } |
| 103 | + |
| 104 | + if ( ! isset( $options['headers']['xi-api-key'] ) ) { |
| 105 | + $options['headers']['xi-api-key'] = $api_key; |
| 106 | + } |
| 107 | + |
| 108 | + if ( ! isset( $options['headers']['Content-Type'] ) ) { |
| 109 | + $options['headers']['Content-Type'] = 'application/json'; |
| 110 | + } |
| 111 | + |
| 112 | + // Make the request. |
| 113 | + if ( 'post' === $type ) { |
| 114 | + $response = safe_wp_remote_post( $url, $options ); |
| 115 | + } else { |
| 116 | + $response = safe_wp_remote_get( $url, $options ); |
| 117 | + } |
| 118 | + |
| 119 | + // Parse out the response. |
| 120 | + if ( is_wp_error( $response ) ) { |
| 121 | + return $response; |
| 122 | + } |
| 123 | + |
| 124 | + $body = wp_remote_retrieve_body( $response ); |
| 125 | + $code = wp_remote_retrieve_response_code( $response ); |
| 126 | + $json = json_decode( $body, true ); |
| 127 | + |
| 128 | + if ( 200 !== $code ) { |
| 129 | + if ( isset( $json['detail']['message'] ) ) { |
| 130 | + return new WP_Error( $json['detail']['status'] ?? $code, $json['detail']['message'] ?? esc_html__( 'An error occurred', 'classifai' ) ); |
| 131 | + } else { |
| 132 | + return new WP_Error( $code, esc_html__( 'An error occurred', 'classifai' ) ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if ( json_last_error() === JSON_ERROR_NONE ) { |
| 137 | + if ( empty( $json['error'] ) ) { |
| 138 | + return $json; |
| 139 | + } else { |
| 140 | + $message = $json['error']['message'] ?? esc_html__( 'An error occurred', 'classifai' ); |
| 141 | + return new WP_Error( $code, $message ); |
| 142 | + } |
| 143 | + } elseif ( ! empty( wp_remote_retrieve_response_message( $response ) ) ) { |
| 144 | + return new WP_Error( $code, wp_remote_retrieve_response_message( $response ) ); |
| 145 | + } else { |
| 146 | + return new WP_Error( 'Invalid JSON: ' . json_last_error_msg(), $body ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sanitize the API key, showing an error message if needed. |
| 152 | + * |
| 153 | + * @param array $new_settings Incoming settings, if any. |
| 154 | + * @param array $settings Current settings, if any. |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + public function sanitize_api_key_settings( array $new_settings = [], array $settings = [] ): array { |
| 158 | + $models = $this->get_models( $new_settings[ static::ID ]['api_key'] ?? '' ); |
| 159 | + |
| 160 | + $new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated']; |
| 161 | + $new_settings[ static::ID ]['models'] = $settings[ static::ID ]['models']; |
| 162 | + |
| 163 | + if ( is_wp_error( $models ) ) { |
| 164 | + $new_settings[ static::ID ]['authenticated'] = false; |
| 165 | + $new_settings[ static::ID ]['models'] = []; |
| 166 | + $error_message = $models->get_error_message(); |
| 167 | + |
| 168 | + add_settings_error( |
| 169 | + 'api_key', |
| 170 | + 'classifai-auth', |
| 171 | + $error_message, |
| 172 | + 'error' |
| 173 | + ); |
| 174 | + } else { |
| 175 | + $new_settings[ static::ID ]['authenticated'] = true; |
| 176 | + $new_settings[ static::ID ]['models'] = $models; |
| 177 | + } |
| 178 | + |
| 179 | + $new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] ); |
| 180 | + |
| 181 | + return $new_settings; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get the available models. |
| 186 | + * |
| 187 | + * @param string $api_key The API key. |
| 188 | + * @return array|WP_Error |
| 189 | + */ |
| 190 | + protected function get_models( string $api_key = '' ) { |
| 191 | + // Check that we have credentials before hitting the API. |
| 192 | + if ( empty( $api_key ) ) { |
| 193 | + return new WP_Error( 'auth', esc_html__( 'Please enter your ElevenLabs API key.', 'classifai' ) ); |
| 194 | + } |
| 195 | + |
| 196 | + $response = $this->request( $this->get_api_url( $this->model_path ), $api_key, 'get' ); |
| 197 | + |
| 198 | + if ( is_wp_error( $response ) ) { |
| 199 | + return $response; |
| 200 | + } |
| 201 | + |
| 202 | + // Get the model data we need. |
| 203 | + $models = array_map( |
| 204 | + fn( $model ) => [ |
| 205 | + 'id' => $model['model_id'] ?? '', |
| 206 | + 'display_name' => $model['name'] ?? '', |
| 207 | + ], |
| 208 | + $response |
| 209 | + ); |
| 210 | + |
| 211 | + return $models; |
| 212 | + } |
| 213 | +} |
0 commit comments