OSDN Git Service

Implement `wp theme get`
authorDaniel Bachhuber <d@danielbachhuber.com>
Tue, 20 Aug 2013 05:57:01 +0000 (22:57 -0700)
committerDaniel Bachhuber <d@danielbachhuber.com>
Tue, 20 Aug 2013 05:57:01 +0000 (22:57 -0700)
features/theme.feature
php/commands/theme.php
php/utils.php

index a42199f..9aa1d8b 100644 (file)
@@ -68,3 +68,14 @@ Feature: Manage WordPress themes
     Then STDOUT should be a table containing rows:
       | name  | status   | update    | version   |
       | p2    | active   | available | 1.4.1     |
+
+  Scenario: Get details about an installed theme
+    Given a WP install
+
+    When I run `wp theme install twentyeleven`
+    Then STDOUT should not be empty
+
+    When I run `wp theme get twentyeleven`
+    Then STDOUT should be a table containing rows:
+      | Field | Value          |
+      | name  | Twenty Eleven  |
index 9adcce6..87ce9cc 100644 (file)
@@ -250,6 +250,62 @@ class Theme_Command extends \WP_CLI\CommandWithUpgrade {
        }
 
        /**
+        * Get a theme
+        * 
+        * ## OPTIONS
+        *
+        * <theme>
+        * : The theme to get.
+        * 
+        * * --format=<format>
+        * : The format to use when printing the theme, acceptable values:
+        *
+        *   - **table**: Outputs all fields of the theme as a table.
+        *
+        *   - **json**: Outputs all fields in JSON format.
+        * 
+        * ## EXAMPLES
+        *
+        *     wp theme get twentytwelve
+        * 
+        * @synopsis <theme> [--format=<format>]
+        */
+       public function get( $args, $assoc_args ) {
+
+               $defaults = array(
+                       'format' => 'table'
+               );
+               $assoc_args = array_merge( $defaults, $assoc_args );
+
+               $theme = $this->parse_name( $args[0] );
+
+               // WP_Theme object employs magic getter, unfortunately
+               $theme_vars = array( 'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet', 'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
+               );
+               $theme_obj = new stdClass;
+               foreach( $theme_vars as $var ) {
+                       $theme_obj->$var = $theme->$var;
+               }
+
+               switch ( $assoc_args['format'] ) {
+
+                       case 'table':
+                               unset( $theme_obj->tags );
+                               $fields = get_object_vars( $theme_obj );
+                               \WP_CLI\Utils\assoc_array_to_table( $fields );
+                               break;
+
+                       case 'json':
+                               WP_CLI::print_value( $theme_obj, $assoc_args );
+                               break;
+
+                       default:
+                               \WP_CLI::error( "Invalid format: " . $assoc_args['format'] );
+                               break;
+               }
+       }
+
+       /**
         * Update a theme.
         *
         * ## OPTIONS
index 125bd96..1b3b1a6 100644 (file)
@@ -303,6 +303,28 @@ function format_items( $format, $items, $fields ) {
 }
 
 /**
+ * Format an associative array as a table
+ *
+ * @param array     $fields    Fields and values to format
+ */
+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
+               );
+       }
+
+       format_items( 'table', $rows, array( 'Field', 'Value' ) );
+}
+
+/**
  * Write data as CSV to a given file.
  *
  * @param resource $fd         File descriptor