Skip to content

Commit ec25ca5

Browse files
committed
Fix bug in post generation
1 parent c356e0a commit ec25ca5

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/Post_Command.php

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -729,56 +729,51 @@ public function generate( $args, $assoc_args ) {
729729
'post_title' => '',
730730
];
731731

732-
$final_post_data = array_merge( $defaults, $assoc_args );
733-
$post_date_gmt = $final_post_data['post_date_gmt'];
734-
$post_date = $final_post_data['post_date'];
735-
$post_type = $final_post_data['post_type'];
736-
$post_author = $final_post_data['post_author'];
737-
$count = $final_post_data['count'];
738-
$max_depth = $final_post_data['max_depth'];
739-
$post_status = $final_post_data['post_status'];
732+
$post_data = array_merge( $defaults, $assoc_args );
740733

741734
$call_time = current_time( 'mysql' );
742735

743-
if ( false === $post_date_gmt ) {
744-
$post_date_gmt = $post_date ?: $call_time;
736+
if ( false === $post_data['post_date_gmt'] ) {
737+
$post_data['post_date_gmt'] = $post_data['post_date'] ?: $call_time;
745738
}
746739

747-
if ( false === $post_date ) {
748-
$post_date = $post_date_gmt ?: $call_time;
740+
if ( false === $post_data['post_date'] ) {
741+
$post_data['post_date'] = $post_data['post_date_gmt'] ?: $call_time;
749742
}
750743

751-
if ( ! post_type_exists( $post_type ) ) {
752-
WP_CLI::error( "'{$post_type}' is not a registered post type." );
744+
if ( ! post_type_exists( $post_data['post_type'] ) ) {
745+
WP_CLI::error( "'{$post_data['post_type']}' is not a registered post type." );
753746
}
754747

755-
if ( $post_author ) {
756-
$user_fetcher = new UserFetcher();
757-
$post_author = $user_fetcher->get_check( $post_author )->ID;
748+
if ( $post_data['post_author'] ) {
749+
$user_fetcher = new UserFetcher();
750+
$post_data['post_author'] = $user_fetcher->get_check( $post_data['post_author'] )->ID;
758751
}
759752

760753
if ( Utils\get_flag_value( $assoc_args, 'post_content' ) ) {
761754
if ( ! EntityUtils::has_stdin() ) {
762755
WP_CLI::error( 'The parameter `post_content` reads from STDIN.' );
763756
}
764757

765-
$post_content = file_get_contents( 'php://stdin' );
758+
$post_data['post_content'] = file_get_contents( 'php://stdin' );
766759
}
767760

768761
// Get the total number of posts.
769-
$total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s", $post_type ) );
762+
$total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s", $post_data['post_type'] ) );
770763

771-
$label = ! empty( $post_title ) ? $post_title : get_post_type_object( $post_type )->labels->singular_name;
764+
$label = ! empty( $post_data['post_title'] )
765+
? $post_data['post_title']
766+
: get_post_type_object( $post_data['post_type'] )->labels->singular_name;
772767

773-
$hierarchical = get_post_type_object( $post_type )->hierarchical;
768+
$hierarchical = get_post_type_object( $post_data['post_type'] )->hierarchical;
774769

775-
$limit = $count + $total;
770+
$limit = $post_data['count'] + $total;
776771

777772
$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );
778773

779774
$notify = false;
780775
if ( 'progress' === $format ) {
781-
$notify = Utils\make_progress_bar( 'Generating posts', $count );
776+
$notify = Utils\make_progress_bar( 'Generating posts', $post_data['count'] );
782777
}
783778

784779
$previous_post_id = 0;
@@ -789,7 +784,7 @@ public function generate( $args, $assoc_args ) {
789784

790785
if ( $hierarchical ) {
791786

792-
if ( $this->maybe_make_child() && $current_depth < $max_depth ) {
787+
if ( $this->maybe_make_child() && $current_depth < $post_data['max_depth'] ) {
793788

794789
$current_parent = $previous_post_id;
795790
$current_depth++;
@@ -803,15 +798,19 @@ public function generate( $args, $assoc_args ) {
803798
}
804799

805800
$args = [
806-
'post_type' => $post_type,
807-
'post_title' => ! empty( $post_title ) && $index === $total ? $label : "{$label} {$index}",
808-
'post_status' => $post_status,
809-
'post_author' => $post_author,
801+
'post_type' => $post_data['post_type'],
802+
'post_title' => ( ! empty( $post_data['post_title'] ) && $index === $total )
803+
? $label
804+
: "{$label} {$index}",
805+
'post_status' => $post_data['post_status'],
806+
'post_author' => $post_data['post_author'],
810807
'post_parent' => $current_parent,
811-
'post_name' => ! empty( $post_title ) ? sanitize_title( $post_title . ( $index === $total ? '' : "-{$index}" ) ) : "post-{$index}",
812-
'post_date' => $post_date,
813-
'post_date_gmt' => $post_date_gmt,
814-
'post_content' => $post_content,
808+
'post_name' => ! empty( $post_data['post_title'] )
809+
? sanitize_title( $post_data['post_title'] . ( $index === $total ? '' : "-{$index}" ) )
810+
: "post-{$index}",
811+
'post_date' => $post_data['post_date'],
812+
'post_date_gmt' => $post_data['post_date_gmt'],
813+
'post_content' => $post_data['post_content'],
815814
];
816815

817816
$post_id = wp_insert_post( $args, true );

0 commit comments

Comments
 (0)