-
Notifications
You must be signed in to change notification settings - Fork 9
/
class-wp-cli-unsplash-command.php
128 lines (105 loc) · 3.24 KB
/
class-wp-cli-unsplash-command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class WP_CLI_Unsplash_Command extends WP_CLI_Command {
/**
* 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();
}
/**
* Process downloaded image
*
* @param string $tmp_file
* @param int|bool $media_author
* @param string $media_date
*
* @return bool
*/
private function _process_downloaded_image( $tmp_file, $media_author, $media_date ) {
if ( 'image/jpeg' !== ( $mime = mime_content_type( $tmp_file ) ) ) {
WP_CLI::warning( 'Invalid image type.' );
return false;
}
$info = pathinfo( $tmp_file );
$name = ( isset( $info['filename'] ) ? $info['filename'] : 'unsplash' );
$file_array = array(
'name' => $name . '.jpeg',
'type' => $mime,
'tmp_name' => $tmp_file,
'error' => 0,
'size' => filesize( $tmp_file ),
);
if ( 'random' === $media_date ) {
$timestamp = current_time( 'timestamp' ) - mt_rand( 0, 315576000 ); // In last 10 years
$media_date = gmdate( 'Y-m-d H:i:s', $timestamp );
}
$file = wp_handle_sideload( $file_array, array( 'test_form' => false ), $media_date );
if ( isset( $file['error'] ) ) {
WP_CLI::warning( 'Error uploading file.' );
return false;
}
$attachment = array(
'post_mime_type' => $file['type'],
'guid' => $file['url'],
'post_title' => $name,
'post_author' => $media_author,
'post_date' => $media_date,
);
// Save the attachment metadata
$id = wp_insert_attachment( $attachment, $file['file'] );
if ( is_wp_error( $id ) ) {
WP_CLI::warning( 'Error creating attachment.' );
return false;
}
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file['file'] ) );
}
}
WP_CLI::add_command( 'unsplash', 'WP_CLI_Unsplash_Command' );