OSDN Git Service

Use WP_CLI::launch to open editor
authorgoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 19:21:20 +0000 (11:21 -0800)
committergoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 19:21:20 +0000 (11:21 -0800)
Also use wp_tempnam to create file, and give the created file a
meaningful name rather than just using system or php's tempfile name.

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

index d741b88..f4b8f0b 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 ) )
+                       if ( $output = \WP_CLI\Utils\launch_editor_for_input( $input, 'WP-CLI: New Post' ) )
                                $assoc_args['post_content'] = $output;
                        else
                                $assoc_args['post_content'] = $input;
@@ -59,7 +59,7 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
                        return;
                }
 
-               $r = \WP_CLI\Utils\launch_editor_for_input( $post->post_content );
+               $r = \WP_CLI\Utils\launch_editor_for_input( $post->post_content, "WP-CLI post $post_id"  );
 
                if ( !$r ) {
                        \WP_CLI::error( "Aborting. No change made to post.", false );
index c68545a..68cc356 100644 (file)
@@ -265,10 +265,11 @@ function output_csv( $rows, $headers = array() ) {
  *
  * @param      str     $content        Text to edit (eg post content)
  * @return             str|bool        Edited text, if file is saved from editor
+ *                                                     False, if no change to file
  */
-function launch_editor_for_input( $input ) {
+function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
 
-       $tmpfile = tempnam( '/tmp', 'wp-cli' );
+       $tmpfile = wp_tempnam( $title );
 
        if ( !$tmpfile )
                die( 'Error creating temporary file.' );
@@ -277,7 +278,7 @@ function launch_editor_for_input( $input ) {
        fwrite( $handle, $input );
        fclose( $handle );
 
-       $out = `\${EDITOR:-vi} "$tmpfile" 3>&1 1>&2 2>&3 || exit $?`;
+       \WP_CLI::launch( "\${EDITOR:-vi} '$tmpfile'" );
 
        $handle = fopen( $tmpfile, 'r' );
        $output = fread( $handle, filesize( $tmpfile )  );