├── README.md ├── class-wp-cli-unsplash-command.php └── wp-cli-unsplash-command.php /README.md: -------------------------------------------------------------------------------- 1 | # WP-CLI Unsplash Command 2 | 3 | Download and import images from [Unsplash](https://unsplash.com) into your Media Library. Useful for generating demo data. 4 | 5 | ## Usage 6 | 7 | Basic usage, which will import 10 images: 8 | 9 | `wp unsplash` 10 | 11 | #### `count=` 12 | 13 | Import 100 images: 14 | 15 | `wp unsplash --count=100` 16 | 17 | #### `media_date=` 18 | 19 | Import images with a specific attachment date: 20 | 21 | `wp unsplash --media_date=2016-01-25` 22 | 23 | Import images with random attachment dates: 24 | 25 | `wp unsplash --media_date=random` 26 | 27 | #### `media_dimensions=` 28 | 29 | Import images with specific dimensions: 30 | 31 | `wp unsplash --media_dimensions=1080x720` 32 | -------------------------------------------------------------------------------- /class-wp-cli-unsplash-command.php: -------------------------------------------------------------------------------- 1 | ] 10 | * : How many media items to generate. Default: 10 11 | * 12 | * [--media_author=] 13 | * : The author of the generated media. Default: none 14 | * 15 | * [--media_date=] 16 | * : The date of the generated media. Default: current date 17 | * 18 | * [--media_dimensions=] 19 | * : The dimensions of the generated media. Default: none 20 | * 21 | * ## EXAMPLES 22 | * 23 | * wp unsplash --count=10 24 | * wp unsplash --media_date=random 25 | * wp unsplash --media_dimensions=1080x720 26 | */ 27 | public function __invoke( $args, $assoc_args = array() ) { 28 | $defaults = array( 29 | 'count' => 10, 30 | 'media_author' => false, 31 | 'media_date' => current_time( 'mysql' ), 32 | 'media_dimensions' => false, 33 | ); 34 | extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP ); 35 | 36 | if ( $media_author ) { 37 | $user_fetcher = new \WP_CLI\Fetchers\User; 38 | $media_author = $user_fetcher->get_check( $media_author )->ID; 39 | } 40 | 41 | $url = 'https://source.unsplash.com/random/'; 42 | 43 | if ( $media_dimensions ) { 44 | $url .= $media_dimensions; 45 | } 46 | 47 | $notify = \WP_CLI\Utils\make_progress_bar( 'Generating media', $count ); 48 | 49 | for ( $i = 0; $i < $count; $i++ ) { 50 | $tmp_file = download_url( $url ); 51 | 52 | if ( ! is_wp_error( $tmp_file ) ) { 53 | $this->_process_downloaded_image( $tmp_file, $media_author, $media_date ); 54 | } else { 55 | WP_CLI::warning( 'Could not download image from Unsplash API.' ); 56 | } 57 | 58 | if ( file_exists( $tmp_file ) ) { 59 | unlink( $tmp_file ); 60 | } 61 | 62 | $notify->tick(); 63 | } 64 | 65 | $notify->finish(); 66 | } 67 | 68 | /** 69 | * Process downloaded image 70 | * 71 | * @param string $tmp_file 72 | * @param int|bool $media_author 73 | * @param string $media_date 74 | * 75 | * @return bool 76 | */ 77 | private function _process_downloaded_image( $tmp_file, $media_author, $media_date ) { 78 | if ( 'image/jpeg' !== ( $mime = mime_content_type( $tmp_file ) ) ) { 79 | WP_CLI::warning( 'Invalid image type.' ); 80 | 81 | return false; 82 | } 83 | 84 | $info = pathinfo( $tmp_file ); 85 | $name = ( isset( $info['filename'] ) ? $info['filename'] : 'unsplash' ); 86 | $file_array = array( 87 | 'name' => $name . '.jpeg', 88 | 'type' => $mime, 89 | 'tmp_name' => $tmp_file, 90 | 'error' => 0, 91 | 'size' => filesize( $tmp_file ), 92 | ); 93 | 94 | if ( 'random' === $media_date ) { 95 | $timestamp = current_time( 'timestamp' ) - mt_rand( 0, 315576000 ); // In last 10 years 96 | $media_date = gmdate( 'Y-m-d H:i:s', $timestamp ); 97 | } 98 | 99 | $file = wp_handle_sideload( $file_array, array( 'test_form' => false ), $media_date ); 100 | 101 | if ( isset( $file['error'] ) ) { 102 | WP_CLI::warning( 'Error uploading file.' ); 103 | 104 | return false; 105 | } 106 | 107 | $attachment = array( 108 | 'post_mime_type' => $file['type'], 109 | 'guid' => $file['url'], 110 | 'post_title' => $name, 111 | 'post_author' => $media_author, 112 | 'post_date' => $media_date, 113 | ); 114 | 115 | // Save the attachment metadata 116 | $id = wp_insert_attachment( $attachment, $file['file'] ); 117 | 118 | if ( is_wp_error( $id ) ) { 119 | WP_CLI::warning( 'Error creating attachment.' ); 120 | 121 | return false; 122 | } 123 | 124 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file['file'] ) ); 125 | } 126 | } 127 | 128 | WP_CLI::add_command( 'unsplash', 'WP_CLI_Unsplash_Command' ); 129 | -------------------------------------------------------------------------------- /wp-cli-unsplash-command.php: -------------------------------------------------------------------------------- 1 |