-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMenu_Command.php
More file actions
201 lines (180 loc) · 4.55 KB
/
Menu_Command.php
File metadata and controls
201 lines (180 loc) · 4.55 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
<?php
use WP_CLI\Formatter;
use WP_CLI\Utils;
/**
* Lists, creates, assigns, and deletes the active theme's navigation menus.
*
* See the [Navigation Menus](https://developer.wordpress.org/themes/functionality/navigation-menus/) reference in the Theme Handbook.
*
* ## EXAMPLES
*
* # Create a new menu
* $ wp menu create "My Menu"
* Success: Created menu 200.
*
* # List existing menus
* $ wp menu list
* +---------+----------+----------+-----------+-------+
* | term_id | name | slug | locations | count |
* +---------+----------+----------+-----------+-------+
* | 200 | My Menu | my-menu | | 0 |
* | 177 | Top Menu | top-menu | primary | 7 |
* +---------+----------+----------+-----------+-------+
*
* # Create a new menu link item
* $ wp menu item add-custom my-menu Apple http://apple.com --porcelain
* 1922
*
* # Assign the 'my-menu' menu to the 'primary' location
* $ wp menu location assign my-menu primary
* Success: Assigned location primary to menu my-menu.
*
* @package wp-cli
*/
class Menu_Command extends WP_CLI_Command {
protected $obj_type = 'nav_menu';
protected $obj_fields = [
'term_id',
'name',
'slug',
'locations',
'count',
];
/**
* Creates a new menu.
*
* ## OPTIONS
*
* <menu-name>
* : A descriptive name for the menu.
*
* [--porcelain]
* : Output just the new menu id.
*
* ## EXAMPLES
*
* $ wp menu create "My Menu"
* Success: Created menu 200.
*/
public function create( $args, $assoc_args ) {
$menu_id = wp_create_nav_menu( $args[0] );
if ( is_wp_error( $menu_id ) ) {
WP_CLI::error( $menu_id->get_error_message() );
} elseif ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $menu_id );
} else {
WP_CLI::success( "Created menu {$menu_id}." );
}
}
/**
* Deletes one or more menus.
*
* ## OPTIONS
*
* <menu>...
* : The name, slug, or term ID for the menu(s).
*
* ## EXAMPLES
*
* $ wp menu delete "My Menu"
* Deleted menu 'My Menu'.
* Success: Deleted 1 of 1 menus.
*/
public function delete( $args, $assoc_args ) {
$count = 0;
$errors = 0;
foreach ( $args as $arg ) {
$ret = wp_delete_nav_menu( $arg );
if ( ! $ret || is_wp_error( $ret ) ) {
WP_CLI::warning( "Couldn't delete menu '{$arg}'." );
++$errors;
} else {
WP_CLI::log( "Deleted menu '{$arg}'." );
++$count;
}
}
Utils\report_batch_operation_results( 'menu', 'delete', count( $args ), $count, $errors );
}
/**
* Gets a list of menus.
*
* ## OPTIONS
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - ids
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each menu:
*
* * term_id
* * name
* * slug
* * count
*
* These fields are optionally available:
*
* * term_group
* * term_taxonomy_id
* * taxonomy
* * description
* * parent
* * locations
*
* ## EXAMPLES
*
* $ wp menu list
* +---------+----------+----------+-----------+-------+
* | term_id | name | slug | locations | count |
* +---------+----------+----------+-----------+-------+
* | 200 | My Menu | my-menu | | 0 |
* | 177 | Top Menu | top-menu | primary | 7 |
* +---------+----------+----------+-----------+-------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$menus = wp_get_nav_menus();
$menu_locations = get_nav_menu_locations();
foreach ( $menus as &$menu ) {
$menu->locations = [];
foreach ( $menu_locations as $location => $term_id ) {
if ( $term_id === $menu->term_id ) {
$menu->locations[] = $location;
}
}
// Normalize the data for some output formats.
if ( ! isset( $assoc_args['format'] ) || in_array( $assoc_args['format'], [ 'csv', 'table' ], true ) ) {
$menu->locations = implode( ',', $menu->locations );
}
}
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = array_map(
function ( $o ) {
return $o->term_id;
},
$menus
);
$formatter->display_items( $ids );
} else {
$formatter->display_items( $menus );
}
}
protected function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->obj_fields, $this->obj_type );
}
}