author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Navigation Menu functions |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Nav_Menus |
|
7 |
* @since 3.0.0 |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* Returns a navigation menu object. |
|
12 |
* |
|
13 |
* @since 3.0.0 |
|
14 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @param int|string|WP_Term $menu Menu ID, slug, name, or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* @return WP_Term|false False if $menu param isn't supplied or term does not exist, menu object if successful. |
0 | 17 |
*/ |
18 |
function wp_get_nav_menu_object( $menu ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
$menu_obj = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
if ( is_object( $menu ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
$menu_obj = $menu; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
} |
0 | 24 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
if ( $menu && ! $menu_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
$menu_obj = get_term( $menu, 'nav_menu' ); |
0 | 27 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
if ( ! $menu_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
$menu_obj = get_term_by( 'slug', $menu, 'nav_menu' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
} |
0 | 31 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
if ( ! $menu_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
$menu_obj = get_term_by( 'name', $menu, 'nav_menu' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
if ( ! $menu_obj || is_wp_error( $menu_obj ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
$menu_obj = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
} |
0 | 40 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
* Filters the nav_menu term retrieved for wp_get_nav_menu_object(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* @param WP_Term|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* @param int|string|WP_Term $menu The menu ID, slug, name, or object passed to wp_get_nav_menu_object(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu ); |
0 | 50 |
} |
51 |
||
52 |
/** |
|
53 |
* Check if the given ID is a navigation menu. |
|
54 |
* |
|
55 |
* Returns true if it is; false otherwise. |
|
56 |
* |
|
57 |
* @since 3.0.0 |
|
58 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @param int|string|WP_Term $menu Menu ID, slug, name, or object of menu to check. |
0 | 60 |
* @return bool Whether the menu exists. |
61 |
*/ |
|
62 |
function is_nav_menu( $menu ) { |
|
9 | 63 |
if ( ! $menu ) { |
0 | 64 |
return false; |
9 | 65 |
} |
0 | 66 |
|
67 |
$menu_obj = wp_get_nav_menu_object( $menu ); |
|
68 |
||
69 |
if ( |
|
70 |
$menu_obj && |
|
71 |
! is_wp_error( $menu_obj ) && |
|
72 |
! empty( $menu_obj->taxonomy ) && |
|
73 |
'nav_menu' == $menu_obj->taxonomy |
|
9 | 74 |
) { |
0 | 75 |
return true; |
9 | 76 |
} |
0 | 77 |
|
78 |
return false; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* Registers navigation menu locations for a theme. |
0 | 83 |
* |
84 |
* @since 3.0.0 |
|
85 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* @global array $_wp_registered_nav_menus |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* |
0 | 88 |
* @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text. |
89 |
*/ |
|
90 |
function register_nav_menus( $locations = array() ) { |
|
91 |
global $_wp_registered_nav_menus; |
|
92 |
||
93 |
add_theme_support( 'menus' ); |
|
94 |
||
95 |
$_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations ); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
* Unregisters a navigation menu location for a theme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* @since 3.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* @global array $_wp_registered_nav_menus |
0 | 103 |
* |
5 | 104 |
* @param string $location The menu location identifier. |
0 | 105 |
* @return bool True on success, false on failure. |
106 |
*/ |
|
107 |
function unregister_nav_menu( $location ) { |
|
108 |
global $_wp_registered_nav_menus; |
|
109 |
||
9 | 110 |
if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[ $location ] ) ) { |
111 |
unset( $_wp_registered_nav_menus[ $location ] ); |
|
5 | 112 |
if ( empty( $_wp_registered_nav_menus ) ) { |
113 |
_remove_theme_support( 'menus' ); |
|
114 |
} |
|
0 | 115 |
return true; |
116 |
} |
|
117 |
return false; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* Registers a navigation menu location for a theme. |
0 | 122 |
* |
123 |
* @since 3.0.0 |
|
124 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* @param string $location Menu location identifier, like a slug. |
0 | 126 |
* @param string $description Menu location descriptive text. |
127 |
*/ |
|
128 |
function register_nav_menu( $location, $description ) { |
|
129 |
register_nav_menus( array( $location => $description ) ); |
|
130 |
} |
|
131 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* Retrieves all registered navigation menu locations in a theme. |
0 | 133 |
* |
134 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* @global array $_wp_registered_nav_menus |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* @return array Registered navigation menu locations. If none are registered, an empty array. |
0 | 139 |
*/ |
140 |
function get_registered_nav_menus() { |
|
141 |
global $_wp_registered_nav_menus; |
|
9 | 142 |
if ( isset( $_wp_registered_nav_menus ) ) { |
0 | 143 |
return $_wp_registered_nav_menus; |
9 | 144 |
} |
0 | 145 |
return array(); |
146 |
} |
|
147 |
||
148 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* Retrieves all registered navigation menu locations and the menus assigned to them. |
0 | 150 |
* |
151 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @return array Registered navigation menu locations and the menus assigned them. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
* If none are registered, an empty array. |
0 | 155 |
*/ |
156 |
||
157 |
function get_nav_menu_locations() { |
|
158 |
$locations = get_theme_mod( 'nav_menu_locations' ); |
|
159 |
return ( is_array( $locations ) ) ? $locations : array(); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* Determines whether a registered nav menu location has a menu assigned to it. |
0 | 164 |
* |
165 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* |
0 | 167 |
* @param string $location Menu location identifier. |
168 |
* @return bool Whether location has a menu. |
|
169 |
*/ |
|
170 |
function has_nav_menu( $location ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
$has_nav_menu = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
|
5 | 173 |
$registered_nav_menus = get_registered_nav_menus(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
if ( isset( $registered_nav_menus[ $location ] ) ) { |
9 | 175 |
$locations = get_nav_menu_locations(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
$has_nav_menu = ! empty( $locations[ $location ] ); |
5 | 177 |
} |
178 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
* Filters whether a nav menu is assigned to the specified location. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
* @param bool $has_nav_menu Whether there is a menu assigned to a location. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* @param string $location Menu location. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
return apply_filters( 'has_nav_menu', $has_nav_menu, $location ); |
0 | 188 |
} |
189 |
||
190 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
* Returns the name of a navigation menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* @param string $location Menu location identifier. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* @return string Menu name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
function wp_get_nav_menu_name( $location ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
$menu_name = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
$locations = get_nav_menu_locations(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
if ( isset( $locations[ $location ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
$menu = wp_get_nav_menu_object( $locations[ $location ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
if ( $menu && $menu->name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
$menu_name = $menu->name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* Filters the navigation menu name being returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
* @param string $menu_name Menu name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
* @param string $location Menu location identifier. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
* Determines whether the given ID is a nav menu item. |
0 | 224 |
* |
225 |
* @since 3.0.0 |
|
226 |
* |
|
227 |
* @param int $menu_item_id The ID of the potential nav menu item. |
|
228 |
* @return bool Whether the given ID is that of a nav menu item. |
|
229 |
*/ |
|
230 |
function is_nav_menu_item( $menu_item_id = 0 ) { |
|
231 |
return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) ); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
* Creates a navigation menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* Note that `$menu_name` is expected to be pre-slashed. |
0 | 238 |
* |
239 |
* @since 3.0.0 |
|
240 |
* |
|
5 | 241 |
* @param string $menu_name Menu name. |
242 |
* @return int|WP_Error Menu ID on success, WP_Error object on failure. |
|
0 | 243 |
*/ |
244 |
function wp_create_nav_menu( $menu_name ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
// expected_slashed ($menu_name) |
0 | 246 |
return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) ); |
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Delete a Navigation Menu. |
|
251 |
* |
|
252 |
* @since 3.0.0 |
|
253 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @param int|string|WP_Term $menu Menu ID, slug, name, or object. |
5 | 255 |
* @return bool|WP_Error True on success, false or WP_Error object on failure. |
0 | 256 |
*/ |
257 |
function wp_delete_nav_menu( $menu ) { |
|
258 |
$menu = wp_get_nav_menu_object( $menu ); |
|
9 | 259 |
if ( ! $menu ) { |
0 | 260 |
return false; |
9 | 261 |
} |
0 | 262 |
|
263 |
$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' ); |
|
264 |
if ( ! empty( $menu_objects ) ) { |
|
265 |
foreach ( $menu_objects as $item ) { |
|
266 |
wp_delete_post( $item ); |
|
267 |
} |
|
268 |
} |
|
269 |
||
270 |
$result = wp_delete_term( $menu->term_id, 'nav_menu' ); |
|
271 |
||
272 |
// Remove this menu from any locations. |
|
273 |
$locations = get_nav_menu_locations(); |
|
274 |
foreach ( $locations as $location => $menu_id ) { |
|
9 | 275 |
if ( $menu_id == $menu->term_id ) { |
0 | 276 |
$locations[ $location ] = 0; |
9 | 277 |
} |
0 | 278 |
} |
279 |
set_theme_mod( 'nav_menu_locations', $locations ); |
|
280 |
||
9 | 281 |
if ( $result && ! is_wp_error( $result ) ) { |
5 | 282 |
|
283 |
/** |
|
284 |
* Fires after a navigation menu has been successfully deleted. |
|
285 |
* |
|
286 |
* @since 3.0.0 |
|
287 |
* |
|
288 |
* @param int $term_id ID of the deleted menu. |
|
289 |
*/ |
|
0 | 290 |
do_action( 'wp_delete_nav_menu', $menu->term_id ); |
9 | 291 |
} |
0 | 292 |
|
293 |
return $result; |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Save the properties of a menu or create a new menu with those properties. |
|
298 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
* Note that `$menu_data` is expected to be pre-slashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* |
0 | 301 |
* @since 3.0.0 |
302 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* @param int $menu_id The ID of the menu or "0" to create a new menu. |
0 | 304 |
* @param array $menu_data The array of menu data. |
5 | 305 |
* @return int|WP_Error Menu ID on success, WP_Error object on failure. |
0 | 306 |
*/ |
307 |
function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
// expected_slashed ($menu_data) |
0 | 309 |
$menu_id = (int) $menu_id; |
310 |
||
311 |
$_menu = wp_get_nav_menu_object( $menu_id ); |
|
312 |
||
313 |
$args = array( |
|
9 | 314 |
'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), |
315 |
'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), |
|
316 |
'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ), |
|
0 | 317 |
'slug' => null, |
318 |
); |
|
319 |
||
320 |
// double-check that we're not going to have one menu take the name of another |
|
321 |
$_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); |
|
322 |
if ( |
|
323 |
$_possible_existing && |
|
324 |
! is_wp_error( $_possible_existing ) && |
|
325 |
isset( $_possible_existing->term_id ) && |
|
326 |
$_possible_existing->term_id != $menu_id |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
) { |
9 | 328 |
return new WP_Error( |
329 |
'menu_exists', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
/* translators: %s: menu name */ |
9 | 331 |
sprintf( |
332 |
__( 'The menu name %s conflicts with another menu name. Please try another.' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
} |
0 | 337 |
|
338 |
// menu doesn't already exist, so create a new menu |
|
339 |
if ( ! $_menu || is_wp_error( $_menu ) ) { |
|
340 |
$menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); |
|
341 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
if ( $menu_exists ) { |
9 | 343 |
return new WP_Error( |
344 |
'menu_exists', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
/* translators: %s: menu name */ |
9 | 346 |
sprintf( |
347 |
__( 'The menu name %s conflicts with another menu name. Please try another.' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
} |
0 | 352 |
|
353 |
$_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args ); |
|
354 |
||
9 | 355 |
if ( is_wp_error( $_menu ) ) { |
0 | 356 |
return $_menu; |
9 | 357 |
} |
0 | 358 |
|
5 | 359 |
/** |
360 |
* Fires after a navigation menu is successfully created. |
|
361 |
* |
|
362 |
* @since 3.0.0 |
|
363 |
* |
|
364 |
* @param int $term_id ID of the new menu. |
|
365 |
* @param array $menu_data An array of menu data. |
|
366 |
*/ |
|
0 | 367 |
do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data ); |
368 |
||
369 |
return (int) $_menu['term_id']; |
|
370 |
} |
|
371 |
||
9 | 372 |
if ( ! $_menu || ! isset( $_menu->term_id ) ) { |
0 | 373 |
return 0; |
9 | 374 |
} |
0 | 375 |
|
376 |
$menu_id = (int) $_menu->term_id; |
|
377 |
||
378 |
$update_response = wp_update_term( $menu_id, 'nav_menu', $args ); |
|
379 |
||
9 | 380 |
if ( is_wp_error( $update_response ) ) { |
0 | 381 |
return $update_response; |
9 | 382 |
} |
0 | 383 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
$menu_id = (int) $update_response['term_id']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
|
5 | 386 |
/** |
387 |
* Fires after a navigation menu has been successfully updated. |
|
388 |
* |
|
389 |
* @since 3.0.0 |
|
390 |
* |
|
391 |
* @param int $menu_id ID of the updated menu. |
|
392 |
* @param array $menu_data An array of menu data. |
|
393 |
*/ |
|
0 | 394 |
do_action( 'wp_update_nav_menu', $menu_id, $menu_data ); |
395 |
return $menu_id; |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Save the properties of a menu item or create a new one. |
|
400 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* The menu-item-title, menu-item-description, and menu-item-attr-title are expected |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
* to be pre-slashed since they are passed directly into `wp_insert_post()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* |
0 | 404 |
* @since 3.0.0 |
405 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @param array $menu_item_data The menu item's data. |
5 | 409 |
* @return int|WP_Error The menu item's database ID or WP_Error object on failure. |
0 | 410 |
*/ |
411 |
function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) { |
|
9 | 412 |
$menu_id = (int) $menu_id; |
0 | 413 |
$menu_item_db_id = (int) $menu_item_db_id; |
414 |
||
415 |
// make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects |
|
9 | 416 |
if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) { |
5 | 417 |
return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) ); |
9 | 418 |
} |
0 | 419 |
|
420 |
$menu = wp_get_nav_menu_object( $menu_id ); |
|
421 |
||
5 | 422 |
if ( ! $menu && 0 !== $menu_id ) { |
423 |
return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) ); |
|
424 |
} |
|
425 |
||
426 |
if ( is_wp_error( $menu ) ) { |
|
0 | 427 |
return $menu; |
5 | 428 |
} |
0 | 429 |
|
430 |
$defaults = array( |
|
9 | 431 |
'menu-item-db-id' => $menu_item_db_id, |
432 |
'menu-item-object-id' => 0, |
|
433 |
'menu-item-object' => '', |
|
434 |
'menu-item-parent-id' => 0, |
|
435 |
'menu-item-position' => 0, |
|
436 |
'menu-item-type' => 'custom', |
|
437 |
'menu-item-title' => '', |
|
438 |
'menu-item-url' => '', |
|
0 | 439 |
'menu-item-description' => '', |
9 | 440 |
'menu-item-attr-title' => '', |
441 |
'menu-item-target' => '', |
|
442 |
'menu-item-classes' => '', |
|
443 |
'menu-item-xfn' => '', |
|
444 |
'menu-item-status' => '', |
|
0 | 445 |
); |
446 |
||
447 |
$args = wp_parse_args( $menu_item_data, $defaults ); |
|
448 |
||
449 |
if ( 0 == $menu_id ) { |
|
450 |
$args['menu-item-position'] = 1; |
|
451 |
} elseif ( 0 == (int) $args['menu-item-position'] ) { |
|
9 | 452 |
$menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); |
453 |
$last_item = array_pop( $menu_items ); |
|
0 | 454 |
$args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items ); |
455 |
} |
|
456 |
||
457 |
$original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0; |
|
458 |
||
9 | 459 |
if ( 'custom' === $args['menu-item-type'] ) { |
460 |
// If custom menu item, trim the URL. |
|
461 |
$args['menu-item-url'] = trim( $args['menu-item-url'] ); |
|
462 |
} else { |
|
463 |
/* |
|
464 |
* If non-custom menu item, then: |
|
465 |
* - use the original object's URL. |
|
466 |
* - blank default title to sync with the original object's title. |
|
467 |
*/ |
|
0 | 468 |
|
469 |
$args['menu-item-url'] = ''; |
|
470 |
||
471 |
$original_title = ''; |
|
472 |
if ( 'taxonomy' == $args['menu-item-type'] ) { |
|
473 |
$original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); |
|
9 | 474 |
$original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); |
0 | 475 |
} elseif ( 'post_type' == $args['menu-item-type'] ) { |
476 |
||
477 |
$original_object = get_post( $args['menu-item-object-id'] ); |
|
478 |
$original_parent = (int) $original_object->post_parent; |
|
9 | 479 |
$original_title = $original_object->post_title; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
} elseif ( 'post_type_archive' == $args['menu-item-type'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
$original_object = get_post_type_object( $args['menu-item-object'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
if ( $original_object ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
$original_title = $original_object->labels->archives; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
} |
0 | 485 |
} |
486 |
||
9 | 487 |
if ( $args['menu-item-title'] == $original_title ) { |
0 | 488 |
$args['menu-item-title'] = ''; |
9 | 489 |
} |
0 | 490 |
|
491 |
// hack to get wp to create a post object when too many properties are empty |
|
9 | 492 |
if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] ) { |
0 | 493 |
$args['menu-item-description'] = ' '; |
9 | 494 |
} |
0 | 495 |
} |
496 |
||
497 |
// Populate the menu item object |
|
498 |
$post = array( |
|
9 | 499 |
'menu_order' => $args['menu-item-position'], |
500 |
'ping_status' => 0, |
|
0 | 501 |
'post_content' => $args['menu-item-description'], |
502 |
'post_excerpt' => $args['menu-item-attr-title'], |
|
9 | 503 |
'post_parent' => $original_parent, |
504 |
'post_title' => $args['menu-item-title'], |
|
505 |
'post_type' => 'nav_menu_item', |
|
0 | 506 |
); |
507 |
||
508 |
$update = 0 != $menu_item_db_id; |
|
509 |
||
510 |
// New menu item. Default is draft status |
|
511 |
if ( ! $update ) { |
|
9 | 512 |
$post['ID'] = 0; |
0 | 513 |
$post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft'; |
9 | 514 |
$menu_item_db_id = wp_insert_post( $post ); |
515 |
if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) { |
|
0 | 516 |
return $menu_item_db_id; |
9 | 517 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
* Fires immediately after a new navigation menu item has been added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
* @see wp_update_nav_menu_item() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
* @param int $menu_id ID of the updated menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
* @param int $menu_item_db_id ID of the new menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
* @param array $args An array of arguments used to update/add the menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args ); |
0 | 531 |
} |
532 |
||
5 | 533 |
// Associate the menu item with the menu term |
534 |
// Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms() |
|
9 | 535 |
if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) { |
5 | 536 |
wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' ); |
537 |
} |
|
538 |
||
0 | 539 |
if ( 'custom' == $args['menu-item-type'] ) { |
540 |
$args['menu-item-object-id'] = $menu_item_db_id; |
|
9 | 541 |
$args['menu-item-object'] = 'custom'; |
0 | 542 |
} |
543 |
||
544 |
$menu_item_db_id = (int) $menu_item_db_id; |
|
545 |
||
9 | 546 |
update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) ); |
0 | 547 |
update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) ); |
548 |
update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) ); |
|
9 | 549 |
update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) ); |
550 |
update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) ); |
|
0 | 551 |
|
552 |
$args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) ); |
|
9 | 553 |
$args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) ); |
0 | 554 |
update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] ); |
555 |
update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); |
|
9 | 556 |
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) ); |
0 | 557 |
|
9 | 558 |
if ( 0 == $menu_id ) { |
0 | 559 |
update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() ); |
9 | 560 |
} elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) ) { |
0 | 561 |
delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' ); |
9 | 562 |
} |
0 | 563 |
|
564 |
// Update existing menu item. Default is publish status |
|
565 |
if ( $update ) { |
|
9 | 566 |
$post['ID'] = $menu_item_db_id; |
0 | 567 |
$post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish'; |
568 |
wp_update_post( $post ); |
|
569 |
} |
|
570 |
||
5 | 571 |
/** |
572 |
* Fires after a navigation menu item has been updated. |
|
573 |
* |
|
574 |
* @since 3.0.0 |
|
575 |
* |
|
576 |
* @see wp_update_nav_menu_item() |
|
577 |
* |
|
578 |
* @param int $menu_id ID of the updated menu. |
|
579 |
* @param int $menu_item_db_id ID of the updated menu item. |
|
580 |
* @param array $args An array of arguments used to update a menu item. |
|
581 |
*/ |
|
582 |
do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); |
|
0 | 583 |
|
584 |
return $menu_item_db_id; |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Returns all navigation menu objects. |
|
589 |
* |
|
590 |
* @since 3.0.0 |
|
5 | 591 |
* @since 4.1.0 Default value of the 'orderby' argument was changed from 'none' |
592 |
* to 'name'. |
|
0 | 593 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
* @param array $args Optional. Array of arguments passed on to get_terms(). |
5 | 595 |
* Default empty array. |
596 |
* @return array Menu objects. |
|
0 | 597 |
*/ |
598 |
function wp_get_nav_menus( $args = array() ) { |
|
9 | 599 |
$defaults = array( |
600 |
'hide_empty' => false, |
|
601 |
'orderby' => 'name', |
|
602 |
); |
|
603 |
$args = wp_parse_args( $args, $defaults ); |
|
5 | 604 |
|
605 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
* Filters the navigation menu objects being returned. |
5 | 607 |
* |
608 |
* @since 3.0.0 |
|
609 |
* |
|
610 |
* @see get_terms() |
|
611 |
* |
|
612 |
* @param array $menus An array of menu objects. |
|
613 |
* @param array $args An array of arguments used to retrieve menu objects. |
|
614 |
*/ |
|
9 | 615 |
return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args ), $args ); |
0 | 616 |
} |
617 |
||
618 |
/** |
|
5 | 619 |
* Return if a menu item is valid. |
620 |
* |
|
621 |
* @link https://core.trac.wordpress.org/ticket/13958 |
|
0 | 622 |
* |
623 |
* @since 3.2.0 |
|
624 |
* @access private |
|
625 |
* |
|
5 | 626 |
* @param object $item The menu item to check. |
627 |
* @return bool False if invalid, otherwise true. |
|
0 | 628 |
*/ |
629 |
function _is_valid_nav_menu_item( $item ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
return empty( $item->_invalid ); |
0 | 631 |
} |
632 |
||
633 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
* Retrieves all menu items of a navigation menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
* Note: Most arguments passed to the `$args` parameter – save for 'output_key' – are |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
* specifically for retrieving nav_menu_item posts from get_posts() and may only |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
* indirectly affect the ultimate ordering and content of the resulting nav menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* items that get returned from this function. |
0 | 640 |
* |
641 |
* @since 3.0.0 |
|
642 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* @staticvar array $fetched |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* @param int|string|WP_Term $menu Menu ID, slug, name, or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* @param array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
* Optional. Arguments to pass to get_posts(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* @type string $order How to order nav menu items as queried with get_posts(). Will be ignored |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* if 'output' is ARRAY_A. Default 'ASC'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* @type string $orderby Field to order menu items by as retrieved from get_posts(). Supply an orderby |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* field via 'output_key' to affect the output order of nav menu items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* Default 'menu_order'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* @type string $post_type Menu items post type. Default 'nav_menu_item'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @type string $post_status Menu items post status. Default 'publish'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* @type string $output How to order outputted menu items. Default ARRAY_A. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
* @type string $output_key Key to use for ordering the actual menu items that get returned. Note that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
* that is not a get_posts() argument and will only affect output of menu items |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* processed in this function. Default 'menu_order'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* @type bool $nopaging Whether to retrieve all menu items (true) or paginate (false). Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* @return false|array $items Array of menu items, otherwise false. |
0 | 663 |
*/ |
664 |
function wp_get_nav_menu_items( $menu, $args = array() ) { |
|
665 |
$menu = wp_get_nav_menu_object( $menu ); |
|
666 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
if ( ! $menu ) { |
0 | 668 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
} |
0 | 670 |
|
671 |
static $fetched = array(); |
|
672 |
||
673 |
$items = get_objects_in_term( $menu->term_id, 'nav_menu' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
if ( is_wp_error( $items ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
} |
0 | 677 |
|
9 | 678 |
$defaults = array( |
679 |
'order' => 'ASC', |
|
680 |
'orderby' => 'menu_order', |
|
681 |
'post_type' => 'nav_menu_item', |
|
682 |
'post_status' => 'publish', |
|
683 |
'output' => ARRAY_A, |
|
684 |
'output_key' => 'menu_order', |
|
685 |
'nopaging' => true, |
|
686 |
); |
|
687 |
$args = wp_parse_args( $args, $defaults ); |
|
5 | 688 |
$args['include'] = $items; |
0 | 689 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
if ( ! empty( $items ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
$items = get_posts( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
$items = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
} |
0 | 695 |
|
696 |
// Get all posts and terms at once to prime the caches |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) { |
9 | 698 |
$fetched[ $menu->term_id ] = true; |
699 |
$posts = array(); |
|
700 |
$terms = array(); |
|
0 | 701 |
foreach ( $items as $item ) { |
702 |
$object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); |
|
9 | 703 |
$object = get_post_meta( $item->ID, '_menu_item_object', true ); |
704 |
$type = get_post_meta( $item->ID, '_menu_item_type', true ); |
|
0 | 705 |
|
9 | 706 |
if ( 'post_type' == $type ) { |
707 |
$posts[ $object ][] = $object_id; |
|
708 |
} elseif ( 'taxonomy' == $type ) { |
|
709 |
$terms[ $object ][] = $object_id; |
|
710 |
} |
|
0 | 711 |
} |
712 |
||
713 |
if ( ! empty( $posts ) ) { |
|
9 | 714 |
foreach ( array_keys( $posts ) as $post_type ) { |
715 |
get_posts( |
|
716 |
array( |
|
717 |
'post__in' => $posts[ $post_type ], |
|
718 |
'post_type' => $post_type, |
|
719 |
'nopaging' => true, |
|
720 |
'update_post_term_cache' => false, |
|
721 |
) |
|
722 |
); |
|
0 | 723 |
} |
724 |
} |
|
9 | 725 |
unset( $posts ); |
0 | 726 |
|
727 |
if ( ! empty( $terms ) ) { |
|
9 | 728 |
foreach ( array_keys( $terms ) as $taxonomy ) { |
729 |
get_terms( |
|
730 |
$taxonomy, |
|
731 |
array( |
|
732 |
'include' => $terms[ $taxonomy ], |
|
733 |
'hierarchical' => false, |
|
734 |
) |
|
735 |
); |
|
0 | 736 |
} |
737 |
} |
|
9 | 738 |
unset( $terms ); |
0 | 739 |
} |
740 |
||
741 |
$items = array_map( 'wp_setup_nav_menu_item', $items ); |
|
742 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
if ( ! is_admin() ) { // Remove invalid items only in front end |
0 | 744 |
$items = array_filter( $items, '_is_valid_nav_menu_item' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
} |
0 | 746 |
|
747 |
if ( ARRAY_A == $args['output'] ) { |
|
9 | 748 |
$items = wp_list_sort( |
749 |
$items, |
|
750 |
array( |
|
751 |
$args['output_key'] => 'ASC', |
|
752 |
) |
|
753 |
); |
|
754 |
$i = 1; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
foreach ( $items as $k => $item ) { |
9 | 756 |
$items[ $k ]->{$args['output_key']} = $i++; |
0 | 757 |
} |
758 |
} |
|
759 |
||
5 | 760 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
* Filters the navigation menu items being returned. |
5 | 762 |
* |
763 |
* @since 3.0.0 |
|
764 |
* |
|
765 |
* @param array $items An array of menu item post objects. |
|
766 |
* @param object $menu The menu object. |
|
767 |
* @param array $args An array of arguments used to retrieve menu item objects. |
|
768 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args ); |
0 | 770 |
} |
771 |
||
772 |
/** |
|
773 |
* Decorates a menu item object with the shared navigation menu item properties. |
|
774 |
* |
|
775 |
* Properties: |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
776 |
* - ID: The term_id if the menu item represents a taxonomy term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
777 |
* - attr_title: The title attribute of the link element for this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
* - classes: The array of class attribute values for the link element of this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
* - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
* - description: The description of this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
* - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
* - object: The type of object originally represented, such as "category," "post", or "attachment." |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
* - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
* - post_parent: The DB ID of the original object's parent object, if any (0 otherwise). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
* - post_title: A "no title" label if menu item represents a post that lacks a title. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
* - target: The target attribute of the link element for this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
* - title: The title of this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
* - type: The family of objects originally represented, such as "post_type" or "taxonomy." |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
* - type_label: The singular label used to describe this type of menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
* - url: The URL to which this menu item points. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
* - xfn: The XFN relationship expressed in the link of this menu item. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
* - _invalid: Whether the menu item represents an object that no longer exists. |
0 | 793 |
* |
794 |
* @since 3.0.0 |
|
795 |
* |
|
796 |
* @param object $menu_item The menu item to modify. |
|
797 |
* @return object $menu_item The menu item with standard menu item properties. |
|
798 |
*/ |
|
799 |
function wp_setup_nav_menu_item( $menu_item ) { |
|
800 |
if ( isset( $menu_item->post_type ) ) { |
|
801 |
if ( 'nav_menu_item' == $menu_item->post_type ) { |
|
9 | 802 |
$menu_item->db_id = (int) $menu_item->ID; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
$menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent; |
9 | 804 |
$menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id; |
805 |
$menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object; |
|
806 |
$menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type; |
|
0 | 807 |
|
808 |
if ( 'post_type' == $menu_item->type ) { |
|
809 |
$object = get_post_type_object( $menu_item->object ); |
|
810 |
if ( $object ) { |
|
811 |
$menu_item->type_label = $object->labels->singular_name; |
|
812 |
} else { |
|
813 |
$menu_item->type_label = $menu_item->object; |
|
9 | 814 |
$menu_item->_invalid = true; |
0 | 815 |
} |
816 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
if ( 'trash' === get_post_status( $menu_item->object_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
$menu_item->_invalid = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
|
0 | 821 |
$menu_item->url = get_permalink( $menu_item->object_id ); |
822 |
||
823 |
$original_object = get_post( $menu_item->object_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
/** This filter is documented in wp-includes/post-template.php */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID ); |
5 | 826 |
|
827 |
if ( '' === $original_title ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
/* translators: %d: ID of a post */ |
5 | 829 |
$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID ); |
830 |
} |
|
831 |
||
0 | 832 |
$menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; |
833 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
} elseif ( 'post_type_archive' == $menu_item->type ) { |
9 | 835 |
$object = get_post_type_object( $menu_item->object ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
836 |
if ( $object ) { |
9 | 837 |
$menu_item->title = '' == $menu_item->post_title ? $object->labels->archives : $menu_item->post_title; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
838 |
$post_type_description = $object->description; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
839 |
} else { |
9 | 840 |
$menu_item->_invalid = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
$post_type_description = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
$menu_item->type_label = __( 'Post Type Archive' ); |
9 | 845 |
$post_content = wp_trim_words( $menu_item->post_content, 200 ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
$post_type_description = '' == $post_content ? $post_type_description : $post_content; |
9 | 847 |
$menu_item->url = get_post_type_archive_link( $menu_item->object ); |
0 | 848 |
} elseif ( 'taxonomy' == $menu_item->type ) { |
849 |
$object = get_taxonomy( $menu_item->object ); |
|
850 |
if ( $object ) { |
|
851 |
$menu_item->type_label = $object->labels->singular_name; |
|
852 |
} else { |
|
853 |
$menu_item->type_label = $menu_item->object; |
|
9 | 854 |
$menu_item->_invalid = true; |
0 | 855 |
} |
856 |
||
9 | 857 |
$term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); |
858 |
$menu_item->url = ! is_wp_error( $term_url ) ? $term_url : ''; |
|
0 | 859 |
|
860 |
$original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' ); |
|
9 | 861 |
if ( is_wp_error( $original_title ) ) { |
0 | 862 |
$original_title = false; |
9 | 863 |
} |
0 | 864 |
$menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; |
865 |
||
866 |
} else { |
|
9 | 867 |
$menu_item->type_label = __( 'Custom Link' ); |
868 |
$menu_item->title = $menu_item->post_title; |
|
869 |
$menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url; |
|
0 | 870 |
} |
871 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
$menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target; |
0 | 873 |
|
5 | 874 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
* Filters a navigation menu item's title attribute. |
5 | 876 |
* |
877 |
* @since 3.0.0 |
|
878 |
* |
|
879 |
* @param string $item_title The menu item title attribute. |
|
880 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
$menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title; |
0 | 882 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
if ( ! isset( $menu_item->description ) ) { |
5 | 884 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
* Filters a navigation menu item's description. |
5 | 886 |
* |
887 |
* @since 3.0.0 |
|
888 |
* |
|
889 |
* @param string $description The menu item description. |
|
890 |
*/ |
|
891 |
$menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) ); |
|
892 |
} |
|
0 | 893 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
$menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes; |
9 | 895 |
$menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn; |
0 | 896 |
} else { |
9 | 897 |
$menu_item->db_id = 0; |
0 | 898 |
$menu_item->menu_item_parent = 0; |
9 | 899 |
$menu_item->object_id = (int) $menu_item->ID; |
900 |
$menu_item->type = 'post_type'; |
|
0 | 901 |
|
9 | 902 |
$object = get_post_type_object( $menu_item->post_type ); |
903 |
$menu_item->object = $object->name; |
|
0 | 904 |
$menu_item->type_label = $object->labels->singular_name; |
905 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
if ( '' === $menu_item->post_title ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
907 |
/* translators: %d: ID of a post */ |
0 | 908 |
$menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
} |
0 | 910 |
|
9 | 911 |
$menu_item->title = $menu_item->post_title; |
912 |
$menu_item->url = get_permalink( $menu_item->ID ); |
|
0 | 913 |
$menu_item->target = ''; |
914 |
||
5 | 915 |
/** This filter is documented in wp-includes/nav-menu.php */ |
0 | 916 |
$menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' ); |
5 | 917 |
|
918 |
/** This filter is documented in wp-includes/nav-menu.php */ |
|
0 | 919 |
$menu_item->description = apply_filters( 'nav_menu_description', '' ); |
9 | 920 |
$menu_item->classes = array(); |
921 |
$menu_item->xfn = ''; |
|
0 | 922 |
} |
923 |
} elseif ( isset( $menu_item->taxonomy ) ) { |
|
9 | 924 |
$menu_item->ID = $menu_item->term_id; |
925 |
$menu_item->db_id = 0; |
|
0 | 926 |
$menu_item->menu_item_parent = 0; |
9 | 927 |
$menu_item->object_id = (int) $menu_item->term_id; |
928 |
$menu_item->post_parent = (int) $menu_item->parent; |
|
929 |
$menu_item->type = 'taxonomy'; |
|
0 | 930 |
|
9 | 931 |
$object = get_taxonomy( $menu_item->taxonomy ); |
932 |
$menu_item->object = $object->name; |
|
0 | 933 |
$menu_item->type_label = $object->labels->singular_name; |
934 |
||
9 | 935 |
$menu_item->title = $menu_item->name; |
936 |
$menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy ); |
|
937 |
$menu_item->target = ''; |
|
938 |
$menu_item->attr_title = ''; |
|
0 | 939 |
$menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy ); |
9 | 940 |
$menu_item->classes = array(); |
941 |
$menu_item->xfn = ''; |
|
0 | 942 |
|
943 |
} |
|
944 |
||
5 | 945 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
946 |
* Filters a navigation menu item object. |
5 | 947 |
* |
948 |
* @since 3.0.0 |
|
949 |
* |
|
950 |
* @param object $menu_item The menu item object. |
|
951 |
*/ |
|
0 | 952 |
return apply_filters( 'wp_setup_nav_menu_item', $menu_item ); |
953 |
} |
|
954 |
||
955 |
/** |
|
956 |
* Get the menu items associated with a particular object. |
|
957 |
* |
|
958 |
* @since 3.0.0 |
|
959 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
* @param int $object_id The ID of the original object. |
0 | 961 |
* @param string $object_type The type of object, such as "taxonomy" or "post_type." |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
* @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to |
0 | 963 |
* @return array The array of menu item IDs; empty array if none; |
964 |
*/ |
|
965 |
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) { |
|
9 | 966 |
$object_id = (int) $object_id; |
0 | 967 |
$menu_item_ids = array(); |
968 |
||
9 | 969 |
$query = new WP_Query; |
0 | 970 |
$menu_items = $query->query( |
971 |
array( |
|
9 | 972 |
'meta_key' => '_menu_item_object_id', |
973 |
'meta_value' => $object_id, |
|
974 |
'post_status' => 'any', |
|
975 |
'post_type' => 'nav_menu_item', |
|
0 | 976 |
'posts_per_page' => -1, |
977 |
) |
|
978 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
foreach ( (array) $menu_items as $menu_item ) { |
0 | 980 |
if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) { |
5 | 981 |
$menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true ); |
982 |
if ( |
|
983 |
'post_type' == $object_type && |
|
984 |
'post_type' == $menu_item_type |
|
985 |
) { |
|
986 |
$menu_item_ids[] = (int) $menu_item->ID; |
|
987 |
} elseif ( |
|
988 |
'taxonomy' == $object_type && |
|
989 |
'taxonomy' == $menu_item_type && |
|
990 |
get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy |
|
991 |
) { |
|
992 |
$menu_item_ids[] = (int) $menu_item->ID; |
|
993 |
} |
|
0 | 994 |
} |
995 |
} |
|
996 |
||
997 |
return array_unique( $menu_item_ids ); |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* Callback for handling a menu item when its original object is deleted. |
|
1002 |
* |
|
1003 |
* @since 3.0.0 |
|
1004 |
* @access private |
|
1005 |
* |
|
1006 |
* @param int $object_id The ID of the original object being trashed. |
|
1007 |
*/ |
|
1008 |
function _wp_delete_post_menu_item( $object_id = 0 ) { |
|
1009 |
$object_id = (int) $object_id; |
|
1010 |
||
1011 |
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' ); |
|
1012 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1013 |
foreach ( (array) $menu_item_ids as $menu_item_id ) { |
0 | 1014 |
wp_delete_post( $menu_item_id, true ); |
1015 |
} |
|
1016 |
} |
|
1017 |
||
1018 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
* Serves as a callback for handling a menu item when its original object is deleted. |
0 | 1020 |
* |
1021 |
* @since 3.0.0 |
|
1022 |
* @access private |
|
1023 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1024 |
* @param int $object_id Optional. The ID of the original object being trashed. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1025 |
* @param int $tt_id Term taxonomy ID. Unused. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1026 |
* @param string $taxonomy Taxonomy slug. |
0 | 1027 |
*/ |
1028 |
function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) { |
|
1029 |
$object_id = (int) $object_id; |
|
1030 |
||
1031 |
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy ); |
|
1032 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1033 |
foreach ( (array) $menu_item_ids as $menu_item_id ) { |
0 | 1034 |
wp_delete_post( $menu_item_id, true ); |
1035 |
} |
|
1036 |
} |
|
1037 |
||
1038 |
/** |
|
1039 |
* Automatically add newly published page objects to menus with that as an option. |
|
1040 |
* |
|
1041 |
* @since 3.0.0 |
|
1042 |
* @access private |
|
1043 |
* |
|
1044 |
* @param string $new_status The new status of the post object. |
|
1045 |
* @param string $old_status The old status of the post object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1046 |
* @param object $post The post object being transitioned from one status to another. |
0 | 1047 |
*/ |
1048 |
function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { |
|
9 | 1049 |
if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) { |
0 | 1050 |
return; |
9 | 1051 |
} |
1052 |
if ( ! empty( $post->post_parent ) ) { |
|
0 | 1053 |
return; |
9 | 1054 |
} |
0 | 1055 |
$auto_add = get_option( 'nav_menu_options' ); |
9 | 1056 |
if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) { |
0 | 1057 |
return; |
9 | 1058 |
} |
0 | 1059 |
$auto_add = $auto_add['auto_add']; |
9 | 1060 |
if ( empty( $auto_add ) || ! is_array( $auto_add ) ) { |
0 | 1061 |
return; |
9 | 1062 |
} |
0 | 1063 |
|
1064 |
$args = array( |
|
1065 |
'menu-item-object-id' => $post->ID, |
|
9 | 1066 |
'menu-item-object' => $post->post_type, |
1067 |
'menu-item-type' => 'post_type', |
|
1068 |
'menu-item-status' => 'publish', |
|
0 | 1069 |
); |
1070 |
||
1071 |
foreach ( $auto_add as $menu_id ) { |
|
1072 |
$items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); |
|
9 | 1073 |
if ( ! is_array( $items ) ) { |
0 | 1074 |
continue; |
9 | 1075 |
} |
0 | 1076 |
foreach ( $items as $item ) { |
9 | 1077 |
if ( $post->ID == $item->object_id ) { |
0 | 1078 |
continue 2; |
9 | 1079 |
} |
0 | 1080 |
} |
1081 |
wp_update_nav_menu_item( $menu_id, 0, $args ); |
|
1082 |
} |
|
1083 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1085 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1086 |
* Delete auto-draft posts associated with the supplied changeset. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1087 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1088 |
* @since 4.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1090 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1091 |
* @param int $post_id Post ID for the customize_changeset. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1093 |
function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1095 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1096 |
if ( ! $post || 'customize_changeset' !== $post->post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
$data = json_decode( $post->post_content, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1101 |
if ( empty( $data['nav_menus_created_posts']['value'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1105 |
foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
if ( empty( $stub_post_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1107 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1108 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1109 |
if ( 'auto-draft' === get_post_status( $stub_post_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
wp_delete_post( $stub_post_id, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1111 |
} elseif ( 'draft' === get_post_status( $stub_post_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1112 |
wp_trash_post( $stub_post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
delete_post_meta( $stub_post_id, '_customize_changeset_uuid' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1115 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1119 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
* Handle menu config after theme change. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
function _wp_menus_changed() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
$old_nav_menu_locations = get_option( 'theme_switch_menu_locations', array() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
$new_nav_menu_locations = get_nav_menu_locations(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
$mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
delete_option( 'theme_switch_menu_locations' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
* Maps nav menu locations according to assignments in previously active theme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
* @param array $new_nav_menu_locations New nav menu locations assignments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
* @param array $old_nav_menu_locations Old nav menu locations assignments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
* @return array Nav menus mapped to new nav menu locations. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
$registered_nav_menus = get_registered_nav_menus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
$new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
// Short-circuit if there are no old nav menu location assignments to map. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1148 |
if ( empty( $old_nav_menu_locations ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
return $new_nav_menu_locations; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
// If old and new theme have just one location, map it and we're done. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
$new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
return $new_nav_menu_locations; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
$old_locations = array_keys( $old_nav_menu_locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
// Map locations with the same slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
foreach ( $registered_nav_menus as $location => $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
if ( in_array( $location, $old_locations, true ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
$new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
unset( $old_nav_menu_locations[ $location ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1165 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1167 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
// If there are no old nav menu locations left, then we're done. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
if ( empty( $old_nav_menu_locations ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
return $new_nav_menu_locations; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
* If old and new theme both have locations that contain phrases |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* from within the same group, make an educated guess and map it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
$common_slug_groups = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
array( 'social' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
// Go through each group... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
foreach ( $common_slug_groups as $slug_group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
// ...and see if any of these slugs... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
foreach ( $slug_group as $slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
// ...and any of the new menu locations... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
foreach ( $registered_nav_menus as $new_location => $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
// ...actually match! |
9 | 1193 |
if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) { |
1194 |
continue; |
|
1195 |
} elseif ( is_numeric( $new_location ) && $new_location !== $slug ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
// Then see if any of the old locations... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
foreach ( $old_nav_menu_locations as $location => $menu_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
// ...and any slug in the same group... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
foreach ( $slug_group as $slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
// ... have a match as well. |
9 | 1206 |
if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) { |
1207 |
continue; |
|
1208 |
} elseif ( is_numeric( $location ) && $location !== $slug ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
// Make sure this location wasn't mapped and removed previously. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
if ( ! empty( $old_nav_menu_locations[ $location ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
// We have a match that can be mapped! |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
$new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
// Remove the mapped location so it can't be mapped again. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
unset( $old_nav_menu_locations[ $location ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
// Go back and check the next new menu location. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
continue 3; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
} // endforeach ( $slug_group as $slug ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
} // endforeach ( $old_nav_menu_locations as $location => $menu_id ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
} // endforeach foreach ( $registered_nav_menus as $new_location => $name ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
} // endforeach ( $slug_group as $slug ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
} // endforeach ( $common_slug_groups as $slug_group ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
return $new_nav_menu_locations; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
} |