0
|
1 |
<?php |
|
2 |
|
|
3 |
/** |
|
4 |
* Create HTML list of nav menu input items. |
|
5 |
* |
|
6 |
* @package WordPress |
|
7 |
* @since 3.0.0 |
|
8 |
* @uses Walker_Nav_Menu |
|
9 |
*/ |
|
10 |
class Walker_Nav_Menu_Edit extends Walker_Nav_Menu { |
|
11 |
/** |
|
12 |
* Starts the list before the elements are added. |
|
13 |
* |
|
14 |
* @see Walker_Nav_Menu::start_lvl() |
|
15 |
* |
|
16 |
* @since 3.0.0 |
|
17 |
* |
|
18 |
* @param string $output Passed by reference. |
|
19 |
* @param int $depth Depth of menu item. Used for padding. |
|
20 |
* @param array $args Not used. |
|
21 |
*/ |
|
22 |
function start_lvl( &$output, $depth = 0, $args = array() ) {} |
|
23 |
|
|
24 |
/** |
|
25 |
* Ends the list of after the elements are added. |
|
26 |
* |
|
27 |
* @see Walker_Nav_Menu::end_lvl() |
|
28 |
* |
|
29 |
* @since 3.0.0 |
|
30 |
* |
|
31 |
* @param string $output Passed by reference. |
|
32 |
* @param int $depth Depth of menu item. Used for padding. |
|
33 |
* @param array $args Not used. |
|
34 |
*/ |
|
35 |
function end_lvl( &$output, $depth = 0, $args = array() ) {} |
|
36 |
|
|
37 |
/** |
|
38 |
* Start the element output. |
|
39 |
* |
|
40 |
* @see Walker_Nav_Menu::start_el() |
|
41 |
* @since 3.0.0 |
|
42 |
* |
|
43 |
* @param string $output Passed by reference. Used to append additional content. |
|
44 |
* @param object $item Menu item data object. |
|
45 |
* @param int $depth Depth of menu item. Used for padding. |
|
46 |
* @param array $args Not used. |
|
47 |
* @param int $id Not used. |
|
48 |
*/ |
|
49 |
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
|
50 |
global $_wp_nav_menu_max_depth; |
|
51 |
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth; |
|
52 |
|
|
53 |
ob_start(); |
|
54 |
$item_id = esc_attr( $item->ID ); |
|
55 |
$removed_args = array( |
|
56 |
'action', |
|
57 |
'customlink-tab', |
|
58 |
'edit-menu-item', |
|
59 |
'menu-item', |
|
60 |
'page-tab', |
|
61 |
'_wpnonce', |
|
62 |
); |
|
63 |
|
|
64 |
$original_title = ''; |
|
65 |
if ( 'taxonomy' == $item->type ) { |
|
66 |
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' ); |
|
67 |
if ( is_wp_error( $original_title ) ) |
|
68 |
$original_title = false; |
|
69 |
} elseif ( 'post_type' == $item->type ) { |
|
70 |
$original_object = get_post( $item->object_id ); |
|
71 |
$original_title = get_the_title( $original_object->ID ); |
|
72 |
} |
|
73 |
|
|
74 |
$classes = array( |
|
75 |
'menu-item menu-item-depth-' . $depth, |
|
76 |
'menu-item-' . esc_attr( $item->object ), |
|
77 |
'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'), |
|
78 |
); |
|
79 |
|
|
80 |
$title = $item->title; |
|
81 |
|
|
82 |
if ( ! empty( $item->_invalid ) ) { |
|
83 |
$classes[] = 'menu-item-invalid'; |
|
84 |
/* translators: %s: title of menu item which is invalid */ |
|
85 |
$title = sprintf( __( '%s (Invalid)' ), $item->title ); |
|
86 |
} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) { |
|
87 |
$classes[] = 'pending'; |
|
88 |
/* translators: %s: title of menu item in draft status */ |
|
89 |
$title = sprintf( __('%s (Pending)'), $item->title ); |
|
90 |
} |
|
91 |
|
|
92 |
$title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label; |
|
93 |
|
|
94 |
$submenu_text = ''; |
|
95 |
if ( 0 == $depth ) |
|
96 |
$submenu_text = 'style="display: none;"'; |
|
97 |
|
|
98 |
?> |
|
99 |
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>"> |
|
100 |
<dl class="menu-item-bar"> |
|
101 |
<dt class="menu-item-handle"> |
|
102 |
<span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span> |
|
103 |
<span class="item-controls"> |
|
104 |
<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span> |
|
105 |
<span class="item-order hide-if-js"> |
|
106 |
<a href="<?php |
|
107 |
echo wp_nonce_url( |
|
108 |
add_query_arg( |
|
109 |
array( |
|
110 |
'action' => 'move-up-menu-item', |
|
111 |
'menu-item' => $item_id, |
|
112 |
), |
|
113 |
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) ) |
|
114 |
), |
|
115 |
'move-menu_item' |
|
116 |
); |
|
117 |
?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">↑</abbr></a> |
|
118 |
| |
|
119 |
<a href="<?php |
|
120 |
echo wp_nonce_url( |
|
121 |
add_query_arg( |
|
122 |
array( |
|
123 |
'action' => 'move-down-menu-item', |
|
124 |
'menu-item' => $item_id, |
|
125 |
), |
|
126 |
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) ) |
|
127 |
), |
|
128 |
'move-menu_item' |
|
129 |
); |
|
130 |
?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">↓</abbr></a> |
|
131 |
</span> |
|
132 |
<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php esc_attr_e('Edit Menu Item'); ?>" href="<?php |
|
133 |
echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) ); |
|
134 |
?>"><?php _e( 'Edit Menu Item' ); ?></a> |
|
135 |
</span> |
|
136 |
</dt> |
|
137 |
</dl> |
|
138 |
|
|
139 |
<div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>"> |
|
140 |
<?php if( 'custom' == $item->type ) : ?> |
|
141 |
<p class="field-url description description-wide"> |
|
142 |
<label for="edit-menu-item-url-<?php echo $item_id; ?>"> |
|
143 |
<?php _e( 'URL' ); ?><br /> |
|
144 |
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" /> |
|
145 |
</label> |
|
146 |
</p> |
|
147 |
<?php endif; ?> |
|
148 |
<p class="description description-thin"> |
|
149 |
<label for="edit-menu-item-title-<?php echo $item_id; ?>"> |
|
150 |
<?php _e( 'Navigation Label' ); ?><br /> |
|
151 |
<input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->title ); ?>" /> |
|
152 |
</label> |
|
153 |
</p> |
|
154 |
<p class="description description-thin"> |
|
155 |
<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>"> |
|
156 |
<?php _e( 'Title Attribute' ); ?><br /> |
|
157 |
<input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_excerpt ); ?>" /> |
|
158 |
</label> |
|
159 |
</p> |
|
160 |
<p class="field-link-target description"> |
|
161 |
<label for="edit-menu-item-target-<?php echo $item_id; ?>"> |
|
162 |
<input type="checkbox" id="edit-menu-item-target-<?php echo $item_id; ?>" value="_blank" name="menu-item-target[<?php echo $item_id; ?>]"<?php checked( $item->target, '_blank' ); ?> /> |
|
163 |
<?php _e( 'Open link in a new window/tab' ); ?> |
|
164 |
</label> |
|
165 |
</p> |
|
166 |
<p class="field-css-classes description description-thin"> |
|
167 |
<label for="edit-menu-item-classes-<?php echo $item_id; ?>"> |
|
168 |
<?php _e( 'CSS Classes (optional)' ); ?><br /> |
|
169 |
<input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" /> |
|
170 |
</label> |
|
171 |
</p> |
|
172 |
<p class="field-xfn description description-thin"> |
|
173 |
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>"> |
|
174 |
<?php _e( 'Link Relationship (XFN)' ); ?><br /> |
|
175 |
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" /> |
|
176 |
</label> |
|
177 |
</p> |
|
178 |
<p class="field-description description description-wide"> |
|
179 |
<label for="edit-menu-item-description-<?php echo $item_id; ?>"> |
|
180 |
<?php _e( 'Description' ); ?><br /> |
|
181 |
<textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html( $item->description ); // textarea_escaped ?></textarea> |
|
182 |
<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span> |
|
183 |
</label> |
|
184 |
</p> |
|
185 |
|
|
186 |
<p class="field-move hide-if-no-js description description-wide"> |
|
187 |
<label> |
|
188 |
<span><?php _e( 'Move' ); ?></span> |
|
189 |
<a href="#" class="menus-move-up"><?php _e( 'Up one' ); ?></a> |
|
190 |
<a href="#" class="menus-move-down"><?php _e( 'Down one' ); ?></a> |
|
191 |
<a href="#" class="menus-move-left"></a> |
|
192 |
<a href="#" class="menus-move-right"></a> |
|
193 |
<a href="#" class="menus-move-top"><?php _e( 'To the top' ); ?></a> |
|
194 |
</label> |
|
195 |
</p> |
|
196 |
|
|
197 |
<div class="menu-item-actions description-wide submitbox"> |
|
198 |
<?php if( 'custom' != $item->type && $original_title !== false ) : ?> |
|
199 |
<p class="link-to-original"> |
|
200 |
<?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?> |
|
201 |
</p> |
|
202 |
<?php endif; ?> |
|
203 |
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php |
|
204 |
echo wp_nonce_url( |
|
205 |
add_query_arg( |
|
206 |
array( |
|
207 |
'action' => 'delete-menu-item', |
|
208 |
'menu-item' => $item_id, |
|
209 |
), |
|
210 |
admin_url( 'nav-menus.php' ) |
|
211 |
), |
|
212 |
'delete-menu_item_' . $item_id |
|
213 |
); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) ); |
|
214 |
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a> |
|
215 |
</div> |
|
216 |
|
|
217 |
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" /> |
|
218 |
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" /> |
|
219 |
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" /> |
|
220 |
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" /> |
|
221 |
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" /> |
|
222 |
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" /> |
|
223 |
</div><!-- .menu-item-settings--> |
|
224 |
<ul class="menu-item-transport"></ul> |
|
225 |
<?php |
|
226 |
$output .= ob_get_clean(); |
|
227 |
} |
|
228 |
|
|
229 |
} // Walker_Nav_Menu_Edit |
|
230 |
|
|
231 |
/** |
|
232 |
* Create HTML list of nav menu input items. |
|
233 |
* |
|
234 |
* @package WordPress |
|
235 |
* @since 3.0.0 |
|
236 |
* @uses Walker_Nav_Menu |
|
237 |
*/ |
|
238 |
class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu { |
|
239 |
function __construct( $fields = false ) { |
|
240 |
if ( $fields ) { |
|
241 |
$this->db_fields = $fields; |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
/** |
|
246 |
* Starts the list before the elements are added. |
|
247 |
* |
|
248 |
* @see Walker_Nav_Menu::start_lvl() |
|
249 |
* |
|
250 |
* @since 3.0.0 |
|
251 |
* |
|
252 |
* @param string $output Passed by reference. Used to append additional content. |
|
253 |
* @param int $depth Depth of page. Used for padding. |
|
254 |
* @param array $args Not used. |
|
255 |
*/ |
|
256 |
function start_lvl( &$output, $depth = 0, $args = array() ) { |
|
257 |
$indent = str_repeat( "\t", $depth ); |
|
258 |
$output .= "\n$indent<ul class='children'>\n"; |
|
259 |
} |
|
260 |
|
|
261 |
/** |
|
262 |
* Ends the list of after the elements are added. |
|
263 |
* |
|
264 |
* @see Walker_Nav_Menu::end_lvl() |
|
265 |
* |
|
266 |
* @since 3.0.0 |
|
267 |
* |
|
268 |
* @param string $output Passed by reference. Used to append additional content. |
|
269 |
* @param int $depth Depth of page. Used for padding. |
|
270 |
* @param array $args Not used. |
|
271 |
*/ |
|
272 |
function end_lvl( &$output, $depth = 0, $args = array() ) { |
|
273 |
$indent = str_repeat( "\t", $depth ); |
|
274 |
$output .= "\n$indent</ul>"; |
|
275 |
} |
|
276 |
|
|
277 |
/** |
|
278 |
* Start the element output. |
|
279 |
* |
|
280 |
* @see Walker_Nav_Menu::start_el() |
|
281 |
* |
|
282 |
* @since 3.0.0 |
|
283 |
* |
|
284 |
* @param string $output Passed by reference. Used to append additional content. |
|
285 |
* @param object $item Menu item data object. |
|
286 |
* @param int $depth Depth of menu item. Used for padding. |
|
287 |
* @param array $args Not used. |
|
288 |
* @param int $id Not used. |
|
289 |
*/ |
|
290 |
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
|
291 |
global $_nav_menu_placeholder; |
|
292 |
|
|
293 |
$_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1; |
|
294 |
$possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder; |
|
295 |
$possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0; |
|
296 |
|
|
297 |
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; |
|
298 |
|
|
299 |
$output .= $indent . '<li>'; |
|
300 |
$output .= '<label class="menu-item-title">'; |
|
301 |
$output .= '<input type="checkbox" class="menu-item-checkbox'; |
|
302 |
|
|
303 |
if ( ! empty( $item->front_or_home ) ) |
|
304 |
$output .= ' add-to-top'; |
|
305 |
|
|
306 |
$output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> '; |
|
307 |
|
|
308 |
if ( ! empty( $item->label ) ) { |
|
309 |
$title = $item->label; |
|
310 |
} elseif ( isset( $item->post_type ) ) { |
|
311 |
/** This filter is documented in wp-includes/post-template.php */ |
|
312 |
$title = apply_filters( 'the_title', $item->post_title, $item->ID ); |
|
313 |
if ( ! empty( $item->front_or_home ) && _x( 'Home', 'nav menu home label' ) !== $title ) |
|
314 |
$title = sprintf( _x( 'Home: %s', 'nav menu front page title' ), $title ); |
|
315 |
} |
|
316 |
|
|
317 |
$output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title ); |
|
318 |
$output .= '</label>'; |
|
319 |
|
|
320 |
// Menu item hidden fields |
|
321 |
$output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />'; |
|
322 |
$output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />'; |
|
323 |
$output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="'. esc_attr( $item->menu_item_parent ) .'" />'; |
|
324 |
$output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="'. esc_attr( $item->type ) .'" />'; |
|
325 |
$output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />'; |
|
326 |
$output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />'; |
|
327 |
$output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />'; |
|
328 |
$output .= '<input type="hidden" class="menu-item-attr_title" name="menu-item[' . $possible_object_id . '][menu-item-attr_title]" value="'. esc_attr( $item->attr_title ) .'" />'; |
|
329 |
$output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />'; |
|
330 |
$output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />'; |
|
331 |
} |
|
332 |
|
|
333 |
} // Walker_Nav_Menu_Checklist |
|
334 |
|
|
335 |
/** |
|
336 |
* Prints the appropriate response to a menu quick search. |
|
337 |
* |
|
338 |
* @since 3.0.0 |
|
339 |
* |
|
340 |
* @param array $request The unsanitized request values. |
|
341 |
*/ |
|
342 |
function _wp_ajax_menu_quick_search( $request = array() ) { |
|
343 |
$args = array(); |
|
344 |
$type = isset( $request['type'] ) ? $request['type'] : ''; |
|
345 |
$object_type = isset( $request['object_type'] ) ? $request['object_type'] : ''; |
|
346 |
$query = isset( $request['q'] ) ? $request['q'] : ''; |
|
347 |
$response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json'; |
|
348 |
|
|
349 |
if ( 'markup' == $response_format ) { |
|
350 |
$args['walker'] = new Walker_Nav_Menu_Checklist; |
|
351 |
} |
|
352 |
|
|
353 |
if ( 'get-post-item' == $type ) { |
|
354 |
if ( post_type_exists( $object_type ) ) { |
|
355 |
if ( isset( $request['ID'] ) ) { |
|
356 |
$object_id = (int) $request['ID']; |
|
357 |
if ( 'markup' == $response_format ) { |
|
358 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args ); |
|
359 |
} elseif ( 'json' == $response_format ) { |
|
360 |
$post_obj = get_post( $object_id ); |
|
361 |
echo json_encode( |
|
362 |
array( |
|
363 |
'ID' => $object_id, |
|
364 |
'post_title' => get_the_title( $object_id ), |
|
365 |
'post_type' => get_post_type( $object_id ), |
|
366 |
) |
|
367 |
); |
|
368 |
echo "\n"; |
|
369 |
} |
|
370 |
} |
|
371 |
} elseif ( taxonomy_exists( $object_type ) ) { |
|
372 |
if ( isset( $request['ID'] ) ) { |
|
373 |
$object_id = (int) $request['ID']; |
|
374 |
if ( 'markup' == $response_format ) { |
|
375 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args ); |
|
376 |
} elseif ( 'json' == $response_format ) { |
|
377 |
$post_obj = get_term( $object_id, $object_type ); |
|
378 |
echo json_encode( |
|
379 |
array( |
|
380 |
'ID' => $object_id, |
|
381 |
'post_title' => $post_obj->name, |
|
382 |
'post_type' => $object_type, |
|
383 |
) |
|
384 |
); |
|
385 |
echo "\n"; |
|
386 |
} |
|
387 |
} |
|
388 |
|
|
389 |
} |
|
390 |
|
|
391 |
} elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) { |
|
392 |
if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) { |
|
393 |
query_posts(array( |
|
394 |
'posts_per_page' => 10, |
|
395 |
'post_type' => $matches[2], |
|
396 |
's' => $query, |
|
397 |
)); |
|
398 |
if ( ! have_posts() ) |
|
399 |
return; |
|
400 |
while ( have_posts() ) { |
|
401 |
the_post(); |
|
402 |
if ( 'markup' == $response_format ) { |
|
403 |
$var_by_ref = get_the_ID(); |
|
404 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args ); |
|
405 |
} elseif ( 'json' == $response_format ) { |
|
406 |
echo json_encode( |
|
407 |
array( |
|
408 |
'ID' => get_the_ID(), |
|
409 |
'post_title' => get_the_title(), |
|
410 |
'post_type' => get_post_type(), |
|
411 |
) |
|
412 |
); |
|
413 |
echo "\n"; |
|
414 |
} |
|
415 |
} |
|
416 |
} elseif ( 'taxonomy' == $matches[1] ) { |
|
417 |
$terms = get_terms( $matches[2], array( |
|
418 |
'name__like' => $query, |
|
419 |
'number' => 10, |
|
420 |
)); |
|
421 |
if ( empty( $terms ) || is_wp_error( $terms ) ) |
|
422 |
return; |
|
423 |
foreach( (array) $terms as $term ) { |
|
424 |
if ( 'markup' == $response_format ) { |
|
425 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args ); |
|
426 |
} elseif ( 'json' == $response_format ) { |
|
427 |
echo json_encode( |
|
428 |
array( |
|
429 |
'ID' => $term->term_id, |
|
430 |
'post_title' => $term->name, |
|
431 |
'post_type' => $matches[2], |
|
432 |
) |
|
433 |
); |
|
434 |
echo "\n"; |
|
435 |
} |
|
436 |
} |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
|
|
441 |
/** |
|
442 |
* Register nav menu metaboxes and advanced menu items |
|
443 |
* |
|
444 |
* @since 3.0.0 |
|
445 |
**/ |
|
446 |
function wp_nav_menu_setup() { |
|
447 |
// Register meta boxes |
|
448 |
wp_nav_menu_post_type_meta_boxes(); |
|
449 |
add_meta_box( 'add-custom-links', __( 'Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' ); |
|
450 |
wp_nav_menu_taxonomy_meta_boxes(); |
|
451 |
|
|
452 |
// Register advanced menu items (columns) |
|
453 |
add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' ); |
|
454 |
|
|
455 |
// If first time editing, disable advanced items by default. |
|
456 |
if( false === get_user_option( 'managenav-menuscolumnshidden' ) ) { |
|
457 |
$user = wp_get_current_user(); |
|
458 |
update_user_option($user->ID, 'managenav-menuscolumnshidden', |
|
459 |
array( 0 => 'link-target', 1 => 'css-classes', 2 => 'xfn', 3 => 'description', ), |
|
460 |
true); |
|
461 |
} |
|
462 |
} |
|
463 |
|
|
464 |
/** |
|
465 |
* Limit the amount of meta boxes to just links, pages and cats for first time users. |
|
466 |
* |
|
467 |
* @since 3.0.0 |
|
468 |
**/ |
|
469 |
function wp_initial_nav_menu_meta_boxes() { |
|
470 |
global $wp_meta_boxes; |
|
471 |
|
|
472 |
if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) ) |
|
473 |
return; |
|
474 |
|
|
475 |
$initial_meta_boxes = array( 'nav-menu-theme-locations', 'add-page', 'add-custom-links', 'add-category' ); |
|
476 |
$hidden_meta_boxes = array(); |
|
477 |
|
|
478 |
foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) { |
|
479 |
foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) { |
|
480 |
foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) { |
|
481 |
if ( in_array( $box['id'], $initial_meta_boxes ) ) { |
|
482 |
unset( $box['id'] ); |
|
483 |
} else { |
|
484 |
$hidden_meta_boxes[] = $box['id']; |
|
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
|
|
490 |
$user = wp_get_current_user(); |
|
491 |
update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true ); |
|
492 |
} |
|
493 |
|
|
494 |
/** |
|
495 |
* Creates metaboxes for any post type menu item. |
|
496 |
* |
|
497 |
* @since 3.0.0 |
|
498 |
*/ |
|
499 |
function wp_nav_menu_post_type_meta_boxes() { |
|
500 |
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); |
|
501 |
|
|
502 |
if ( ! $post_types ) |
|
503 |
return; |
|
504 |
|
|
505 |
foreach ( $post_types as $post_type ) { |
|
506 |
$post_type = apply_filters( 'nav_menu_meta_box_object', $post_type ); |
|
507 |
if ( $post_type ) { |
|
508 |
$id = $post_type->name; |
|
509 |
// give pages a higher priority |
|
510 |
$priority = ( 'page' == $post_type->name ? 'core' : 'default' ); |
|
511 |
add_meta_box( "add-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type ); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
|
|
516 |
/** |
|
517 |
* Creates metaboxes for any taxonomy menu item. |
|
518 |
* |
|
519 |
* @since 3.0.0 |
|
520 |
*/ |
|
521 |
function wp_nav_menu_taxonomy_meta_boxes() { |
|
522 |
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' ); |
|
523 |
|
|
524 |
if ( !$taxonomies ) |
|
525 |
return; |
|
526 |
|
|
527 |
foreach ( $taxonomies as $tax ) { |
|
528 |
$tax = apply_filters( 'nav_menu_meta_box_object', $tax ); |
|
529 |
if ( $tax ) { |
|
530 |
$id = $tax->name; |
|
531 |
add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax ); |
|
532 |
} |
|
533 |
} |
|
534 |
} |
|
535 |
|
|
536 |
/** |
|
537 |
* Check whether to disable the Menu Locations meta box submit button |
|
538 |
* |
|
539 |
* @since 3.6.0 |
|
540 |
* |
|
541 |
* @uses global $one_theme_location_no_menus to determine if no menus exist |
|
542 |
* @uses disabled() to output the disabled attribute in $other_attributes param in submit_button() |
|
543 |
* |
|
544 |
* @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu |
|
545 |
* @return string Disabled attribute if at least one menu exists, false if not |
|
546 |
*/ |
|
547 |
function wp_nav_menu_disabled_check( $nav_menu_selected_id ) { |
|
548 |
global $one_theme_location_no_menus; |
|
549 |
|
|
550 |
if ( $one_theme_location_no_menus ) |
|
551 |
return false; |
|
552 |
|
|
553 |
return disabled( $nav_menu_selected_id, 0 ); |
|
554 |
} |
|
555 |
|
|
556 |
/** |
|
557 |
* Displays a metabox for the custom links menu item. |
|
558 |
* |
|
559 |
* @since 3.0.0 |
|
560 |
*/ |
|
561 |
function wp_nav_menu_item_link_meta_box() { |
|
562 |
global $_nav_menu_placeholder, $nav_menu_selected_id; |
|
563 |
|
|
564 |
$_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1; |
|
565 |
|
|
566 |
?> |
|
567 |
<div class="customlinkdiv" id="customlinkdiv"> |
|
568 |
<input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" /> |
|
569 |
<p id="menu-item-url-wrap"> |
|
570 |
<label class="howto" for="custom-menu-item-url"> |
|
571 |
<span><?php _e('URL'); ?></span> |
|
572 |
<input id="custom-menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" type="text" class="code menu-item-textbox" value="http://" /> |
|
573 |
</label> |
|
574 |
</p> |
|
575 |
|
|
576 |
<p id="menu-item-name-wrap"> |
|
577 |
<label class="howto" for="custom-menu-item-name"> |
|
578 |
<span><?php _e( 'Link Text' ); ?></span> |
|
579 |
<input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox input-with-default-title" title="<?php esc_attr_e('Menu Item'); ?>" /> |
|
580 |
</label> |
|
581 |
</p> |
|
582 |
|
|
583 |
<p class="button-controls"> |
|
584 |
<span class="add-to-menu"> |
|
585 |
<input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" /> |
|
586 |
<span class="spinner"></span> |
|
587 |
</span> |
|
588 |
</p> |
|
589 |
|
|
590 |
</div><!-- /.customlinkdiv --> |
|
591 |
<?php |
|
592 |
} |
|
593 |
|
|
594 |
/** |
|
595 |
* Displays a metabox for a post type menu item. |
|
596 |
* |
|
597 |
* @since 3.0.0 |
|
598 |
* |
|
599 |
* @param string $object Not used. |
|
600 |
* @param string $post_type The post type object. |
|
601 |
*/ |
|
602 |
function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) { |
|
603 |
global $_nav_menu_placeholder, $nav_menu_selected_id; |
|
604 |
|
|
605 |
$post_type_name = $post_type['args']->name; |
|
606 |
|
|
607 |
// paginate browsing for large numbers of post objects |
|
608 |
$per_page = 50; |
|
609 |
$pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; |
|
610 |
$offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0; |
|
611 |
|
|
612 |
$args = array( |
|
613 |
'offset' => $offset, |
|
614 |
'order' => 'ASC', |
|
615 |
'orderby' => 'title', |
|
616 |
'posts_per_page' => $per_page, |
|
617 |
'post_type' => $post_type_name, |
|
618 |
'suppress_filters' => true, |
|
619 |
'update_post_term_cache' => false, |
|
620 |
'update_post_meta_cache' => false |
|
621 |
); |
|
622 |
|
|
623 |
if ( isset( $post_type['args']->_default_query ) ) |
|
624 |
$args = array_merge($args, (array) $post_type['args']->_default_query ); |
|
625 |
|
|
626 |
// @todo transient caching of these results with proper invalidation on updating of a post of this type |
|
627 |
$get_posts = new WP_Query; |
|
628 |
$posts = $get_posts->query( $args ); |
|
629 |
if ( ! $get_posts->post_count ) { |
|
630 |
echo '<p>' . __( 'No items.' ) . '</p>'; |
|
631 |
return; |
|
632 |
} |
|
633 |
|
|
634 |
$post_type_object = get_post_type_object($post_type_name); |
|
635 |
|
|
636 |
$num_pages = $get_posts->max_num_pages; |
|
637 |
|
|
638 |
$page_links = paginate_links( array( |
|
639 |
'base' => add_query_arg( |
|
640 |
array( |
|
641 |
$post_type_name . '-tab' => 'all', |
|
642 |
'paged' => '%#%', |
|
643 |
'item-type' => 'post_type', |
|
644 |
'item-object' => $post_type_name, |
|
645 |
) |
|
646 |
), |
|
647 |
'format' => '', |
|
648 |
'prev_text' => __('«'), |
|
649 |
'next_text' => __('»'), |
|
650 |
'total' => $num_pages, |
|
651 |
'current' => $pagenum |
|
652 |
)); |
|
653 |
|
|
654 |
if ( !$posts ) |
|
655 |
$error = '<li id="error">'. $post_type['args']->labels->not_found .'</li>'; |
|
656 |
|
|
657 |
$db_fields = false; |
|
658 |
if ( is_post_type_hierarchical( $post_type_name ) ) { |
|
659 |
$db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' ); |
|
660 |
} |
|
661 |
|
|
662 |
$walker = new Walker_Nav_Menu_Checklist( $db_fields ); |
|
663 |
|
|
664 |
$current_tab = 'most-recent'; |
|
665 |
if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) { |
|
666 |
$current_tab = $_REQUEST[$post_type_name . '-tab']; |
|
667 |
} |
|
668 |
|
|
669 |
if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) { |
|
670 |
$current_tab = 'search'; |
|
671 |
} |
|
672 |
|
|
673 |
$removed_args = array( |
|
674 |
'action', |
|
675 |
'customlink-tab', |
|
676 |
'edit-menu-item', |
|
677 |
'menu-item', |
|
678 |
'page-tab', |
|
679 |
'_wpnonce', |
|
680 |
); |
|
681 |
|
|
682 |
?> |
|
683 |
<div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv"> |
|
684 |
<ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs"> |
|
685 |
<li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
686 |
<a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'most-recent', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"> |
|
687 |
<?php _e( 'Most Recent' ); ?> |
|
688 |
</a> |
|
689 |
</li> |
|
690 |
<li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
691 |
<a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"> |
|
692 |
<?php _e( 'View All' ); ?> |
|
693 |
</a> |
|
694 |
</li> |
|
695 |
<li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
696 |
<a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"> |
|
697 |
<?php _e( 'Search'); ?> |
|
698 |
</a> |
|
699 |
</li> |
|
700 |
</ul><!-- .posttype-tabs --> |
|
701 |
|
|
702 |
<div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php |
|
703 |
echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
704 |
?>"> |
|
705 |
<ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear"> |
|
706 |
<?php |
|
707 |
$recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) ); |
|
708 |
$most_recent = $get_posts->query( $recent_args ); |
|
709 |
$args['walker'] = $walker; |
|
710 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args ); |
|
711 |
?> |
|
712 |
</ul> |
|
713 |
</div><!-- /.tabs-panel --> |
|
714 |
|
|
715 |
<div class="tabs-panel <?php |
|
716 |
echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
717 |
?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search"> |
|
718 |
<?php |
|
719 |
if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) { |
|
720 |
$searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] ); |
|
721 |
$search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) ); |
|
722 |
} else { |
|
723 |
$searched = ''; |
|
724 |
$search_results = array(); |
|
725 |
} |
|
726 |
?> |
|
727 |
<p class="quick-search-wrap"> |
|
728 |
<input type="search" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" /> |
|
729 |
<span class="spinner"></span> |
|
730 |
<?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?> |
|
731 |
</p> |
|
732 |
|
|
733 |
<ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear"> |
|
734 |
<?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?> |
|
735 |
<?php |
|
736 |
$args['walker'] = $walker; |
|
737 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); |
|
738 |
?> |
|
739 |
<?php elseif ( is_wp_error( $search_results ) ) : ?> |
|
740 |
<li><?php echo $search_results->get_error_message(); ?></li> |
|
741 |
<?php elseif ( ! empty( $searched ) ) : ?> |
|
742 |
<li><?php _e('No results found.'); ?></li> |
|
743 |
<?php endif; ?> |
|
744 |
</ul> |
|
745 |
</div><!-- /.tabs-panel --> |
|
746 |
|
|
747 |
<div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php |
|
748 |
echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
749 |
?>"> |
|
750 |
<?php if ( ! empty( $page_links ) ) : ?> |
|
751 |
<div class="add-menu-item-pagelinks"> |
|
752 |
<?php echo $page_links; ?> |
|
753 |
</div> |
|
754 |
<?php endif; ?> |
|
755 |
<ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear"> |
|
756 |
<?php |
|
757 |
$args['walker'] = $walker; |
|
758 |
|
|
759 |
// if we're dealing with pages, let's put a checkbox for the front page at the top of the list |
|
760 |
if ( 'page' == $post_type_name ) { |
|
761 |
$front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0; |
|
762 |
if ( ! empty( $front_page ) ) { |
|
763 |
$front_page_obj = get_post( $front_page ); |
|
764 |
$front_page_obj->front_or_home = true; |
|
765 |
array_unshift( $posts, $front_page_obj ); |
|
766 |
} else { |
|
767 |
$_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1; |
|
768 |
array_unshift( $posts, (object) array( |
|
769 |
'front_or_home' => true, |
|
770 |
'ID' => 0, |
|
771 |
'object_id' => $_nav_menu_placeholder, |
|
772 |
'post_content' => '', |
|
773 |
'post_excerpt' => '', |
|
774 |
'post_parent' => '', |
|
775 |
'post_title' => _x('Home', 'nav menu home label'), |
|
776 |
'post_type' => 'nav_menu_item', |
|
777 |
'type' => 'custom', |
|
778 |
'url' => home_url('/'), |
|
779 |
) ); |
|
780 |
} |
|
781 |
} |
|
782 |
|
|
783 |
$posts = apply_filters( 'nav_menu_items_'.$post_type_name, $posts, $args, $post_type ); |
|
784 |
$checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args ); |
|
785 |
|
|
786 |
if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) { |
|
787 |
$checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items); |
|
788 |
|
|
789 |
} |
|
790 |
|
|
791 |
echo $checkbox_items; |
|
792 |
?> |
|
793 |
</ul> |
|
794 |
<?php if ( ! empty( $page_links ) ) : ?> |
|
795 |
<div class="add-menu-item-pagelinks"> |
|
796 |
<?php echo $page_links; ?> |
|
797 |
</div> |
|
798 |
<?php endif; ?> |
|
799 |
</div><!-- /.tabs-panel --> |
|
800 |
|
|
801 |
<p class="button-controls"> |
|
802 |
<span class="list-controls"> |
|
803 |
<a href="<?php |
|
804 |
echo esc_url( add_query_arg( |
|
805 |
array( |
|
806 |
$post_type_name . '-tab' => 'all', |
|
807 |
'selectall' => 1, |
|
808 |
), |
|
809 |
remove_query_arg( $removed_args ) |
|
810 |
)); |
|
811 |
?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a> |
|
812 |
</span> |
|
813 |
|
|
814 |
<span class="add-to-menu"> |
|
815 |
<input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-post-type-menu-item" id="<?php esc_attr_e( 'submit-posttype-' . $post_type_name ); ?>" /> |
|
816 |
<span class="spinner"></span> |
|
817 |
</span> |
|
818 |
</p> |
|
819 |
|
|
820 |
</div><!-- /.posttypediv --> |
|
821 |
<?php |
|
822 |
} |
|
823 |
|
|
824 |
/** |
|
825 |
* Displays a metabox for a taxonomy menu item. |
|
826 |
* |
|
827 |
* @since 3.0.0 |
|
828 |
* |
|
829 |
* @param string $object Not used. |
|
830 |
* @param string $taxonomy The taxonomy object. |
|
831 |
*/ |
|
832 |
function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) { |
|
833 |
global $nav_menu_selected_id; |
|
834 |
$taxonomy_name = $taxonomy['args']->name; |
|
835 |
|
|
836 |
// paginate browsing for large numbers of objects |
|
837 |
$per_page = 50; |
|
838 |
$pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; |
|
839 |
$offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0; |
|
840 |
|
|
841 |
$args = array( |
|
842 |
'child_of' => 0, |
|
843 |
'exclude' => '', |
|
844 |
'hide_empty' => false, |
|
845 |
'hierarchical' => 1, |
|
846 |
'include' => '', |
|
847 |
'number' => $per_page, |
|
848 |
'offset' => $offset, |
|
849 |
'order' => 'ASC', |
|
850 |
'orderby' => 'name', |
|
851 |
'pad_counts' => false, |
|
852 |
); |
|
853 |
|
|
854 |
$terms = get_terms( $taxonomy_name, $args ); |
|
855 |
|
|
856 |
if ( ! $terms || is_wp_error($terms) ) { |
|
857 |
echo '<p>' . __( 'No items.' ) . '</p>'; |
|
858 |
return; |
|
859 |
} |
|
860 |
|
|
861 |
$num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page ); |
|
862 |
|
|
863 |
$page_links = paginate_links( array( |
|
864 |
'base' => add_query_arg( |
|
865 |
array( |
|
866 |
$taxonomy_name . '-tab' => 'all', |
|
867 |
'paged' => '%#%', |
|
868 |
'item-type' => 'taxonomy', |
|
869 |
'item-object' => $taxonomy_name, |
|
870 |
) |
|
871 |
), |
|
872 |
'format' => '', |
|
873 |
'prev_text' => __('«'), |
|
874 |
'next_text' => __('»'), |
|
875 |
'total' => $num_pages, |
|
876 |
'current' => $pagenum |
|
877 |
)); |
|
878 |
|
|
879 |
$db_fields = false; |
|
880 |
if ( is_taxonomy_hierarchical( $taxonomy_name ) ) { |
|
881 |
$db_fields = array( 'parent' => 'parent', 'id' => 'term_id' ); |
|
882 |
} |
|
883 |
|
|
884 |
$walker = new Walker_Nav_Menu_Checklist( $db_fields ); |
|
885 |
|
|
886 |
$current_tab = 'most-used'; |
|
887 |
if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) { |
|
888 |
$current_tab = $_REQUEST[$taxonomy_name . '-tab']; |
|
889 |
} |
|
890 |
|
|
891 |
if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) { |
|
892 |
$current_tab = 'search'; |
|
893 |
} |
|
894 |
|
|
895 |
$removed_args = array( |
|
896 |
'action', |
|
897 |
'customlink-tab', |
|
898 |
'edit-menu-item', |
|
899 |
'menu-item', |
|
900 |
'page-tab', |
|
901 |
'_wpnonce', |
|
902 |
); |
|
903 |
|
|
904 |
?> |
|
905 |
<div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv"> |
|
906 |
<ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs"> |
|
907 |
<li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
908 |
<a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-pop" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'most-used', remove_query_arg($removed_args))); ?>#tabs-panel-<?php echo $taxonomy_name; ?>-pop"> |
|
909 |
<?php _e( 'Most Used' ); ?> |
|
910 |
</a> |
|
911 |
</li> |
|
912 |
<li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
913 |
<a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#tabs-panel-<?php echo $taxonomy_name; ?>-all"> |
|
914 |
<?php _e( 'View All' ); ?> |
|
915 |
</a> |
|
916 |
</li> |
|
917 |
<li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>> |
|
918 |
<a class="nav-tab-link" data-type="tabs-panel-search-taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>"> |
|
919 |
<?php _e( 'Search' ); ?> |
|
920 |
</a> |
|
921 |
</li> |
|
922 |
</ul><!-- .taxonomy-tabs --> |
|
923 |
|
|
924 |
<div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php |
|
925 |
echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
926 |
?>"> |
|
927 |
<ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" > |
|
928 |
<?php |
|
929 |
$popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) ); |
|
930 |
$args['walker'] = $walker; |
|
931 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args ); |
|
932 |
?> |
|
933 |
</ul> |
|
934 |
</div><!-- /.tabs-panel --> |
|
935 |
|
|
936 |
<div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php |
|
937 |
echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
938 |
?>"> |
|
939 |
<?php if ( ! empty( $page_links ) ) : ?> |
|
940 |
<div class="add-menu-item-pagelinks"> |
|
941 |
<?php echo $page_links; ?> |
|
942 |
</div> |
|
943 |
<?php endif; ?> |
|
944 |
<ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear"> |
|
945 |
<?php |
|
946 |
$args['walker'] = $walker; |
|
947 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args ); |
|
948 |
?> |
|
949 |
</ul> |
|
950 |
<?php if ( ! empty( $page_links ) ) : ?> |
|
951 |
<div class="add-menu-item-pagelinks"> |
|
952 |
<?php echo $page_links; ?> |
|
953 |
</div> |
|
954 |
<?php endif; ?> |
|
955 |
</div><!-- /.tabs-panel --> |
|
956 |
|
|
957 |
<div class="tabs-panel <?php |
|
958 |
echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
|
959 |
?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>"> |
|
960 |
<?php |
|
961 |
if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) { |
|
962 |
$searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ); |
|
963 |
$search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) ); |
|
964 |
} else { |
|
965 |
$searched = ''; |
|
966 |
$search_results = array(); |
|
967 |
} |
|
968 |
?> |
|
969 |
<p class="quick-search-wrap"> |
|
970 |
<input type="search" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" /> |
|
971 |
<span class="spinner"></span> |
|
972 |
<?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?> |
|
973 |
</p> |
|
974 |
|
|
975 |
<ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear"> |
|
976 |
<?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?> |
|
977 |
<?php |
|
978 |
$args['walker'] = $walker; |
|
979 |
echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); |
|
980 |
?> |
|
981 |
<?php elseif ( is_wp_error( $search_results ) ) : ?> |
|
982 |
<li><?php echo $search_results->get_error_message(); ?></li> |
|
983 |
<?php elseif ( ! empty( $searched ) ) : ?> |
|
984 |
<li><?php _e('No results found.'); ?></li> |
|
985 |
<?php endif; ?> |
|
986 |
</ul> |
|
987 |
</div><!-- /.tabs-panel --> |
|
988 |
|
|
989 |
<p class="button-controls"> |
|
990 |
<span class="list-controls"> |
|
991 |
<a href="<?php |
|
992 |
echo esc_url(add_query_arg( |
|
993 |
array( |
|
994 |
$taxonomy_name . '-tab' => 'all', |
|
995 |
'selectall' => 1, |
|
996 |
), |
|
997 |
remove_query_arg($removed_args) |
|
998 |
)); |
|
999 |
?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a> |
|
1000 |
</span> |
|
1001 |
|
|
1002 |
<span class="add-to-menu"> |
|
1003 |
<input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-taxonomy-menu-item" id="<?php esc_attr_e( 'submit-taxonomy-' . $taxonomy_name ); ?>" /> |
|
1004 |
<span class="spinner"></span> |
|
1005 |
</span> |
|
1006 |
</p> |
|
1007 |
|
|
1008 |
</div><!-- /.taxonomydiv --> |
|
1009 |
<?php |
|
1010 |
} |
|
1011 |
|
|
1012 |
/** |
|
1013 |
* Save posted nav menu item data. |
|
1014 |
* |
|
1015 |
* @since 3.0.0 |
|
1016 |
* |
|
1017 |
* @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item. |
|
1018 |
* @param array $menu_data The unsanitized posted menu item data. |
|
1019 |
* @return array The database IDs of the items saved |
|
1020 |
*/ |
|
1021 |
function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) { |
|
1022 |
$menu_id = (int) $menu_id; |
|
1023 |
$items_saved = array(); |
|
1024 |
|
|
1025 |
if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) { |
|
1026 |
|
|
1027 |
// Loop through all the menu items' POST values |
|
1028 |
foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) { |
|
1029 |
if ( |
|
1030 |
empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked |
|
1031 |
( |
|
1032 |
! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set |
|
1033 |
in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default |
|
1034 |
! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page) |
|
1035 |
! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists |
|
1036 |
) |
|
1037 |
) { |
|
1038 |
continue; // then this potential menu item is not getting added to this menu |
|
1039 |
} |
|
1040 |
|
|
1041 |
// if this possible menu item doesn't actually have a menu database ID yet |
|
1042 |
if ( |
|
1043 |
empty( $_item_object_data['menu-item-db-id'] ) || |
|
1044 |
( 0 > $_possible_db_id ) || |
|
1045 |
$_possible_db_id != $_item_object_data['menu-item-db-id'] |
|
1046 |
) { |
|
1047 |
$_actual_db_id = 0; |
|
1048 |
} else { |
|
1049 |
$_actual_db_id = (int) $_item_object_data['menu-item-db-id']; |
|
1050 |
} |
|
1051 |
|
|
1052 |
$args = array( |
|
1053 |
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ), |
|
1054 |
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ), |
|
1055 |
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ), |
|
1056 |
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ), |
|
1057 |
'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ), |
|
1058 |
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ), |
|
1059 |
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ), |
|
1060 |
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ), |
|
1061 |
'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ), |
|
1062 |
'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ), |
|
1063 |
'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ), |
|
1064 |
'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ), |
|
1065 |
'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ), |
|
1066 |
); |
|
1067 |
|
|
1068 |
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args ); |
|
1069 |
|
|
1070 |
} |
|
1071 |
} |
|
1072 |
return $items_saved; |
|
1073 |
} |
|
1074 |
|
|
1075 |
/** |
|
1076 |
* Adds custom arguments to some of the meta box object types. |
|
1077 |
* |
|
1078 |
* @since 3.0.0 |
|
1079 |
* |
|
1080 |
* @access private |
|
1081 |
* |
|
1082 |
* @param object $object The post type or taxonomy meta-object. |
|
1083 |
* @return object The post type of taxonomy object. |
|
1084 |
*/ |
|
1085 |
function _wp_nav_menu_meta_box_object( $object = null ) { |
|
1086 |
if ( isset( $object->name ) ) { |
|
1087 |
|
|
1088 |
if ( 'page' == $object->name ) { |
|
1089 |
$object->_default_query = array( |
|
1090 |
'orderby' => 'menu_order title', |
|
1091 |
'post_status' => 'publish', |
|
1092 |
); |
|
1093 |
|
|
1094 |
// posts should show only published items |
|
1095 |
} elseif ( 'post' == $object->name ) { |
|
1096 |
$object->_default_query = array( |
|
1097 |
'post_status' => 'publish', |
|
1098 |
); |
|
1099 |
|
|
1100 |
// cats should be in reverse chronological order |
|
1101 |
} elseif ( 'category' == $object->name ) { |
|
1102 |
$object->_default_query = array( |
|
1103 |
'orderby' => 'id', |
|
1104 |
'order' => 'DESC', |
|
1105 |
); |
|
1106 |
|
|
1107 |
// custom post types should show only published items |
|
1108 |
} else { |
|
1109 |
$object->_default_query = array( |
|
1110 |
'post_status' => 'publish', |
|
1111 |
); |
|
1112 |
} |
|
1113 |
} |
|
1114 |
|
|
1115 |
return $object; |
|
1116 |
} |
|
1117 |
|
|
1118 |
/** |
|
1119 |
* Returns the menu formatted to edit. |
|
1120 |
* |
|
1121 |
* @since 3.0.0 |
|
1122 |
* |
|
1123 |
* @param string $menu_id The ID of the menu to format. |
|
1124 |
* @return string|WP_Error $output The menu formatted to edit or error object on failure. |
|
1125 |
*/ |
|
1126 |
function wp_get_nav_menu_to_edit( $menu_id = 0 ) { |
|
1127 |
$menu = wp_get_nav_menu_object( $menu_id ); |
|
1128 |
|
|
1129 |
// If the menu exists, get its items. |
|
1130 |
if ( is_nav_menu( $menu ) ) { |
|
1131 |
$menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') ); |
|
1132 |
$result = '<div id="menu-instructions" class="post-body-plain'; |
|
1133 |
$result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">'; |
|
1134 |
$result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>'; |
|
1135 |
$result .= '</div>'; |
|
1136 |
|
|
1137 |
if( empty($menu_items) ) |
|
1138 |
return $result . ' <ul class="menu" id="menu-to-edit"> </ul>'; |
|
1139 |
|
|
1140 |
$walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id ); |
|
1141 |
|
|
1142 |
if ( class_exists( $walker_class_name ) ) |
|
1143 |
$walker = new $walker_class_name; |
|
1144 |
else |
|
1145 |
return new WP_Error( 'menu_walker_not_exist', sprintf( __('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name ) ); |
|
1146 |
|
|
1147 |
$some_pending_menu_items = $some_invalid_menu_items = false; |
|
1148 |
foreach( (array) $menu_items as $menu_item ) { |
|
1149 |
if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status ) |
|
1150 |
$some_pending_menu_items = true; |
|
1151 |
if ( ! empty( $menu_item->_invalid ) ) |
|
1152 |
$some_invalid_menu_items = true; |
|
1153 |
} |
|
1154 |
|
|
1155 |
if ( $some_pending_menu_items ) |
|
1156 |
$result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>'; |
|
1157 |
|
|
1158 |
if ( $some_invalid_menu_items ) |
|
1159 |
$result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>'; |
|
1160 |
|
|
1161 |
$result .= '<ul class="menu" id="menu-to-edit"> '; |
|
1162 |
$result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) ); |
|
1163 |
$result .= ' </ul> '; |
|
1164 |
return $result; |
|
1165 |
} elseif ( is_wp_error( $menu ) ) { |
|
1166 |
return $menu; |
|
1167 |
} |
|
1168 |
|
|
1169 |
} |
|
1170 |
|
|
1171 |
/** |
|
1172 |
* Returns the columns for the nav menus page. |
|
1173 |
* |
|
1174 |
* @since 3.0.0 |
|
1175 |
* |
|
1176 |
* @return string|WP_Error $output The menu formatted to edit or error object on failure. |
|
1177 |
*/ |
|
1178 |
function wp_nav_menu_manage_columns() { |
|
1179 |
return array( |
|
1180 |
'_title' => __('Show advanced menu properties'), |
|
1181 |
'cb' => '<input type="checkbox" />', |
|
1182 |
'link-target' => __('Link Target'), |
|
1183 |
'css-classes' => __('CSS Classes'), |
|
1184 |
'xfn' => __('Link Relationship (XFN)'), |
|
1185 |
'description' => __('Description'), |
|
1186 |
); |
|
1187 |
} |
|
1188 |
|
|
1189 |
/** |
|
1190 |
* Deletes orphaned draft menu items |
|
1191 |
* |
|
1192 |
* @access private |
|
1193 |
* @since 3.0.0 |
|
1194 |
* |
|
1195 |
*/ |
|
1196 |
function _wp_delete_orphaned_draft_menu_items() { |
|
1197 |
global $wpdb; |
|
1198 |
$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
|
1199 |
|
|
1200 |
// delete orphaned draft menu items |
|
1201 |
$menu_items_to_delete = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) ); |
|
1202 |
|
|
1203 |
foreach( (array) $menu_items_to_delete as $menu_item_id ) |
|
1204 |
wp_delete_post( $menu_item_id, true ); |
|
1205 |
} |
|
1206 |
add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items'); |
|
1207 |
|
|
1208 |
/** |
|
1209 |
* Saves nav menu items |
|
1210 |
* |
|
1211 |
* @since 3.6.0 |
|
1212 |
* |
|
1213 |
* @uses wp_get_nav_menu_items() to retrieve the nav menu's menu items |
|
1214 |
* @uses wp_defer_term_counter() to enable then disable term counting |
|
1215 |
* |
|
1216 |
* @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu |
|
1217 |
* @param string $nav_menu_selected_title Title of the currently-selected menu |
|
1218 |
* @return array $messages The menu updated message |
|
1219 |
*/ |
|
1220 |
function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) { |
|
1221 |
$unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish' ) ); |
|
1222 |
|
|
1223 |
$menu_items = array(); |
|
1224 |
// Index menu items by db ID |
|
1225 |
foreach ( $unsorted_menu_items as $_item ) |
|
1226 |
$menu_items[$_item->db_id] = $_item; |
|
1227 |
|
|
1228 |
$post_fields = array( |
|
1229 |
'menu-item-db-id', 'menu-item-object-id', 'menu-item-object', |
|
1230 |
'menu-item-parent-id', 'menu-item-position', 'menu-item-type', |
|
1231 |
'menu-item-title', 'menu-item-url', 'menu-item-description', |
|
1232 |
'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn' |
|
1233 |
); |
|
1234 |
|
|
1235 |
wp_defer_term_counting( true ); |
|
1236 |
// Loop through all the menu items' POST variables |
|
1237 |
if ( ! empty( $_POST['menu-item-db-id'] ) ) { |
|
1238 |
foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) { |
|
1239 |
|
|
1240 |
// Menu item title can't be blank |
|
1241 |
if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] ) |
|
1242 |
continue; |
|
1243 |
|
|
1244 |
$args = array(); |
|
1245 |
foreach ( $post_fields as $field ) |
|
1246 |
$args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : ''; |
|
1247 |
|
|
1248 |
$menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args ); |
|
1249 |
|
|
1250 |
if ( is_wp_error( $menu_item_db_id ) ) |
|
1251 |
$messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>'; |
|
1252 |
elseif ( isset( $menu_items[$menu_item_db_id] ) ) |
|
1253 |
unset( $menu_items[$menu_item_db_id] ); |
|
1254 |
} |
|
1255 |
} |
|
1256 |
|
|
1257 |
// Remove menu items from the menu that weren't in $_POST |
|
1258 |
if ( ! empty( $menu_items ) ) { |
|
1259 |
foreach ( array_keys( $menu_items ) as $menu_item_id ) { |
|
1260 |
if ( is_nav_menu_item( $menu_item_id ) ) { |
|
1261 |
wp_delete_post( $menu_item_id ); |
|
1262 |
} |
|
1263 |
} |
|
1264 |
} |
|
1265 |
|
|
1266 |
// Store 'auto-add' pages. |
|
1267 |
$auto_add = ! empty( $_POST['auto-add-pages'] ); |
|
1268 |
$nav_menu_option = (array) get_option( 'nav_menu_options' ); |
|
1269 |
if ( ! isset( $nav_menu_option['auto_add'] ) ) |
|
1270 |
$nav_menu_option['auto_add'] = array(); |
|
1271 |
if ( $auto_add ) { |
|
1272 |
if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) |
|
1273 |
$nav_menu_option['auto_add'][] = $nav_menu_selected_id; |
|
1274 |
} else { |
|
1275 |
if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) ) |
|
1276 |
unset( $nav_menu_option['auto_add'][$key] ); |
|
1277 |
} |
|
1278 |
// Remove nonexistent/deleted menus |
|
1279 |
$nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) ); |
|
1280 |
update_option( 'nav_menu_options', $nav_menu_option ); |
|
1281 |
|
|
1282 |
wp_defer_term_counting( false ); |
|
1283 |
|
|
1284 |
do_action( 'wp_update_nav_menu', $nav_menu_selected_id ); |
|
1285 |
|
|
1286 |
$messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%1$s</strong> has been updated.' ), $nav_menu_selected_title ) . '</p></div>'; |
|
1287 |
unset( $menu_items, $unsorted_menu_items ); |
|
1288 |
|
|
1289 |
return $messages; |
|
1290 |
} |