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