OSDN Git Service

Use file_put_contents/file_get_contents instead of fwrite/fread
authorgoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 23:59:24 +0000 (15:59 -0800)
committergoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 23:59:24 +0000 (15:59 -0800)
Much simpler this way. Also does away with the need to check for empty
output, although now we have to check the return value of
`launch_editor_for_input` strictly against false, since its possible for
it to return an empty string now (editing a post and deleting all the
content).

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

index f4b8f0b..8714b7c 100644 (file)
@@ -61,7 +61,7 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
 
                $r = \WP_CLI\Utils\launch_editor_for_input( $post->post_content, "WP-CLI post $post_id"  );
 
-               if ( !$r ) {
+               if ( $r === false ) {
                        \WP_CLI::error( "Aborting. No change made to post.", false );
                        return;
                }
index bb0ee3d..2f3f5e6 100644 (file)
@@ -274,20 +274,12 @@ function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
        if ( !$tmpfile )
                die( 'Error creating temporary file.' );
 
-       $handle = fopen( $tmpfile, 'w+t' );
-       fwrite( $handle, $input );
-       fclose( $handle );
+       file_put_contents( $tmpfile, $input );
 
        \WP_CLI::launch( "\${EDITOR:-vi} '$tmpfile'" );
 
-       $filesize = filesize( $tmpfile );
+       $output = file_get_contents( $tmpfile );
 
-       if ( $filesize === 0 )
-               return false;
-
-       $handle = fopen( $tmpfile, 'r' );
-       $output = fread( $handle, $filesize );
-       fclose( $handle );
        unlink( $tmpfile );
 
        if ( $output === $input )