-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.php
More file actions
154 lines (122 loc) · 3.92 KB
/
command.php
File metadata and controls
154 lines (122 loc) · 3.92 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
if ( !defined( 'WP_CLI' ) || !WP_CLI ) return;
class MD_CLI_Plugin_Favorites extends WP_CLI_Command {
/**
* List plugins a user has favorited in the WordPress.org plugins directory.
*
* ## OPTIONS
*
* <user>
* : The username of the wordpress.org account whose favorite plugins you are listing.
*
* [--verbose]
* : Display much more information about each plugin.
*
* ## EXAMPLES
*
* wp plugin favorites matt
* wp plugin favorites matt --verbose
* wp plugin favorites matt | xargs wp plugin install --activate
* wp plugin favorites matt | grep -vwE "(hello-dolly|bbpress)" | xargs wp plugin install --activate
*
* @synopsis <user> [--verbose]
*/
public function __invoke( $args, $assoc_args ) {
list( $user ) = $args;
extract( $assoc_args = wp_parse_args( $assoc_args, array(
'verbose' => false
) ) );
// get access to plugins_api
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$api_args = array(
'user' => $user
);
if ( $verbose ) {
$api_args['fields'] = array(
'last_updated' => true,
'active_installs' => true
);
}
$api = plugins_api( 'query_plugins', $api_args );
$plugins = isset( $api->plugins ) ? $api->plugins : false;
if ( !$plugins ) {
WP_CLI::log( 'No favorite plugins found.' );
return;
}
if ( $verbose ) {
$this->verbose( $plugins );
return;
}
foreach( $plugins as $plugin ) {
$plugin = (object) $plugin;
WP_CLI::log( $plugin->slug );
}
}
/**
* Output a favorite plugins list verbosely. Display a table with most of the data available
* from WordPress.org.
*
* @param object $plugins Result of the plugins_api() call.
*/
private function verbose( $plugins ) {
$props = array(
'name' => 'Name',
'author' => 'Author',
'last_updated' => 'Updated (Version)',
'rating' => 'Rating (#)',
'active_installs' => 'Active Installs',
'requires' => 'Requires/Tested'
);
$rows = array();
foreach( $plugins as $plugin ) {
$plugin = (object) $plugin;
$columns = array();
foreach( $props as $prop => $label ) {
$columns[$label] = '';
$plugin->{$prop} = isset( $plugin->{$prop} ) ? $plugin->{$prop} : '--';
// get the value
$columns[$label] = $plugin->{$prop};
// some values need to be cleaned up
switch( $prop ) {
// remove html link from author
case 'author' :
$columns[$label] = strip_tags( $columns[$label] );
break;
// output rating out of 5, and include # of reviews
case 'rating' :
$rating = number_format( ( (int) $columns[$label] / 100 ) * 5, 1 );
$num_ratings = isset( $plugin->num_ratings ) ? $plugin->num_ratings : 0;
$columns[$label] = $rating . '/5' . ' (' . $num_ratings . ')';
break;
// format last_updated date and include version
case 'last_updated' :
$version = isset( $plugin->version ) ? ' (' . $plugin->version . ')' : '';
$columns[$label] = date( 'Y-m-d', strtotime( $columns[$label] ) ) . $version;
break;
// add commas to active install count, and right-justify
case 'active_installs' :
$count = number_format( $columns[$label] );
// 15 is width of column
$extra_space = 15 - strlen( $count );
$space = '';
for( $i = 0; $i < $extra_space; $i++ ) {
$space .= ' ';
}
$columns[$label] = $space . $count;
break;
// include 'tested up to' with requires
case 'requires' :
if ( !$columns[$label] ) $columns[$label] = '--';
$tested = isset( $plugin->tested ) ? $plugin->tested : '--';
$columns[$label] .= '/' . $tested;
break;
}
}
$rows[$plugin->slug] = $columns;
}
// output as list table
$formatter = new \WP_CLI\Formatter( $assoc_args, array_values( $props ), 'plugin' );
$formatter->display_items( $rows );
}
}
WP_CLI::add_command( 'plugin favorites', 'MD_CLI_Plugin_Favorites' );