Repository navigation
Expand file tree
/
Copy pathConnectors_Command.php
More file actions
258 lines (232 loc) · 8.5 KB
/
Copy pathConnectors_Command.php
File metadata and controls
258 lines (232 loc) · 8.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace WP_CLI\AI;
use WP_CLI;
use WP_CLI_Command;
/**
* Lists and retrieves information about AI connectors.
*
* ## EXAMPLES
*
* # List all available connectors
* $ wp connectors list
*
* # Get details about a specific connector
* $ wp connectors get openai
*/
class Connectors_Command extends WP_CLI_Command {
/**
* Default fields for list output.
*
* @var string[]
*/
protected $default_fields = [
'id',
'name',
'description',
'status',
];
/**
* Lists all available AI connectors.
*
* ## OPTIONS
*
* [--status=<status>]
* : Filter connectors by status.
* ---
* options:
* - connected
* - active
* - installed
* - uninstalled
* ---
*
* [--fields=<fields>]
* : Comma-separated list of fields to include in the output.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # List all connectors
* $ wp connectors list
* +-----------+-----------+-----------------------------------------------+-------------+
* | id | name | description | status |
* +-----------+-----------+-----------------------------------------------+-------------+
* | anthropic | Anthropic | Text generation with Claude. | uninstalled |
* | google | Google | Text and image generation with Gemini... | uninstalled |
* | openai | OpenAI | Text and image generation with GPT and Dall-E | connected |
* +-----------+-----------+-----------------------------------------------+-------------+
*
* # List only connected connectors
* $ wp connectors list --status=connected
*
* @subcommand list
* @when after_wp_load
*
* @param string[] $args Positional arguments. Unused.
* @param array{status: string, fields: string, format: string} $assoc_args Associative arguments.
* @return void
*/
public function list_( $args, $assoc_args ) {
$connectors = wp_get_connectors();
$items = array();
foreach ( $connectors as $connector_id => $connector ) {
if ( ! is_array( $connector ) ) {
continue;
}
$items[] = $this->build_connector_item( $connector_id, $connector );
}
if ( isset( $assoc_args['status'] ) ) {
$status_filter = $assoc_args['status'];
$items = array_values(
array_filter(
$items,
static function ( array $item ) use ( $status_filter ) {
return $item['status'] === $status_filter;
}
)
);
}
$format = $assoc_args['format'] ?? 'table';
$fields = isset( $assoc_args['fields'] )
? explode( ',', $assoc_args['fields'] )
: $this->default_fields;
WP_CLI\Utils\format_items( $format, $items, $fields );
}
/**
* Gets details about a specific AI connector.
*
* ## OPTIONS
*
* <connector>
* : The connector ID (e.g., openai, anthropic, google).
*
* [--fields=<fields>]
* : Comma-separated list of fields to include in the output.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get details for the OpenAI connector
* $ wp connectors get openai
* +-----------------+-----------------------------------------------+
* | Field | Value |
* +-----------------+-----------------------------------------------+
* | id | openai |
* | name | OpenAI |
* | description | Text and image generation with GPT and Dall-E |
* | status | connected |
* | credentials_url | https://platform.openai.com/api-keys |
* | api_key | ••••••••••••6789 |
* +-----------------+-----------------------------------------------+
*
* @when after_wp_load
*
* @param array{0: string} $args Positional arguments.
* @param array{fields: string, format: string} $assoc_args Associative arguments.
* @return void
*/
public function get( $args, $assoc_args ) {
list( $connector_id ) = $args;
$connector = wp_get_connector( $connector_id );
if ( ! $connector ) {
WP_CLI::error( sprintf( 'Connector "%s" not found.', $connector_id ) );
}
$item = $this->build_connector_item( $connector_id, $connector );
// Retrieve and append the (possibly masked) API key.
$auth = is_array( $connector['authentication'] ) ? $connector['authentication'] : array();
$method = isset( $auth['method'] ) && is_string( $auth['method'] ) ? $auth['method'] : '';
$setting = isset( $auth['setting_name'] ) && is_string( $auth['setting_name'] ) ? $auth['setting_name'] : '';
$api_key = '';
if ( 'api_key' === $method && '' !== $setting ) {
// The option_* filter registered by WP core masks the value automatically.
$raw = get_option( $setting, '' );
$api_key = is_string( $raw ) ? $raw : '';
}
$item['api_key'] = $api_key;
$default_fields = array( 'id', 'name', 'description', 'status', 'credentials_url', 'api_key' );
$formatter = new \WP_CLI\Formatter( $assoc_args, $default_fields );
$formatter->display_item( $item );
}
/**
* Builds a flat item array from a connector settings array.
*
* @param string $connector_id The connector ID.
* @param mixed[] $connector Connector settings from wp_get_connectors().
* @return array{id: string, name: string, description: string, status: string, type: string, auth_method: string, credentials_url: string, plugin_file: string}
*/
private function build_connector_item( string $connector_id, array $connector ): array {
$auth = is_array( $connector['authentication'] ) ? $connector['authentication'] : array();
$plugin = isset( $connector['plugin'] ) && is_array( $connector['plugin'] ) ? $connector['plugin'] : array();
$plugin_file = isset( $plugin['file'] ) && is_string( $plugin['file'] ) ? $plugin['file'] : '';
return array(
'id' => $connector_id,
'name' => $this->scalar_to_string( $connector['name'] ?? '' ),
'description' => $this->scalar_to_string( $connector['description'] ?? '' ),
'status' => $this->get_connector_status( $connector_id, $connector ),
'type' => $this->scalar_to_string( $connector['type'] ?? '' ),
'auth_method' => isset( $auth['method'] ) && is_string( $auth['method'] ) ? $auth['method'] : '',
'credentials_url' => isset( $auth['credentials_url'] ) && is_string( $auth['credentials_url'] ) ? $auth['credentials_url'] : '',
'plugin_file' => $plugin_file,
);
}
/**
* Returns the status of a connector.
*
* Possible values: 'connected', 'active', 'installed', 'uninstalled'.
*
* @param string $connector_id The connector ID.
* @param mixed[] $connector Connector settings from wp_get_connectors().
* @return string
*/
private function get_connector_status( string $connector_id, array $connector ): string {
$auth = is_array( $connector['authentication'] ) ? $connector['authentication'] : array();
$plugin = isset( $connector['plugin'] ) && is_array( $connector['plugin'] ) ? $connector['plugin'] : array();
$plugin_file = isset( $plugin['file'] ) && is_string( $plugin['file'] ) ? $plugin['file'] : '';
$method = isset( $auth['method'] ) && is_string( $auth['method'] ) ? $auth['method'] : '';
$setting = isset( $auth['setting_name'] ) && is_string( $auth['setting_name'] ) ? $auth['setting_name'] : '';
if ( 'api_key' === $method && '' !== $setting ) {
// The option_* filter registered by WP core masks the value automatically.
$raw = get_option( $setting, '' );
if ( is_string( $raw ) && '' !== $raw ) {
return 'connected';
}
}
if ( ! $plugin_file || is_plugin_active( $plugin_file ) ) {
return 'active';
}
$is_installed = file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $plugin_file ) );
if ( $is_installed ) {
return 'installed';
}
return 'uninstalled';
}
/**
* Casts a scalar value to string, returning an empty string for non-scalars.
*
* @param mixed $value The value to cast.
* @return string
*/
private function scalar_to_string( $value ): string {
return is_scalar( $value ) ? (string) $value : '';
}
}