WP_Customize_Nav_Menus

Customize Nav Menus class.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-customize-nav-menus.php  
  1. final class WP_Customize_Nav_Menus { 
  2.  
  3. /** 
  4. * WP_Customize_Manager instance. 
  5. * @since 4.3.0 
  6. * @access public 
  7. * @var WP_Customize_Manager 
  8. */ 
  9. public $manager; 
  10.  
  11. /** 
  12. * Previewed Menus. 
  13. * @since 4.3.0 
  14. * @access public 
  15. * @var array 
  16. */ 
  17. public $previewed_menus; 
  18.  
  19. /** 
  20. * Constructor. 
  21. * @since 4.3.0 
  22. * @access public 
  23. * @param object $manager An instance of the WP_Customize_Manager class. 
  24. */ 
  25. public function __construct( $manager ) { 
  26. $this->previewed_menus = array(); 
  27. $this->manager = $manager; 
  28.  
  29. // Skip useless hooks when the user can't manage nav menus anyway. 
  30. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  31. return; 
  32.  
  33. add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) ); 
  34. add_action( 'wp_ajax_load-available-menu-items-customizer', array( $this, 'ajax_load_available_items' ) ); 
  35. add_action( 'wp_ajax_search-available-menu-items-customizer', array( $this, 'ajax_search_available_items' ) ); 
  36. add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  37.  
  38. // Needs to run after core Navigation section is set up. 
  39. add_action( 'customize_register', array( $this, 'customize_register' ), 11 ); 
  40.  
  41. add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_dynamic_setting_args' ), 10, 2 ); 
  42. add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_dynamic_setting_class' ), 10, 3 ); 
  43. add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_templates' ) ); 
  44. add_action( 'customize_controls_print_footer_scripts', array( $this, 'available_items_template' ) ); 
  45. add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); 
  46.  
  47. // Selective Refresh partials. 
  48. add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); 
  49.  
  50. /** 
  51. * Adds a nonce for customizing menus. 
  52. * @since 4.5.0 
  53. * @access public 
  54. * @param array $nonces Array of nonces. 
  55. * @return array $nonces Modified array of nonces. 
  56. */ 
  57. public function filter_nonces( $nonces ) { 
  58. $nonces['customize-menus'] = wp_create_nonce( 'customize-menus' ); 
  59. return $nonces; 
  60.  
  61. /** 
  62. * Ajax handler for loading available menu items. 
  63. * @since 4.3.0 
  64. * @access public 
  65. */ 
  66. public function ajax_load_available_items() { 
  67. check_ajax_referer( 'customize-menus', 'customize-menus-nonce' ); 
  68.  
  69. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  70. wp_die( -1 ); 
  71.  
  72. if ( empty( $_POST['type'] ) || empty( $_POST['object'] ) ) { 
  73. wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' ); 
  74.  
  75. $type = sanitize_key( $_POST['type'] ); 
  76. $object = sanitize_key( $_POST['object'] ); 
  77. $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ); 
  78. $items = $this->load_available_items_query( $type, $object, $page ); 
  79.  
  80. if ( is_wp_error( $items ) ) { 
  81. wp_send_json_error( $items->get_error_code() ); 
  82. } else { 
  83. wp_send_json_success( array( 'items' => $items ) ); 
  84.  
  85. /** 
  86. * Performs the post_type and taxonomy queries for loading available menu items. 
  87. * @since 4.3.0 
  88. * @access public 
  89. * @param string $type Optional. Accepts any custom object type and has built-in support for 
  90. * 'post_type' and 'taxonomy'. Default is 'post_type'. 
  91. * @param string $object Optional. Accepts any registered taxonomy or post type name. Default is 'page'. 
  92. * @param int $page Optional. The page number used to generate the query offset. Default is '0'. 
  93. * @return WP_Error|array Returns either a WP_Error object or an array of menu items. 
  94. */ 
  95. public function load_available_items_query( $type = 'post_type', $object = 'page', $page = 0 ) { 
  96. $items = array(); 
  97.  
  98. if ( 'post_type' === $type ) { 
  99. $post_type = get_post_type_object( $object ); 
  100. if ( ! $post_type ) { 
  101. return new WP_Error( 'nav_menus_invalid_post_type' ); 
  102.  
  103. if ( 0 === $page && 'page' === $object ) { 
  104. // Add "Home" link. Treat as a page, but switch to custom on add. 
  105. $items[] = array( 
  106. 'id' => 'home',  
  107. 'title' => _x( 'Home', 'nav menu home label' ),  
  108. 'type' => 'custom',  
  109. 'type_label' => __( 'Custom Link' ),  
  110. 'object' => '',  
  111. 'url' => home_url(),  
  112. ); 
  113. } elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) { 
  114. // Add a post type archive link. 
  115. $items[] = array( 
  116. 'id' => $object . '-archive',  
  117. 'title' => $post_type->labels->archives,  
  118. 'type' => 'post_type_archive',  
  119. 'type_label' => __( 'Post Type Archive' ),  
  120. 'object' => $object,  
  121. 'url' => get_post_type_archive_link( $object ),  
  122. ); 
  123.  
  124. $posts = get_posts( array( 
  125. 'numberposts' => 10,  
  126. 'offset' => 10 * $page,  
  127. 'orderby' => 'date',  
  128. 'order' => 'DESC',  
  129. 'post_type' => $object,  
  130. ) ); 
  131. foreach ( $posts as $post ) { 
  132. $post_title = $post->post_title; 
  133. if ( '' === $post_title ) { 
  134. /** translators: %d: ID of a post */ 
  135. $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); 
  136. $items[] = array( 
  137. 'id' => "post-{$post->ID}",  
  138. 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  139. 'type' => 'post_type',  
  140. 'type_label' => get_post_type_object( $post->post_type )->labels->singular_name,  
  141. 'object' => $post->post_type,  
  142. 'object_id' => intval( $post->ID ),  
  143. 'url' => get_permalink( intval( $post->ID ) ),  
  144. ); 
  145. } elseif ( 'taxonomy' === $type ) { 
  146. $terms = get_terms( $object, array( 
  147. 'child_of' => 0,  
  148. 'exclude' => '',  
  149. 'hide_empty' => false,  
  150. 'hierarchical' => 1,  
  151. 'include' => '',  
  152. 'number' => 10,  
  153. 'offset' => 10 * $page,  
  154. 'order' => 'DESC',  
  155. 'orderby' => 'count',  
  156. 'pad_counts' => false,  
  157. ) ); 
  158. if ( is_wp_error( $terms ) ) { 
  159. return $terms; 
  160.  
  161. foreach ( $terms as $term ) { 
  162. $items[] = array( 
  163. 'id' => "term-{$term->term_id}",  
  164. 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  165. 'type' => 'taxonomy',  
  166. 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,  
  167. 'object' => $term->taxonomy,  
  168. 'object_id' => intval( $term->term_id ),  
  169. 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ),  
  170. ); 
  171.  
  172. /** 
  173. * Filters the available menu items. 
  174. * @since 4.3.0 
  175. * @param array $items The array of menu items. 
  176. * @param string $type The object type. 
  177. * @param string $object The object name. 
  178. * @param int $page The current page number. 
  179. */ 
  180. $items = apply_filters( 'customize_nav_menu_available_items', $items, $type, $object, $page ); 
  181.  
  182. return $items; 
  183.  
  184. /** 
  185. * Ajax handler for searching available menu items. 
  186. * @since 4.3.0 
  187. * @access public 
  188. */ 
  189. public function ajax_search_available_items() { 
  190. check_ajax_referer( 'customize-menus', 'customize-menus-nonce' ); 
  191.  
  192. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  193. wp_die( -1 ); 
  194.  
  195. if ( empty( $_POST['search'] ) ) { 
  196. wp_send_json_error( 'nav_menus_missing_search_parameter' ); 
  197.  
  198. $p = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0; 
  199. if ( $p < 1 ) { 
  200. $p = 1; 
  201.  
  202. $s = sanitize_text_field( wp_unslash( $_POST['search'] ) ); 
  203. $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) ); 
  204.  
  205. if ( empty( $items ) ) { 
  206. wp_send_json_error( array( 'message' => __( 'No results found.' ) ) ); 
  207. } else { 
  208. wp_send_json_success( array( 'items' => $items ) ); 
  209.  
  210. /** 
  211. * Performs post queries for available-item searching. 
  212. * Based on WP_Editor::wp_link_query(). 
  213. * @since 4.3.0 
  214. * @access public 
  215. * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments. 
  216. * @return array Menu items. 
  217. */ 
  218. public function search_available_items_query( $args = array() ) { 
  219. $items = array(); 
  220.  
  221. $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 
  222. $query = array( 
  223. 'post_type' => array_keys( $post_type_objects ),  
  224. 'suppress_filters' => true,  
  225. 'update_post_term_cache' => false,  
  226. 'update_post_meta_cache' => false,  
  227. 'post_status' => 'publish',  
  228. 'posts_per_page' => 20,  
  229. ); 
  230.  
  231. $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1; 
  232. $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0; 
  233.  
  234. if ( isset( $args['s'] ) ) { 
  235. $query['s'] = $args['s']; 
  236.  
  237. // Query posts. 
  238. $get_posts = new WP_Query( $query ); 
  239.  
  240. // Check if any posts were found. 
  241. if ( $get_posts->post_count ) { 
  242. foreach ( $get_posts->posts as $post ) { 
  243. $post_title = $post->post_title; 
  244. if ( '' === $post_title ) { 
  245. /** translators: %d: ID of a post */ 
  246. $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); 
  247. $items[] = array( 
  248. 'id' => 'post-' . $post->ID,  
  249. 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  250. 'type' => 'post_type',  
  251. 'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,  
  252. 'object' => $post->post_type,  
  253. 'object_id' => intval( $post->ID ),  
  254. 'url' => get_permalink( intval( $post->ID ) ),  
  255. ); 
  256.  
  257. // Query taxonomy terms. 
  258. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' ); 
  259. $terms = get_terms( $taxonomies, array( 
  260. 'name__like' => $args['s'],  
  261. 'number' => 20,  
  262. 'offset' => 20 * ($args['pagenum'] - 1),  
  263. ) ); 
  264.  
  265. // Check if any taxonomies were found. 
  266. if ( ! empty( $terms ) ) { 
  267. foreach ( $terms as $term ) { 
  268. $items[] = array( 
  269. 'id' => 'term-' . $term->term_id,  
  270. 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  271. 'type' => 'taxonomy',  
  272. 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,  
  273. 'object' => $term->taxonomy,  
  274. 'object_id' => intval( $term->term_id ),  
  275. 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ),  
  276. ); 
  277.  
  278. /** 
  279. * Filters the available menu items during a search request. 
  280. * @since 4.5.0 
  281. * @param array $items The array of menu items. 
  282. * @param array $args Includes 'pagenum' and 's' (search) arguments. 
  283. */ 
  284. $items = apply_filters( 'customize_nav_menu_searched_items', $items, $args ); 
  285.  
  286. return $items; 
  287.  
  288. /** 
  289. * Enqueue scripts and styles for Customizer pane. 
  290. * @since 4.3.0 
  291. * @access public 
  292. */ 
  293. public function enqueue_scripts() { 
  294. wp_enqueue_style( 'customize-nav-menus' ); 
  295. wp_enqueue_script( 'customize-nav-menus' ); 
  296.  
  297. $temp_nav_menu_setting = new WP_Customize_Nav_Menu_Setting( $this->manager, 'nav_menu[-1]' ); 
  298. $temp_nav_menu_item_setting = new WP_Customize_Nav_Menu_Item_Setting( $this->manager, 'nav_menu_item[-1]' ); 
  299.  
  300. // Pass data to JS. 
  301. $settings = array( 
  302. 'allMenus' => wp_get_nav_menus(),  
  303. 'itemTypes' => $this->available_item_types(),  
  304. 'l10n' => array( 
  305. 'untitled' => _x( '(no label)', 'missing menu item navigation label' ),  
  306. 'unnamed' => _x( '(unnamed)', 'Missing menu name.' ),  
  307. 'custom_label' => __( 'Custom Link' ),  
  308. /** translators: %s: menu location */ 
  309. 'menuLocation' => _x( '(Currently set to: %s)', 'menu' ),  
  310. 'menuNameLabel' => __( 'Menu Name' ),  
  311. 'itemAdded' => __( 'Menu item added' ),  
  312. 'itemDeleted' => __( 'Menu item deleted' ),  
  313. 'menuAdded' => __( 'Menu created' ),  
  314. 'menuDeleted' => __( 'Menu deleted' ),  
  315. 'movedUp' => __( 'Menu item moved up' ),  
  316. 'movedDown' => __( 'Menu item moved down' ),  
  317. 'movedLeft' => __( 'Menu item moved out of submenu' ),  
  318. 'movedRight' => __( 'Menu item is now a sub-item' ),  
  319. /** translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ 
  320. 'customizingMenus' => sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'nav_menus' )->title ) ),  
  321. /** translators: %s: title of menu item which is invalid */ 
  322. 'invalidTitleTpl' => __( '%s (Invalid)' ),  
  323. /** translators: %s: title of menu item in draft status */ 
  324. 'pendingTitleTpl' => __( '%s (Pending)' ),  
  325. 'itemsFound' => __( 'Number of items found: %d' ),  
  326. 'itemsFoundMore' => __( 'Additional items found: %d' ),  
  327. 'itemsLoadingMore' => __( 'Loading more results... please wait.' ),  
  328. 'reorderModeOn' => __( 'Reorder mode enabled' ),  
  329. 'reorderModeOff' => __( 'Reorder mode closed' ),  
  330. 'reorderLabelOn' => esc_attr__( 'Reorder menu items' ),  
  331. 'reorderLabelOff' => esc_attr__( 'Close reorder mode' ),  
  332. ),  
  333. 'settingTransport' => 'postMessage',  
  334. 'phpIntMax' => PHP_INT_MAX,  
  335. 'defaultSettingValues' => array( 
  336. 'nav_menu' => $temp_nav_menu_setting->default,  
  337. 'nav_menu_item' => $temp_nav_menu_item_setting->default,  
  338. ),  
  339. 'locationSlugMappedToName' => get_registered_nav_menus(),  
  340. ); 
  341.  
  342. $data = sprintf( 'var _wpCustomizeNavMenusSettings = %s;', wp_json_encode( $settings ) ); 
  343. wp_scripts()->add_data( 'customize-nav-menus', 'data', $data ); 
  344.  
  345. // This is copied from nav-menus.php, and it has an unfortunate object name of `menus`. 
  346. $nav_menus_l10n = array( 
  347. 'oneThemeLocationNoMenus' => null,  
  348. 'moveUp' => __( 'Move up one' ),  
  349. 'moveDown' => __( 'Move down one' ),  
  350. 'moveToTop' => __( 'Move to the top' ),  
  351. /** translators: %s: previous item name */ 
  352. 'moveUnder' => __( 'Move under %s' ),  
  353. /** translators: %s: previous item name */ 
  354. 'moveOutFrom' => __( 'Move out from under %s' ),  
  355. /** translators: %s: previous item name */ 
  356. 'under' => __( 'Under %s' ),  
  357. /** translators: %s: previous item name */ 
  358. 'outFrom' => __( 'Out from under %s' ),  
  359. /** translators: 1: item name, 2: item position, 3: total number of items */ 
  360. 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),  
  361. /** translators: 1: item name, 2: item position, 3: parent item name */ 
  362. 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),  
  363. ); 
  364. wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n ); 
  365.  
  366. /** 
  367. * Filters a dynamic setting's constructor args. 
  368. * For a dynamic setting to be registered, this filter must be employed 
  369. * to override the default false value with an array of args to pass to 
  370. * the WP_Customize_Setting constructor. 
  371. * @since 4.3.0 
  372. * @access public 
  373. * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor. 
  374. * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. 
  375. * @return array|false 
  376. */ 
  377. public function filter_dynamic_setting_args( $setting_args, $setting_id ) { 
  378. if ( preg_match( WP_Customize_Nav_Menu_Setting::ID_PATTERN, $setting_id ) ) { 
  379. $setting_args = array( 
  380. 'type' => WP_Customize_Nav_Menu_Setting::TYPE,  
  381. 'transport' => 'postMessage',  
  382. ); 
  383. } elseif ( preg_match( WP_Customize_Nav_Menu_Item_Setting::ID_PATTERN, $setting_id ) ) { 
  384. $setting_args = array( 
  385. 'type' => WP_Customize_Nav_Menu_Item_Setting::TYPE,  
  386. 'transport' => 'postMessage',  
  387. ); 
  388. return $setting_args; 
  389.  
  390. /** 
  391. * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass. 
  392. * @since 4.3.0 
  393. * @access public 
  394. * @param string $setting_class WP_Customize_Setting or a subclass. 
  395. * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. 
  396. * @param array $setting_args WP_Customize_Setting or a subclass. 
  397. * @return string 
  398. */ 
  399. public function filter_dynamic_setting_class( $setting_class, $setting_id, $setting_args ) { 
  400. unset( $setting_id ); 
  401.  
  402. if ( ! empty( $setting_args['type'] ) && WP_Customize_Nav_Menu_Setting::TYPE === $setting_args['type'] ) { 
  403. $setting_class = 'WP_Customize_Nav_Menu_Setting'; 
  404. } elseif ( ! empty( $setting_args['type'] ) && WP_Customize_Nav_Menu_Item_Setting::TYPE === $setting_args['type'] ) { 
  405. $setting_class = 'WP_Customize_Nav_Menu_Item_Setting'; 
  406. return $setting_class; 
  407.  
  408. /** 
  409. * Add the customizer settings and controls. 
  410. * @since 4.3.0 
  411. * @access public 
  412. */ 
  413. public function customize_register() { 
  414.  
  415. // Require JS-rendered control types. 
  416. $this->manager->register_panel_type( 'WP_Customize_Nav_Menus_Panel' ); 
  417. $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Control' ); 
  418. $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Name_Control' ); 
  419. $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Auto_Add_Control' ); 
  420. $this->manager->register_control_type( 'WP_Customize_Nav_Menu_Item_Control' ); 
  421.  
  422. // Create a panel for Menus. 
  423. $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>'; 
  424. if ( current_theme_supports( 'widgets' ) ) { 
  425. /** translators: URL to the widgets panel of the customizer */ 
  426. $description .= '<p>' . sprintf( __( 'Menus can be displayed in locations defined by your theme or in <a href="%s">widget areas</a> by adding a “Custom Menu” widget.' ), "javascript:wp.customize.panel( 'widgets' ).focus();" ) . '</p>'; 
  427. } else { 
  428. $description .= '<p>' . __( 'Menus can be displayed in locations defined by your theme.' ) . '</p>'; 
  429. $this->manager->add_panel( new WP_Customize_Nav_Menus_Panel( $this->manager, 'nav_menus', array( 
  430. 'title' => __( 'Menus' ),  
  431. 'description' => $description,  
  432. 'priority' => 100,  
  433. // 'theme_supports' => 'menus|widgets', @todo allow multiple theme supports 
  434. ) ) ); 
  435. $menus = wp_get_nav_menus(); 
  436.  
  437. // Menu locations. 
  438. $locations = get_registered_nav_menus(); 
  439. $num_locations = count( array_keys( $locations ) ); 
  440. if ( 1 == $num_locations ) { 
  441. $description = '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>'; 
  442. } else { 
  443. /** translators: %s: number of menu locations */ 
  444. $description = '<p>' . sprintf( _n( 'Your theme supports %s menu. Select which menu appears in each location.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . '</p>'; 
  445. if ( current_theme_supports( 'widgets' ) ) { 
  446. /** translators: URL to the widgets panel of the customizer */ 
  447. $description .= '<p>' . sprintf( __( 'You can also place menus in <a href="%s">widget areas</a> with the “Custom Menu” widget.' ), "javascript:wp.customize.panel( 'widgets' ).focus();" ) . '</p>'; 
  448.  
  449. $this->manager->add_section( 'menu_locations', array( 
  450. 'title' => __( 'Menu Locations' ),  
  451. 'panel' => 'nav_menus',  
  452. 'priority' => 5,  
  453. 'description' => $description,  
  454. ) ); 
  455.  
  456. $choices = array( '0' => __( '— Select —' ) ); 
  457. foreach ( $menus as $menu ) { 
  458. $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '…' ); 
  459.  
  460. foreach ( $locations as $location => $description ) { 
  461. $setting_id = "nav_menu_locations[{$location}]"; 
  462.  
  463. $setting = $this->manager->get_setting( $setting_id ); 
  464. if ( $setting ) { 
  465. $setting->transport = 'postMessage'; 
  466. remove_filter( "customize_sanitize_{$setting_id}", 'absint' ); 
  467. add_filter( "customize_sanitize_{$setting_id}", array( $this, 'intval_base10' ) ); 
  468. } else { 
  469. $this->manager->add_setting( $setting_id, array( 
  470. 'sanitize_callback' => array( $this, 'intval_base10' ),  
  471. 'theme_supports' => 'menus',  
  472. 'type' => 'theme_mod',  
  473. 'transport' => 'postMessage',  
  474. 'default' => 0,  
  475. ) ); 
  476.  
  477. $this->manager->add_control( new WP_Customize_Nav_Menu_Location_Control( $this->manager, $setting_id, array( 
  478. 'label' => $description,  
  479. 'location_id' => $location,  
  480. 'section' => 'menu_locations',  
  481. 'choices' => $choices,  
  482. ) ) ); 
  483.  
  484. // Register each menu as a Customizer section, and add each menu item to each menu. 
  485. foreach ( $menus as $menu ) { 
  486. $menu_id = $menu->term_id; 
  487.  
  488. // Create a section for each menu. 
  489. $section_id = 'nav_menu[' . $menu_id . ']'; 
  490. $this->manager->add_section( new WP_Customize_Nav_Menu_Section( $this->manager, $section_id, array( 
  491. 'title' => html_entity_decode( $menu->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),  
  492. 'priority' => 10,  
  493. 'panel' => 'nav_menus',  
  494. ) ) ); 
  495.  
  496. $nav_menu_setting_id = 'nav_menu[' . $menu_id . ']'; 
  497. $this->manager->add_setting( new WP_Customize_Nav_Menu_Setting( $this->manager, $nav_menu_setting_id, array( 
  498. 'transport' => 'postMessage',  
  499. ) ) ); 
  500.  
  501. // Add the menu contents. 
  502. $menu_items = (array) wp_get_nav_menu_items( $menu_id ); 
  503.  
  504. foreach ( array_values( $menu_items ) as $i => $item ) { 
  505.  
  506. // Create a setting for each menu item (which doesn't actually manage data, currently). 
  507. $menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']'; 
  508.  
  509. $value = (array) $item; 
  510. $value['nav_menu_term_id'] = $menu_id; 
  511. $this->manager->add_setting( new WP_Customize_Nav_Menu_Item_Setting( $this->manager, $menu_item_setting_id, array( 
  512. 'value' => $value,  
  513. 'transport' => 'postMessage',  
  514. ) ) ); 
  515.  
  516. // Create a control for each menu item. 
  517. $this->manager->add_control( new WP_Customize_Nav_Menu_Item_Control( $this->manager, $menu_item_setting_id, array( 
  518. 'label' => $item->title,  
  519. 'section' => $section_id,  
  520. 'priority' => 10 + $i,  
  521. ) ) ); 
  522.  
  523. // Note: other controls inside of this section get added dynamically in JS via the MenuSection.ready() function. 
  524.  
  525. // Add the add-new-menu section and controls. 
  526. $this->manager->add_section( new WP_Customize_New_Menu_Section( $this->manager, 'add_menu', array( 
  527. 'title' => __( 'Add a Menu' ),  
  528. 'panel' => 'nav_menus',  
  529. 'priority' => 999,  
  530. ) ) ); 
  531.  
  532. $this->manager->add_control( 'new_menu_name', array( 
  533. 'label' => '',  
  534. 'section' => 'add_menu',  
  535. 'type' => 'text',  
  536. 'settings' => array(),  
  537. 'input_attrs' => array( 
  538. 'class' => 'menu-name-field',  
  539. 'placeholder' => __( 'New menu name' ),  
  540. ),  
  541. ) ); 
  542.  
  543. $this->manager->add_control( new WP_Customize_New_Menu_Control( $this->manager, 'create_new_menu', array( 
  544. 'section' => 'add_menu',  
  545. 'settings' => array(),  
  546. ) ) ); 
  547.  
  548. /** 
  549. * Get the base10 intval. 
  550. * This is used as a setting's sanitize_callback; we can't use just plain 
  551. * intval because the second argument is not what intval() expects. 
  552. * @since 4.3.0 
  553. * @access public 
  554. * @param mixed $value Number to convert. 
  555. * @return int Integer. 
  556. */ 
  557. public function intval_base10( $value ) { 
  558. return intval( $value, 10 ); 
  559.  
  560. /** 
  561. * Return an array of all the available item types. 
  562. * @since 4.3.0 
  563. * @access public 
  564. * @return array The available menu item types. 
  565. */ 
  566. public function available_item_types() { 
  567. $item_types = array(); 
  568.  
  569. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 
  570. if ( $post_types ) { 
  571. foreach ( $post_types as $slug => $post_type ) { 
  572. $item_types[] = array( 
  573. 'title' => $post_type->labels->name,  
  574. 'type' => 'post_type',  
  575. 'object' => $post_type->name,  
  576. ); 
  577.  
  578. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); 
  579. if ( $taxonomies ) { 
  580. foreach ( $taxonomies as $slug => $taxonomy ) { 
  581. if ( 'post_format' === $taxonomy && ! current_theme_supports( 'post-formats' ) ) { 
  582. continue; 
  583. $item_types[] = array( 
  584. 'title' => $taxonomy->labels->name,  
  585. 'type' => 'taxonomy',  
  586. 'object' => $taxonomy->name,  
  587. ); 
  588.  
  589. /** 
  590. * Filters the available menu item types. 
  591. * @since 4.3.0 
  592. * @param array $item_types Custom menu item types. 
  593. */ 
  594. $item_types = apply_filters( 'customize_nav_menu_available_item_types', $item_types ); 
  595.  
  596. return $item_types; 
  597.  
  598. /** 
  599. * Print the JavaScript templates used to render Menu Customizer components. 
  600. * Templates are imported into the JS use wp.template. 
  601. * @since 4.3.0 
  602. * @access public 
  603. */ 
  604. public function print_templates() { 
  605. ?> 
  606. <script type="text/html" id="tmpl-available-menu-item"> 
  607. <li id="menu-item-tpl-{{ data.id }}" class="menu-item-tpl" data-menu-item-id="{{ data.id }}"> 
  608. <div class="menu-item-bar"> 
  609. <div class="menu-item-handle"> 
  610. <span class="item-type" aria-hidden="true">{{ data.type_label }}</span> 
  611. <span class="item-title" aria-hidden="true"> 
  612. <span class="menu-item-title<# if ( ! data.title ) { #> no-title<# } #>">{{ data.title || wp.customize.Menus.data.l10n.untitled }}</span> 
  613. </span> 
  614. <button type="button" class="button-link item-add"> 
  615. <span class="screen-reader-text"><?php 
  616. /** translators: 1: Title of a menu item, 2: Type of a menu item */ 
  617. printf( __( 'Add to menu: %1$s (%2$s)' ), '{{ data.title || wp.customize.Menus.data.l10n.untitled }}', '{{ data.type_label }}' ); 
  618. ?></span> 
  619. </button> 
  620. </div> 
  621. </div> 
  622. </li> 
  623. </script> 
  624.  
  625. <script type="text/html" id="tmpl-menu-item-reorder-nav"> 
  626. <div class="menu-item-reorder-nav"> 
  627. <?php 
  628. printf( 
  629. '<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>',  
  630. __( 'Move up' ),  
  631. __( 'Move down' ),  
  632. __( 'Move one level up' ),  
  633. __( 'Move one level down' ) 
  634. ); 
  635. ?> 
  636. </div> 
  637. </script> 
  638. <?php 
  639.  
  640. /** 
  641. * Print the html template used to render the add-menu-item frame. 
  642. * @since 4.3.0 
  643. * @access public 
  644. */ 
  645. public function available_items_template() { 
  646. ?> 
  647. <div id="available-menu-items" class="accordion-container"> 
  648. <div class="customize-section-title"> 
  649. <button type="button" class="customize-section-back" tabindex="-1"> 
  650. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> 
  651. </button> 
  652. <h3> 
  653. <span class="customize-action"> 
  654. <?php 
  655. /** translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ 
  656. printf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'nav_menus' )->title ) ); 
  657. ?> 
  658. </span> 
  659. <?php _e( 'Add Menu Items' ); ?> 
  660. </h3> 
  661. </div> 
  662. <div id="available-menu-items-search" class="accordion-section cannot-expand"> 
  663. <div class="accordion-section-title"> 
  664. <label class="screen-reader-text" for="menu-items-search"><?php _e( 'Search Menu Items' ); ?></label> 
  665. <input type="text" id="menu-items-search" placeholder="<?php esc_attr_e( 'Search menu items…' ) ?>" aria-describedby="menu-items-search-desc" /> 
  666. <p class="screen-reader-text" id="menu-items-search-desc"><?php _e( 'The search results will be updated as you type.' ); ?></p> 
  667. <span class="spinner"></span> 
  668. </div> 
  669. <button type="button" class="clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results' ); ?></span></button> 
  670. <ul class="accordion-section-content" data-type="search"></ul> 
  671. </div> 
  672. <div id="new-custom-menu-item" class="accordion-section"> 
  673. <h4 class="accordion-section-title" role="presentation"> 
  674. <?php _e( 'Custom Links' ); ?> 
  675. <button type="button" class="button-link" aria-expanded="false"> 
  676. <span class="screen-reader-text"><?php _e( 'Toggle section: Custom Links' ); ?></span> 
  677. <span class="toggle-indicator" aria-hidden="true"></span> 
  678. </button> 
  679. </h4> 
  680. <div class="accordion-section-content customlinkdiv"> 
  681. <input type="hidden" value="custom" id="custom-menu-item-type" name="menu-item[-1][menu-item-type]" /> 
  682. <p id="menu-item-url-wrap" class="wp-clearfix"> 
  683. <label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label> 
  684. <input id="custom-menu-item-url" name="menu-item[-1][menu-item-url]" type="text" class="code menu-item-textbox" value="http://"> 
  685. </p> 
  686. <p id="menu-item-name-wrap" class="wp-clearfix"> 
  687. <label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label> 
  688. <input id="custom-menu-item-name" name="menu-item[-1][menu-item-title]" type="text" class="regular-text menu-item-textbox"> 
  689. </p> 
  690. <p class="button-controls"> 
  691. <span class="add-to-menu"> 
  692. <input type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="custom-menu-item-submit"> 
  693. <span class="spinner"></span> 
  694. </span> 
  695. </p> 
  696. </div> 
  697. </div> 
  698. <?php 
  699. // Containers for per-post-type item browsing; items added with JS. 
  700. foreach ( $this->available_item_types() as $available_item_type ) { 
  701. $id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] ); 
  702. ?> 
  703. <div id="<?php echo esc_attr( $id ); ?>" class="accordion-section"> 
  704. <h4 class="accordion-section-title" role="presentation"> 
  705. <?php echo esc_html( $available_item_type['title'] ); ?> 
  706. <span class="spinner"></span> 
  707. <span class="no-items"><?php _e( 'No items' ); ?></span> 
  708. <button type="button" class="button-link" aria-expanded="false"> 
  709. <span class="screen-reader-text"><?php 
  710. /** translators: %s: Title of a section with menu items */ 
  711. printf( __( 'Toggle section: %s' ), esc_html( $available_item_type['title'] ) ); ?></span> 
  712. <span class="toggle-indicator" aria-hidden="true"></span> 
  713. </button> 
  714. </h4> 
  715. <ul class="accordion-section-content" data-type="<?php echo esc_attr( $available_item_type['type'] ); ?>" data-object="<?php echo esc_attr( $available_item_type['object'] ); ?>"></ul> 
  716. </div> 
  717. <?php 
  718. ?> 
  719. </div><!-- #available-menu-items --> 
  720. <?php 
  721.  
  722. // 
  723. // Start functionality specific to partial-refresh of menu changes in Customizer preview. 
  724. // 
  725.   
  726. /** 
  727. * Nav menu args used for each instance, keyed by the args HMAC. 
  728. * @since 4.3.0 
  729. * @access public 
  730. * @var array 
  731. */ 
  732. public $preview_nav_menu_instance_args = array(); 
  733.  
  734. /** 
  735. * Filters arguments for dynamic nav_menu selective refresh partials. 
  736. * @since 4.5.0 
  737. * @access public 
  738. * @param array|false $partial_args Partial args. 
  739. * @param string $partial_id Partial ID. 
  740. * @return array Partial args. 
  741. */ 
  742. public function customize_dynamic_partial_args( $partial_args, $partial_id ) { 
  743.  
  744. if ( preg_match( '/^nav_menu_instance\[[0-9a-f]{32}\]$/', $partial_id ) ) { 
  745. if ( false === $partial_args ) { 
  746. $partial_args = array(); 
  747. $partial_args = array_merge( 
  748. $partial_args,  
  749. array( 
  750. 'type' => 'nav_menu_instance',  
  751. 'render_callback' => array( $this, 'render_nav_menu_partial' ),  
  752. 'container_inclusive' => true,  
  753. 'settings' => array(), // Empty because the nav menu instance may relate to a menu or a location. 
  754. 'capability' => 'edit_theme_options',  
  755. ); 
  756.  
  757. return $partial_args; 
  758.  
  759. /** 
  760. * Add hooks for the Customizer preview. 
  761. * @since 4.3.0 
  762. * @access public 
  763. */ 
  764. public function customize_preview_init() { 
  765. add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) ); 
  766. add_filter( 'wp_nav_menu_args', array( $this, 'filter_wp_nav_menu_args' ), 1000 ); 
  767. add_filter( 'wp_nav_menu', array( $this, 'filter_wp_nav_menu' ), 10, 2 ); 
  768. add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 ); 
  769. add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) ); 
  770.  
  771. /** 
  772. * Keep track of the arguments that are being passed to wp_nav_menu(). 
  773. * @since 4.3.0 
  774. * @access public 
  775. * @see wp_nav_menu() 
  776. * @see WP_Customize_Widgets_Partial_Refresh::filter_dynamic_sidebar_params() 
  777. * @param array $args An array containing wp_nav_menu() arguments. 
  778. * @return array Arguments. 
  779. */ 
  780. public function filter_wp_nav_menu_args( $args ) { 
  781. /** 
  782. * The following conditions determine whether or not this instance of 
  783. * wp_nav_menu() can use selective refreshed. A wp_nav_menu() can be 
  784. * selective refreshed if... 
  785. */ 
  786. $can_partial_refresh = ( 
  787. // ...if wp_nav_menu() is directly echoing out the menu (and thus isn't manipulating the string after generated),  
  788. ! empty( $args['echo'] ) 
  789. && 
  790. // ...and if the fallback_cb can be serialized to JSON, since it will be included in the placement context data,  
  791. ( empty( $args['fallback_cb'] ) || is_string( $args['fallback_cb'] ) ) 
  792. && 
  793. // ...and if the walker can also be serialized to JSON, since it will be included in the placement context data as well,  
  794. ( empty( $args['walker'] ) || is_string( $args['walker'] ) ) 
  795. // ...and if it has a theme location assigned or an assigned menu to display,  
  796. && ( 
  797. ! empty( $args['theme_location'] ) 
  798. || 
  799. ( ! empty( $args['menu'] ) && ( is_numeric( $args['menu'] ) || is_object( $args['menu'] ) ) ) 
  800. && 
  801. // ...and if the nav menu would be rendered with a wrapper container element (upon which to attach data-* attributes). 
  802. ! empty( $args['container'] ) 
  803. || 
  804. ( isset( $args['items_wrap'] ) && '<' === substr( $args['items_wrap'], 0, 1 ) ) 
  805. ); 
  806. $args['can_partial_refresh'] = $can_partial_refresh; 
  807.  
  808. $exported_args = $args; 
  809.  
  810. // Empty out args which may not be JSON-serializable. 
  811. if ( ! $can_partial_refresh ) { 
  812. $exported_args['fallback_cb'] = ''; 
  813. $exported_args['walker'] = ''; 
  814.  
  815. /** 
  816. * Replace object menu arg with a term_id menu arg, as this exports better 
  817. * to JS and is easier to compare hashes. 
  818. */ 
  819. if ( ! empty( $exported_args['menu'] ) && is_object( $exported_args['menu'] ) ) { 
  820. $exported_args['menu'] = $exported_args['menu']->term_id; 
  821.  
  822. ksort( $exported_args ); 
  823. $exported_args['args_hmac'] = $this->hash_nav_menu_args( $exported_args ); 
  824.  
  825. $args['customize_preview_nav_menus_args'] = $exported_args; 
  826. $this->preview_nav_menu_instance_args[ $exported_args['args_hmac'] ] = $exported_args; 
  827. return $args; 
  828.  
  829. /** 
  830. * Prepares wp_nav_menu() calls for partial refresh. 
  831. * Injects attributes into container element. 
  832. * @since 4.3.0 
  833. * @access public 
  834. * @see wp_nav_menu() 
  835. * @param string $nav_menu_content The HTML content for the navigation menu. 
  836. * @param object $args An object containing wp_nav_menu() arguments. 
  837. * @return null 
  838. */ 
  839. public function filter_wp_nav_menu( $nav_menu_content, $args ) { 
  840. if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) { 
  841. $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) ); 
  842. $attributes .= ' data-customize-partial-type="nav_menu_instance"'; 
  843. $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) ); 
  844. $nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $nav_menu_content, 1 ); 
  845. return $nav_menu_content; 
  846.  
  847. /** 
  848. * Hashes (hmac) the nav menu arguments to ensure they are not tampered with when 
  849. * submitted in the Ajax request. 
  850. * Note that the array is expected to be pre-sorted. 
  851. * @since 4.3.0 
  852. * @access public 
  853. * @param array $args The arguments to hash. 
  854. * @return string Hashed nav menu arguments. 
  855. */ 
  856. public function hash_nav_menu_args( $args ) { 
  857. return wp_hash( serialize( $args ) ); 
  858.  
  859. /** 
  860. * Enqueue scripts for the Customizer preview. 
  861. * @since 4.3.0 
  862. * @access public 
  863. */ 
  864. public function customize_preview_enqueue_deps() { 
  865. wp_enqueue_script( 'customize-preview-nav-menus' ); // Note that we have overridden this. 
  866. wp_enqueue_style( 'customize-preview' ); 
  867.  
  868. /** 
  869. * Exports data from PHP to JS. 
  870. * @since 4.3.0 
  871. * @access public 
  872. */ 
  873. public function export_preview_data() { 
  874.  
  875. // Why not wp_localize_script? Because we're not localizing, and it forces values into strings. 
  876. $exports = array( 
  877. 'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args,  
  878. ); 
  879. printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) ); 
  880.  
  881. /** 
  882. * Export any wp_nav_menu() calls during the rendering of any partials. 
  883. * @since 4.5.0 
  884. * @access public 
  885. * @param array $response Response. 
  886. * @return array Response. 
  887. */ 
  888. public function export_partial_rendered_nav_menu_instances( $response ) { 
  889. $response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args; 
  890. return $response; 
  891.  
  892. /** 
  893. * Render a specific menu via wp_nav_menu() using the supplied arguments. 
  894. * @since 4.3.0 
  895. * @access public 
  896. * @see wp_nav_menu() 
  897. * @param WP_Customize_Partial $partial Partial. 
  898. * @param array $nav_menu_args Nav menu args supplied as container context. 
  899. * @return string|false 
  900. */ 
  901. public function render_nav_menu_partial( $partial, $nav_menu_args ) { 
  902. unset( $partial ); 
  903.  
  904. if ( ! isset( $nav_menu_args['args_hmac'] ) ) { 
  905. // Error: missing_args_hmac. 
  906. return false; 
  907.  
  908. $nav_menu_args_hmac = $nav_menu_args['args_hmac']; 
  909. unset( $nav_menu_args['args_hmac'] ); 
  910.  
  911. ksort( $nav_menu_args ); 
  912. if ( ! hash_equals( $this->hash_nav_menu_args( $nav_menu_args ), $nav_menu_args_hmac ) ) { 
  913. // Error: args_hmac_mismatch. 
  914. return false; 
  915.  
  916. ob_start(); 
  917. wp_nav_menu( $nav_menu_args ); 
  918. $content = ob_get_clean(); 
  919.  
  920. return $content;