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