"""
Test content.
"""
+
+ When I run `wp post get --format=content {POST_ID}`
+ Then it should run without errors
+ And STDOUT should be:
+ """
+ Test content.
+ """
+
+ When I run `wp post get --format=table {POST_ID}`
+ Then it should run without errors
+ And STDOUT should be a table containing rows:
+ """
+ Field Value
+ ID {POST_ID}
+ post_title Test post
+ post_content Test content.
+ """
+
+ When I run `wp post get --format=csv {POST_ID}`
+ Then it should run without errors
+ And STDOUT should be a table containing rows:
+ """
+ Field,Value
+ ID,{POST_ID}
+ post_title,"Test post"
+ post_content,"Test content."
+ """
+
+ When I run `wp post get --format=json {POST_ID}`
+ Then it should run without errors
+ And STDOUT should be JSON containing:
+ """
+ {"ID":{POST_ID},"post_title":"Test post","post_content":"Test content."}
+ """
}
);
+$steps->Then( '/^STDOUT should be a table containing rows:$/',
+ function ( $world, PyStringNode $expected ) {
+ $output = $world->result->STDOUT;
+ $outputRows = explode( "\n", rtrim( $output, "\n" ) );
+
+ $expected = $world->replace_variables( (string) $expected );
+ $expectedRows = explode( "\n", rtrim( $expected, "\n" ) );
+
+ // the first row is the header and must be present
+ if ( $expectedRows[0] != $outputRows[0] ) {
+ throw new \Exception( $output );
+ }
+
+ unset($outputRows[0]);
+ unset($expectedRows[0]);
+ $matches = array_intersect( $expectedRows, $outputRows );
+ if ( count( $expectedRows ) != count( $matches ) ) {
+ throw new \Exception( $output );
+ }
+ }
+);
+
+$steps->Then( '/^STDOUT should be JSON containing:$/',
+ function ( $world, PyStringNode $expected ) {
+ $output = $world->result->STDOUT;
+ $outputJson = json_decode( $output );
+
+ $expected = $world->replace_variables( (string) $expected );
+ $expectedJson = json_decode( $expected );
+
+ if ( !compareJson( $expectedJson, $outputJson ) ) {
+ throw new \Exception( $output );
+ }
+});
+
$steps->Then( '/^(STDOUT|STDERR) should be empty$/',
function ( $world, $stream ) {
if ( !empty( $world->result->$stream ) ) {
assertFileExists( $world->get_path( $path ) );
}
);
+
+
+function compareJson( $expected, $actual ) {
+ if ( gettype( $expected ) != gettype( $actual ) ) {
+ return false;
+ }
+
+ if ( is_object( $expected ) ) {
+ foreach ( get_object_vars( $expected ) as $name => $value ) {
+ if ( !compareJson( $value, $actual->$name ) ) {
+ return false;
+ }
+ }
+ } else if ( is_array( $expected ) ) {
+ foreach ( $expected as $key => $value ) {
+ if ( !compareJson( $value, $actual[$key] ) ) {
+ return false;
+ }
+ }
+ } else {
+ return $expected === $actual;
+ }
+
+ return true;
+}
## OPTIONS
+* `[--format=<format>]`:
+
+ The format to use when printing the post, acceptable values:
+
+ content: outputs only the post's content (default)
+
+ table: output all fields of the post as a table, note that the
+ post_content field is output last to simplify parsing
+
+ csv: output all fields in Comma Separated Value format
+
+ json: output all fields in JavaScript Object Notation format
+
* `<id>`:
The ID of the post to get.
\fBwp\-post\-get\fR \- Get a post\'s content by ID\.
.
.SH "SYNOPSIS"
-wp post get \fIid\fR
+wp post get [\-\-format=\fIformat\fR] \fIid\fR
.
.SH "OPTIONS"
.
.TP
+\fB[\-\-format=<format>]\fR:
+.
+.IP
+The format to use when printing the post, acceptable values:
+.
+.IP
+content: outputs only the post\'s content (default)
+.
+.IP
+table: output all fields of the post as a table, note that the post_content field is output last to simplify parsing
+.
+.IP
+csv: output all fields in Comma Separated Value format
+.
+.IP
+json: output all fields in JavaScript Object Notation format
+.
+.TP
\fB<id>\fR:
.
.IP
public function edit( $args, $_ ) {
$post_id = $args[0];
if ( !$post_id || !$post = get_post( $post_id ) )
- \WP_CLI::error( "Failed opening post $post_id to edit." );
+ \WP_CLI::error( "Failed opening post $post_id to edit." );
$r = $this->_edit( $post->post_content, "WP-CLI post $post_id" );
if ( $r === false )
- \WP_CLI::warning( 'No change made to post content.', 'Aborted' );
+ \WP_CLI::warning( 'No change made to post content.', 'Aborted' );
else
- parent::update( $args, array( 'post_content' => $r ) );
+ parent::update( $args, array( 'post_content' => $r ) );
}
protected function _edit( $content, $title ) {
}
/**
- * Get a post's content by ID.
- *
- * @synopsis <id>
- */
- public function get( $args, $_ ) {
+ * Get a post's content by ID.
+ *
+ * @synopsis [--format=<format>] <id>
+ */
+ public function get( $args, $assoc_args ) {
+ $assoc_args = wp_parse_args( $assoc_args, array(
+ 'format' => 'content'
+ ) );
+ $format = $assoc_args['format'];
+
$post_id = $args[0];
if ( !$post_id || !$post = get_post( $post_id ) )
- \WP_CLI::error( "Failed opening post $post_id to get." );
+ \WP_CLI::error( "Failed opening post $post_id to get." );
+
+ switch ( $assoc_args['format'] ) {
+
+ case 'content':
+ echo($post->post_content);
+ break;
- echo($post->post_content);
+ case 'table':
+ case 'csv':
+ $items = array();
+ foreach ( get_object_vars( $post ) as $field => $value ) {
+ if ( 'post_content' === $field )
+ continue;
+
+ $item = new \stdClass;
+ $item->Field = $field;
+ $item->Value = $value;
+ $items[] = $item;
+ }
+ $content = new \stdClass;
+ $content->Field = 'post_content';
+ $content->Value = $post->post_content;
+ $items[] = $content;
+
+ \WP_CLI\Utils\format_items( $format, array( 'Field', 'Value' ), $items );
+ break;
+
+ case 'json':
+ echo( json_encode( $post ) );
+ break;
+
+ default:
+ \WP_CLI::error( "Invalid value for format: " . $format );
+ }
}
/**