-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwp-rest-cli.php
59 lines (43 loc) · 1.4 KB
/
wp-rest-cli.php
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
<?php
/**
* Use WP-API at the command line.
*/
WP_CLI::add_hook( 'after_wp_load', function(){
if ( ! class_exists( 'WP_REST_Server' ) ) {
return;
}
global $wp_rest_server;
define( 'REST_REQUEST', true );
$wp_rest_server = new WP_REST_Server;
do_action( 'rest_api_init', $wp_rest_server );
foreach( $wp_rest_server->get_routes() as $route => $endpoints ) {
if ( false === stripos( $route, '/wp/v2/comments' ) ) {
continue;
}
$route_data = $wp_rest_server->get_data_for_route( $route, $endpoints, 'help' );
$parent = "rest {$route_data['schema']['title']}";
$fields = array();
foreach( $route_data['schema']['properties'] as $key => $args ) {
if ( in_array( 'embed', $args['context'] ) ) {
$fields[] = $key;
}
}
foreach( $endpoints as $endpoint ) {
if ( array( 'GET' => true ) == $endpoint['methods']
&& '(?P<id>[\d]+)' !== substr( $route, -13 ) ) {
$callable = function( $args, $assoc_args ) use( $route, $fields ){
$defaults = array(
'fields' => $fields,
);
$assoc_args = array_merge( $defaults, $assoc_args );
$response = rest_do_request( new WP_REST_Request( 'GET', $route ) );
if ( $error = $response->as_error() ) {
WP_CLI::error( $error );
}
WP_CLI\Utils\format_items( 'table', $response->get_data(), $assoc_args['fields'] );
};
WP_CLI::add_command( "{$parent} list", $callable );
}
}
}
});