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
160 lines (144 loc) · 3.06 KB
/
Copy pathSidebar_Command.php
File metadata and controls
160 lines (144 loc) · 3.06 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
<?php
use WP_CLI\Utils;
use WP_CLI\Formatter;
/**
* Lists registered sidebars.
*
* A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) is any widgetized area of your theme.
*
* ## EXAMPLES
*
* # List sidebars
* $ wp sidebar list --fields=name,id --format=csv
* name,id
* "Widget Area",sidebar-1
* "Inactive Widgets",wp_inactive_widgets
*/
class Sidebar_Command extends WP_CLI_Command {
private $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
* - ids
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each sidebar:
*
* * name
* * id
* * description
*
* These fields are optionally available:
*
* * class
* * before_widget
* * after_widget
* * before_title
* * after_title
*
* ## EXAMPLES
*
* $ wp sidebar list --fields=name,id --format=csv
* name,id
* "Widget Area",sidebar-1
* "Inactive Widgets",wp_inactive_widgets
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
global $wp_registered_sidebars;
Utils\wp_register_unused_sidebar();
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$sidebars = wp_list_pluck( $wp_registered_sidebars, 'id' );
} else {
$sidebars = $wp_registered_sidebars;
}
$formatter = new Formatter( $assoc_args, $this->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
* - csv
* - json
* - ids
* - count
* - 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;
Utils\wp_register_unused_sidebar();
$id = $args[0];
if ( ! array_key_exists( $id, $wp_registered_sidebars ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}
$formatter = new Formatter( $assoc_args, $this->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 wp_inactive_widgets && echo "exists"
*
* @when after_wp_load
*/
public function exists( $args ) {
global $wp_registered_sidebars;
Utils\wp_register_unused_sidebar();
if ( array_key_exists( $args[0], $wp_registered_sidebars ) ) {
WP_CLI::halt( 0 );
}
WP_CLI::halt( 1 );
}
}