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