Created
May 2, 2015 14:00
-
-
Save gedex/fd27a2c40877b6bce0ab to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Generate Data (users and posts). | |
*/ | |
class Gen_Data_Command extends WP_CLI_Command { | |
const DEFAULT_GEN_NUM_USERS = 2; | |
const DEFAULT_GEN_NUM_POSTS = 1; | |
const GEN_USERS_OPTION = 'gen_data_users'; | |
const GEN_POSTS_OPTION = 'gen_data_posts'; | |
/** | |
* Generate Data. | |
* | |
* ## OPTIONS | |
* | |
* [--num_users] | |
* : Number of users to generate, default to 2 | |
* | |
* [--num_posts] | |
* : Number of posts to generate for each user, default to 1 | |
* | |
* [--include_cpt] | |
* : Whether to generate posts in CPT too | |
* | |
* ## EXAMPLES | |
* | |
* wp gen-data generate | |
*/ | |
public function generate( $args = array(), $assoc_args = array() ) { | |
$assoc_args = wp_parse_args( | |
$assoc_args, | |
array( | |
'num_users' => self::DEFAULT_GEN_NUM_USERS, | |
'num_posts' => self::DEFAULT_GEN_NUM_POSTS, | |
'include_cpt' => 0, | |
) | |
); | |
$assoc_args = array_map( 'absint', $assoc_args ); | |
if ( ! $assoc_args['num_users'] || ! $assoc_args['num_posts'] ) { | |
WP_CLI::error( __( 'num_users and num_posts must be non-zero', 'gen-data' ) ); | |
return; | |
} | |
// Generate users. | |
$users = $this->generate_users( $assoc_args['num_users'] ); | |
// Generate posts for each user. | |
foreach ( $users as $role => $user_ids ) { | |
foreach ( $user_ids as $user_id ) { | |
$this->generate_posts( | |
$assoc_args['num_posts'], | |
$user_id, | |
$assoc_args['include_cpt'] | |
); | |
} | |
} | |
} | |
/** | |
* Generate users data. | |
* | |
* @param int $count Number of users to create | |
* @return array Generated users, where key is role name and value is | |
* an array containing user IDs | |
*/ | |
protected function generate_users( $count ) { | |
$roles = get_editable_roles(); | |
unset( $roles['administrator'] ); | |
$generated_users = $this->get_previous_generated_users(); | |
WP_CLI::line( sprintf( __( 'Generating %d users, %d user for each role..', 'gen-data' ), $count * count( $roles ), $count ) ); | |
foreach ( $roles as $role => $info ) { | |
for ( $i = 1; $i <= $count; $i++ ) { | |
$login = sprintf( '%s_%d', $role, $i ); | |
$user_id = wp_insert_user( array( | |
'user_login' => $login, | |
'user_pass' => '123', | |
'role' => $role, | |
) ); | |
if ( $user_id && ! is_wp_error( $user_id ) ) { | |
$generated_users[ $role ][ $user_id ] = $user_id; | |
WP_CLI::success( sprintf( __( 'User %s (ID %d) is successfully created.', 'gen-data' ), $login, $user_id ) ); | |
} else { | |
WP_CLI::error( | |
sprintf( | |
__( 'Error creating user %s: %s', 'gen-data' ), | |
$login, | |
is_wp_error( $user_id ) ? $user_id->get_error_message() : __( 'Unknown error', 'gen-data' ) | |
) | |
); | |
} | |
} | |
} | |
$this->save_generated_users( $generated_users ); | |
return $generated_users; | |
} | |
/** | |
* Get previous generated users that's saved in options. | |
* | |
* @return array | |
*/ | |
protected function get_previous_generated_users() { | |
return get_option( self::GEN_USERS_OPTION, array() ); | |
} | |
/** | |
* Save generated users into options. | |
* | |
* @return bool | |
*/ | |
protected function save_generated_users( $users = array() ) { | |
return update_option( self::GEN_USERS_OPTION, $users ); | |
} | |
/** | |
* Generate posts data. | |
* | |
* @param int $count Number of posts to create | |
* @param int $user_id User to associate with the posts as author | |
* @return void | |
*/ | |
protected function generate_posts( $count, $user_id, $include_cpt = false ) { | |
$post_types = array( 'post' => 'post' ); | |
if ( $include_cpt ) { | |
$post_types = array_merge( | |
$post_types, | |
get_post_types( array( | |
'public' => true, | |
'_builtin' => false, | |
) ) | |
); | |
} | |
$statuses = array( 'draft', 'publish', 'future' ); | |
$total_posts = $count * count( $post_types ) * count( $statuses ); | |
$generated_posts = array(); | |
$user = get_user_by( 'id', $user_id ); | |
$pg_bar = \WP_CLI\Utils\make_progress_bar( | |
sprintf( | |
__( 'Generating %d posts (%d post type(s), %d post statuses) for user %s', 'gen-data' ), | |
$total_posts, | |
count( $post_types ), | |
count( $statuses ), | |
$user->get( 'user_login' ) | |
), | |
$total_posts | |
); | |
foreach ( $post_types as $post_type ) { | |
foreach ( $statuses as $status ) { | |
for ( $i = 1; $i <= $count; $i++ ) { | |
$args = array( | |
'post_title' => sprintf( '%s #%d by %s', $post_type, $i, $user->get( 'user_login' ) ), | |
'post_content' => '', | |
'post_status' => $status, | |
'post_author' => $user_id, | |
'post_type' => $post_type, | |
); | |
if ( 'future' === $status ) { | |
$args['post_date'] = date( 'Y-m-d H:i:s', strtotime( '+1 month', current_time( 'timestamp' ) ) ); | |
} | |
$post_id = wp_insert_post( $args ); | |
if ( $post_id && ! is_wp_error( $post_id ) ) { | |
$generated_posts[] = $post_id; | |
} | |
$pg_bar->tick(); | |
} | |
} | |
} | |
$pg_bar->finish(); | |
$this->save_generated_posts( $user_id, $generated_posts ); | |
if ( count( $generated_posts ) === $total_posts ) { | |
WP_CLI::success( sprintf( __( 'Successfully generating %d posts for user %s', 'gen-data' ), $total_posts, $user->get( 'user_login' ) ) ); | |
} elseif ( count( $generated_posts ) > 0 ) { | |
WP_CLI::warning( sprintf( __( 'Only %d posts were created, %d failed', 'gen-data' ), count( $generated_posts ), $total_posts - count( $generated_posts ) ) ); | |
} else { | |
WP_CLI::error( sprintf( __( 'No posts generated for user %s', 'gen-data' ), $user->get( 'user_login' ) ) ); | |
} | |
} | |
/** | |
* Get previous generated posts for given user ID that's saved in options. | |
* | |
* @param int $user_id User ID | |
* @return array | |
*/ | |
protected function get_previous_generated_posts( $user_id ) { | |
return get_option( self::GEN_POSTS_OPTION . '_' . $user_id, array() ); | |
} | |
/** | |
* Save generated posts of given user ID into options. | |
* | |
* @param int $user_id User ID | |
* @return bool | |
*/ | |
protected function save_generated_posts( $user_id, $posts = array() ) { | |
return update_option( self::GEN_POSTS_OPTION . '_' . $user_id, $posts ); | |
} | |
/** | |
* Remove previously generated data. | |
* | |
* @return void | |
*/ | |
public function remove_generated() { | |
$generated_users = $this->get_previous_generated_users(); | |
if ( empty( $generated_users ) ) { | |
WP_CLI::line( __( 'Generated data found is not found', 'gen-data' ) ); | |
return; | |
} | |
foreach ( $generated_users as $role => $user_ids ) { | |
foreach ( $user_ids as $user_id => $__ ) { | |
$succeed = wp_delete_user( $user_id ); | |
if ( $succeed ) { | |
unset( $generated_users[ $role ][ $user_id ] ); | |
$this->save_generated_posts( $user_id, array() ); | |
WP_CLI::success( sprintf( __( 'User with ID %d is successfully deleted.', 'gen-data' ), $user_id ) ); | |
} else { | |
WP_CLI::success( sprintf( __( 'Failed to delete User with ID %d.', 'gen-data' ), $user_id ) ); | |
} | |
} | |
if ( empty( $generated_users[ $role ] ) ) { | |
unset( $generated_users[ $role ] ); | |
} | |
} | |
$this->save_generated_users( $generated_users ); | |
} | |
} | |
WP_CLI::add_command( 'gen-data', 'Gen_Data_Command' ); |
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 | |
/** | |
* Plugin Name: Generate data | |
* Plugin URI: gedex.web.id | |
* Description: Generate data for drafts for friends. Useful for development. | |
* Version: 0.1.0 | |
* Author: Akeda Bagus | |
* Author URI: gedex.web.id | |
* License: GPLv2 or later | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Text Domain: gen-data | |
*/ | |
function gen_data_cli_init() { | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
include dirname( __FILE__ ) . '/gen-data-command.php'; | |
} | |
} | |
add_action( 'plugins_loaded', 'gen_data_cli_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment