Skip to content

Commit

Permalink
feature: add seed subscriptions cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 18, 2023
1 parent 394a57c commit cbe0996
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
3 changes: 2 additions & 1 deletion includes/class-command-line-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
57 changes: 21 additions & 36 deletions includes/commands/class-seed-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use WP\Notifications;
use WP\Notifications\Subscription_Factory;
use WP_CLI;

/**
Expand All @@ -21,41 +22,34 @@ class Seed_Subscriptions {
*
* ## OPTIONS
*
* [--all=<all>]
* : Create subscriptions for all users to all channels.
* default: 10
*
* [--preview=<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
*/
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,
)
);
}
Expand All @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions wp-feature-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit cbe0996

Please sign in to comment.