forked from wp-cli/widget-command
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebar_Command.php
More file actions
204 lines (180 loc) · 3.99 KB
/
Copy pathSidebar_Command.php
File metadata and controls
204 lines (180 loc) · 3.99 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
use WP_CLI\Utils;
use WP_CLI\Formatter;
/**
* Manage registered sidebars.
*
* A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/)
* is any widgetized area of your theme.
*/
class Sidebar_Command extends WP_CLI_Command {
/**
* Default fields for sidebar output.
*
* @var array
*/
private $default_fields = [
'name',
'id',
'description',
];
/**
* Lists registered sidebars.
*
* ## OPTIONS
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - ids
* - count
* ---
*
* ## EXAMPLES
*
* $ wp sidebar list
* $ wp sidebar list --fields=name,id
* $ wp sidebar list --format=ids
* $ wp sidebar list --format=count
*
* @subcommand list
* @when after_wp_load
*/
public function list_( $args, $assoc_args ) {
global $wp_registered_sidebars;
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
Utils\wp_register_unused_sidebar();
}
$sidebars = $wp_registered_sidebars;
if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
WP_CLI::line( implode( ' ', wp_list_pluck( $sidebars, 'id' ) ) );
return;
}
if ( isset( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
WP_CLI::line( count( $sidebars ) );
return;
}
$formatter = new Formatter( $assoc_args, $this->default_fields );
$formatter->display_items( $sidebars );
}
/**
* Get details about a specific sidebar.
*
* ## OPTIONS
*
* <id>
* : The sidebar ID.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp sidebar get sidebar-1
* $ wp sidebar get wp_inactive_widgets --format=json
*
* @when after_wp_load
*/
public function get( $args, $assoc_args ) {
global $wp_registered_sidebars;
$id = $args[0];
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}
$formatter = new Formatter( $assoc_args, $this->default_fields );
$formatter->display_item( $wp_registered_sidebars[ $id ] );
}
/**
* Check if a sidebar exists.
*
* ## OPTIONS
*
* <id>
* : The sidebar ID.
*
* ## EXAMPLES
*
* $ wp sidebar exists sidebar-1
* $ wp sidebar exists foo && echo "exists"
*
* @when after_wp_load
*/
public function exists( $args ) {
global $wp_registered_sidebars;
if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
WP_CLI::halt( 0 );
}
WP_CLI::halt( 1 );
}
/**
* List widgets assigned to a sidebar.
*
* ## OPTIONS
*
* <id>
* : The sidebar ID.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - ids
* ---
*
* ## EXAMPLES
*
* $ wp sidebar widgets sidebar-1
* $ wp sidebar widgets wp_inactive_widgets --format=ids
*
* @when after_wp_load
*/
public function widgets( $args, $assoc_args ) {
global $wp_registered_sidebars;
$id = $args[0];
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}
$sidebars_widgets = wp_get_sidebars_widgets();
$widgets = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : [];
if ( empty( $widgets ) ) {
WP_CLI::warning( "No widgets found in sidebar '{$id}'." );
return;
}
if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
WP_CLI::line( implode( ' ', $widgets ) );
return;
}
$items = array_map(
function ( $widget_id ) {
return [ 'id' => $widget_id ];
},
$widgets
);
$formatter = new Formatter( $assoc_args, [ 'id' ] );
$formatter->display_items( $items );
}
}