OSDN Git Service

Make error messages and output codes more logical
authorgoldenapples <goldenapplesdesign@gmail.com>
Thu, 14 Feb 2013 23:29:16 +0000 (15:29 -0800)
committergoldenapples <goldenapplesdesign@gmail.com>
Thu, 14 Feb 2013 23:29:16 +0000 (15:29 -0800)
Use `WP_CLI::error` for any fatal errors. Output a warning rather than
an error if user aborts the editor (At this point it exits without
performing any action just like an error would, but it shouldn't be
considered an error. Also, its possible that commands using the editor
function will go on to perform other actions.)

Also, whitespace cleanup and minor refactoring... making a helper
method `_edit()` in post.php to make editor functions more reusable.

php/commands/post.php
php/utils.php

index 8714b7c..5a27d54 100644 (file)
@@ -20,7 +20,7 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
                        $input = ( isset( $assoc_args['post_content'] ) ) ?
                                $assoc_args['post_content'] : '';
 
-                       if ( $output = \WP_CLI\Utils\launch_editor_for_input( $input, 'WP-CLI: New Post' ) )
+                       if ( $output = $this->_edit( $input, 'WP-CLI: New Post' ) )
                                $assoc_args['post_content'] = $output;
                        else
                                $assoc_args['post_content'] = $input;
@@ -50,24 +50,23 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
        /**
         * Launch system editor to edit post content
         *
-        * @synopsis <id>...
+        * @synopsis <id>
         */
-       public function edit( $args, $assoc_args ) {
+       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.", false );
-                       return;
-               }
+               if ( !$post_id || !$post = get_post( $post_id ) )
+                        \WP_CLI::error( "Failed opening post $post_id to edit." );
 
-               $r = \WP_CLI\Utils\launch_editor_for_input( $post->post_content, "WP-CLI post $post_id"  );
+               $r = $this->_edit( $post->post_content, "WP-CLI post $post_id" );
 
-               if ( $r === false ) {
-                       \WP_CLI::error( "Aborting. No change made to post.", false );
-                       return;
-               }
+               if ( $r === false )
+                        \WP_CLI::warning( 'No change made to post content.', 'Aborted' );
+               else
+                        parent::update( $args, array( 'post_content' => $r ) );
+       }
 
-               $assoc_args['post_content'] = $r;
-               parent::update( $args, $assoc_args );
+       protected function _edit( $content, $title ) {
+               return \WP_CLI\Utils\launch_editor_for_input( $content, $title );
        }
 
        /**
index 2f3f5e6..26be917 100644 (file)
@@ -272,7 +272,7 @@ function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
        $tmpfile = wp_tempnam( $title );
 
        if ( !$tmpfile )
-               die( 'Error creating temporary file.' );
+               \WP_CLI::error( 'Error creating temporary file.' );
 
        file_put_contents( $tmpfile, $input );