OSDN Git Service

add url subcommand to post, comment and site commands
authorRodrigo Primo <rodrigo@hacklab.com.br>
Thu, 12 Dec 2013 21:52:21 +0000 (19:52 -0200)
committerRodrigo Primo <rodrigo@hacklab.com.br>
Thu, 12 Dec 2013 22:39:07 +0000 (20:39 -0200)
features/comment.feature
features/post.feature
features/site.feature
php/WP_CLI/CommandWithDBObject.php
php/WP_CLI/Fetchers/Site.php [new file with mode: 0644]
php/commands/comment.php
php/commands/post.php
php/commands/site.php

index 96b21e7..90f8783 100644 (file)
@@ -45,3 +45,9 @@ Feature: Manage WordPress comments
       """
       1
       """
+
+    When I run `wp comment url 1`
+    Then STDOUT should be:
+      """
+      http://example.com/?p=1#comment-1
+      """
\ No newline at end of file
index d7d9588..a4154e2 100644 (file)
@@ -92,6 +92,12 @@ Feature: Manage WordPress posts
       """
       This is some bunkum.
       """
+      
+    When I run `wp post url {POST_ID}`
+    Then STDOUT should be:
+      """
+      http://example.com/?p=3
+      """
 
 
   Scenario: Creating/listing posts
index bfbd492..d40d99e 100644 (file)
@@ -1,5 +1,14 @@
 Feature: Manage sites in a multisite installation
 
+  Scenario: Create a site
+    Given a WP multisite install
+    
+    When I try `wp site create --slug=first --network_id=1000`
+    Then STDERR should contain:
+      """
+      Network with id 1000 does not exist.
+      """
+
   Scenario: Delete a site by id
     Given a WP multisite install
 
@@ -38,7 +47,7 @@ Feature: Manage sites in a multisite installation
     When I try the previous command again
     Then the return code should be 1
 
- Scenario: Empty a site
 Scenario: Empty a site
     Given a WP install
 
     When I run `wp post create --post_title='Test post' --post_content='Test content.' --porcelain`
@@ -55,3 +64,12 @@ Feature: Manage sites in a multisite installation
 
     When I run `wp term list post_tag --format=ids`
     Then STDOUT should be empty
+
+  Scenario: Get site info
+    Given a WP multisite install
+    
+    When I run `wp site url 1`
+    Then STDOUT should be:
+      """
+      http://example.com
+      """
\ No newline at end of file
index 7ea97c7..390d958 100644 (file)
@@ -82,5 +82,10 @@ abstract class CommandWithDBObject extends \WP_CLI_Command {
        protected function get_formatter( &$assoc_args ) {
                return new \WP_CLI\Formatter( $assoc_args, $this->obj_fields, $this->obj_type );
        }
+       
+       protected function _url( $args, $callback ) {
+               $object = $this->fetcher->get_check( $args[0] );
+               \WP_CLI::print_value( $callback( $object->{$this->obj_id_key} ) );
+       }
 }
 
diff --git a/php/WP_CLI/Fetchers/Site.php b/php/WP_CLI/Fetchers/Site.php
new file mode 100644 (file)
index 0000000..d35eaa7
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace WP_CLI\Fetchers;
+
+class Site extends Base {
+
+       protected $msg = "Could not find the site with ID %d.";
+
+       public function get( $site_id ) {
+               return $this->_get_site( $site_id );
+       }
+       
+       /**
+        * Get site (network) data for a given id.
+        *
+        * @param int     $site_id
+        * @return bool|array False if no network found with given id, array otherwise
+        */
+       private function _get_site( $site_id ) {
+               global $wpdb;
+
+               // Load site data
+               $sites = $wpdb->get_results( $wpdb->prepare(
+                       "SELECT * FROM $wpdb->site WHERE id = %d", $site_id ) );
+
+               if ( !empty( $sites ) ) {
+                       // Only care about domain and path which are set here
+                       return $sites[0];
+               }
+
+               return false;
+       }
+}
\ No newline at end of file
index 6a45b83..5fbe0e4 100644 (file)
@@ -369,6 +369,22 @@ class Comment_Command extends \WP_CLI\CommandWithDBObject {
                        WP_CLI::success( "Comment with ID $args[0] exists." );
                }
        }
+       
+       /**
+        * Get comment url
+        * 
+        * ## OPTIONS
+        *
+        * <id>
+        * : The ID of the comment to get the URL.
+        *
+        * ## EXAMPLES
+        *
+        *     wp comment url 123
+        */
+       public function url( $args ) {
+               parent::_url( $args, 'get_comment_link' );
+       }
 }
 
 WP_CLI::add_command( 'comment', 'Comment_Command' );
index d90bc93..57624f1 100644 (file)
@@ -355,6 +355,22 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
                // @codingStandardsIgnoreEnd
        }
 
+       /**
+        * Get post url
+        * 
+        * ## OPTIONS
+        *
+        * <id>
+        * : The ID of the post to get the URL.
+        *
+        * ## EXAMPLES
+        *
+        *     wp post url 123
+        */
+       public function url( $args ) {
+               parent::_url( $args, 'get_permalink' );
+       }
+
        private function maybe_make_child() {
                // 50% chance of making child post
                return ( mt_rand(1, 2) == 1 );
index 111a915..48f40b3 100644 (file)
@@ -9,6 +9,10 @@ class Site_Command extends \WP_CLI\CommandWithDBObject {
 
        protected $obj_type = 'site';
 
+       public function __construct() {
+               $this->fetcher = new \WP_CLI\Fetchers\Site;
+       }
+
        /**
         * Delete comments.
         */
@@ -217,7 +221,7 @@ class Site_Command extends \WP_CLI\CommandWithDBObject {
 
                // Site
                if ( !empty( $assoc_args['network_id'] ) ) {
-                       $site = $this->_get_site( $assoc_args['network_id'] );
+                       $site = $this->fetcher->get( $assoc_args['network_id'] );
                        if ( $site === false ) {
                                WP_CLI::error( sprintf( 'Network with id %d does not exist.', $assoc_args['network_id'] ) );
                        }
@@ -364,24 +368,19 @@ class Site_Command extends \WP_CLI\CommandWithDBObject {
        }
 
        /**
-        * Get site (network) data for a given id.
+        * Get site url
+        * 
+        * ## OPTIONS
+        *
+        * <id>
+        * : The ID of the site to get the URL.
+        *
+        * ## EXAMPLES
         *
-        * @param int     $site_id
-        * @return bool|array False if no network found with given id, array otherwise
+        *     wp site url 123
         */
-       private function _get_site( $site_id ) {
-               global $wpdb;
-
-               // Load site data
-               $sites = $wpdb->get_results( $wpdb->prepare(
-                       "SELECT * FROM $wpdb->site WHERE id = %d", $site_id ) );
-
-               if ( !empty( $sites ) ) {
-                       // Only care about domain and path which are set here
-                       return $sites[0];
-               }
-
-               return false;
+       public function url( $args ) {
+               parent::_url( $args, 'get_site_url' );
        }
 }