OSDN Git Service

extract assoc_array_to_table() utility.
authorscribu <mail@scribu.net>
Sun, 14 Jul 2013 12:56:46 +0000 (15:56 +0300)
committerscribu <mail@scribu.net>
Sun, 14 Jul 2013 12:56:46 +0000 (15:56 +0300)
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
php/commands/post.php

index fa735be..083b068 100644 (file)
@@ -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 );
        }
+
 }
 
index d378366..8ef3c72 100644 (file)
@@ -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':