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