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