Last active
March 13, 2016 20:04
-
-
Save danielbachhuber/cd61d10d1fa8d7651dad to your computer and use it in GitHub Desktop.
Compare query arguments supported by each WP REST API endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Prepare a table comparing query arguments supported by each endpoint | |
* | |
* wp eval-file query-params --skip-wordpress | |
*/ | |
$endpoints = array( | |
'posts' => '/wp/v2/posts', | |
'pages' => '/wp/v2/pages', | |
'media' => '/wp/v2/media', | |
'comments' => '/wp/v2/comments', | |
'categories' => '/wp/v2/categories', | |
'tags' => '/wp/v2/tags', | |
'users' => '/wp/v2/users', | |
); | |
$endpoint_args = $params = $output = array(); | |
$response = \WP_CLI\Utils\http_request( 'GET', 'http://wordpress-develop.dev/wp-json/' ); | |
$args = json_decode( $response->body, true ); | |
foreach( $endpoints as $type => $path ) { | |
$endpoint_args[ $type ] = array_keys( $args['routes'][ $path ]['endpoints'][0]['args'] ); | |
$params = array_merge( $params, $endpoint_args[ $type ] ); | |
} | |
$params = array_unique( $params ); | |
sort( $params ); | |
foreach( $params as $key ) { | |
$param_data = array( | |
'param' => $key, | |
); | |
foreach( array_keys( $endpoints ) as $endpoint ) { | |
if ( in_array( $key, $endpoint_args[ $endpoint ] ) ) { | |
$param_data[ $endpoint ] = ' x '; | |
} else { | |
$param_data[ $endpoint ] = ''; | |
} | |
} | |
$output[] = $param_data; | |
} | |
\WP_CLI\Utils\format_items( 'table', $output, array_merge( array( 'param' ), array_keys( $endpoints ) ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment