-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-media.php
70 lines (61 loc) · 1.49 KB
/
get-media.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
<?php
/**
* Manage post featured image
*
* ## EXAMPLES
*
* wp get-media get_featured 83 --return=id
* wp get-media get_featured 83 --return=url
*
* wp get-media set_featured 83 2283
*
* @author Hugo Solar. <[email protected]>
*/
class Get_Media extends \WP_CLI_Command{
/**
* Get feature image from a post
*
* @synopsis <id> [--return=<return>]
*/
public function get_featured( $args, $assoc_args ){
list( $post_id ) = $args;
if (!isset($post_id)) {
WP_CLI::error('You mus specify the post ID');
}
if (!empty($assoc_args['return']) ) {
$return = $assoc_args['return'];
} else {
$return = 'id';
}
$img = \wp_get_post_thumbnail_id( absint($post_id) );
if ($return == 'url') {
$img = \wp_get_attachment_url( $img );
}
if ( \is_wp_error( $img ) ) {
WP_CLI::warning( $terms->get_error_message() );
} else {
WP_CLI::print_value( $img, $assoc_args );
}
}
/**
* Get feature image from a post
*
* @synopsis <id> <attachment_id>
*/
public function set_featured($args, $assoc_args) {
list( $post_id, $attachment_id ) = $args;
if (!isset($post_id)) {
WP_CLI::error('You mus specify the post ID');
}
if (!isset($attachment_id)) {
WP_CLI::error('You mus specify the Attachment ID');
}
$img = \set_post_thumbnail( $post_id, $attachment_id );
if ( \is_wp_error( $img ) ) {
WP_CLI::warning( $terms->get_error_message() );
} else {
WP_CLI::print_value( $img, $assoc_args );
}
}
}
WP_CLI::add_command( 'get-media', 'Get_Media' );