Ticket #34923: 34923.6.diff
File 34923.6.diff, 21.5 KB (added by , 8 years ago) |
---|
-
src/wp-admin/css/customize-nav-menus.css
diff --git src/wp-admin/css/customize-nav-menus.css src/wp-admin/css/customize-nav-menus.css index afe0cbc..ff5c69a 100644
61 61 text-align: right; 62 62 } 63 63 64 .customize-control-nav_menu_item.has-notifications .menu-item-handle { 65 border-left: 4px solid #00a0d2; 66 } 67 64 68 .wp-customizer .menu-item-settings { 65 69 max-width: 100%; 66 70 overflow: hidden; … … 497 501 color: #23282d; 498 502 } 499 503 500 #available-menu-items .a ccordion-section-content {504 #available-menu-items .available-menu-items-list { 501 505 overflow-y: auto; 502 506 max-height: 200px; /* This gets set in JS to fit the screen size, and based on # of sections. */ 503 507 background: transparent; … … 534 538 } 535 539 536 540 #available-menu-items .accordion-section-content { 537 padding: 1px 15px 15px 15px;538 margin: 0;539 541 max-height: 290px; 542 margin: 0; 543 padding: 0; 544 position: relative; 545 background: transparent; 546 } 547 548 #available-menu-items .accordion-section-content .available-menu-items-list { 549 margin: 0 0 45px 0; 550 padding: 1px 15px 15px 15px; 551 } 552 553 #available-menu-items .accordion-section-content .available-menu-items-list:only-child { /* Types that do not support new items for the current user */ 554 margin-bottom: 0; 555 } 556 557 #new-custom-menu-item .accordion-section-content { 558 padding: 0 15px 15px 15px; 559 } 560 561 #available-menu-items .accordion-section-content .new-content-item { 562 width: calc(100% - 30px); 563 padding: 8px 15px; 564 position: absolute; 565 bottom: 0; 566 z-index: 10; 567 background: #eee; 568 display: flex; 569 } 570 571 #available-menu-items .new-content-item .create-item-input { 572 flex-grow: 10; 573 margin-left: 5px; 574 } 575 #available-menu-items .new-content-item .add-content { 576 padding-left: 6px; 577 flex-grow: 1; 578 } 579 580 #available-menu-items .new-content-item .add-content:before { 581 content: "\f543"; 582 font: 20px/16px dashicons; 583 position: relative; 584 display: inline-block; 585 top: 5px; 586 left: -2px; 540 587 } 541 588 542 589 #available-menu-items .menu-item-tpl { -
src/wp-admin/js/customize-nav-menus.js
diff --git src/wp-admin/js/customize-nav-menus.js src/wp-admin/js/customize-nav-menus.js index 25495fe..5bdda8b 100644
80 80 }); 81 81 api.Menus.availableMenuItems = new api.Menus.AvailableItemCollection( api.Menus.data.availableMenuItems ); 82 82 83 api.Menus.insertedAutoDrafts = []; 84 85 /** 86 * Insert a new `auto-draft` post. 87 * 88 * @param {object} params - Parameters for the draft post to create. 89 * @param {string} params.post_type - Post type to add. 90 * @param {number} params.title - Post title to use. 91 * @return {jQuery.promise} Promise resolved with the added post. 92 */ 93 api.Menus.insertAutoDraftPost = function insertAutoDraftPost( params ) { 94 var request, deferred = $.Deferred(); 95 96 request = wp.ajax.post( 'customize-nav-menus-insert-auto-draft', { 97 'customize-menus-nonce': api.settings.nonce['customize-menus'], 98 'wp_customize': 'on', 99 'params': params 100 } ); 101 102 request.done( function( response ) { 103 if ( response.postId ) { 104 deferred.resolve( response ); 105 api.Menus.insertedAutoDrafts.push( response.postId ); 106 api( 'nav_menus_created_posts' ).set( _.clone( api.Menus.insertedAutoDrafts ) ); 107 } 108 } ); 109 110 request.fail( function( response ) { 111 var error = response || ''; 112 113 if ( 'undefined' !== typeof response.message ) { 114 error = response.message; 115 } 116 117 console.error( error ); 118 deferred.rejectWith( error ); 119 } ); 120 121 return deferred.promise(); 122 }; 123 83 124 /** 84 125 * wp.customize.Menus.AvailableMenuItemsPanelView 85 126 * … … 100 141 'click .menu-item-tpl': '_submit', 101 142 'click #custom-menu-item-submit': '_submitLink', 102 143 'keypress #custom-menu-item-name': '_submitLink', 144 'click .new-content-item .add-content': '_submitNew', 145 'keypress .create-item-input': '_submitNew', 103 146 'keydown': 'keyboardAccessible' 104 147 }, 105 148 … … 115 158 pages: {}, 116 159 sectionContent: '', 117 160 loading: false, 161 addingNew: false, 118 162 119 163 initialize: function() { 120 164 var self = this; … … 124 168 } 125 169 126 170 this.$search = $( '#menu-items-search' ); 127 this.sectionContent = this.$el.find( '.a ccordion-section-content' );171 this.sectionContent = this.$el.find( '.available-menu-items-list' ); 128 172 129 173 this.debounceSearch = _.debounce( self.search, 500 ); 130 174 … … 160 204 161 205 // Load more items. 162 206 this.sectionContent.scroll( function() { 163 var totalHeight = self.$el.find( '.accordion-section.open .a ccordion-section-content' ).prop( 'scrollHeight' ),207 var totalHeight = self.$el.find( '.accordion-section.open .available-menu-items-list' ).prop( 'scrollHeight' ), 164 208 visibleHeight = self.$el.find( '.accordion-section.open' ).height(); 165 209 166 210 if ( ! self.loading && $( this ).scrollTop() > 3 / 4 * totalHeight - visibleHeight ) { … … 337 381 } 338 382 items = new api.Menus.AvailableItemCollection( items ); // @todo Why is this collection created and then thrown away? 339 383 self.collection.add( items.models ); 340 typeInner = availableMenuItemContainer.find( '.a ccordion-section-content' );384 typeInner = availableMenuItemContainer.find( '.available-menu-items-list' ); 341 385 items.each(function( menuItem ) { 342 386 typeInner.append( itemTemplate( menuItem.attributes ) ); 343 387 }); … … 356 400 357 401 // Adjust the height of each section of items to fit the screen. 358 402 itemSectionHeight: function() { 359 var sections, totalHeight, accordionHeight, diff;403 var sections, lists, totalHeight, accordionHeight, diff, totalWidth, button, buttonWidth; 360 404 totalHeight = window.innerHeight; 361 405 sections = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .accordion-section-content' ); 362 accordionHeight = 46 * ( 2 + sections.length ) - 13; // Magic numbers. 406 lists = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .available-menu-items-list:not(":only-child")' ); 407 accordionHeight = 46 * ( 1 + sections.length ) + 14; // Magic numbers. 363 408 diff = totalHeight - accordionHeight; 364 409 if ( 120 < diff && 290 > diff ) { 365 410 sections.css( 'max-height', diff ); 411 lists.css( 'max-height', ( diff - 60 ) ); 366 412 } 367 413 }, 368 414 … … 456 502 itemName.val( '' ); 457 503 }, 458 504 505 // Submit handler for keypress (enter) on field and click on button. 506 _submitNew: function( event ) { 507 var container; 508 509 // Only proceed with keypress if it is Enter. 510 if ( 'keypress' === event.type && 13 !== event.which ) { 511 return; 512 } 513 514 if ( this.addingNew ) { 515 return; 516 } 517 518 container = $( event.target ).closest( '.accordion-section' ); 519 520 this.submitNew( container ); 521 }, 522 523 // Creates a new object and adds an associated menu item to the menu. 524 submitNew: function( container ) { 525 var panel = this, 526 itemName = container.find( '.create-item-input' ), 527 title = itemName.val(), 528 dataContainer = container.find( '.available-menu-items-list' ), 529 itemType = dataContainer.data( 'type' ), 530 itemObject = dataContainer.data( 'object' ), 531 itemTypeLabel = dataContainer.data( 'type_label' ), 532 promise; 533 534 if ( ! this.currentMenuControl ) { 535 return; 536 } 537 538 if ( '' === itemName.val() ) { 539 itemName.addClass( 'invalid' ); 540 return; 541 } else { 542 container.find( '.accordion-section-title' ).addClass( 'loading' ); 543 } 544 545 // Only posts are supported currently. 546 if ( 'post_type' !== itemType ) { 547 return; 548 } 549 550 panel.addingNew = true; 551 itemName.attr( 'disabled', 'disabled' ); 552 promise = api.Menus.insertAutoDraftPost( { 553 post_title: title, 554 post_type: itemObject, 555 post_status: 'publish' 556 } ); 557 promise.done( function( data ) { 558 var menuItem = { 559 'title': itemName.val(), 560 'type': itemType, 561 'type_label': itemTypeLabel, 562 'object': itemObject, 563 'object_id': data.postId, 564 'url': data.url 565 }, availableItems, $content, itemTemplate; 566 567 // Add new item to menu. 568 panel.currentMenuControl.addItemToMenu( menuItem ); 569 570 // Add the new item to the list of available items. 571 menuItem.id = 'post-' + data.postId; // `id` is used for available menu item Backbone models. 572 availableItems = new api.Menus.AvailableItemCollection( [ menuItem ] ); 573 api.Menus.availableMenuItemsPanel.collection.add( availableItems.models ); 574 $content = container.find( '.available-menu-items-list' ), 575 itemTemplate = wp.template( 'available-menu-item' ); 576 $content.prepend( itemTemplate( menuItem ) ); 577 $content.scrollTop(); 578 579 // Reset the create content form. 580 itemName.val( '' ) 581 .removeAttr( 'disabled' ) 582 .focus(); 583 panel.addingNew = false; 584 container.find( '.accordion-section-title' ).removeClass( 'loading' ); 585 } ); 586 }, 587 459 588 // Opens the panel. 460 589 open: function( menuControl ) { 461 590 this.currentMenuControl = menuControl; … … 2545 2674 if ( data.nav_menu_updates || data.nav_menu_item_updates ) { 2546 2675 api.Menus.applySavedData( data ); 2547 2676 } 2677 2678 // Reset list of inserted auto draft post IDs. 2679 api.Menus.insertedAutoDrafts = []; 2548 2680 } ); 2549 2681 2550 2682 // Open and focus menu control. -
src/wp-includes/class-wp-customize-manager.php
diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php index 5ee3b6d..6c96995 100644
final class WP_Customize_Manager { 1535 1535 <script type="text/html" id="tmpl-customize-control-notifications"> 1536 1536 <ul> 1537 1537 <# _.each( data.notifications, function( notification ) { #> 1538 <li class="notice notice-{{ notification.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ notification.code }}" data-type="{{ notification.type }}">{{ notification.message || notification.code}}</li>1538 <li class="notice notice-{{ notification.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ notification.code }}" data-type="{{ notification.type }}">{{{ notification.message || notification.code }}}</li> 1539 1539 <# } ); #> 1540 1540 </ul> 1541 1541 </script> -
src/wp-includes/class-wp-customize-nav-menus.php
diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php index f9832a6..218bc4c 100644
final class WP_Customize_Nav_Menus { 56 56 add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) ); 57 57 add_action( 'wp_ajax_load-available-menu-items-customizer', array( $this, 'ajax_load_available_items' ) ); 58 58 add_action( 'wp_ajax_search-available-menu-items-customizer', array( $this, 'ajax_search_available_items' ) ); 59 add_action( 'wp_ajax_customize-nav-menus-insert-auto-draft', array( $this, 'ajax_insert_auto_draft_post' ) ); 59 60 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 60 61 // Needs to run after core Navigation section is set up.62 61 add_action( 'customize_register', array( $this, 'customize_register' ), 11 ); 63 64 62 add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_dynamic_setting_args' ), 10, 2 ); 65 63 add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_dynamic_setting_class' ), 10, 3 ); 66 64 add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_templates' ) ); 67 65 add_action( 'customize_controls_print_footer_scripts', array( $this, 'available_items_template' ) ); 68 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 add_action( 'customize_save_nav_menus_created_posts', array( $this, 'publish_auto_draft_posts' ) ); 69 69 70 70 // Selective Refresh partials. 71 71 add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); … … final class WP_Customize_Nav_Menus { 356 356 public function enqueue_scripts() { 357 357 wp_enqueue_style( 'customize-nav-menus' ); 358 358 wp_enqueue_script( 'customize-nav-menus' ); 359 wp_enqueue_script( 'customize-posts' ); 359 360 360 361 $temp_nav_menu_setting = new WP_Customize_Nav_Menu_Setting( $this->manager, 'nav_menu[-1]' ); 361 362 $temp_nav_menu_item_setting = new WP_Customize_Nav_Menu_Item_Setting( $this->manager, 'nav_menu_item[-1]' ); … … final class WP_Customize_Nav_Menus { 392 393 'reorderModeOff' => __( 'Reorder mode closed' ), 393 394 'reorderLabelOn' => esc_attr__( 'Reorder menu items' ), 394 395 'reorderLabelOff' => esc_attr__( 'Close reorder mode' ), 396 /* translators: %1$s: post type, %2$s: post title, %3$s: link to edit post */ 397 'newPostPublished' => __( 'New %1$s %2$s published. Edit it <a href="%3$s" target="_blank">here<span class="screen-reader-text"> (link opens in new tab)</span></a>.' ), 395 398 ), 396 399 'settingTransport' => 'postMessage', 397 400 'phpIntMax' => PHP_INT_MAX, … … final class WP_Customize_Nav_Menus { 400 403 'nav_menu_item' => $temp_nav_menu_item_setting->default, 401 404 ), 402 405 'locationSlugMappedToName' => get_registered_nav_menus(), 406 'editPostURL' => admin_url( 'post.php?post=%d&action=edit' ), 403 407 ); 404 408 405 409 $data = sprintf( 'var _wpCustomizeNavMenusSettings = %s;', wp_json_encode( $settings ) ); … … final class WP_Customize_Nav_Menus { 626 630 'section' => 'add_menu', 627 631 'settings' => array(), 628 632 ) ) ); 633 634 $this->manager->add_setting( new WP_Customize_Filter_Setting( $this->manager, 'nav_menus_created_posts', array( 635 'transport' => 'postMessage', 636 'default' => array(), 637 'sanitize_callback' => 'wp_parse_id_list', 638 ) ) ); 629 639 } 630 640 631 641 /** … … final class WP_Customize_Nav_Menus { 660 670 foreach ( $post_types as $slug => $post_type ) { 661 671 $item_types[] = array( 662 672 'title' => $post_type->labels->name, 673 'label' => $post_type->labels->singular_name, 663 674 'type' => 'post_type', 664 675 'object' => $post_type->name, 665 676 ); … … final class WP_Customize_Nav_Menus { 674 685 } 675 686 $item_types[] = array( 676 687 'title' => $taxonomy->labels->name, 688 'label' => $taxonomy->labels->singular_name, 677 689 'type' => 'taxonomy', 678 690 'object' => $taxonomy->name, 679 691 ); … … final class WP_Customize_Nav_Menus { 693 705 } 694 706 695 707 /** 708 * Add a new `auto-draft` post. 709 * 710 * @access public 711 * @since 4.7.0 712 * 713 * @param string $post_type The post type. 714 * @param string $title The post title. 715 * @return WP_Post|WP_Error 716 */ 717 public function insert_auto_draft_post( $post_type, $title ) { 718 719 $post_type_obj = get_post_type_object( $post_type ); 720 if ( ! $post_type_obj ) { 721 return new WP_Error( 'unknown_post_type', __( 'Unknown post type', 'customize-posts' ) ); 722 } 723 724 add_filter( 'wp_insert_post_empty_content', '__return_false' ); 725 $args = array( 726 'post_status' => 'auto-draft', 727 'post_type' => $post_type, 728 'post_title' => $title, 729 'post_name' => sanitize_title( $title ), // Auto-drafts are allowed to have empty post_names, so we need to explicitly set it. 730 ); 731 $r = wp_insert_post( wp_slash( $args ), true ); 732 remove_filter( 'wp_insert_post_empty_content', '__return_false' ); 733 734 if ( is_wp_error( $r ) ) { 735 return $r; 736 } else { 737 return get_post( $r ); 738 } 739 } 740 741 /** 742 * Ajax handler for adding a new auto-draft post. 743 * 744 * @action wp_ajax_customize-posts-insert-auto-draft 745 * @access public 746 * @since 4.7.0 747 */ 748 public function ajax_insert_auto_draft_post() { 749 if ( ! check_ajax_referer( 'customize-menus', 'customize-menus-nonce' ) ) { 750 status_header( 400 ); 751 wp_send_json_error( 'bad_nonce' ); 752 } 753 754 if ( ! current_user_can( 'customize' ) ) { 755 status_header( 403 ); 756 wp_send_json_error( 'customize_not_allowed' ); 757 } 758 759 if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { 760 status_header( 400 ); 761 wp_send_json_error( 'missing_params' ); 762 } 763 764 $params = wp_unslash( $_POST['params'] ); 765 766 if ( empty( $params['post_type'] ) ) { 767 status_header( 400 ); 768 wp_send_json_error( 'missing_post_type_param' ); 769 } 770 771 $post_type_object = get_post_type_object( $params['post_type'] ); 772 if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) { 773 status_header( 403 ); 774 wp_send_json_error( 'insufficient_post_permissions' ); 775 } 776 777 if ( ! $params['title'] ) { 778 $params['title'] = ''; 779 } 780 781 $r = $this->insert_auto_draft_post( $post_type_object->name, $params['post_title'] ); 782 if ( is_wp_error( $r ) ) { 783 $error = $r; 784 if ( ! empty( $post_type_object->labels->singular_name ) ) { 785 $singular_name = $post_type_object->labels->singular_name; 786 } else { 787 $singular_name = __( 'Post' ); 788 } 789 790 $data = array( 791 /* translators: %1$s is the post type name and %2$s is the error message. */ 792 'message' => sprintf( __( '%1$s could not be created: %2$s' ), $singular_name, $error->get_error_message() ), 793 ); 794 wp_send_json_error( $data ); 795 } else { 796 $post = $r; 797 $data = array( 798 'postId' => $post->ID, 799 'url' => get_permalink( $post->ID ), 800 ); 801 wp_send_json_success( $data ); 802 } 803 } 804 805 /** 696 806 * Print the JavaScript templates used to render Menu Customizer components. 697 807 * 698 808 * Templates are imported into the JS use wp.template. … … final class WP_Customize_Nav_Menus { 768 878 <span class="spinner"></span> 769 879 </div> 770 880 <button type="button" class="clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results' ); ?></span></button> 771 <ul class="accordion-section-content " data-type="search"></ul>881 <ul class="accordion-section-content available-menu-items-list" data-type="search"></ul> 772 882 </div> 773 883 <div id="new-custom-menu-item" class="accordion-section"> 774 884 <h4 class="accordion-section-title" role="presentation"> … … final class WP_Customize_Nav_Menus { 797 907 </div> 798 908 </div> 799 909 <?php 800 // Containers for per-post-type item browsing; items added with JS. 910 /** 911 * Filter the content types that do not allow new items to be created from nav menus. 912 * 913 * Types are formated as 'post_type'|'taxonomy' _ post_type_name; for example, 'taxonomy_post_format'. 914 * Taxonomies are not yet supported by this UI but will be in the future. Post types are only available 915 * here if `show_in_nav_menus` is true. 916 * 917 * @since 4.7.0 918 * 919 * @param array $types Array of disallowed types. 920 */ 921 $disallowed_new_content_types = apply_filters( 'customize_nav_menus_disallow_new_content_types', array( 'taxonomy_post_format' ) ); 922 923 // Containers for per-post-type item browsing; items are added with JS. 801 924 foreach ( $this->available_item_types() as $available_item_type ) { 802 925 $id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] ); 803 926 ?> … … final class WP_Customize_Nav_Menus { 813 936 <span class="toggle-indicator" aria-hidden="true"></span> 814 937 </button> 815 938 </h4> 816 <ul class="accordion-section-content" data-type="<?php echo esc_attr( $available_item_type['type'] ); ?>" data-object="<?php echo esc_attr( $available_item_type['object'] ); ?>"></ul> 939 <div class="accordion-section-content"> 940 <?php if ( 'post_type' === $available_item_type['type'] && ! in_array( $available_item_type['type'] . '_' . $available_item_type['object'], $disallowed_new_content_types, true ) ) : ?> 941 <?php $post_type_obj = get_post_type_object( $available_item_type['object'] ); ?> 942 <?php if ( current_user_can( $post_type_obj->cap->create_posts ) && current_user_can( $post_type_obj->cap->publish_posts ) ) : ?> 943 <div class="new-content-item"> 944 <input type="text" class="create-item-input" placeholder="<?php 945 /* translators: %s: Singular title of post type or taxonomy */ 946 printf( __( 'Create New %s' ), $post_type_obj->labels->singular_name ); ?>"> 947 <button type="button" class="button add-content"><?php _e( 'Add' ); ?></button> 948 </div> 949 <?php endif; ?> 950 <?php endif; ?> 951 <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( $available_item_type['label'] ); ?>"></ul> 952 </div> 817 953 </div> 818 954 <?php 819 955 } … … final class WP_Customize_Nav_Menus { 881 1017 } 882 1018 883 1019 /** 1020 * Make the auto-draft status protected so that it can be queried. 1021 * 1022 * @since 4.7.0 1023 * @access public 1024 */ 1025 public function make_auto_draft_status_previewable() { 1026 global $wp_post_statuses; 1027 $wp_post_statuses['auto-draft']->protected = true; 1028 } 1029 1030 /** 1031 * Publish the auto-draft posts that were created for nav menu items. 1032 * 1033 * @since 4.7.0 1034 * @access public 1035 * 1036 * @param WP_Customize_Setting $setting Customizer Setting object. 1037 */ 1038 public function publish_auto_draft_posts( $setting ) { 1039 $value = $setting->post_value(); 1040 if ( ! empty( $value ) ) { 1041 foreach ( array_filter( $value ) as $post_id ) { 1042 $post = get_post( $post_id ); 1043 if ( $post && 'auto-draft' === $post->post_status ) { 1044 wp_publish_post( $post ); 1045 } 1046 } 1047 } 1048 } 1049 1050 /** 884 1051 * Keep track of the arguments that are being passed to wp_nav_menu(). 885 1052 * 886 1053 * @since 4.3.0 -
src/wp-includes/class-wp-customize-setting.php
diff --git src/wp-includes/class-wp-customize-setting.php src/wp-includes/class-wp-customize-setting.php index a0d04d3..a16b100 100644
class WP_Customize_Setting { 498 498 /** 499 499 * Fetch and sanitize the $_POST value for the setting. 500 500 * 501 * During a save request prior to save, post_value() provides the new value while value() does not. 502 * 501 503 * @since 3.4.0 502 504 * 503 505 * @param mixed $default A default value which is used as a fallback. Default is null.