Skip to content

Commit e76e2f3

Browse files
committed
Keep placeholder rows selected across pagination
Rows that do not map to a real object, such as Home and post type archives, are rendered with the negative $_nav_menu_placeholder as their object ID. That counter restarts on every request, so keying cached selections on it dropped those rows when the checklist reloaded. Identify them by URL and title instead, which stay the same between renders. Real pages, posts and terms keep using their object ID.
1 parent 3f2cfe0 commit e76e2f3

2 files changed

Lines changed: 149 additions & 5 deletions

File tree

‎src/js/_enqueues/lib/nav-menu.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,26 @@
458458
},
459459

460460
getSelectedMenuItemKey : function( menuItemData, listItemDBID ) {
461-
var key;
461+
var key,
462+
objectId = parseInt( menuItemData['menu-item-object-id'], 10 ),
462463

463-
if ( menuItemData['menu-item-object-id'] || menuItemData['menu-item-url'] ) {
464+
/*
465+
* Rows that do not map to a real object, such as Home and post
466+
* type archives, are rendered with the negative
467+
* $_nav_menu_placeholder as their object ID. That counter
468+
* restarts on every request, so the same row comes back with a
469+
* different ID after the checklist is reloaded. Identify those
470+
* rows by URL and title instead, which stay the same.
471+
*/
472+
isPlaceholder = isNaN( objectId ) || objectId <= 0;
473+
474+
if ( ! isPlaceholder || menuItemData['menu-item-url'] ) {
464475
key = [
465476
menuItemData['menu-item-type'] || '',
466477
menuItemData['menu-item-object'] || '',
467-
menuItemData['menu-item-object-id'] || '',
468-
'custom' === menuItemData['menu-item-type'] ? menuItemData['menu-item-url'] || '' : '',
469-
'custom' === menuItemData['menu-item-type'] ? menuItemData['menu-item-title'] || '' : ''
478+
isPlaceholder ? '' : objectId,
479+
isPlaceholder ? menuItemData['menu-item-url'] || '' : '',
480+
isPlaceholder ? menuItemData['menu-item-title'] || '' : ''
470481
].join( ':' );
471482

472483
return key.replace( /[^\w-]/g, '_' );

‎tests/qunit/wp-admin/js/nav-menu.js‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,37 @@
157157
'</li>';
158158
}
159159

160+
/**
161+
* Builds the synthetic Home row.
162+
*
163+
* When no static front page is set, nav-menu.php gives Home the
164+
* placeholder as its object ID, so the ID changes on every render.
165+
*
166+
* @return {string} The `li` markup.
167+
*/
168+
function homeItem() {
169+
return checklistItem( placeholder - 1, {
170+
type: 'custom',
171+
object: 'custom',
172+
title: 'Home',
173+
url: 'https://example.org/'
174+
} );
175+
}
176+
177+
/**
178+
* Builds a post type archive row, which also uses a placeholder ID.
179+
*
180+
* @return {string} The `li` markup.
181+
*/
182+
function archiveItem() {
183+
return checklistItem( placeholder - 1, {
184+
type: 'post_type_archive',
185+
object: 'post',
186+
title: 'Post Archives',
187+
url: 'https://example.org/archive/'
188+
} );
189+
}
190+
160191
/**
161192
* Wraps items in a meta box with an active and an inactive tab panel.
162193
*
@@ -325,6 +356,40 @@
325356
);
326357
} );
327358

359+
QUnit.test( 'getSelectedMenuItemKey() identifies Home by URL, not by its placeholder.', function( assert ) {
360+
var firstRender = {
361+
'menu-item-type': 'custom',
362+
'menu-item-object': 'custom',
363+
'menu-item-object-id': '-1',
364+
'menu-item-url': 'https://example.org/',
365+
'menu-item-title': 'Home'
366+
},
367+
secondRender = $.extend( {}, firstRender, { 'menu-item-object-id': '-4' } );
368+
369+
assert.strictEqual(
370+
api.getSelectedMenuItemKey( firstRender, -1 ),
371+
api.getSelectedMenuItemKey( secondRender, -4 ),
372+
'Home keeps one key across renders even though its object ID is a placeholder.'
373+
);
374+
} );
375+
376+
QUnit.test( 'getSelectedMenuItemKey() identifies a post type archive across renders.', function( assert ) {
377+
var firstRender = {
378+
'menu-item-type': 'post_type_archive',
379+
'menu-item-object': 'post',
380+
'menu-item-object-id': '-2',
381+
'menu-item-url': 'https://example.org/archive/',
382+
'menu-item-title': 'Post Archives'
383+
},
384+
secondRender = $.extend( {}, firstRender, { 'menu-item-object-id': '-9' } );
385+
386+
assert.strictEqual(
387+
api.getSelectedMenuItemKey( firstRender, -2 ),
388+
api.getSelectedMenuItemKey( secondRender, -9 ),
389+
'A post type archive keeps one key across renders.'
390+
);
391+
} );
392+
328393
QUnit.test( 'getSelectedMenuItemKey() keeps two custom links apart.', function( assert ) {
329394
var first = {
330395
'menu-item-type': 'custom',
@@ -467,6 +532,74 @@
467532
assert.strictEqual( checkboxFor( metaboxEl, 13 ).prop( 'checked' ), true, 'Item 13 is ticked again.' );
468533
} );
469534

535+
QUnit.test( 'A ticked Home row survives pagination.', function( assert ) {
536+
renderScreen( metabox( 'posttype-page', 'posttypediv', [ homeItem(), checklistItem( 12 ) ] ) );
537+
538+
var metaboxEl = fixture.find( '#posttype-page' ),
539+
homeId = metaboxEl.find( 'input.menu-item-checkbox' ).first().val(),
540+
pageOneAgain;
541+
542+
metaboxEl.find( 'input.menu-item-checkbox' ).first().trigger( 'click' );
543+
assert.strictEqual( countSelected( metaboxEl ), 1, 'Home was cached.' );
544+
545+
// A later request renders Home under a different placeholder.
546+
newRequest();
547+
nextPlaceholder();
548+
nextPlaceholder();
549+
pageOneAgain = metabox( 'posttype-page', 'posttypediv', [ homeItem(), checklistItem( 12 ) ] );
550+
551+
$.post = function( url, data, success ) {
552+
success( JSON.stringify( { 'replace-id': 'posttype-page', markup: pageOneAgain } ) );
553+
};
554+
fixture.find( 'a.page-numbers' ).trigger( 'click' );
555+
556+
metaboxEl = fixture.find( '#posttype-page' );
557+
assert.notStrictEqual(
558+
metaboxEl.find( 'input.menu-item-checkbox' ).first().val(),
559+
homeId,
560+
'Home really did come back under a different object ID.'
561+
);
562+
assert.strictEqual(
563+
metaboxEl.find( 'input.menu-item-checkbox' ).first().prop( 'checked' ),
564+
true,
565+
'Home is still ticked.'
566+
);
567+
} );
568+
569+
QUnit.test( 'A ticked post type archive row survives pagination.', function( assert ) {
570+
renderScreen( metabox( 'posttype-post', 'posttypediv', [ archiveItem(), checklistItem( 31 ) ] ) );
571+
572+
var metaboxEl = fixture.find( '#posttype-post' ),
573+
archiveId = metaboxEl.find( 'input.menu-item-checkbox' ).first().val(),
574+
pageOneAgain;
575+
576+
metaboxEl.find( 'input.menu-item-checkbox' ).first().trigger( 'click' );
577+
assert.strictEqual( countSelected( metaboxEl ), 1, 'The archive row was cached.' );
578+
579+
// A later request renders the archive under a different placeholder.
580+
newRequest();
581+
nextPlaceholder();
582+
nextPlaceholder();
583+
pageOneAgain = metabox( 'posttype-post', 'posttypediv', [ archiveItem(), checklistItem( 31 ) ] );
584+
585+
$.post = function( url, data, success ) {
586+
success( JSON.stringify( { 'replace-id': 'posttype-post', markup: pageOneAgain } ) );
587+
};
588+
fixture.find( 'a.page-numbers' ).trigger( 'click' );
589+
590+
metaboxEl = fixture.find( '#posttype-post' );
591+
assert.notStrictEqual(
592+
metaboxEl.find( 'input.menu-item-checkbox' ).first().val(),
593+
archiveId,
594+
'The archive row really did come back under a different object ID.'
595+
);
596+
assert.strictEqual(
597+
metaboxEl.find( 'input.menu-item-checkbox' ).first().prop( 'checked' ),
598+
true,
599+
'The archive row is still ticked.'
600+
);
601+
} );
602+
470603
QUnit.test( 'Taxonomy meta boxes keep their selections across pagination.', function( assert ) {
471604
renderScreen( metabox( 'taxonomy-category', 'taxonomydiv', [
472605
checklistItem( 5, { type: 'taxonomy', object: 'category', title: 'News' } ),

0 commit comments

Comments
 (0)