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