Last active
March 31, 2026 20:02
-
-
Save k2sobot/fe0eeceee510f8cffdfe63915f672b50 to your computer and use it in GitHub Desktop.
WP-CLI command to generate test login records for When Last Login plugin (wll_records CPT)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WP-CLI command to generate test login records for When Last Login. | |
| * | |
| * INSTALLATION: | |
| * Add this line to your child theme's functions.php or any plugin file: | |
| * require_once __DIR__ . '/wll-generate-records.php'; | |
| * | |
| * USAGE: | |
| * wp wll-generate-records 1000 | |
| * wp wll-generate-records 5000 --user_id=1 | |
| * wp wll-generate-records 10000 --days=365 | |
| * | |
| * @package When_Last_Login | |
| */ | |
| // Bail if not WP-CLI. | |
| if ( ! defined( 'WP_CLI' ) ) { | |
| return; | |
| } | |
| /** | |
| * Generate test login records for When Last Login plugin. | |
| */ | |
| class WLL_Generate_Records_Command extends WP_CLI_Command { | |
| /** | |
| * Generate test login records. | |
| * | |
| * ## OPTIONS | |
| * | |
| * <count> | |
| * : Number of login records to generate. | |
| * | |
| * [--user_id=<user_id>] | |
| * : Specific user ID to associate records with. If not provided, uses random existing users. | |
| * | |
| * [--days=<days>] | |
| * : Spread records over this many days in the past. Default: 90 | |
| * | |
| * [--batch-size=<batch-size>] | |
| * : Number of records to insert per batch. Default: 100 | |
| * | |
| * ## EXAMPLES | |
| * | |
| * # Generate 1000 login records | |
| * wp wll-generate-records 1000 | |
| * | |
| * # Generate 5000 records for user ID 1 | |
| * wp wll-generate-records 5000 --user_id=1 | |
| * | |
| * # Generate 10000 records spread over a year | |
| * wp wll-generate-records 10000 --days=365 | |
| * | |
| * # Generate with larger batch size for speed | |
| * wp wll-generate-records 50000 --batch-size=500 | |
| * | |
| * @when after_wp_load | |
| */ | |
| public function __invoke( $args, $assoc_args ) { | |
| $count = isset( $args[0] ) ? (int) $args[0] : 0; | |
| if ( $count < 1 ) { | |
| WP_CLI::error( 'Please provide a valid count greater than 0.' ); | |
| } | |
| $user_id = isset( $assoc_args['user_id'] ) ? (int) $assoc_args['user_id'] : 0; | |
| $days = isset( $assoc_args['days'] ) ? (int) $assoc_args['days'] : 90; | |
| $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; | |
| // Get users. | |
| if ( $user_id > 0 ) { | |
| $user = get_user_by( 'id', $user_id ); | |
| if ( ! $user ) { | |
| WP_CLI::error( "User ID {$user_id} not found." ); | |
| } | |
| $users = array( $user ); | |
| } else { | |
| $users = get_users( array( | |
| 'number' => 50, | |
| 'fields' => array( 'ID', 'display_name' ) | |
| ) ); | |
| if ( empty( $users ) ) { | |
| WP_CLI::error( 'No users found. Please create users first.' ); | |
| } | |
| } | |
| $user_count = count( $users ); | |
| WP_CLI::log( "Generating {$count} login records using {$user_count} user(s) over {$days} days..." ); | |
| $progress = \WP_CLI\Utils\make_progress_bar( 'Creating records', $count ); | |
| $batches = ceil( $count / $batch_size ); | |
| $created = 0; | |
| // IP addresses pool for variety. | |
| $ip_pool = $this->generate_ip_pool( 50 ); | |
| // Browsers and OS pools. | |
| $browsers = array( 'Chrome', 'Firefox', 'Safari', 'Edge', 'Opera' ); | |
| $os_list = array( 'Windows 10', 'Windows 11', 'macOS', 'Linux', 'Android', 'iOS' ); | |
| $devices = array( 'Desktop', 'Mobile', 'Tablet' ); | |
| for ( $b = 0; $b < $batches; $b++ ) { | |
| $batch_count = min( $batch_size, $count - $created ); | |
| for ( $i = 0; $i < $batch_count; $i++ ) { | |
| // Pick random user. | |
| $user = $users[ array_rand( $users ) ]; | |
| $user_id_actual = is_object( $user ) ? $user->ID : $user; | |
| $display_name = is_object( $user ) ? $user->display_name : 'User'; | |
| // Generate random timestamp within the date range. | |
| $timestamp = time() - rand( 0, $days * DAY_IN_SECONDS ); | |
| $date = date( 'Y-m-d H:i:s', $timestamp ); | |
| // Random IP. | |
| $ip = $ip_pool[ array_rand( $ip_pool ) ]; | |
| // Create post title. | |
| $post_title = $display_name . ' has logged in at ' . $date; | |
| // Insert the post. | |
| $post_id = wp_insert_post( array( | |
| 'post_title' => $post_title, | |
| 'post_status' => 'publish', | |
| 'post_author' => $user_id_actual, | |
| 'post_type' => 'wll_records', | |
| 'post_date' => $date, | |
| 'post_date_gmt' => get_gmt_from_date( $date ), | |
| ), true ); | |
| if ( ! is_wp_error( $post_id ) ) { | |
| // Add IP address meta. | |
| update_post_meta( $post_id, 'wll_user_ip_address', $ip ); | |
| // Optional: Add additional meta for testing. | |
| update_post_meta( $post_id, 'wll_browser', $browsers[ array_rand( $browsers ) ] ); | |
| update_post_meta( $post_id, 'wll_os', $os_list[ array_rand( $os_list ) ] ); | |
| update_post_meta( $post_id, 'wll_device', $devices[ array_rand( $devices ) ] ); | |
| } | |
| $progress->tick(); | |
| $created++; | |
| } | |
| // Clear cache periodically to avoid memory issues. | |
| if ( $b > 0 && $b % 10 === 0 ) { | |
| wp_cache_flush(); | |
| } | |
| } | |
| $progress->finish(); | |
| WP_CLI::success( "Created {$created} login records." ); | |
| WP_CLI::log( "View them at: " . admin_url( 'edit.php?post_type=wll_records' ) ); | |
| } | |
| /** | |
| * Generate a pool of random IP addresses. | |
| * | |
| * @param int $count Number of IPs to generate. | |
| * @return array | |
| */ | |
| private function generate_ip_pool( $count = 50 ) { | |
| $ips = array(); | |
| // Add some common IP patterns. | |
| $common = array( | |
| '192.168.1.', | |
| '10.0.0.', | |
| '172.16.0.', | |
| '192.168.0.', | |
| ); | |
| for ( $i = 0; $i < $count; $i++ ) { | |
| $prefix = $common[ $i % count( $common ) ]; | |
| $ips[] = $prefix . rand( 1, 254 ); | |
| } | |
| return $ips; | |
| } | |
| } | |
| // Register the command. | |
| WP_CLI::add_command( 'wll-generate-records', 'WLL_Generate_Records_Command' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment