-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathOpenAI.php
More file actions
180 lines (154 loc) · 5.34 KB
/
OpenAI.php
File metadata and controls
180 lines (154 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* OpenAI shared functionality
*/
namespace Classifai\Providers\OpenAI;
use Classifai\Providers\OpenAI\APIRequest;
use WP_Error;
use function Classifai\get_all_post_statuses;
trait OpenAI {
/**
* OpenAI model URL
*
* @var string
*/
protected $model_url = 'https://api.openai.com/v1/models';
/**
* Sanitize the API key, showing an error message if needed.
*
* @param array $new_settings Incoming settings, if any.
* @param array $settings Current settings, if any.
* @return array
*/
public function sanitize_api_key_settings( array $new_settings = [], array $settings = [] ): array {
$authenticated = $this->authenticate_credentials( $new_settings[ static::ID ]['api_key'] ?? '' );
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
if ( is_wp_error( $authenticated ) ) {
$new_settings[ static::ID ]['authenticated'] = false;
$error_message = $authenticated->get_error_message();
// For response code 429, credentials are valid but rate limit is reached.
if ( 429 === (int) $authenticated->get_error_code() ) {
$new_settings[ static::ID ]['authenticated'] = true;
$error_message = str_replace( 'plan and billing details', '<a href="https://platform.openai.com/account/billing/overview" target="_blank" rel="noopener">plan and billing details</a>', $error_message );
} else {
$error_message = str_replace( 'https://platform.openai.com/account/api-keys', '<a href="https://platform.openai.com/account/api-keys" target="_blank" rel="noopener">https://platform.openai.com/account/api-keys</a>', $error_message );
}
add_settings_error(
'api_key',
'classifai-auth',
$error_message,
'error'
);
} else {
$new_settings[ static::ID ]['authenticated'] = true;
}
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
return $new_settings;
}
/**
* Authenticate our credentials.
*
* @param string $api_key Api Key.
* @return bool|WP_Error
*/
protected function authenticate_credentials( string $api_key = '' ) {
// Check that we have credentials before hitting the API.
if ( empty( $api_key ) ) {
return new WP_Error( 'auth', esc_html__( 'Please enter your OpenAI API key.', 'classifai' ) );
}
// Make request to ensure credentials work.
$request = new APIRequest( $api_key );
$response = $request->get( $this->model_url, [ 'use_vip' => true ] );
return ! is_wp_error( $response ) ? true : $response;
}
/**
* Get available post types to use in settings.
*
* @return array
*/
public function get_post_types_for_settings(): array {
$post_types = [];
$post_type_objs = get_post_types( [], 'objects' );
$post_type_objs = array_filter( $post_type_objs, 'is_post_type_viewable' );
unset( $post_type_objs['attachment'] );
foreach ( $post_type_objs as $post_type ) {
$post_types[ $post_type->name ] = $post_type->label;
}
/**
* Filter post types shown in settings.
*
* @since 2.2.0
* @hook classifai_openai_settings_post_types
*
* @param array $post_types Array of post types to show in settings.
* @param object $this Current instance of the class.
*
* @return array Array of post types.
*/
return apply_filters( 'classifai_openai_settings_post_types', $post_types, $this );
}
/**
* Get available post statuses to use in settings.
*
* @return array
*/
public function get_post_statuses_for_settings(): array {
$post_statuses = get_all_post_statuses();
/**
* Filter post statuses shown in settings.
*
* @since 2.2.0
* @hook classifai_openai_settings_post_statuses
*
* @param array $post_statuses Array of post statuses to show in settings.
* @param object $this Current instance of the class.
*
* @return array Array of post statuses.
*/
return apply_filters( 'classifai_openai_settings_post_statuses', $post_statuses, $this );
}
/**
* Get available taxonomies to use in settings.
*
* @return array
*/
public function get_taxonomies_for_settings(): array {
$taxonomies = get_taxonomies( [], 'objects' );
$taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' );
$supported = [];
foreach ( $taxonomies as $taxonomy ) {
$supported[ $taxonomy->name ] = $taxonomy->labels->singular_name;
}
/**
* Filter taxonomies shown in settings.
*
* @since 2.2.0
* @hook classifai_openai_settings_taxonomies
*
* @param array $supported Array of supported taxonomies.
* @param object $this Current instance of the class.
*
* @return array Array of taxonomies.
*/
return apply_filters( 'classifai_openai_settings_taxonomies', $supported, $this );
}
/**
* The list of supported taxonomies.
*
* @param \Classifai\Features\Feature $feature Feature to check.
* @return array
*/
public function get_supported_taxonomies( \Classifai\Features\Feature $feature ): array {
$provider = $feature->get_feature_provider_instance();
$settings = $feature->get_settings( $provider::ID );
$taxonomies = [];
if ( ! empty( $settings ) && isset( $settings['taxonomies'] ) ) {
foreach ( $settings['taxonomies'] as $taxonomy => $enabled ) {
if ( ! empty( $enabled ) ) {
$taxonomies[] = $taxonomy;
}
}
}
return $taxonomies;
}
}