|
| 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 | + 'plural_name' => 'Comments', |
| 21 | + 'singular_name' => 'Comment', |
| 22 | + 'schema_route' => '/wp-json/wp/v2/comments/schema', |
| 23 | + ), |
| 24 | + 'media' => array( |
| 25 | + 'plural_name' => 'Media', |
| 26 | + 'singular_name' => 'Attachment', |
| 27 | + 'schema_route' => '/wp-json/wp/v2/media/schema', |
| 28 | + ), |
| 29 | + 'pages' => array( |
| 30 | + 'plural_name' => 'Pages', |
| 31 | + 'singular_name' => 'Page', |
| 32 | + 'schema_route' => '/wp-json/wp/v2/pages/schema', |
| 33 | + ), |
| 34 | + 'posts' => array( |
| 35 | + 'plural_name' => 'Posts', |
| 36 | + 'singular_name' => 'Post', |
| 37 | + 'schema_route' => '/wp-json/wp/v2/posts/schema', |
| 38 | + ), |
| 39 | + 'post_meta' => array( |
| 40 | + 'plural_name' => 'Meta for a Post', |
| 41 | + 'singular_name' => 'Meta for a Post', |
| 42 | + 'schema_route' => '/wp-json/wp/v2/posts/meta/schema', |
| 43 | + ), |
| 44 | + 'post_revisions' => array( |
| 45 | + 'plural_name' => 'Revisions for a Post', |
| 46 | + 'singular_name' => 'Revision for a Post', |
| 47 | + 'schema_route' => '/wp-json/wp/v2/posts/revisions/schema', |
| 48 | + ), |
| 49 | + 'taxonomies' => array( |
| 50 | + 'plural_name' => 'Taxonomies', |
| 51 | + 'singular_name' => 'Taxonomy', |
| 52 | + 'schema_route' => '/wp-json/wp/v2/taxonomies/schema', |
| 53 | + ), |
| 54 | + 'terms' => array( |
| 55 | + 'plural_name' => 'Terms', |
| 56 | + 'singular_name' => 'Term', |
| 57 | + 'schema_route' => '/wp-json/wp/v2/terms/category/schema', |
| 58 | + ), |
| 59 | + 'users' => array( |
| 60 | + 'plural_name' => 'Users', |
| 61 | + 'singular_name' => 'User', |
| 62 | + 'schema_route' => '/wp-json/wp/v2/users/schema', |
| 63 | + ), |
| 64 | + ); |
| 65 | + |
| 66 | + $dev_domain = rtrim( getenv( 'WP_API_DEV_DOMAIN' ), '/' ); |
| 67 | + |
| 68 | + foreach( $endpoints as $file_name => $attributes ) { |
| 69 | + |
| 70 | + $response = Requests::get( $dev_domain . $attributes['schema_route'] ); |
| 71 | + if ( 200 !== $response->status_code ) { |
| 72 | + echo "Error fetching schema: {$attributes['schema_route']} (HTTP code {$response->status_code})"; |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + $response_data = json_decode( $response->body ); |
| 77 | + // Prepare the data because Mustache is opinionated |
| 78 | + $properties = array(); |
| 79 | + if ( ! empty( $response_data->properties ) ) { |
| 80 | + foreach( $response_data->properties as $name => $args ) { |
| 81 | + $property = array( |
| 82 | + 'name' => $name, |
| 83 | + 'description' => ! empty( $args->description ) ? $args->description : '', |
| 84 | + 'type' => ! empty( $args->type ) ? $args->type : '', |
| 85 | + 'context' => ! empty( $args->context ) ? implode( $args->context, ', ' ) : '', |
| 86 | + ); |
| 87 | + if ( ! empty( $args->format ) ) { |
| 88 | + $property['type'] .= ',' . $args->format; |
| 89 | + } |
| 90 | + $properties[] = $property; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $attributes['schema_properties'] = $properties; |
| 95 | + file_put_contents( dirname( __FILE__ ) . '/_includes/routes/' . $file_name . '.md', render( 'endpoint.mustache', $attributes ) ); |
| 96 | + } |
| 97 | + |
| 98 | +}); |
| 99 | + |
| 100 | +desc( 'Build the site.' ); |
| 101 | +task( 'default', 'endpoint-list' ); |
0 commit comments