From cbe0996e3be7dbb389a6c99a2b5aef5499bfea6d Mon Sep 17 00:00:00 2001 From: John Hooks Date: Mon, 17 Apr 2023 22:51:23 -0700 Subject: [PATCH] feature: add seed subscriptions cli command --- includes/class-command-line-interface.php | 3 +- .../commands/class-seed-subscriptions.php | 57 +++++++------------ wp-feature-notifications.php | 1 + 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/includes/class-command-line-interface.php b/includes/class-command-line-interface.php index c99e0101..a7b29270 100644 --- a/includes/class-command-line-interface.php +++ b/includes/class-command-line-interface.php @@ -29,6 +29,7 @@ static function boot() { */ static function add_commands() { WP_CLI::add_command( 'notifications channel list', Commands\Channel_List::class ); - WP_CLI::add_command( 'notifications seed-users', Commands\Seed_Users::class ); + WP_CLI::add_command( 'notifications seed subscriptions', Commands\Seed_Subscriptions::class ); + WP_CLI::add_command( 'notifications seed users', Commands\Seed_Users::class ); } } diff --git a/includes/commands/class-seed-subscriptions.php b/includes/commands/class-seed-subscriptions.php index 48f4506f..6f47b96b 100644 --- a/includes/commands/class-seed-subscriptions.php +++ b/includes/commands/class-seed-subscriptions.php @@ -4,6 +4,7 @@ use Exception; use WP\Notifications; +use WP\Notifications\Subscription_Factory; use WP_CLI; /** @@ -21,17 +22,13 @@ class Seed_Subscriptions { * * ## OPTIONS * - * [--all=] - * : Create subscriptions for all users to all channels. - * default: 10 - * * [--preview=] * : Preview generated data * default: false' * * ## EXAMPLES * - * wp notifications seed-users --count=10 --preview=true + * wp notifications seed subscriptions --preview=true * * @when after_wp_load */ @@ -39,23 +36,20 @@ public function __invoke( $args, $assoc_args ) { global $wpdb; // Get CLI args - $all = WP_CLI\Utils\get_flag_value( $assoc_args, 'all', $default = false ); $preview = WP_CLI\Utils\get_flag_value( $assoc_args, 'preview', $default = false ); try { - $channels = Notifications\Channel_Registry::get_instance()->get_all_registered(); + $channels = Notifications\Channel_Registry::get_instance()->get_all_registered(); + /** + * @var Notifications\Subscription[] + */ $subscriptions = array(); - for ( $i = 0; $i < $count; $i++ ) { - array_push( - $users, + foreach ( $channels as $channel ) { + $subscriptions[] = Subscription_Factory::get_instance()->make( array( - 'user_login' => $faker->unique()->userName(), - 'user_email' => $faker->unique()->email(), - 'user_pass' => 'password', - 'first_name' => $faker->firstName(), - 'last_name' => $faker->lastName(), - 'role' => 'author', + 'channel_name' => $channel->get_name(), + 'user_id' => 1, ) ); } @@ -66,31 +60,22 @@ public function __invoke( $args, $assoc_args ) { if ( $preview ) { WP_CLI\Utils\format_items( 'table', - $users, - 'user_login,user_email,user_pass,role' + array_map( + function ( Notifications\Subscription $subscription ) { + return $subscription->jsonSerialize(); + }, + $subscriptions + ), + 'channel_name,user_id,snoozed_until' ); } else { - $progress = WP_CLI\Utils\make_progress_bar( 'Generating ', $count ); + $progress = WP_CLI\Utils\make_progress_bar( 'Generating ', count( $subscriptions ) ); try { - /** - * @var int[] - */ - $user_ids = array(); - - foreach ( $users as $user ) { - $user_id = wp_insert_user( - wp_slash( - $user - ) - ); - - if ( is_wp_error( $user_id ) ) { - throw $user_id; - } - - array_push( $user_ids, $user_id ); + $subscription_repo = Notifications\Subscription_Repository::get_instance(); + foreach ( $subscriptions as $subscription ) { + $subscription_repo->insert( $subscription ); $progress->tick(); } diff --git a/wp-feature-notifications.php b/wp-feature-notifications.php index 6c194460..2c03ccaa 100644 --- a/wp-feature-notifications.php +++ b/wp-feature-notifications.php @@ -79,6 +79,7 @@ require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/demo.php'; require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/restapi/class-notification-controller.php'; require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/commands/class-channel-list.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/commands/class-seed-subscriptions.php'; require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/commands/class-seed-users.php'; require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/class-command-line-interface.php';