|
| 1 | +<?php |
| 2 | +require_once dirname( __FILE__ ) . '/vendor/autoload.php'; |
| 3 | + |
| 4 | +if ( ! getenv( 'WP_API_DEV_DOMAIN' ) ) { |
| 5 | + echo "`WP_API_DEV_DOMAIN` environment variable must be set to build the site."; |
| 6 | + exit(1); |
| 7 | +} |
| 8 | + |
| 9 | +function render( $path, $binding ) { |
| 10 | + $m = new Mustache_Engine; |
| 11 | + $template = file_get_contents( __DIR__ . "/templates/$path" ); |
| 12 | + return $m->render( $template, $binding ); |
| 13 | +} |
| 14 | + |
| 15 | +desc( 'Build the endpoint documentation.' ); |
| 16 | +task( 'endpoint-list', function( $app ){ |
| 17 | + |
| 18 | + $endpoints = array( |
| 19 | + 'comments' => array( |
| 20 | + 'name' => 'Comments', |
| 21 | + 'schema_path' => '/wp-json/wp/v2/comments/schema', |
| 22 | + ), |
| 23 | + ); |
| 24 | + |
| 25 | + $dev_domain = rtrim( getenv( 'WP_API_DEV_DOMAIN' ), '/' ); |
| 26 | + |
| 27 | + foreach( $endpoints as $file_name => $attributes ) { |
| 28 | + |
| 29 | + $response = Requests::get( $dev_domain . $attributes['schema_path'] ); |
| 30 | + if ( 200 !== $response->status_code ) { |
| 31 | + echo "Error fetching schema (HTTP code {$response->status_code})"; |
| 32 | + exit(1); |
| 33 | + } |
| 34 | + |
| 35 | + $response_data = json_decode( $response->body ); |
| 36 | + // Prepare the data because Mustache is opinionated |
| 37 | + $properties = array(); |
| 38 | + if ( ! empty( $response_data->properties ) ) { |
| 39 | + foreach( $response_data->properties as $name => $args ) { |
| 40 | + $properties[] = array( |
| 41 | + 'name' => $name, |
| 42 | + 'description' => ! empty( $args->description ) ? $args->description : '', |
| 43 | + 'type' => ! empty( $args->type ) ? $args->type : '', |
| 44 | + 'context' => ! empty( $args->context ) ? implode( $args->context, ', ' ) : '', |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $data = array( |
| 50 | + 'name' => $attributes['name'], |
| 51 | + 'schema_properties' => $properties, |
| 52 | + ); |
| 53 | + file_put_contents( dirname( __FILE__ ) . '/_includes/routes/' . $file_name . '.md', render( 'endpoint.mustache', $data ) ); |
| 54 | + } |
| 55 | + |
| 56 | +}); |
| 57 | + |
| 58 | +desc( 'Build the site.' ); |
| 59 | +task( 'default', 'endpoint-list' ); |
0 commit comments