From 7973a4ae5fa6e2b629110d529e233aec706b0e13 Mon Sep 17 00:00:00 2001 From: scribu Date: Sun, 14 Jul 2013 15:56:46 +0300 Subject: [PATCH] extract assoc_array_to_table() utility. Also: - unset the 'format_content' field; it can be just as long as 'post_content'. - unset the 'filter' field; it's a runtime value, not persistent data --- php/WP_CLI/CommandWithDBObject.php | 18 ++++++++++++++++++ php/commands/post.php | 19 +++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/php/WP_CLI/CommandWithDBObject.php b/php/WP_CLI/CommandWithDBObject.php index fa735be9..083b0687 100644 --- a/php/WP_CLI/CommandWithDBObject.php +++ b/php/WP_CLI/CommandWithDBObject.php @@ -68,6 +68,23 @@ abstract class CommandWithDBObject extends \WP_CLI_Command { return $status; } + protected function assoc_array_to_table( $fields ) { + $rows = array(); + + foreach ( $fields as $field => $value ) { + if ( !is_string($value) ) { + $value = json_encode( $value ); + } + + $rows[] = (object) array( + 'Field' => $field, + 'Value' => $value + ); + } + + \WP_CLI\Utils\format_items( 'table', $rows, array( 'Field', 'Value' ) ); + } + public function delete( $args, $assoc_args ) { $status = 0; @@ -78,5 +95,6 @@ abstract class CommandWithDBObject extends \WP_CLI_Command { exit( $status ); } + } diff --git a/php/commands/post.php b/php/commands/post.php index d3783660..8ef3c727 100644 --- a/php/commands/post.php +++ b/php/commands/post.php @@ -110,22 +110,9 @@ class Post_Command extends \WP_CLI\CommandWithDBObject { break; case 'table': - $items = array(); - foreach ( get_object_vars( $post ) as $field => $value ) { - if ( 'post_content' === $field ) - continue; - - if ( !is_string($value) ) { - $value = json_encode($value); - } - - $item = new \stdClass; - $item->Field = $field; - $item->Value = $value; - $items[] = $item; - } - - \WP_CLI\Utils\format_items( $format, $items, array( 'Field', 'Value' ) ); + $fields = get_object_vars( $post ); + unset( $fields['filter'], $fields['post_content'], $fields['format_content'] ); + $this->assoc_array_to_table( $fields ); break; case 'json': -- 2.11.0