-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeature_Controller.php
More file actions
538 lines (490 loc) · 14 KB
/
Feature_Controller.php
File metadata and controls
538 lines (490 loc) · 14 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php declare( strict_types=1 );
namespace LiquidWeb\Harbor\API\REST\V1;
use LiquidWeb\Harbor\Features\Error_Code;
use LiquidWeb\Harbor\Features\Feature_Resource;
use LiquidWeb\Harbor\Features\Manager;
use LiquidWeb\Harbor\Features\Types\Feature;
use LiquidWeb\Harbor\Utils\Cast;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
* WP REST API controller for managing features.
*
* Supports listing, retrieving, enabling, disabling, and updating features.
* Restricted to logged-in Administrators (manage_options capability).
*
* @since 1.0.0
*/
class Feature_Controller extends WP_REST_Controller {
/**
* The REST API namespace.
*
* @since 1.0.0
*
* @var string
*/
protected $namespace = 'liquidweb/harbor/v1';
/**
* The REST API route base.
*
* @since 1.0.0
*
* @var string
*/
protected $rest_base = 'features';
/**
* The feature manager instance.
*
* @since 1.0.0
*
* @var Manager
*/
private Manager $manager;
/**
* Constructor for the feature REST API controller.
*
* @since 1.0.0
*
* @param Manager $manager The feature manager.
*
* @return void
*/
public function __construct( Manager $manager ) {
$this->manager = $manager;
}
/**
* Registers the routes.
*
* @since 1.0.0
*
* @return void
*/
public function register_routes(): void {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'check_permissions' ],
'args' => $this->get_collection_params(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<slug>[a-zA-Z0-9_-]+)',
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_item' ],
'permission_callback' => [ $this, 'check_permissions' ],
'args' => [
'slug' => [
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
],
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<slug>[a-zA-Z0-9_-]+)/enable',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'enable' ],
'permission_callback' => [ $this, 'check_permissions' ],
'args' => $this->get_slug_args(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<slug>[a-zA-Z0-9_-]+)/disable',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'disable' ],
'permission_callback' => [ $this, 'check_permissions' ],
'args' => $this->get_slug_args(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<slug>[a-zA-Z0-9_-]+)/update',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_item' ],
'permission_callback' => [ $this, 'check_permissions' ],
'args' => $this->get_slug_args(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
}
/**
* Permission callback: require manage_options capability.
*
* @since 1.0.0
*
* @return bool
*/
public function check_permissions(): bool {
return current_user_can( 'manage_options' );
}
/**
* Lists features with optional filters.
*
* @since 1.0.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error
*/
public function get_items( $request ) {
$features = $this->manager->get_all();
if ( is_wp_error( $features ) ) {
return $this->ensure_error_status( $features );
}
$product = $request->get_param( 'product' );
$tier = $request->get_param( 'tier' );
$available = $request->get_param( 'available' );
$type = $request->get_param( 'type' );
if ( $product !== null || $tier !== null || $available !== null || $type !== null ) {
$features = $features->filter(
$product !== null ? Cast::to_string( $product ) : null,
$tier !== null ? Cast::to_string( $tier ) : null,
$available !== null ? Cast::to_bool( $available ) : null,
$type !== null ? Cast::to_string( $type ) : null
);
}
$data = [];
foreach ( $features as $feature ) {
$data[] = Feature_Resource::from_feature( $feature )->to_array();
}
return new WP_REST_Response( $data );
}
/**
* Retrieves a single feature by slug.
*
* @since 1.0.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error
*/
public function get_item( $request ) {
$slug = Cast::to_string( $request->get_param( 'slug' ) );
$features = $this->manager->get_all();
if ( is_wp_error( $features ) ) {
return $this->ensure_error_status( $features );
}
$feature = $features->get( $slug );
if ( ! $feature ) {
return new WP_Error(
Error_Code::FEATURE_NOT_FOUND,
sprintf( 'Feature "%s" not found.', $slug ),
[ 'status' => 404 ]
);
}
return new WP_REST_Response( Feature_Resource::from_feature( $feature )->to_array() );
}
/**
* Enables a feature.
*
* @since 1.0.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error
*/
public function enable( WP_REST_Request $request ) {
$slug = Cast::to_string( $request->get_param( 'slug' ) );
$feature = $this->manager->enable( $slug );
if ( is_wp_error( $feature ) ) {
return $this->ensure_error_status( $feature );
}
return new WP_REST_Response(
Feature_Resource::from_feature( $feature )->to_array()
);
}
/**
* Disables a feature.
*
* @since 1.0.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error
*/
public function disable( WP_REST_Request $request ) {
$slug = Cast::to_string( $request->get_param( 'slug' ) );
$feature = $this->manager->disable( $slug );
if ( is_wp_error( $feature ) ) {
return $this->ensure_error_status( $feature );
}
return new WP_REST_Response(
Feature_Resource::from_feature( $feature )->to_array()
);
}
/**
* Triggers an update for a feature.
*
* @since 1.0.0
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error
*/
public function update_item( $request ) {
$slug = Cast::to_string( $request->get_param( 'slug' ) );
$feature = $this->manager->update( $slug );
if ( is_wp_error( $feature ) ) {
return $this->ensure_error_status( $feature );
}
return new WP_REST_Response(
Feature_Resource::from_feature( $feature )->to_array()
);
}
/**
* Gets the schema for a single feature response.
*
* @since 1.0.0
*
* @return array<string, mixed>
*/
public function get_item_schema(): array {
if ( $this->schema ) {
/** @var array<string, mixed> */
return $this->add_additional_fields_schema( $this->schema );
}
$base_properties = [
'slug' => [
'description' => __( 'The feature slug.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'name' => [
'description' => __( 'The feature display name.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'description' => [
'description' => __( 'The feature description.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'product' => [
'description' => __( 'The product the feature belongs to.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'tier' => [
'description' => __( 'The feature tier.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'type' => [
'description' => __( 'The feature type identifier.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
'is_available' => [
'description' => __( 'Whether the feature is available for the current site.', '%TEXTDOMAIN%' ),
'type' => 'boolean',
'readonly' => true,
'context' => [ 'view' ],
],
'documentation_url' => [
'description' => __( 'The URL to the feature documentation.', '%TEXTDOMAIN%' ),
'type' => 'string',
'format' => 'uri',
'readonly' => true,
'context' => [ 'view' ],
],
'is_enabled' => [
'description' => __( 'Whether the feature is currently enabled.', '%TEXTDOMAIN%' ),
'type' => 'boolean',
'readonly' => true,
'context' => [ 'view' ],
],
];
$installable_properties = [
'released_at' => [
'description' => __( 'Release date of the latest version (ISO 8601), or null if not applicable.', '%TEXTDOMAIN%' ),
'type' => [ 'string', 'null' ],
'readonly' => true,
'context' => [ 'view' ],
],
'changelog' => [
'description' => __( 'Changelog HTML for the latest version, or null. Only present for installable features.', '%TEXTDOMAIN%' ),
'type' => [ 'string', 'null' ],
'readonly' => true,
'context' => [ 'view' ],
],
'authors' => [
'description' => __( 'Expected authors for ownership verification.', '%TEXTDOMAIN%' ),
'type' => 'array',
'items' => [
'type' => 'string',
],
'readonly' => true,
'context' => [ 'view' ],
],
'is_dot_org' => [
'description' => __( 'Whether the feature is available on WordPress.org.', '%TEXTDOMAIN%' ),
'type' => 'boolean',
'readonly' => true,
'context' => [ 'view' ],
],
'installed_version' => [
'description' => __( 'The currently installed version, or null if not installed.', '%TEXTDOMAIN%' ),
'type' => [ 'string', 'null' ],
'readonly' => true,
'context' => [ 'view' ],
],
'update_version' => [
'description' => __( 'The version available via WordPress update transients, or null if no update is available.', '%TEXTDOMAIN%' ),
'type' => [ 'string', 'null' ],
'readonly' => true,
'context' => [ 'view' ],
],
];
$plugin_properties = [
'plugin_file' => [
'description' => __( 'The plugin file path relative to the plugins directory.', '%TEXTDOMAIN%' ),
'type' => 'string',
'readonly' => true,
'context' => [ 'view' ],
],
];
$this->schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'feature',
'oneOf' => [
[
'title' => 'plugin',
'type' => 'object',
'additionalProperties' => true,
'properties' => array_merge(
$base_properties,
[ 'type' => array_merge( $base_properties['type'], [ 'enum' => [ Feature::TYPE_PLUGIN ] ] ) ],
$plugin_properties,
$installable_properties
),
],
[
'title' => 'theme',
'type' => 'object',
'additionalProperties' => true,
'properties' => array_merge(
$base_properties,
[ 'type' => array_merge( $base_properties['type'], [ 'enum' => [ Feature::TYPE_THEME ] ] ) ],
$installable_properties
),
],
],
];
/** @var array<string, mixed> */
return $this->add_additional_fields_schema( $this->schema );
}
/**
* Gets the query parameters for the features collection.
*
* @since 1.0.0
*
* @return array<string, array<string, mixed>>
*/
public function get_collection_params(): array {
return [
'product' => [
'description' => __( 'Filter by product.', '%TEXTDOMAIN%' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
'tier' => [
'description' => __( 'Filter by feature tier.', '%TEXTDOMAIN%' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
'available' => [
'description' => __( 'Filter by availability.', '%TEXTDOMAIN%' ),
'type' => 'boolean',
],
'type' => [
'description' => __( 'Filter by feature type.', '%TEXTDOMAIN%' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
];
}
/**
* Ensures a WP_Error has an HTTP status code in its data.
*
* Errors from the Manager and its strategies do not carry HTTP
* status codes. This method maps known error codes to the most
* appropriate HTTP status before the error reaches the REST
* infrastructure (which defaults to 500 when no status is set).
*
* @since 1.0.0
*
* @param WP_Error $error The error to enrich.
*
* @return WP_Error The same instance, with a status code guaranteed.
*/
private function ensure_error_status( WP_Error $error ): WP_Error {
$data = $error->get_error_data();
if ( is_array( $data ) && isset( $data['status'] ) ) {
return $error;
}
$status = Error_Code::http_status( (string) $error->get_error_code() );
$error->add_data(
is_array( $data )
? array_merge( $data, [ 'status' => $status ] )
: [ 'status' => $status ]
);
return $error;
}
/**
* Gets the slug argument definition for toggle endpoints.
*
* @since 1.0.0
*
* @return array<string, array<string, mixed>>
*/
private function get_slug_args(): array {
return [
'slug' => [
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => function ( $slug ): bool {
return Cast::to_bool(
$this->manager->exists(
Cast::to_string( $slug )
)
);
},
],
];
}
}