-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMenu_Location_Command.php
More file actions
173 lines (154 loc) · 3.99 KB
/
Menu_Location_Command.php
File metadata and controls
173 lines (154 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
<?php
use WP_CLI\Formatter;
/**
* Assigns, removes, and lists a menu's locations.
*
* ## EXAMPLES
*
* # List available menu locations
* $ wp menu location list
* +----------+-------------------+
* | location | description |
* +----------+-------------------+
* | primary | Primary Menu |
* | social | Social Links Menu |
* +----------+-------------------+
*
* # Assign the 'primary-menu' menu to the 'primary' location
* $ wp menu location assign primary-menu primary
* Success: Assigned location primary to menu primary-menu.
*
* # Remove the 'primary-menu' menu from the 'primary' location
* $ wp menu location remove primary-menu primary
* Success: Removed location from menu.
*/
class Menu_Location_Command extends WP_CLI_Command {
/**
* Lists locations for the current theme.
*
* ## OPTIONS
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* - ids
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each location:
*
* * name
* * description
*
* ## EXAMPLES
*
* $ wp menu location list
* +----------+-------------------+
* | location | description |
* +----------+-------------------+
* | primary | Primary Menu |
* | social | Social Links Menu |
* +----------+-------------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$locations = get_registered_nav_menus();
$location_objs = [];
foreach ( $locations as $location => $description ) {
$location_obj = new stdClass();
$location_obj->location = $location;
$location_obj->description = $description;
$location_objs[] = $location_obj;
}
$formatter = new Formatter( $assoc_args, [ 'location', 'description' ] );
if ( 'ids' === $formatter->format ) {
$ids = array_map(
function ( $o ) {
return $o->location;
},
$location_objs
);
$formatter->display_items( $ids );
} else {
$formatter->display_items( $location_objs );
}
}
/**
* Assigns a location to a menu.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <location>
* : Location's slug.
*
* ## EXAMPLES
*
* $ wp menu location assign primary-menu primary
* Success: Assigned location primary to menu primary-menu.
*
* @subcommand assign
*
* @param array{string, string} $args
*/
public function assign( $args, $assoc_args ) {
list( $menu, $location ) = $args;
$menu_obj = wp_get_nav_menu_object( $menu );
if ( ! $menu_obj ) {
WP_CLI::error( "Invalid menu {$menu}." );
}
$locations = get_registered_nav_menus();
if ( ! array_key_exists( $location, $locations ) ) {
WP_CLI::error( "Invalid location {$location}." );
}
$locations = get_nav_menu_locations();
$locations[ $location ] = $menu_obj->term_id;
set_theme_mod( 'nav_menu_locations', $locations );
WP_CLI::success( "Assigned location {$location} to menu {$menu}." );
}
/**
* Removes a location from a menu.
*
* ## OPTIONS
*
* <menu>
* : The name, slug, or term ID for the menu.
*
* <location>
* : Location's slug.
*
* ## EXAMPLES
*
* $ wp menu location remove primary-menu primary
* Success: Removed location from menu.
*
* @subcommand remove
*
* @param array{string, string} $args
*/
public function remove( $args, $assoc_args ) {
list( $menu, $location ) = $args;
$menu = wp_get_nav_menu_object( $menu );
if ( false === $menu ) {
WP_CLI::error( 'Invalid menu.' );
}
$locations = get_nav_menu_locations();
if ( ( $locations[ $location ] ?? null ) !== $menu->term_id ) {
WP_CLI::error( "Menu isn't assigned to location." );
}
$locations[ $location ] = 0;
set_theme_mod( 'nav_menu_locations', $locations );
WP_CLI::success( 'Removed location from menu.' );
}
}