@@ -836,7 +836,7 @@ public function list_caps( $args, $assoc_args ) {
836836 * ## OPTIONS
837837 *
838838 * <file>
839- * : The local or remote CSV file of users to import.
839+ * : The local or remote CSV file of users to import. If '-', then reads from STDIN.
840840 *
841841 * [--send-email]
842842 * : Send an email to new users with their account details.
@@ -876,15 +876,40 @@ public function import_csv( $args, $assoc_args ) {
876876 if ( in_array ( $ response_code [0 ], array ( 4 , 5 ) ) ) {
877877 WP_CLI ::error ( "Couldn't access remote CSV file (HTTP {$ response_code } response). " );
878878 }
879- } else if ( ! file_exists ( $ filename ) ) {
879+ } elseif ( '- ' === $ filename ) {
880+ if ( ! WP_CLI \Entity \Utils::has_stdin () ) {
881+ \WP_CLI ::error ( "Unable to read content from STDIN. " );
882+ }
883+ } elseif ( ! file_exists ( $ filename ) ) {
880884 WP_CLI ::error ( sprintf ( "Missing file: %s " , $ filename ) );
881885 }
882886
883887 // Don't send core's emails during the creation / update process
884888 add_filter ( 'send_password_change_email ' , '__return_false ' );
885889 add_filter ( 'send_email_change_email ' , '__return_false ' );
886890
887- foreach ( new \WP_CLI \Iterators \CSV ( $ filename ) as $ i => $ new_user ) {
891+ if ( '- ' === $ filename && WP_CLI \Entity \Utils::has_stdin () ) {
892+ $ file_object = new NoRewindIterator ( new SplFileObject ( "php://stdin " ) );
893+ $ file_object ->setFlags ( SplFileObject::READ_CSV );
894+ $ csv_data = array ();
895+ $ indexes = array ();
896+ foreach ( $ file_object as $ line ) {
897+ if ( empty ( $ line [0 ] ) ) {
898+ continue ;
899+ } elseif ( empty ( $ indexes ) ) {
900+ $ indexes = $ line ;
901+ continue ;
902+ }
903+ foreach ( $ indexes as $ n => $ key ) {
904+ $ data [ $ key ] = $ line [ $ n ];
905+ }
906+ $ csv_data [] = $ data ;
907+ }
908+ } else {
909+ $ csv_data = new \WP_CLI \Iterators \CSV ( $ filename );
910+ }
911+
912+ foreach ( $ csv_data as $ i => $ new_user ) {
888913 $ defaults = array (
889914 'role ' => get_option ('default_role ' ),
890915 'user_pass ' => wp_generate_password (),
0 commit comments