/**
  * Import images from Unsplash into your Media Library.
  *
  * ## OPTIONS
  *
  * [--count=<number>]
  * : How many media items to generate. Default: 10
  *
  * [--media_author=<login>]
  * : The author of the generated media. Default: none
  *
  * [--media_date=<yyyy-mm-dd|random>]
  * : The date of the generated media. Default: current date
  *
  * [--media_dimensions=<dimensions>]
  * : The dimensions of the generated media. Default: none
  *
  * ## EXAMPLES
  *
  *     wp unsplash --count=10
  *     wp unsplash --media_date=random
  *     wp unsplash --media_dimensions=1080x720
  */
 public function __invoke($args, $assoc_args = array())
 {
     $defaults = array('count' => 10, 'media_author' => false, 'media_date' => current_time('mysql'), 'media_dimensions' => false);
     extract(array_merge($defaults, $assoc_args), EXTR_SKIP);
     if ($media_author) {
         $user_fetcher = new \WP_CLI\Fetchers\User();
         $media_author = $user_fetcher->get_check($media_author)->ID;
     }
     $url = 'https://source.unsplash.com/random/';
     if ($media_dimensions) {
         $url .= $media_dimensions;
     }
     $notify = \WP_CLI\Utils\make_progress_bar('Generating media', $count);
     for ($i = 0; $i < $count; $i++) {
         $tmp_file = download_url($url);
         if (!is_wp_error($tmp_file)) {
             $this->_process_downloaded_image($tmp_file, $media_author, $media_date);
         } else {
             WP_CLI::warning('Could not download image from Unsplash API.');
         }
         if (file_exists($tmp_file)) {
             unlink($tmp_file);
         }
         $notify->tick();
     }
     $notify->finish();
 }
Esempio n. 2
0
 /**
  * Generate some posts.
  *
  * ## OPTIONS
  *
  * [--count=<number>]
  * : How many posts to generate. Default: 100
  *
  * [--post_type=<type>]
  * : The type of the generated posts. Default: 'post'
  *
  * [--post_status=<status>]
  * : The status of the generated posts. Default: 'publish'
  *
  * [--post_author=<login>]
  * : The author of the generated posts. Default: none
  *
  * [--post_date=<yyyy-mm-dd>]
  * : The date of the generated posts. Default: current date
  *
  * [--post_content]
  * : If set, the command reads the post_content from STDIN.
  *
  * [--max_depth=<number>]
  * : For hierarchical post types, generate child posts down to a certain depth. Default: 1
  *
  * ## EXAMPLES
  *
  *     wp post generate --count=10 --post_type=page --post_date=1999-01-04
  *     curl http://loripsum.net/api/5 | wp post generate --post_content --count=10
  */
 public function generate($args, $assoc_args)
 {
     global $wpdb;
     $defaults = array('count' => 100, 'max_depth' => 1, 'post_type' => 'post', 'post_status' => 'publish', 'post_author' => false, 'post_date' => current_time('mysql'), 'post_content' => '');
     extract(array_merge($defaults, $assoc_args), EXTR_SKIP);
     // @codingStandardsIgnoreStart
     if (!post_type_exists($post_type)) {
         WP_CLI::error(sprintf("'%s' is not a registered post type.", $post_type));
     }
     if ($post_author) {
         $user_fetcher = new \WP_CLI\Fetchers\User();
         $post_author = $user_fetcher->get_check($post_author)->ID;
     }
     if (\WP_CLI\Utils\get_flag_value($assoc_args, 'post_content')) {
         $post_content = file_get_contents('php://stdin');
     }
     // Get the total number of posts
     $total = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s", $post_type));
     $label = get_post_type_object($post_type)->labels->singular_name;
     $hierarchical = get_post_type_object($post_type)->hierarchical;
     $limit = $count + $total;
     $notify = \WP_CLI\Utils\make_progress_bar('Generating posts', $count);
     $previous_post_id = 0;
     $current_depth = 1;
     $current_parent = 0;
     for ($i = $total; $i < $limit; $i++) {
         if ($hierarchical) {
             if ($this->maybe_make_child() && $current_depth < $max_depth) {
                 $current_parent = $previous_post_id;
                 $current_depth++;
             } else {
                 if ($this->maybe_reset_depth()) {
                     $current_depth = 1;
                     $current_parent = 0;
                 }
             }
         }
         $args = array('post_type' => $post_type, 'post_title' => "{$label} {$i}", 'post_status' => $post_status, 'post_author' => $post_author, 'post_parent' => $current_parent, 'post_name' => "post-{$i}", 'post_date' => $post_date, 'post_content' => $post_content);
         $post_id = wp_insert_post($args, true);
         if (is_wp_error($post_id)) {
             WP_CLI::warning($post_id);
         } else {
             $previous_post_id = $post_id;
         }
         $notify->tick();
     }
     $notify->finish();
     // @codingStandardsIgnoreEnd
 }
Esempio n. 3
0
if (!class_exists('WP_CLI')) {
    return;
}
/**
 * Reset passwords for one or more WordPress users.
 *
 * ## OPTIONS
 *
 * <user>...
 * : Specify one or more user logins or IDs.
 *
 * [--skip-email]
 * : Don't send an email notification to the affected user(s).
 */
$reset_password_command = function ($args, $assoc_args) {
    $skip_email = WP_CLI\Utils\get_flag_value($assoc_args, 'skip-email');
    if ($skip_email) {
        add_filter('send_password_change_email', '__return_false');
    }
    $fetcher = new \WP_CLI\Fetchers\User();
    $users = $fetcher->get_many($args);
    foreach ($users as $user) {
        wp_update_user(array('ID' => $user->ID, 'user_pass' => wp_generate_password()));
        WP_CLI::log("Reset password for {$user->user_login}.");
    }
    if ($skip_email) {
        remove_filter('send_password_change_email', '__return_false');
    }
    WP_CLI::success('Passwords reset.');
};
WP_CLI::add_command('user reset-password', $reset_password_command);