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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
<?php
namespace SolidWP\Mail\Admin\REST;
use SolidWP\Mail\Repository\ProvidersRepository; use SolidWP\Mail\Service\ConnectionService; use WP_REST_Request; use WP_REST_Response; use WP_Error;
/** * REST Controller for managing email connections. * * Provides CRUD operations for email connections including creation, * retrieval, updating, deletion, and activation of connections. */ class Connections extends \WP_REST_Controller {
protected $namespace = 'solid-mail/v1';
protected $rest_base = 'connections';
protected ProvidersRepository $repository;
protected ConnectionService $connection_service;
public function __construct( ProvidersRepository $repository, ConnectionService $connection_service ) { $this->repository = $repository; $this->connection_service = $connection_service; }
/** * Registers the REST routes for connection management. * * @return void */ public function register_routes() { register_rest_route( $this->namespace, '/' . $this->rest_base, [ [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [ $this, 'get_items' ], 'permission_callback' => [ $this, 'get_items_permissions_check' ], ], [ 'methods' => \WP_REST_Server::CREATABLE, 'callback' => [ $this, 'create_item' ], 'permission_callback' => [ $this, 'create_item_permissions_check' ], 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), ], ] );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[a-zA-Z0-9_-]+)', [ [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [ $this, 'get_item' ], 'permission_callback' => [ $this, 'get_item_permissions_check' ], 'args' => [ 'id' => [ 'description' => __( 'Unique identifier for the connection.', 'LION' ), 'type' => 'string', ], ], ], [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => [ $this, 'update_item' ], 'permission_callback' => [ $this, 'update_item_permissions_check' ], 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), ], [ 'methods' => \WP_REST_Server::DELETABLE, 'callback' => [ $this, 'delete_item' ], 'permission_callback' => [ $this, 'delete_item_permissions_check' ], 'args' => [ 'id' => [ 'description' => __( 'Unique identifier for the connection.', 'LION' ), 'type' => 'string', ], ], ], ] ); }
/** * Retrieves all connections. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response Response object containing all connections. */ public function get_items( $request ) { $connections = $this->repository->get_all_providers_as_array(); $data = []; foreach ( $connections as $connection ) { $response = $this->prepare_item_for_response( $connection, $request ); $data[] = $this->prepare_response_for_collection( $response ); }
return rest_ensure_response( $data ); }
/** * Retrieves a specific connection by ID. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response|WP_Error Response object or error if connection not found. */ public function get_item( $request ) { $id = $request['id']; $connection = $this->repository->get_provider_by_id( $id );
if ( ! $connection ) { return new WP_Error( 'connection_not_found', __( 'Connection not found', 'LION' ), [ 'status' => 404 ] ); }
$data = $this->prepare_item_for_response( $connection->to_array(), $request ); return rest_ensure_response( $data ); }
/** * Creates a new connection. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response|WP_Error Response object with created connection or error. */ public function create_item( $request ) { $params = $request->get_params(); $result = $this->connection_service->save_connection( $params );
if ( is_wp_error( $result ) ) { return $result; }
// The service now returns the connection data directly $response = $this->prepare_item_for_response( $result, $request ); $response = rest_ensure_response( $response ); $response->set_status( 201 );
return $response; }
/** * Updates an existing connection. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response|WP_Error Response object with updated connection or error. */ public function update_item( $request ) { $id = $request['id']; $connection = $this->repository->get_provider_by_id( $id );
if ( ! $connection ) { return new WP_Error( 'connection_not_found', __( 'Connection not found', 'LION' ), [ 'status' => 404 ] ); }
$params = $request->get_params(); $params['id'] = $id; // Ensure ID is included for the service
$result = $this->connection_service->save_connection( $params );
if ( is_wp_error( $result ) ) { return $result; }
// The service now returns the connection data directly $response = $this->prepare_item_for_response( $result, $request ); return rest_ensure_response( $response ); }
/** * Deletes a connection. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response|WP_Error Response object with deletion status or error. */ public function delete_item( $request ) { $id = $request['id']; $connection = $this->repository->get_provider_by_id( $id );
if ( ! $connection ) { return new WP_Error( 'connection_not_found', __( 'Connection not found', 'LION' ), [ 'status' => 404 ] ); }
if ( $connection->is_active() ) { return new WP_Error( 'cannot_delete_active', __( 'Cannot delete active connection', 'LION' ), [ 'status' => 400 ] ); }
$this->connection_service->delete_connection( $id );
return new WP_REST_Response( null, \WP_Http::NO_CONTENT ); }
/** * Checks permissions for retrieving connections. * * @param WP_REST_Request $request Full data about the request. * @return bool|WP_Error True if user can access, WP_Error otherwise. */ public function get_items_permissions_check( $request ) { return $this->check_permissions(); }
/** * Checks permissions for retrieving a specific connection. * * @param WP_REST_Request $request Full data about the request. * @return bool|WP_Error True if user can access, WP_Error otherwise. */ public function get_item_permissions_check( $request ) { return $this->check_permissions(); }
/** * Checks permissions for creating a connection. * * @param WP_REST_Request $request Full data about the request. * @return bool|WP_Error True if user can create, WP_Error otherwise. */ public function create_item_permissions_check( $request ) { return $this->check_permissions(); }
/** * Checks permissions for updating a connection. * * @param WP_REST_Request $request Full data about the request. * @return bool|WP_Error True if user can update, WP_Error otherwise. */ public function update_item_permissions_check( $request ) { return $this->check_permissions(); }
/** * Checks permissions for deleting a connection. * * @param WP_REST_Request $request Full data about the request. * @return bool|WP_Error True if user can delete, WP_Error otherwise. */ public function delete_item_permissions_check( $request ) { return $this->check_permissions(); }
/** * Checks if the current user has permission to manage connections. * * @return bool|WP_Error True if user has manage_options capability, WP_Error otherwise. */ private function check_permissions() { if ( ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'rest_cannot_manage', __( 'You do not have permissions to manage connections', 'LION' ), [ 'status' => rest_authorization_required_code() ] ); }
return true; }
/** * Prepares a connection item for the REST response. * * @param array $item Connection data array. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Prepared response object. */ public function prepare_item_for_response( $item, $request ) { $data = [ 'id' => $item['id'], 'name' => $item['name'], 'description' => $item['description'] ?? '', 'from_email' => $item['from_email'] ?? '', 'from_name' => $item['from_name'] ?? '', 'is_active' => $item['is_active'] ?? false, 'is_default' => $item['is_default'] ?? false, 'disable_logs' => $item['disable_logs'] ?? false, 'delivery_method' => $item['delivery_method'] ?? 'smtp', ];
if ( isset( $item['smtp_host'] ) ) { $data['smtp_host'] = $item['smtp_host']; } if ( isset( $item['smtp_port'] ) ) { $data['smtp_port'] = $item['smtp_port']; } if ( isset( $item['smtp_auth'] ) ) { $data['smtp_auth'] = $item['smtp_auth']; } if ( isset( $item['smtp_username'] ) ) { $data['smtp_username'] = $item['smtp_username']; } if ( isset( $item['smtp_secure'] ) ) { $data['smtp_secure'] = $item['smtp_secure']; } if ( isset( $item['smtp_password'] ) ) { $data['smtp_password'] = $item['smtp_password']; }
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
return apply_filters( 'rest_prepare_connection', $response, $item, $request ); }
/** * Retrieves the connection schema for REST API documentation. * * @return array Schema array defining connection properties. */ public function get_item_schema() { $schema = [ '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'connection', 'type' => 'object', 'properties' => [ 'id' => [ 'description' => __( 'Unique identifier for the connection.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], 'readonly' => true, ], 'name' => [ 'description' => __( 'The connection type name.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], 'required' => true, 'enum' => [ 'mailgun', 'brevo', 'sendgrid', 'amazon_ses', 'postmark', 'other' ], ], 'description' => [ 'description' => __( 'Description of the connection.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], ], 'from_email' => [ 'description' => __( 'The email address used as the sender.', 'LION' ), 'type' => 'string', 'format' => 'email', 'context' => [ 'view', 'edit' ], ], 'from_name' => [ 'description' => __( 'The name used as the sender.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], ], 'is_active' => [ 'description' => __( 'Whether this connection is active.', 'LION' ), 'type' => 'boolean', 'context' => [ 'view', 'edit' ], ], 'is_default' => [ 'description' => __( 'Whether this connection is used for email addresses not related to a specific connection.', 'LION' ), 'type' => 'boolean', 'context' => [ 'view', 'edit' ], ], 'disable_logs' => [ 'description' => __( 'Whether logging is disabled for this connection.', 'LION' ), 'type' => 'boolean', 'context' => [ 'view', 'edit' ], ], 'delivery_method' => [ 'description' => __( 'The delivery method used by the connection.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], 'enum' => [ 'smtp', 'api' ], ], 'smtp_host' => [ 'description' => __( 'SMTP server hostname.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], ], 'smtp_port' => [ 'description' => __( 'SMTP server port.', 'LION' ), 'type' => 'integer', 'minimum' => 0, 'context' => [ 'view', 'edit' ], ], 'smtp_auth' => [ 'description' => __( 'Whether SMTP authentication is required.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], 'enum' => [ 'yes', 'no' ], ], 'smtp_username' => [ 'description' => __( 'SMTP username for authentication.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], ], 'smtp_password' => [ 'description' => __( 'SMTP password for authentication.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], ], 'smtp_secure' => [ 'description' => __( 'SMTP encryption method.', 'LION' ), 'type' => 'string', 'context' => [ 'view', 'edit' ], 'enum' => [ '', 'ssl', 'tls' ], ], ], ];
return $this->add_additional_fields_schema( $schema ); } }
|