|
1 <?php |
|
2 /** |
|
3 * WordPress Customize Nav Menus classes |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Customize |
|
7 * @since 4.3.0 |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Customize Nav Menus class. |
|
12 * |
|
13 * Implements menu management in the Customizer. |
|
14 * |
|
15 * @since 4.3.0 |
|
16 * |
|
17 * @see WP_Customize_Manager |
|
18 */ |
|
19 final class WP_Customize_Nav_Menus { |
|
20 |
|
21 /** |
|
22 * WP_Customize_Manager instance. |
|
23 * |
|
24 * @since 4.3.0 |
|
25 * @var WP_Customize_Manager |
|
26 */ |
|
27 public $manager; |
|
28 |
|
29 /** |
|
30 * Original nav menu locations before the theme was switched. |
|
31 * |
|
32 * @since 4.9.0 |
|
33 * @var array |
|
34 */ |
|
35 protected $original_nav_menu_locations; |
|
36 |
|
37 /** |
|
38 * Constructor. |
|
39 * |
|
40 * @since 4.3.0 |
|
41 * |
|
42 * @param object $manager An instance of the WP_Customize_Manager class. |
|
43 */ |
|
44 public function __construct( $manager ) { |
|
45 $this->manager = $manager; |
|
46 $this->original_nav_menu_locations = get_nav_menu_locations(); |
|
47 |
|
48 // See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L469-L499 |
|
49 add_action( 'customize_register', array( $this, 'customize_register' ), 11 ); |
|
50 add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_dynamic_setting_args' ), 10, 2 ); |
|
51 add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_dynamic_setting_class' ), 10, 3 ); |
|
52 add_action( 'customize_save_nav_menus_created_posts', array( $this, 'save_nav_menus_created_posts' ) ); |
|
53 |
|
54 // Skip remaining hooks when the user can't manage nav menus anyway. |
|
55 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
56 return; |
|
57 } |
|
58 |
|
59 add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) ); |
|
60 add_action( 'wp_ajax_load-available-menu-items-customizer', array( $this, 'ajax_load_available_items' ) ); |
|
61 add_action( 'wp_ajax_search-available-menu-items-customizer', array( $this, 'ajax_search_available_items' ) ); |
|
62 add_action( 'wp_ajax_customize-nav-menus-insert-auto-draft', array( $this, 'ajax_insert_auto_draft_post' ) ); |
|
63 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
64 add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_templates' ) ); |
|
65 add_action( 'customize_controls_print_footer_scripts', array( $this, 'available_items_template' ) ); |
|
66 add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); |
|
67 add_action( 'customize_preview_init', array( $this, 'make_auto_draft_status_previewable' ) ); |
|
68 |
|
69 // Selective Refresh partials. |
|
70 add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Adds a nonce for customizing menus. |
|
75 * |
|
76 * @since 4.5.0 |
|
77 * |
|
78 * @param array $nonces Array of nonces. |
|
79 * @return array $nonces Modified array of nonces. |
|
80 */ |
|
81 public function filter_nonces( $nonces ) { |
|
82 $nonces['customize-menus'] = wp_create_nonce( 'customize-menus' ); |
|
83 return $nonces; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Ajax handler for loading available menu items. |
|
88 * |
|
89 * @since 4.3.0 |
|
90 */ |
|
91 public function ajax_load_available_items() { |
|
92 check_ajax_referer( 'customize-menus', 'customize-menus-nonce' ); |
|
93 |
|
94 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
95 wp_die( -1 ); |
|
96 } |
|
97 |
|
98 $all_items = array(); |
|
99 $item_types = array(); |
|
100 if ( isset( $_POST['item_types'] ) && is_array( $_POST['item_types'] ) ) { |
|
101 $item_types = wp_unslash( $_POST['item_types'] ); |
|
102 } elseif ( isset( $_POST['type'] ) && isset( $_POST['object'] ) ) { // Back compat. |
|
103 $item_types[] = array( |
|
104 'type' => wp_unslash( $_POST['type'] ), |
|
105 'object' => wp_unslash( $_POST['object'] ), |
|
106 'page' => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ), |
|
107 ); |
|
108 } else { |
|
109 wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' ); |
|
110 } |
|
111 |
|
112 foreach ( $item_types as $item_type ) { |
|
113 if ( empty( $item_type['type'] ) || empty( $item_type['object'] ) ) { |
|
114 wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' ); |
|
115 } |
|
116 $type = sanitize_key( $item_type['type'] ); |
|
117 $object = sanitize_key( $item_type['object'] ); |
|
118 $page = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] ); |
|
119 $items = $this->load_available_items_query( $type, $object, $page ); |
|
120 if ( is_wp_error( $items ) ) { |
|
121 wp_send_json_error( $items->get_error_code() ); |
|
122 } |
|
123 $all_items[ $item_type['type'] . ':' . $item_type['object'] ] = $items; |
|
124 } |
|
125 |
|
126 wp_send_json_success( array( 'items' => $all_items ) ); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Performs the post_type and taxonomy queries for loading available menu items. |
|
131 * |
|
132 * @since 4.3.0 |
|
133 * |
|
134 * @param string $type Optional. Accepts any custom object type and has built-in support for |
|
135 * 'post_type' and 'taxonomy'. Default is 'post_type'. |
|
136 * @param string $object Optional. Accepts any registered taxonomy or post type name. Default is 'page'. |
|
137 * @param int $page Optional. The page number used to generate the query offset. Default is '0'. |
|
138 * @return WP_Error|array Returns either a WP_Error object or an array of menu items. |
|
139 */ |
|
140 public function load_available_items_query( $type = 'post_type', $object = 'page', $page = 0 ) { |
|
141 $items = array(); |
|
142 |
|
143 if ( 'post_type' === $type ) { |
|
144 $post_type = get_post_type_object( $object ); |
|
145 if ( ! $post_type ) { |
|
146 return new WP_Error( 'nav_menus_invalid_post_type' ); |
|
147 } |
|
148 |
|
149 if ( 0 === $page && 'page' === $object ) { |
|
150 // Add "Home" link. Treat as a page, but switch to custom on add. |
|
151 $items[] = array( |
|
152 'id' => 'home', |
|
153 'title' => _x( 'Home', 'nav menu home label' ), |
|
154 'type' => 'custom', |
|
155 'type_label' => __( 'Custom Link' ), |
|
156 'object' => '', |
|
157 'url' => home_url(), |
|
158 ); |
|
159 } elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) { |
|
160 // Add a post type archive link. |
|
161 $items[] = array( |
|
162 'id' => $object . '-archive', |
|
163 'title' => $post_type->labels->archives, |
|
164 'type' => 'post_type_archive', |
|
165 'type_label' => __( 'Post Type Archive' ), |
|
166 'object' => $object, |
|
167 'url' => get_post_type_archive_link( $object ), |
|
168 ); |
|
169 } |
|
170 |
|
171 // Prepend posts with nav_menus_created_posts on first page. |
|
172 $posts = array(); |
|
173 if ( 0 === $page && $this->manager->get_setting( 'nav_menus_created_posts' ) ) { |
|
174 foreach ( $this->manager->get_setting( 'nav_menus_created_posts' )->value() as $post_id ) { |
|
175 $auto_draft_post = get_post( $post_id ); |
|
176 if ( $post_type->name === $auto_draft_post->post_type ) { |
|
177 $posts[] = $auto_draft_post; |
|
178 } |
|
179 } |
|
180 } |
|
181 |
|
182 $posts = array_merge( $posts, get_posts( array( |
|
183 'numberposts' => 10, |
|
184 'offset' => 10 * $page, |
|
185 'orderby' => 'date', |
|
186 'order' => 'DESC', |
|
187 'post_type' => $object, |
|
188 ) ) ); |
|
189 |
|
190 foreach ( $posts as $post ) { |
|
191 $post_title = $post->post_title; |
|
192 if ( '' === $post_title ) { |
|
193 /* translators: %d: ID of a post */ |
|
194 $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); |
|
195 } |
|
196 $items[] = array( |
|
197 'id' => "post-{$post->ID}", |
|
198 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
199 'type' => 'post_type', |
|
200 'type_label' => get_post_type_object( $post->post_type )->labels->singular_name, |
|
201 'object' => $post->post_type, |
|
202 'object_id' => intval( $post->ID ), |
|
203 'url' => get_permalink( intval( $post->ID ) ), |
|
204 ); |
|
205 } |
|
206 } elseif ( 'taxonomy' === $type ) { |
|
207 $terms = get_terms( $object, array( |
|
208 'child_of' => 0, |
|
209 'exclude' => '', |
|
210 'hide_empty' => false, |
|
211 'hierarchical' => 1, |
|
212 'include' => '', |
|
213 'number' => 10, |
|
214 'offset' => 10 * $page, |
|
215 'order' => 'DESC', |
|
216 'orderby' => 'count', |
|
217 'pad_counts' => false, |
|
218 ) ); |
|
219 if ( is_wp_error( $terms ) ) { |
|
220 return $terms; |
|
221 } |
|
222 |
|
223 foreach ( $terms as $term ) { |
|
224 $items[] = array( |
|
225 'id' => "term-{$term->term_id}", |
|
226 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
227 'type' => 'taxonomy', |
|
228 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name, |
|
229 'object' => $term->taxonomy, |
|
230 'object_id' => intval( $term->term_id ), |
|
231 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ), |
|
232 ); |
|
233 } |
|
234 } |
|
235 |
|
236 /** |
|
237 * Filters the available menu items. |
|
238 * |
|
239 * @since 4.3.0 |
|
240 * |
|
241 * @param array $items The array of menu items. |
|
242 * @param string $type The object type. |
|
243 * @param string $object The object name. |
|
244 * @param int $page The current page number. |
|
245 */ |
|
246 $items = apply_filters( 'customize_nav_menu_available_items', $items, $type, $object, $page ); |
|
247 |
|
248 return $items; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Ajax handler for searching available menu items. |
|
253 * |
|
254 * @since 4.3.0 |
|
255 */ |
|
256 public function ajax_search_available_items() { |
|
257 check_ajax_referer( 'customize-menus', 'customize-menus-nonce' ); |
|
258 |
|
259 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
260 wp_die( -1 ); |
|
261 } |
|
262 |
|
263 if ( empty( $_POST['search'] ) ) { |
|
264 wp_send_json_error( 'nav_menus_missing_search_parameter' ); |
|
265 } |
|
266 |
|
267 $p = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0; |
|
268 if ( $p < 1 ) { |
|
269 $p = 1; |
|
270 } |
|
271 |
|
272 $s = sanitize_text_field( wp_unslash( $_POST['search'] ) ); |
|
273 $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) ); |
|
274 |
|
275 if ( empty( $items ) ) { |
|
276 wp_send_json_error( array( 'message' => __( 'No results found.' ) ) ); |
|
277 } else { |
|
278 wp_send_json_success( array( 'items' => $items ) ); |
|
279 } |
|
280 } |
|
281 |
|
282 /** |
|
283 * Performs post queries for available-item searching. |
|
284 * |
|
285 * Based on WP_Editor::wp_link_query(). |
|
286 * |
|
287 * @since 4.3.0 |
|
288 * |
|
289 * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments. |
|
290 * @return array Menu items. |
|
291 */ |
|
292 public function search_available_items_query( $args = array() ) { |
|
293 $items = array(); |
|
294 |
|
295 $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); |
|
296 $query = array( |
|
297 'post_type' => array_keys( $post_type_objects ), |
|
298 'suppress_filters' => true, |
|
299 'update_post_term_cache' => false, |
|
300 'update_post_meta_cache' => false, |
|
301 'post_status' => 'publish', |
|
302 'posts_per_page' => 20, |
|
303 ); |
|
304 |
|
305 $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1; |
|
306 $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0; |
|
307 |
|
308 if ( isset( $args['s'] ) ) { |
|
309 $query['s'] = $args['s']; |
|
310 } |
|
311 |
|
312 $posts = array(); |
|
313 |
|
314 // Prepend list of posts with nav_menus_created_posts search results on first page. |
|
315 $nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' ); |
|
316 if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting->value() ) > 0 ) { |
|
317 $stub_post_query = new WP_Query( array_merge( |
|
318 $query, |
|
319 array( |
|
320 'post_status' => 'auto-draft', |
|
321 'post__in' => $nav_menus_created_posts_setting->value(), |
|
322 'posts_per_page' => -1, |
|
323 ) |
|
324 ) ); |
|
325 $posts = array_merge( $posts, $stub_post_query->posts ); |
|
326 } |
|
327 |
|
328 // Query posts. |
|
329 $get_posts = new WP_Query( $query ); |
|
330 $posts = array_merge( $posts, $get_posts->posts ); |
|
331 |
|
332 // Create items for posts. |
|
333 foreach ( $posts as $post ) { |
|
334 $post_title = $post->post_title; |
|
335 if ( '' === $post_title ) { |
|
336 /* translators: %d: ID of a post */ |
|
337 $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); |
|
338 } |
|
339 $items[] = array( |
|
340 'id' => 'post-' . $post->ID, |
|
341 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
342 'type' => 'post_type', |
|
343 'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name, |
|
344 'object' => $post->post_type, |
|
345 'object_id' => intval( $post->ID ), |
|
346 'url' => get_permalink( intval( $post->ID ) ), |
|
347 ); |
|
348 } |
|
349 |
|
350 // Query taxonomy terms. |
|
351 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' ); |
|
352 $terms = get_terms( $taxonomies, array( |
|
353 'name__like' => $args['s'], |
|
354 'number' => 20, |
|
355 'offset' => 20 * ($args['pagenum'] - 1), |
|
356 ) ); |
|
357 |
|
358 // Check if any taxonomies were found. |
|
359 if ( ! empty( $terms ) ) { |
|
360 foreach ( $terms as $term ) { |
|
361 $items[] = array( |
|
362 'id' => 'term-' . $term->term_id, |
|
363 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
364 'type' => 'taxonomy', |
|
365 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name, |
|
366 'object' => $term->taxonomy, |
|
367 'object_id' => intval( $term->term_id ), |
|
368 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ), |
|
369 ); |
|
370 } |
|
371 } |
|
372 |
|
373 // Add "Home" link if search term matches. Treat as a page, but switch to custom on add. |
|
374 if ( isset( $args['s'] ) ) { |
|
375 $title = _x( 'Home', 'nav menu home label' ); |
|
376 $matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] ); |
|
377 if ( $matches ) { |
|
378 $items[] = array( |
|
379 'id' => 'home', |
|
380 'title' => $title, |
|
381 'type' => 'custom', |
|
382 'type_label' => __( 'Custom Link' ), |
|
383 'object' => '', |
|
384 'url' => home_url(), |
|
385 ); |
|
386 } |
|
387 } |
|
388 |
|
389 /** |
|
390 * Filters the available menu items during a search request. |
|
391 * |
|
392 * @since 4.5.0 |
|
393 * |
|
394 * @param array $items The array of menu items. |
|
395 * @param array $args Includes 'pagenum' and 's' (search) arguments. |
|
396 */ |
|
397 $items = apply_filters( 'customize_nav_menu_searched_items', $items, $args ); |
|
398 |
|
399 return $items; |
|
400 } |
|
401 |
|
402 /** |
|
403 * Enqueue scripts and styles for Customizer pane. |
|
404 * |
|
405 * @since 4.3.0 |
|
406 */ |
|
407 public function enqueue_scripts() { |
|
408 wp_enqueue_style( 'customize-nav-menus' ); |
|
409 wp_enqueue_script( 'customize-nav-menus' ); |
|
410 |
|
411 $temp_nav_menu_setting = new WP_Customize_Nav_Menu_Setting( $this->manager, 'nav_menu[-1]' ); |
|
412 $temp_nav_menu_item_setting = new WP_Customize_Nav_Menu_Item_Setting( $this->manager, 'nav_menu_item[-1]' ); |
|
413 |
|
414 $num_locations = count( get_registered_nav_menus() ); |
|
415 if ( 1 === $num_locations ) { |
|
416 $locations_description = __( 'Your theme can display menus in one location.' ); |
|
417 } else { |
|
418 /* translators: %s: number of menu locations */ |
|
419 $locations_description = sprintf( _n( 'Your theme can display menus in %s location.', 'Your theme can display menus in %s locations.', $num_locations ), number_format_i18n( $num_locations ) ); |
|
420 } |
|
421 |
|
422 // Pass data to JS. |
|
423 $settings = array( |
|
424 'allMenus' => wp_get_nav_menus(), |
|
425 'itemTypes' => $this->available_item_types(), |
|
426 'l10n' => array( |
|
427 'untitled' => _x( '(no label)', 'missing menu item navigation label' ), |
|
428 'unnamed' => _x( '(unnamed)', 'Missing menu name.' ), |
|
429 'custom_label' => __( 'Custom Link' ), |
|
430 'page_label' => get_post_type_object( 'page' )->labels->singular_name, |
|
431 /* translators: %s: menu location */ |
|
432 'menuLocation' => _x( '(Currently set to: %s)', 'menu' ), |
|
433 'locationsTitle' => 1 === $num_locations ? __( 'Menu Location' ) : __( 'Menu Locations' ), |
|
434 'locationsDescription' => $locations_description, |
|
435 'menuNameLabel' => __( 'Menu Name' ), |
|
436 'newMenuNameDescription' => __( 'If your theme has multiple menus, giving them clear names will help you manage them.' ), |
|
437 'itemAdded' => __( 'Menu item added' ), |
|
438 'itemDeleted' => __( 'Menu item deleted' ), |
|
439 'menuAdded' => __( 'Menu created' ), |
|
440 'menuDeleted' => __( 'Menu deleted' ), |
|
441 'movedUp' => __( 'Menu item moved up' ), |
|
442 'movedDown' => __( 'Menu item moved down' ), |
|
443 'movedLeft' => __( 'Menu item moved out of submenu' ), |
|
444 'movedRight' => __( 'Menu item is now a sub-item' ), |
|
445 /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ |
|
446 'customizingMenus' => sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'nav_menus' )->title ) ), |
|
447 /* translators: %s: title of menu item which is invalid */ |
|
448 'invalidTitleTpl' => __( '%s (Invalid)' ), |
|
449 /* translators: %s: title of menu item in draft status */ |
|
450 'pendingTitleTpl' => __( '%s (Pending)' ), |
|
451 'itemsFound' => __( 'Number of items found: %d' ), |
|
452 'itemsFoundMore' => __( 'Additional items found: %d' ), |
|
453 'itemsLoadingMore' => __( 'Loading more results... please wait.' ), |
|
454 'reorderModeOn' => __( 'Reorder mode enabled' ), |
|
455 'reorderModeOff' => __( 'Reorder mode closed' ), |
|
456 'reorderLabelOn' => esc_attr__( 'Reorder menu items' ), |
|
457 'reorderLabelOff' => esc_attr__( 'Close reorder mode' ), |
|
458 ), |
|
459 'settingTransport' => 'postMessage', |
|
460 'phpIntMax' => PHP_INT_MAX, |
|
461 'defaultSettingValues' => array( |
|
462 'nav_menu' => $temp_nav_menu_setting->default, |
|
463 'nav_menu_item' => $temp_nav_menu_item_setting->default, |
|
464 ), |
|
465 'locationSlugMappedToName' => get_registered_nav_menus(), |
|
466 ); |
|
467 |
|
468 $data = sprintf( 'var _wpCustomizeNavMenusSettings = %s;', wp_json_encode( $settings ) ); |
|
469 wp_scripts()->add_data( 'customize-nav-menus', 'data', $data ); |
|
470 |
|
471 // This is copied from nav-menus.php, and it has an unfortunate object name of `menus`. |
|
472 $nav_menus_l10n = array( |
|
473 'oneThemeLocationNoMenus' => null, |
|
474 'moveUp' => __( 'Move up one' ), |
|
475 'moveDown' => __( 'Move down one' ), |
|
476 'moveToTop' => __( 'Move to the top' ), |
|
477 /* translators: %s: previous item name */ |
|
478 'moveUnder' => __( 'Move under %s' ), |
|
479 /* translators: %s: previous item name */ |
|
480 'moveOutFrom' => __( 'Move out from under %s' ), |
|
481 /* translators: %s: previous item name */ |
|
482 'under' => __( 'Under %s' ), |
|
483 /* translators: %s: previous item name */ |
|
484 'outFrom' => __( 'Out from under %s' ), |
|
485 /* translators: 1: item name, 2: item position, 3: total number of items */ |
|
486 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ), |
|
487 /* translators: 1: item name, 2: item position, 3: parent item name */ |
|
488 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ), |
|
489 ); |
|
490 wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n ); |
|
491 } |
|
492 |
|
493 /** |
|
494 * Filters a dynamic setting's constructor args. |
|
495 * |
|
496 * For a dynamic setting to be registered, this filter must be employed |
|
497 * to override the default false value with an array of args to pass to |
|
498 * the WP_Customize_Setting constructor. |
|
499 * |
|
500 * @since 4.3.0 |
|
501 * |
|
502 * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor. |
|
503 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. |
|
504 * @return array|false |
|
505 */ |
|
506 public function filter_dynamic_setting_args( $setting_args, $setting_id ) { |
|
507 if ( preg_match( WP_Customize_Nav_Menu_Setting::ID_PATTERN, $setting_id ) ) { |
|
508 $setting_args = array( |
|
509 'type' => WP_Customize_Nav_Menu_Setting::TYPE, |
|
510 'transport' => 'postMessage', |
|
511 ); |
|
512 } elseif ( preg_match( WP_Customize_Nav_Menu_Item_Setting::ID_PATTERN, $setting_id ) ) { |
|
513 $setting_args = array( |
|
514 'type' => WP_Customize_Nav_Menu_Item_Setting::TYPE, |
|
515 'transport' => 'postMessage', |
|
516 ); |
|
517 } |
|
518 return $setting_args; |
|
519 } |
|
520 |
|
521 /** |
|
522 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass. |
|
523 * |
|
524 * @since 4.3.0 |
|
525 * |
|
526 * @param string $setting_class WP_Customize_Setting or a subclass. |
|
527 * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. |
|
528 * @param array $setting_args WP_Customize_Setting or a subclass. |
|
529 * @return string |
|
530 */ |
|
531 public function filter_dynamic_setting_class( $setting_class, $setting_id, $setting_args ) { |
|
532 unset( $setting_id ); |
|
533 |
|
534 if ( ! empty( $setting_args['type'] ) && WP_Customize_Nav_Menu_Setting::TYPE === $setting_args['type'] ) { |
|
535 $setting_class = 'WP_Customize_Nav_Menu_Setting'; |
|
536 } elseif ( ! empty( $setting_args['type'] ) && WP_Customize_Nav_Menu_Item_Setting::TYPE === $setting_args['type'] ) { |
|
537 $setting_class = 'WP_Customize_Nav_Menu_Item_Setting'; |
|
538 } |
|
539 return $setting_class; |
|
540 } |
|
541 |
|
542 /** |
|
543 * Add the customizer settings and controls. |
|
544 * |
|
545 * @since 4.3.0 |
|
546 */ |
|
547 public function customize_register() { |
|
548 $changeset = $this->manager->unsanitized_post_values(); |
|
549 |
|
550 // Preview settings for nav menus early so that the sections and controls will be added properly. |
|
551 $nav_menus_setting_ids = array(); |
|
552 foreach ( array_keys( $changeset ) as $setting_id ) { |
|
553 if ( preg_match( '/^(nav_menu_locations|nav_menu|nav_menu_item)\[/', $setting_id ) ) { |
|
554 $nav_menus_setting_ids[] = $setting_id; |
|
555 } |
|
556 } |
|
557 $settings = $this->manager->add_dynamic_settings( $nav_menus_setting_ids ); |
|
558 if ( $this->manager->settings_previewed() ) { |
|
559 foreach ( $settings as $setting ) { |
|
560 $setting->preview(); |
|
561 } |
|
562 } |
|
563 |
|
564 // Require JS-rendered control types. |
|
565 $this->manager->register_panel_type( 'WP_Customize_Nav_Menus_Panel' ); |
|
566 $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Control' ); |
|
567 $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Name_Control' ); |
|
568 $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Locations_Control' ); |
|
569 $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Auto_Add_Control' ); |
|
570 $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Item_Control' ); |
|
571 |
|
572 // Create a panel for Menus. |
|
573 $description = '<p>' . __( 'This panel is used for managing navigation menus for content you have already published on your site. You can create menus and add items for existing content such as pages, posts, categories, tags, formats, or custom links.' ) . '</p>'; |
|
574 if ( current_theme_supports( 'widgets' ) ) { |
|
575 /* translators: URL to the widgets panel of the customizer */ |
|
576 $description .= '<p>' . sprintf( __( 'Menus can be displayed in locations defined by your theme or in <a href="%s">widget areas</a> by adding a “Navigation Menu” widget.' ), "javascript:wp.customize.panel( 'widgets' ).focus();" ) . '</p>'; |
|
577 } else { |
|
578 $description .= '<p>' . __( 'Menus can be displayed in locations defined by your theme.' ) . '</p>'; |
|
579 } |
|
580 $this->manager->add_panel( new WP_Customize_Nav_Menus_Panel( $this->manager, 'nav_menus', array( |
|
581 'title' => __( 'Menus' ), |
|
582 'description' => $description, |
|
583 'priority' => 100, |
|
584 // 'theme_supports' => 'menus|widgets', @todo allow multiple theme supports |
|
585 ) ) ); |
|
586 $menus = wp_get_nav_menus(); |
|
587 |
|
588 // Menu locations. |
|
589 $locations = get_registered_nav_menus(); |
|
590 $num_locations = count( $locations ); |
|
591 if ( 1 == $num_locations ) { |
|
592 $description = '<p>' . __( 'Your theme can display menus in one location. Select which menu you would like to use.' ) . '</p>'; |
|
593 } else { |
|
594 /* translators: %s: number of menu locations */ |
|
595 $description = '<p>' . sprintf( _n( 'Your theme can display menus in %s location. Select which menu you would like to use.', 'Your theme can display menus in %s locations. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . '</p>'; |
|
596 } |
|
597 |
|
598 if ( current_theme_supports( 'widgets' ) ) { |
|
599 /* translators: URL to the widgets panel of the customizer */ |
|
600 $description .= '<p>' . sprintf( __( 'If your theme has widget areas, you can also add menus there. Visit the <a href="%s">Widgets panel</a> and add a “Navigation Menu widget” to display a menu in a sidebar or footer.' ), "javascript:wp.customize.panel( 'widgets' ).focus();" ) . '</p>'; |
|
601 } |
|
602 |
|
603 $this->manager->add_section( 'menu_locations', array( |
|
604 'title' => 1 === $num_locations ? _x( 'View Location', 'menu locations' ) : _x( 'View All Locations', 'menu locations' ), |
|
605 'panel' => 'nav_menus', |
|
606 'priority' => 30, |
|
607 'description' => $description, |
|
608 ) ); |
|
609 |
|
610 $choices = array( '0' => __( '— Select —' ) ); |
|
611 foreach ( $menus as $menu ) { |
|
612 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' ); |
|
613 } |
|
614 |
|
615 // Attempt to re-map the nav menu location assignments when previewing a theme switch. |
|
616 $mapped_nav_menu_locations = array(); |
|
617 if ( ! $this->manager->is_theme_active() ) { |
|
618 $theme_mods = get_option( 'theme_mods_' . $this->manager->get_stylesheet(), array() ); |
|
619 |
|
620 // If there is no data from a previous activation, start fresh. |
|
621 if ( empty( $theme_mods['nav_menu_locations'] ) ) { |
|
622 $theme_mods['nav_menu_locations'] = array(); |
|
623 } |
|
624 |
|
625 $mapped_nav_menu_locations = wp_map_nav_menu_locations( $theme_mods['nav_menu_locations'], $this->original_nav_menu_locations ); |
|
626 } |
|
627 |
|
628 foreach ( $locations as $location => $description ) { |
|
629 $setting_id = "nav_menu_locations[{$location}]"; |
|
630 |
|
631 $setting = $this->manager->get_setting( $setting_id ); |
|
632 if ( $setting ) { |
|
633 $setting->transport = 'postMessage'; |
|
634 remove_filter( "customize_sanitize_{$setting_id}", 'absint' ); |
|
635 add_filter( "customize_sanitize_{$setting_id}", array( $this, 'intval_base10' ) ); |
|
636 } else { |
|
637 $this->manager->add_setting( $setting_id, array( |
|
638 'sanitize_callback' => array( $this, 'intval_base10' ), |
|
639 'theme_supports' => 'menus', |
|
640 'type' => 'theme_mod', |
|
641 'transport' => 'postMessage', |
|
642 'default' => 0, |
|
643 ) ); |
|
644 } |
|
645 |
|
646 // Override the assigned nav menu location if mapped during previewed theme switch. |
|
647 if ( empty( $changeset[ $setting_id ] ) && isset( $mapped_nav_menu_locations[ $location ] ) ) { |
|
648 $this->manager->set_post_value( $setting_id, $mapped_nav_menu_locations[ $location ] ); |
|
649 } |
|
650 |
|
651 $this->manager->add_control( new WP_Customize_Nav_Menu_Location_Control( $this->manager, $setting_id, array( |
|
652 'label' => $description, |
|
653 'location_id' => $location, |
|
654 'section' => 'menu_locations', |
|
655 'choices' => $choices, |
|
656 ) ) ); |
|
657 } |
|
658 |
|
659 // Register each menu as a Customizer section, and add each menu item to each menu. |
|
660 foreach ( $menus as $menu ) { |
|
661 $menu_id = $menu->term_id; |
|
662 |
|
663 // Create a section for each menu. |
|
664 $section_id = 'nav_menu[' . $menu_id . ']'; |
|
665 $this->manager->add_section( new WP_Customize_Nav_Menu_Section( $this->manager, $section_id, array( |
|
666 'title' => html_entity_decode( $menu->name, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
667 'priority' => 10, |
|
668 'panel' => 'nav_menus', |
|
669 ) ) ); |
|
670 |
|
671 $nav_menu_setting_id = 'nav_menu[' . $menu_id . ']'; |
|
672 $this->manager->add_setting( new WP_Customize_Nav_Menu_Setting( $this->manager, $nav_menu_setting_id, array( |
|
673 'transport' => 'postMessage', |
|
674 ) ) ); |
|
675 |
|
676 // Add the menu contents. |
|
677 $menu_items = (array) wp_get_nav_menu_items( $menu_id ); |
|
678 |
|
679 foreach ( array_values( $menu_items ) as $i => $item ) { |
|
680 |
|
681 // Create a setting for each menu item (which doesn't actually manage data, currently). |
|
682 $menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']'; |
|
683 |
|
684 $value = (array) $item; |
|
685 if ( empty( $value['post_title'] ) ) { |
|
686 $value['title'] = ''; |
|
687 } |
|
688 |
|
689 $value['nav_menu_term_id'] = $menu_id; |
|
690 $this->manager->add_setting( new WP_Customize_Nav_Menu_Item_Setting( $this->manager, $menu_item_setting_id, array( |
|
691 'value' => $value, |
|
692 'transport' => 'postMessage', |
|
693 ) ) ); |
|
694 |
|
695 // Create a control for each menu item. |
|
696 $this->manager->add_control( new WP_Customize_Nav_Menu_Item_Control( $this->manager, $menu_item_setting_id, array( |
|
697 'label' => $item->title, |
|
698 'section' => $section_id, |
|
699 'priority' => 10 + $i, |
|
700 ) ) ); |
|
701 } |
|
702 |
|
703 // Note: other controls inside of this section get added dynamically in JS via the MenuSection.ready() function. |
|
704 } |
|
705 |
|
706 // Add the add-new-menu section and controls. |
|
707 $this->manager->add_section( 'add_menu', array( |
|
708 'type' => 'new_menu', |
|
709 'title' => __( 'New Menu' ), |
|
710 'panel' => 'nav_menus', |
|
711 'priority' => 20, |
|
712 ) ); |
|
713 |
|
714 $this->manager->add_setting( new WP_Customize_Filter_Setting( $this->manager, 'nav_menus_created_posts', array( |
|
715 'transport' => 'postMessage', |
|
716 'type' => 'option', // To prevent theme prefix in changeset. |
|
717 'default' => array(), |
|
718 'sanitize_callback' => array( $this, 'sanitize_nav_menus_created_posts' ), |
|
719 ) ) ); |
|
720 } |
|
721 |
|
722 /** |
|
723 * Get the base10 intval. |
|
724 * |
|
725 * This is used as a setting's sanitize_callback; we can't use just plain |
|
726 * intval because the second argument is not what intval() expects. |
|
727 * |
|
728 * @since 4.3.0 |
|
729 * |
|
730 * @param mixed $value Number to convert. |
|
731 * @return int Integer. |
|
732 */ |
|
733 public function intval_base10( $value ) { |
|
734 return intval( $value, 10 ); |
|
735 } |
|
736 |
|
737 /** |
|
738 * Return an array of all the available item types. |
|
739 * |
|
740 * @since 4.3.0 |
|
741 * @since 4.7.0 Each array item now includes a `$type_label` in addition to `$title`, `$type`, and `$object`. |
|
742 * |
|
743 * @return array The available menu item types. |
|
744 */ |
|
745 public function available_item_types() { |
|
746 $item_types = array(); |
|
747 |
|
748 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); |
|
749 if ( $post_types ) { |
|
750 foreach ( $post_types as $slug => $post_type ) { |
|
751 $item_types[] = array( |
|
752 'title' => $post_type->labels->name, |
|
753 'type_label' => $post_type->labels->singular_name, |
|
754 'type' => 'post_type', |
|
755 'object' => $post_type->name, |
|
756 ); |
|
757 } |
|
758 } |
|
759 |
|
760 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); |
|
761 if ( $taxonomies ) { |
|
762 foreach ( $taxonomies as $slug => $taxonomy ) { |
|
763 if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) { |
|
764 continue; |
|
765 } |
|
766 $item_types[] = array( |
|
767 'title' => $taxonomy->labels->name, |
|
768 'type_label' => $taxonomy->labels->singular_name, |
|
769 'type' => 'taxonomy', |
|
770 'object' => $taxonomy->name, |
|
771 ); |
|
772 } |
|
773 } |
|
774 |
|
775 /** |
|
776 * Filters the available menu item types. |
|
777 * |
|
778 * @since 4.3.0 |
|
779 * @since 4.7.0 Each array item now includes a `$type_label` in addition to `$title`, `$type`, and `$object`. |
|
780 * |
|
781 * @param array $item_types Navigation menu item types. |
|
782 */ |
|
783 $item_types = apply_filters( 'customize_nav_menu_available_item_types', $item_types ); |
|
784 |
|
785 return $item_types; |
|
786 } |
|
787 |
|
788 /** |
|
789 * Add a new `auto-draft` post. |
|
790 * |
|
791 * @since 4.7.0 |
|
792 * |
|
793 * @param array $postarr { |
|
794 * Post array. Note that post_status is overridden to be `auto-draft`. |
|
795 * |
|
796 * @var string $post_title Post title. Required. |
|
797 * @var string $post_type Post type. Required. |
|
798 * @var string $post_name Post name. |
|
799 * @var string $post_content Post content. |
|
800 * } |
|
801 * @return WP_Post|WP_Error Inserted auto-draft post object or error. |
|
802 */ |
|
803 public function insert_auto_draft_post( $postarr ) { |
|
804 if ( ! isset( $postarr['post_type'] ) ) { |
|
805 return new WP_Error( 'unknown_post_type', __( 'Invalid post type.' ) ); |
|
806 } |
|
807 if ( empty( $postarr['post_title'] ) ) { |
|
808 return new WP_Error( 'empty_title', __( 'Empty title' ) ); |
|
809 } |
|
810 if ( ! empty( $postarr['post_status'] ) ) { |
|
811 return new WP_Error( 'status_forbidden', __( 'Status is forbidden' ) ); |
|
812 } |
|
813 |
|
814 /* |
|
815 * If the changeset is a draft, this will change to draft the next time the changeset |
|
816 * is updated; otherwise, auto-draft will persist in autosave revisions, until save. |
|
817 */ |
|
818 $postarr['post_status'] = 'auto-draft'; |
|
819 |
|
820 // Auto-drafts are allowed to have empty post_names, so it has to be explicitly set. |
|
821 if ( empty( $postarr['post_name'] ) ) { |
|
822 $postarr['post_name'] = sanitize_title( $postarr['post_title'] ); |
|
823 } |
|
824 if ( ! isset( $postarr['meta_input'] ) ) { |
|
825 $postarr['meta_input'] = array(); |
|
826 } |
|
827 $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name']; |
|
828 $postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid(); |
|
829 unset( $postarr['post_name'] ); |
|
830 |
|
831 add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
|
832 $r = wp_insert_post( wp_slash( $postarr ), true ); |
|
833 remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
|
834 |
|
835 if ( is_wp_error( $r ) ) { |
|
836 return $r; |
|
837 } else { |
|
838 return get_post( $r ); |
|
839 } |
|
840 } |
|
841 |
|
842 /** |
|
843 * Ajax handler for adding a new auto-draft post. |
|
844 * |
|
845 * @since 4.7.0 |
|
846 */ |
|
847 public function ajax_insert_auto_draft_post() { |
|
848 if ( ! check_ajax_referer( 'customize-menus', 'customize-menus-nonce', false ) ) { |
|
849 wp_send_json_error( 'bad_nonce', 400 ); |
|
850 } |
|
851 |
|
852 if ( ! current_user_can( 'customize' ) ) { |
|
853 wp_send_json_error( 'customize_not_allowed', 403 ); |
|
854 } |
|
855 |
|
856 if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { |
|
857 wp_send_json_error( 'missing_params', 400 ); |
|
858 } |
|
859 |
|
860 $params = wp_unslash( $_POST['params'] ); |
|
861 $illegal_params = array_diff( array_keys( $params ), array( 'post_type', 'post_title' ) ); |
|
862 if ( ! empty( $illegal_params ) ) { |
|
863 wp_send_json_error( 'illegal_params', 400 ); |
|
864 } |
|
865 |
|
866 $params = array_merge( |
|
867 array( |
|
868 'post_type' => '', |
|
869 'post_title' => '', |
|
870 ), |
|
871 $params |
|
872 ); |
|
873 |
|
874 if ( empty( $params['post_type'] ) || ! post_type_exists( $params['post_type'] ) ) { |
|
875 status_header( 400 ); |
|
876 wp_send_json_error( 'missing_post_type_param' ); |
|
877 } |
|
878 |
|
879 $post_type_object = get_post_type_object( $params['post_type'] ); |
|
880 if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
|
881 status_header( 403 ); |
|
882 wp_send_json_error( 'insufficient_post_permissions' ); |
|
883 } |
|
884 |
|
885 $params['post_title'] = trim( $params['post_title'] ); |
|
886 if ( '' === $params['post_title'] ) { |
|
887 status_header( 400 ); |
|
888 wp_send_json_error( 'missing_post_title' ); |
|
889 } |
|
890 |
|
891 $r = $this->insert_auto_draft_post( $params ); |
|
892 if ( is_wp_error( $r ) ) { |
|
893 $error = $r; |
|
894 if ( ! empty( $post_type_object->labels->singular_name ) ) { |
|
895 $singular_name = $post_type_object->labels->singular_name; |
|
896 } else { |
|
897 $singular_name = __( 'Post' ); |
|
898 } |
|
899 |
|
900 $data = array( |
|
901 /* translators: %1$s is the post type name and %2$s is the error message. */ |
|
902 'message' => sprintf( __( '%1$s could not be created: %2$s' ), $singular_name, $error->get_error_message() ), |
|
903 ); |
|
904 wp_send_json_error( $data ); |
|
905 } else { |
|
906 $post = $r; |
|
907 $data = array( |
|
908 'post_id' => $post->ID, |
|
909 'url' => get_permalink( $post->ID ), |
|
910 ); |
|
911 wp_send_json_success( $data ); |
|
912 } |
|
913 } |
|
914 |
|
915 /** |
|
916 * Print the JavaScript templates used to render Menu Customizer components. |
|
917 * |
|
918 * Templates are imported into the JS use wp.template. |
|
919 * |
|
920 * @since 4.3.0 |
|
921 */ |
|
922 public function print_templates() { |
|
923 ?> |
|
924 <script type="text/html" id="tmpl-available-menu-item"> |
|
925 <li id="menu-item-tpl-{{ data.id }}" class="menu-item-tpl" data-menu-item-id="{{ data.id }}"> |
|
926 <div class="menu-item-bar"> |
|
927 <div class="menu-item-handle"> |
|
928 <span class="item-type" aria-hidden="true">{{ data.type_label }}</span> |
|
929 <span class="item-title" aria-hidden="true"> |
|
930 <span class="menu-item-title<# if ( ! data.title ) { #> no-title<# } #>">{{ data.title || wp.customize.Menus.data.l10n.untitled }}</span> |
|
931 </span> |
|
932 <button type="button" class="button-link item-add"> |
|
933 <span class="screen-reader-text"><?php |
|
934 /* translators: 1: Title of a menu item, 2: Type of a menu item */ |
|
935 printf( __( 'Add to menu: %1$s (%2$s)' ), '{{ data.title || wp.customize.Menus.data.l10n.untitled }}', '{{ data.type_label }}' ); |
|
936 ?></span> |
|
937 </button> |
|
938 </div> |
|
939 </div> |
|
940 </li> |
|
941 </script> |
|
942 |
|
943 <script type="text/html" id="tmpl-menu-item-reorder-nav"> |
|
944 <div class="menu-item-reorder-nav"> |
|
945 <?php |
|
946 printf( |
|
947 '<button type="button" class="menus-move-up">%1$s</button><button type="button" class="menus-move-down">%2$s</button><button type="button" class="menus-move-left">%3$s</button><button type="button" class="menus-move-right">%4$s</button>', |
|
948 __( 'Move up' ), |
|
949 __( 'Move down' ), |
|
950 __( 'Move one level up' ), |
|
951 __( 'Move one level down' ) |
|
952 ); |
|
953 ?> |
|
954 </div> |
|
955 </script> |
|
956 |
|
957 <script type="text/html" id="tmpl-nav-menu-delete-button"> |
|
958 <div class="menu-delete-item"> |
|
959 <button type="button" class="button-link button-link-delete"> |
|
960 <?php _e( 'Delete Menu' ); ?> |
|
961 </button> |
|
962 </div> |
|
963 </script> |
|
964 |
|
965 <script type="text/html" id="tmpl-nav-menu-submit-new-button"> |
|
966 <p id="customize-new-menu-submit-description"><?php _e( 'Click “Next” to start adding links to your new menu.' ); ?></p> |
|
967 <button id="customize-new-menu-submit" type="button" class="button" aria-describedby="customize-new-menu-submit-description"><?php _e( 'Next' ); ?></button> |
|
968 </script> |
|
969 |
|
970 <script type="text/html" id="tmpl-nav-menu-locations-header"> |
|
971 <span class="customize-control-title customize-section-title-menu_locations-heading">{{ data.l10n.locationsTitle }}</span> |
|
972 <p class="customize-control-description customize-section-title-menu_locations-description">{{ data.l10n.locationsDescription }}</p> |
|
973 </script> |
|
974 |
|
975 <script type="text/html" id="tmpl-nav-menu-create-menu-section-title"> |
|
976 <p class="add-new-menu-notice"> |
|
977 <?php _e( 'It doesn’t look like your site has any menus yet. Want to build one? Click the button to start.' ); ?> |
|
978 </p> |
|
979 <p class="add-new-menu-notice"> |
|
980 <?php _e( 'You’ll create a menu, assign it a location, and add menu items like links to pages and categories. If your theme has multiple menu areas, you might need to create more than one.' ); ?> |
|
981 </p> |
|
982 <h3> |
|
983 <button type="button" class="button customize-add-menu-button"> |
|
984 <?php _e( 'Create New Menu' ); ?> |
|
985 </button> |
|
986 </h3> |
|
987 </script> |
|
988 <?php |
|
989 } |
|
990 |
|
991 /** |
|
992 * Print the html template used to render the add-menu-item frame. |
|
993 * |
|
994 * @since 4.3.0 |
|
995 */ |
|
996 public function available_items_template() { |
|
997 ?> |
|
998 <div id="available-menu-items" class="accordion-container"> |
|
999 <div class="customize-section-title"> |
|
1000 <button type="button" class="customize-section-back" tabindex="-1"> |
|
1001 <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> |
|
1002 </button> |
|
1003 <h3> |
|
1004 <span class="customize-action"> |
|
1005 <?php |
|
1006 /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ |
|
1007 printf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'nav_menus' )->title ) ); |
|
1008 ?> |
|
1009 </span> |
|
1010 <?php _e( 'Add Menu Items' ); ?> |
|
1011 </h3> |
|
1012 </div> |
|
1013 <div id="available-menu-items-search" class="accordion-section cannot-expand"> |
|
1014 <div class="accordion-section-title"> |
|
1015 <label class="screen-reader-text" for="menu-items-search"><?php _e( 'Search Menu Items' ); ?></label> |
|
1016 <input type="text" id="menu-items-search" placeholder="<?php esc_attr_e( 'Search menu items…' ) ?>" aria-describedby="menu-items-search-desc" /> |
|
1017 <p class="screen-reader-text" id="menu-items-search-desc"><?php _e( 'The search results will be updated as you type.' ); ?></p> |
|
1018 <span class="spinner"></span> |
|
1019 </div> |
|
1020 <div class="search-icon" aria-hidden="true"></div> |
|
1021 <button type="button" class="clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results' ); ?></span></button> |
|
1022 <ul class="accordion-section-content available-menu-items-list" data-type="search"></ul> |
|
1023 </div> |
|
1024 <?php |
|
1025 |
|
1026 // Ensure the page post type comes first in the list. |
|
1027 $item_types = $this->available_item_types(); |
|
1028 $page_item_type = null; |
|
1029 foreach ( $item_types as $i => $item_type ) { |
|
1030 if ( isset( $item_type['object'] ) && 'page' === $item_type['object'] ) { |
|
1031 $page_item_type = $item_type; |
|
1032 unset( $item_types[ $i ] ); |
|
1033 } |
|
1034 } |
|
1035 |
|
1036 $this->print_custom_links_available_menu_item(); |
|
1037 if ( $page_item_type ) { |
|
1038 $this->print_post_type_container( $page_item_type ); |
|
1039 } |
|
1040 // Containers for per-post-type item browsing; items are added with JS. |
|
1041 foreach ( $item_types as $item_type ) { |
|
1042 $this->print_post_type_container( $item_type ); |
|
1043 } |
|
1044 ?> |
|
1045 </div><!-- #available-menu-items --> |
|
1046 <?php |
|
1047 } |
|
1048 |
|
1049 /** |
|
1050 * Print the markup for new menu items. |
|
1051 * |
|
1052 * To be used in the template #available-menu-items. |
|
1053 * |
|
1054 * @since 4.7.0 |
|
1055 * |
|
1056 * @param array $available_item_type Menu item data to output, including title, type, and label. |
|
1057 * @return void |
|
1058 */ |
|
1059 protected function print_post_type_container( $available_item_type ) { |
|
1060 $id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] ); |
|
1061 ?> |
|
1062 <div id="<?php echo esc_attr( $id ); ?>" class="accordion-section"> |
|
1063 <h4 class="accordion-section-title" role="presentation"> |
|
1064 <?php echo esc_html( $available_item_type['title'] ); ?> |
|
1065 <span class="spinner"></span> |
|
1066 <span class="no-items"><?php _e( 'No items' ); ?></span> |
|
1067 <button type="button" class="button-link" aria-expanded="false"> |
|
1068 <span class="screen-reader-text"><?php |
|
1069 /* translators: %s: Title of a section with menu items */ |
|
1070 printf( __( 'Toggle section: %s' ), esc_html( $available_item_type['title'] ) ); ?></span> |
|
1071 <span class="toggle-indicator" aria-hidden="true"></span> |
|
1072 </button> |
|
1073 </h4> |
|
1074 <div class="accordion-section-content"> |
|
1075 <?php if ( 'post_type' === $available_item_type['type'] ) : ?> |
|
1076 <?php $post_type_obj = get_post_type_object( $available_item_type['object'] ); ?> |
|
1077 <?php if ( current_user_can( $post_type_obj->cap->create_posts ) && current_user_can( $post_type_obj->cap->publish_posts ) ) : ?> |
|
1078 <div class="new-content-item"> |
|
1079 <label for="<?php echo esc_attr( 'create-item-input-' . $available_item_type['object'] ); ?>" class="screen-reader-text"><?php echo esc_html( $post_type_obj->labels->add_new_item ); ?></label> |
|
1080 <input type="text" id="<?php echo esc_attr( 'create-item-input-' . $available_item_type['object'] ); ?>" class="create-item-input" placeholder="<?php echo esc_attr( $post_type_obj->labels->add_new_item ); ?>"> |
|
1081 <button type="button" class="button add-content"><?php _e( 'Add' ); ?></button> |
|
1082 </div> |
|
1083 <?php endif; ?> |
|
1084 <?php endif; ?> |
|
1085 <ul class="available-menu-items-list" data-type="<?php echo esc_attr( $available_item_type['type'] ); ?>" data-object="<?php echo esc_attr( $available_item_type['object'] ); ?>" data-type_label="<?php echo esc_attr( isset( $available_item_type['type_label'] ) ? $available_item_type['type_label'] : $available_item_type['type'] ); ?>"></ul> |
|
1086 </div> |
|
1087 </div> |
|
1088 <?php |
|
1089 } |
|
1090 |
|
1091 /** |
|
1092 * Print the markup for available menu item custom links. |
|
1093 * |
|
1094 * @since 4.7.0 |
|
1095 * |
|
1096 * @return void |
|
1097 */ |
|
1098 protected function print_custom_links_available_menu_item() { |
|
1099 ?> |
|
1100 <div id="new-custom-menu-item" class="accordion-section"> |
|
1101 <h4 class="accordion-section-title" role="presentation"> |
|
1102 <?php _e( 'Custom Links' ); ?> |
|
1103 <button type="button" class="button-link" aria-expanded="false"> |
|
1104 <span class="screen-reader-text"><?php _e( 'Toggle section: Custom Links' ); ?></span> |
|
1105 <span class="toggle-indicator" aria-hidden="true"></span> |
|
1106 </button> |
|
1107 </h4> |
|
1108 <div class="accordion-section-content customlinkdiv"> |
|
1109 <input type="hidden" value="custom" id="custom-menu-item-type" name="menu-item[-1][menu-item-type]" /> |
|
1110 <p id="menu-item-url-wrap" class="wp-clearfix"> |
|
1111 <label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label> |
|
1112 <input id="custom-menu-item-url" name="menu-item[-1][menu-item-url]" type="text" class="code menu-item-textbox" value="http://"> |
|
1113 </p> |
|
1114 <p id="menu-item-name-wrap" class="wp-clearfix"> |
|
1115 <label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label> |
|
1116 <input id="custom-menu-item-name" name="menu-item[-1][menu-item-title]" type="text" class="regular-text menu-item-textbox"> |
|
1117 </p> |
|
1118 <p class="button-controls"> |
|
1119 <span class="add-to-menu"> |
|
1120 <input type="submit" class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="custom-menu-item-submit"> |
|
1121 <span class="spinner"></span> |
|
1122 </span> |
|
1123 </p> |
|
1124 </div> |
|
1125 </div> |
|
1126 <?php |
|
1127 } |
|
1128 |
|
1129 // |
|
1130 // Start functionality specific to partial-refresh of menu changes in Customizer preview. |
|
1131 // |
|
1132 |
|
1133 /** |
|
1134 * Nav menu args used for each instance, keyed by the args HMAC. |
|
1135 * |
|
1136 * @since 4.3.0 |
|
1137 * @var array |
|
1138 */ |
|
1139 public $preview_nav_menu_instance_args = array(); |
|
1140 |
|
1141 /** |
|
1142 * Filters arguments for dynamic nav_menu selective refresh partials. |
|
1143 * |
|
1144 * @since 4.5.0 |
|
1145 * |
|
1146 * @param array|false $partial_args Partial args. |
|
1147 * @param string $partial_id Partial ID. |
|
1148 * @return array Partial args. |
|
1149 */ |
|
1150 public function customize_dynamic_partial_args( $partial_args, $partial_id ) { |
|
1151 |
|
1152 if ( preg_match( '/^nav_menu_instance\[[0-9a-f]{32}\]$/', $partial_id ) ) { |
|
1153 if ( false === $partial_args ) { |
|
1154 $partial_args = array(); |
|
1155 } |
|
1156 $partial_args = array_merge( |
|
1157 $partial_args, |
|
1158 array( |
|
1159 'type' => 'nav_menu_instance', |
|
1160 'render_callback' => array( $this, 'render_nav_menu_partial' ), |
|
1161 'container_inclusive' => true, |
|
1162 'settings' => array(), // Empty because the nav menu instance may relate to a menu or a location. |
|
1163 'capability' => 'edit_theme_options', |
|
1164 ) |
|
1165 ); |
|
1166 } |
|
1167 |
|
1168 return $partial_args; |
|
1169 } |
|
1170 |
|
1171 /** |
|
1172 * Add hooks for the Customizer preview. |
|
1173 * |
|
1174 * @since 4.3.0 |
|
1175 */ |
|
1176 public function customize_preview_init() { |
|
1177 add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) ); |
|
1178 add_filter( 'wp_nav_menu_args', array( $this, 'filter_wp_nav_menu_args' ), 1000 ); |
|
1179 add_filter( 'wp_nav_menu', array( $this, 'filter_wp_nav_menu' ), 10, 2 ); |
|
1180 add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 ); |
|
1181 add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) ); |
|
1182 } |
|
1183 |
|
1184 /** |
|
1185 * Make the auto-draft status protected so that it can be queried. |
|
1186 * |
|
1187 * @since 4.7.0 |
|
1188 * |
|
1189 * @global array $wp_post_statuses List of post statuses. |
|
1190 */ |
|
1191 public function make_auto_draft_status_previewable() { |
|
1192 global $wp_post_statuses; |
|
1193 $wp_post_statuses['auto-draft']->protected = true; |
|
1194 } |
|
1195 |
|
1196 /** |
|
1197 * Sanitize post IDs for posts created for nav menu items to be published. |
|
1198 * |
|
1199 * @since 4.7.0 |
|
1200 * |
|
1201 * @param array $value Post IDs. |
|
1202 * @returns array Post IDs. |
|
1203 */ |
|
1204 public function sanitize_nav_menus_created_posts( $value ) { |
|
1205 $post_ids = array(); |
|
1206 foreach ( wp_parse_id_list( $value ) as $post_id ) { |
|
1207 if ( empty( $post_id ) ) { |
|
1208 continue; |
|
1209 } |
|
1210 $post = get_post( $post_id ); |
|
1211 if ( 'auto-draft' !== $post->post_status && 'draft' !== $post->post_status ) { |
|
1212 continue; |
|
1213 } |
|
1214 $post_type_obj = get_post_type_object( $post->post_type ); |
|
1215 if ( ! $post_type_obj ) { |
|
1216 continue; |
|
1217 } |
|
1218 if ( ! current_user_can( $post_type_obj->cap->publish_posts ) || ! current_user_can( $post_type_obj->cap->edit_post, $post_id ) ) { |
|
1219 continue; |
|
1220 } |
|
1221 $post_ids[] = $post->ID; |
|
1222 } |
|
1223 return $post_ids; |
|
1224 } |
|
1225 |
|
1226 /** |
|
1227 * Publish the auto-draft posts that were created for nav menu items. |
|
1228 * |
|
1229 * The post IDs will have been sanitized by already by |
|
1230 * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to |
|
1231 * remove any post IDs for which the user cannot publish or for which the |
|
1232 * post is not an auto-draft. |
|
1233 * |
|
1234 * @since 4.7.0 |
|
1235 * |
|
1236 * @param WP_Customize_Setting $setting Customizer setting object. |
|
1237 */ |
|
1238 public function save_nav_menus_created_posts( $setting ) { |
|
1239 $post_ids = $setting->post_value(); |
|
1240 if ( ! empty( $post_ids ) ) { |
|
1241 foreach ( $post_ids as $post_id ) { |
|
1242 |
|
1243 // Prevent overriding the status that a user may have prematurely updated the post to. |
|
1244 $current_status = get_post_status( $post_id ); |
|
1245 if ( 'auto-draft' !== $current_status && 'draft' !== $current_status ) { |
|
1246 continue; |
|
1247 } |
|
1248 |
|
1249 $target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish'; |
|
1250 $args = array( |
|
1251 'ID' => $post_id, |
|
1252 'post_status' => $target_status, |
|
1253 ); |
|
1254 $post_name = get_post_meta( $post_id, '_customize_draft_post_name', true ); |
|
1255 if ( $post_name ) { |
|
1256 $args['post_name'] = $post_name; |
|
1257 } |
|
1258 |
|
1259 // Note that wp_publish_post() cannot be used because unique slugs need to be assigned. |
|
1260 wp_update_post( wp_slash( $args ) ); |
|
1261 |
|
1262 delete_post_meta( $post_id, '_customize_draft_post_name' ); |
|
1263 } |
|
1264 } |
|
1265 } |
|
1266 |
|
1267 /** |
|
1268 * Keep track of the arguments that are being passed to wp_nav_menu(). |
|
1269 * |
|
1270 * @since 4.3.0 |
|
1271 * @see wp_nav_menu() |
|
1272 * @see WP_Customize_Widgets_Partial_Refresh::filter_dynamic_sidebar_params() |
|
1273 * |
|
1274 * @param array $args An array containing wp_nav_menu() arguments. |
|
1275 * @return array Arguments. |
|
1276 */ |
|
1277 public function filter_wp_nav_menu_args( $args ) { |
|
1278 /* |
|
1279 * The following conditions determine whether or not this instance of |
|
1280 * wp_nav_menu() can use selective refreshed. A wp_nav_menu() can be |
|
1281 * selective refreshed if... |
|
1282 */ |
|
1283 $can_partial_refresh = ( |
|
1284 // ...if wp_nav_menu() is directly echoing out the menu (and thus isn't manipulating the string after generated), |
|
1285 ! empty( $args['echo'] ) |
|
1286 && |
|
1287 // ...and if the fallback_cb can be serialized to JSON, since it will be included in the placement context data, |
|
1288 ( empty( $args['fallback_cb'] ) || is_string( $args['fallback_cb'] ) ) |
|
1289 && |
|
1290 // ...and if the walker can also be serialized to JSON, since it will be included in the placement context data as well, |
|
1291 ( empty( $args['walker'] ) || is_string( $args['walker'] ) ) |
|
1292 // ...and if it has a theme location assigned or an assigned menu to display, |
|
1293 && ( |
|
1294 ! empty( $args['theme_location'] ) |
|
1295 || |
|
1296 ( ! empty( $args['menu'] ) && ( is_numeric( $args['menu'] ) || is_object( $args['menu'] ) ) ) |
|
1297 ) |
|
1298 && |
|
1299 // ...and if the nav menu would be rendered with a wrapper container element (upon which to attach data-* attributes). |
|
1300 ( |
|
1301 ! empty( $args['container'] ) |
|
1302 || |
|
1303 ( isset( $args['items_wrap'] ) && '<' === substr( $args['items_wrap'], 0, 1 ) ) |
|
1304 ) |
|
1305 ); |
|
1306 $args['can_partial_refresh'] = $can_partial_refresh; |
|
1307 |
|
1308 $exported_args = $args; |
|
1309 |
|
1310 // Empty out args which may not be JSON-serializable. |
|
1311 if ( ! $can_partial_refresh ) { |
|
1312 $exported_args['fallback_cb'] = ''; |
|
1313 $exported_args['walker'] = ''; |
|
1314 } |
|
1315 |
|
1316 /* |
|
1317 * Replace object menu arg with a term_id menu arg, as this exports better |
|
1318 * to JS and is easier to compare hashes. |
|
1319 */ |
|
1320 if ( ! empty( $exported_args['menu'] ) && is_object( $exported_args['menu'] ) ) { |
|
1321 $exported_args['menu'] = $exported_args['menu']->term_id; |
|
1322 } |
|
1323 |
|
1324 ksort( $exported_args ); |
|
1325 $exported_args['args_hmac'] = $this->hash_nav_menu_args( $exported_args ); |
|
1326 |
|
1327 $args['customize_preview_nav_menus_args'] = $exported_args; |
|
1328 $this->preview_nav_menu_instance_args[ $exported_args['args_hmac'] ] = $exported_args; |
|
1329 return $args; |
|
1330 } |
|
1331 |
|
1332 /** |
|
1333 * Prepares wp_nav_menu() calls for partial refresh. |
|
1334 * |
|
1335 * Injects attributes into container element. |
|
1336 * |
|
1337 * @since 4.3.0 |
|
1338 * |
|
1339 * @see wp_nav_menu() |
|
1340 * |
|
1341 * @param string $nav_menu_content The HTML content for the navigation menu. |
|
1342 * @param object $args An object containing wp_nav_menu() arguments. |
|
1343 * @return string Nav menu HTML with selective refresh attributes added if partial can be refreshed. |
|
1344 */ |
|
1345 public function filter_wp_nav_menu( $nav_menu_content, $args ) { |
|
1346 if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) { |
|
1347 $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) ); |
|
1348 $attributes .= ' data-customize-partial-type="nav_menu_instance"'; |
|
1349 $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) ); |
|
1350 $nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . str_replace( '\\', '\\\\', $attributes ), $nav_menu_content, 1 ); |
|
1351 } |
|
1352 return $nav_menu_content; |
|
1353 } |
|
1354 |
|
1355 /** |
|
1356 * Hashes (hmac) the nav menu arguments to ensure they are not tampered with when |
|
1357 * submitted in the Ajax request. |
|
1358 * |
|
1359 * Note that the array is expected to be pre-sorted. |
|
1360 * |
|
1361 * @since 4.3.0 |
|
1362 * |
|
1363 * @param array $args The arguments to hash. |
|
1364 * @return string Hashed nav menu arguments. |
|
1365 */ |
|
1366 public function hash_nav_menu_args( $args ) { |
|
1367 return wp_hash( serialize( $args ) ); |
|
1368 } |
|
1369 |
|
1370 /** |
|
1371 * Enqueue scripts for the Customizer preview. |
|
1372 * |
|
1373 * @since 4.3.0 |
|
1374 */ |
|
1375 public function customize_preview_enqueue_deps() { |
|
1376 wp_enqueue_script( 'customize-preview-nav-menus' ); // Note that we have overridden this. |
|
1377 } |
|
1378 |
|
1379 /** |
|
1380 * Exports data from PHP to JS. |
|
1381 * |
|
1382 * @since 4.3.0 |
|
1383 */ |
|
1384 public function export_preview_data() { |
|
1385 |
|
1386 // Why not wp_localize_script? Because we're not localizing, and it forces values into strings. |
|
1387 $exports = array( |
|
1388 'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args, |
|
1389 ); |
|
1390 printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) ); |
|
1391 } |
|
1392 |
|
1393 /** |
|
1394 * Export any wp_nav_menu() calls during the rendering of any partials. |
|
1395 * |
|
1396 * @since 4.5.0 |
|
1397 * |
|
1398 * @param array $response Response. |
|
1399 * @return array Response. |
|
1400 */ |
|
1401 public function export_partial_rendered_nav_menu_instances( $response ) { |
|
1402 $response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args; |
|
1403 return $response; |
|
1404 } |
|
1405 |
|
1406 /** |
|
1407 * Render a specific menu via wp_nav_menu() using the supplied arguments. |
|
1408 * |
|
1409 * @since 4.3.0 |
|
1410 * |
|
1411 * @see wp_nav_menu() |
|
1412 * |
|
1413 * @param WP_Customize_Partial $partial Partial. |
|
1414 * @param array $nav_menu_args Nav menu args supplied as container context. |
|
1415 * @return string|false |
|
1416 */ |
|
1417 public function render_nav_menu_partial( $partial, $nav_menu_args ) { |
|
1418 unset( $partial ); |
|
1419 |
|
1420 if ( ! isset( $nav_menu_args['args_hmac'] ) ) { |
|
1421 // Error: missing_args_hmac. |
|
1422 return false; |
|
1423 } |
|
1424 |
|
1425 $nav_menu_args_hmac = $nav_menu_args['args_hmac']; |
|
1426 unset( $nav_menu_args['args_hmac'] ); |
|
1427 |
|
1428 ksort( $nav_menu_args ); |
|
1429 if ( ! hash_equals( $this->hash_nav_menu_args( $nav_menu_args ), $nav_menu_args_hmac ) ) { |
|
1430 // Error: args_hmac_mismatch. |
|
1431 return false; |
|
1432 } |
|
1433 |
|
1434 ob_start(); |
|
1435 wp_nav_menu( $nav_menu_args ); |
|
1436 $content = ob_get_clean(); |
|
1437 |
|
1438 return $content; |
|
1439 } |
|
1440 } |