From 80a6b2131f86f47f4213045a3d344babad5229b2 Mon Sep 17 00:00:00 2001 From: Nik McLaughlin <72171383+nikmclaughlin@users.noreply.github.com> Date: Thu, 27 Nov 2025 15:48:10 -0800 Subject: [PATCH 1/3] init Review generator class --- includes/Generator/Review.php | 194 ++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 includes/Generator/Review.php diff --git a/includes/Generator/Review.php b/includes/Generator/Review.php new file mode 100644 index 0000000..8d91e59 --- /dev/null +++ b/includes/Generator/Review.php @@ -0,0 +1,194 @@ + $product_id, + 'user_id' => $customer->get_id(), + 'comment_author' => $customer->get_display_name(), + 'comment_author_email' => $customer->get_email(), + 'comment_content' => self::$fake_reviews[ array_rand( self::$fake_reviews ) ], + 'comment_type' => 'review', + 'comment_approved' => 1, + 'rating' => wp_rand( 1, 5 ), + ); + $result = wp_insert_comment( $review ); + + if ( ! $result ) { + return false; + } + + if ( $save ) { + wc_get_product( $product_id )->save(); + } + + $review = get_comment( $result, 'OBJECT' ); + + /** + * Action: Review generator returned a new review. + * + * @since 1.2.0 + * + * @param \WP_Comment $review + */ + do_action( 'smoothgenerator_review_generated', $review ); + + return $review; + } + + /** + * Create multiple reviews. + * + * @param int $amount The number of reviews to create. + * + * @return int[]|\WP_Error + */ + public static function batch( $amount ) { + $amount = self::validate_batch_amount( $amount ); + if ( is_wp_error( $amount ) ) { + return $amount; + } + + $review_ids = array(); + + for ( $i = 1; $i <= $amount; $i++ ) { + $review = self::generate(); + $review_ids[] = $review->comment_ID; + } + + return $review_ids; + } + + /** + * Get randomly selected existing customer. + * + * @return \WC_Customer + */ + protected static function get_existing_customer() { + global $wpdb; + + $total_users = (int) $wpdb->get_var( "SELECT COUNT( * ) FROM {$wpdb->users}" ); + $customer_offset = wp_rand( 0, $total_users ); + $user_id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->users} ORDER BY rand() LIMIT $customer_offset, 1" ); // phpcs:ignore + + return new \WC_Customer( $user_id ); + } + + /** + * Get ID of randomly selected existing product. + * + * @return int ID of random product. + */ + protected static function get_random_product_id() { + global $wpdb; + + $num_existing_products = (int) $wpdb->get_var( + "SELECT COUNT( DISTINCT ID ) + FROM {$wpdb->posts} + WHERE 1=1 + AND post_type='product' + AND post_status='publish'" + ); + + $product_offset = wp_rand( 0, $num_existing_products ); + + $product_id = (int) wc_get_products( array( + 'orderby' => 'rand', + 'limit' => 1, + 'return' => 'ids', + 'offset' => $product_offset, + ))[0]; + + if ( ! wc_get_product( $product_id ) instanceof \WC_Product ) { + return false; + } + + return $product_id; + } +} From 2b18450f975c3cdff059aae64d8412068634177e Mon Sep 17 00:00:00 2001 From: Nik McLaughlin <72171383+nikmclaughlin@users.noreply.github.com> Date: Thu, 27 Nov 2025 15:48:48 -0800 Subject: [PATCH 2/3] add reviews command --- includes/CLI.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/includes/CLI.php b/includes/CLI.php index 823b3fe..49324e1 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -255,6 +255,51 @@ function () use ( $progress ) { WP_CLI::success( $generated . ' terms generated in ' . $display_time ); } + + /** + * Generate customer product reviews. + * + * @param array $args Arguments specified. + */ + public static function reviews( $args ) { + list( $amount ) = $args; + $amount = absint( $amount ); + + $time_start = microtime( true ); + + $progress = \WP_CLI\Utils\make_progress_bar( 'Generating reviews', $amount ); + + add_action( + 'smoothgenerator_review_generated', + function () use ( $progress ) { + $progress->tick(); + } + ); + + $remaining_amount = $amount; + $generated = 0; + + while ( $remaining_amount > 0 ) { + $batch = min( $remaining_amount, Generator\Review::MAX_BATCH_SIZE ); + + $result = Generator\Review::batch( $batch ); + + if ( is_wp_error( $result ) ) { + WP_CLI::error( $result ); + } + + $generated += count( $result ); + $remaining_amount -= $batch; + } + + $progress->finish(); + + $time_end = microtime( true ); + $execution_time = round( ( $time_end - $time_start ), 2 ); + $display_time = $execution_time < 60 ? $execution_time . ' seconds' : human_time_diff( $time_start, $time_end ); + + WP_CLI::success( $generated . ' reviews generated in ' . $display_time ); + } } WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ), array( @@ -438,3 +483,17 @@ function () use ( $progress ) { ), 'longdesc' => "## EXAMPLES\n\nwc generate terms product_tag 10\n\nwc generate terms product_cat 50 --max-depth=3", ) ); + +WP_CLI::add_command( 'wc generate reviews', array( 'WC\SmoothGenerator\CLI', 'reviews' ), array( + 'shortdesc' => 'Generate reviews.', + 'synopsis' => array( + array( + 'name' => 'amount', + 'type' => 'positional', + 'description' => 'The number of reviews to generate.', + 'optional' => true, + 'default' => 10, + ), + ), + 'longdesc' => "## EXAMPLES\n\nwc generate reviews 10\n", +)); From 652046c297ee3cf681aecd87967a4b77803c8f06 Mon Sep 17 00:00:00 2001 From: Nik McLaughlin <72171383+nikmclaughlin@users.noreply.github.com> Date: Thu, 27 Nov 2025 15:49:07 -0800 Subject: [PATCH 3/3] add review generator to router --- includes/Router.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/Router.php b/includes/Router.php index fbe37a7..0a0f790 100644 --- a/includes/Router.php +++ b/includes/Router.php @@ -15,6 +15,7 @@ class Router { 'orders' => Generator\Order::class, 'products' => Generator\Product::class, 'terms' => Generator\Term::class, + 'reviews' => Generator\Review::class, ); /**