Skip to content

Commit

Permalink
Navigation: nav created drafts should not render on frontend (#29692)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar authored Apr 7, 2021
1 parent 060abf4 commit 8de9985
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
13 changes: 12 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ export default function NavigationLinkEdit( {

return {
id: page.id,
postType,
type: postType,
title: page.title.rendered,
url: page.link,
kind: 'post-type',
};
}

Expand Down Expand Up @@ -523,6 +524,8 @@ export default function NavigationLinkEdit( {
url: newURL = '',
opensInNewTab: newOpensInNewTab,
id,
kind: newKind = '',
type: newType = '',
} = {} ) =>
setAttributes( {
url: encodeURI( newURL ),
Expand Down Expand Up @@ -550,6 +553,14 @@ export default function NavigationLinkEdit( {
} )(),
opensInNewTab: newOpensInNewTab,
id,
...( newKind && {
kind: newKind,
} ),
...( newType &&
newType !== 'URL' &&
newType !== 'post-format' && {
type: newType,
} ),
} )
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ const fallbackVariations = [
icon: postIcon,
title: __( 'Post Link' ),
description: __( 'A link to a post.' ),
attributes: { type: 'post' },
attributes: { type: 'post', kind: 'post-type' },
},
{
name: 'page',
icon: pageIcon,
title: __( 'Page Link' ),
description: __( 'A link to a page.' ),
attributes: { type: 'page' },
attributes: { type: 'page', kind: 'post-type' },
},
{
name: 'category',
icon: categoryIcon,
title: __( 'Category Link' ),
description: __( 'A link to a category.' ),
attributes: { type: 'category' },
attributes: { type: 'category', kind: 'taxonomy' },
},
{
name: 'tag',
icon: tagIcon,
title: __( 'Tag Link' ),
description: __( 'A link to a tag.' ),
attributes: { type: 'tag' },
attributes: { type: 'tag', kind: 'taxonomy' },
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Object {
},
Object {
"attributes": Object {
"kind": "post-type",
"type": "post",
},
"description": "A link to a post.",
Expand All @@ -34,6 +35,7 @@ Object {
},
Object {
"attributes": Object {
"kind": "post-type",
"type": "page",
},
"description": "A link to a page.",
Expand All @@ -51,6 +53,7 @@ Object {
},
Object {
"attributes": Object {
"kind": "taxonomy",
"type": "category",
},
"description": "A link to a category.",
Expand All @@ -70,6 +73,7 @@ Object {
},
Object {
"attributes": Object {
"kind": "taxonomy",
"type": "tag",
},
"description": "A link to a tag.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import { __ } from '@wordpress/i18n';
* @typedef { 'post' | 'term' | 'post-format' } WPLinkSearchType
*/

/**
* A link with an id may be of kind post-type or taxonomy
*
* @typedef { 'post-type' | 'taxonomy' } WPKind
*/

/**
* @typedef WPLinkSearchOptions
*
Expand All @@ -29,6 +35,7 @@ import { __ } from '@wordpress/i18n';
* @property {string} url Link url.
* @property {string} title Title of the link.
* @property {string} type The taxonomy or post type slug or type URL.
* @property {WPKind} [kind] Link kind of post-type or taxonomy
*/

/**
Expand Down Expand Up @@ -87,7 +94,16 @@ const fetchLinkSuggestions = async (
type: 'post',
subtype,
} ),
} ).catch( () => [] ) // fail by returning no results
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'post-type', subtype },
};
} );
} )
.catch( () => [] ) // fail by returning no results
);
}

Expand All @@ -101,7 +117,16 @@ const fetchLinkSuggestions = async (
type: 'term',
subtype,
} ),
} ).catch( () => [] )
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'taxonomy', subtype },
};
} );
} )
.catch( () => [] )
);
}

Expand Down Expand Up @@ -146,6 +171,7 @@ const fetchLinkSuggestions = async (
decodeEntities( result.title || '' ) ||
__( '(no title)' ),
type: result.subtype || result.type,
kind: result?.meta?.kind,
};
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe( 'fetchLinkSuggestions', () => {
title: 'Contact Page',
type: 'page',
url: 'http://wordpress.local/contact-page/',
kind: 'post-type',
},
] )
);
Expand All @@ -108,12 +109,14 @@ describe( 'fetchLinkSuggestions', () => {
title: 'Cats',
url: 'http://wordpress.local/category/cats/',
type: 'category',
kind: 'taxonomy',
},
{
id: 1,
title: 'Uncategorized',
url: 'http://wordpress.local/category/uncategorized/',
type: 'category',
kind: 'taxonomy',
},
] )
);
Expand Down Expand Up @@ -155,18 +158,21 @@ describe( 'fetchLinkSuggestions', () => {
title: 'Contact Page',
url: 'http://wordpress.local/contact-page/',
type: 'page',
kind: 'post-type',
},
{
id: 9,
title: 'Cats',
url: 'http://wordpress.local/category/cats/',
type: 'category',
kind: 'taxonomy',
},
{
id: 1,
title: 'Uncategorized',
url: 'http://wordpress.local/category/uncategorized/',
type: 'category',
kind: 'taxonomy',
},
{
id: 'gallery',
Expand Down Expand Up @@ -195,6 +201,7 @@ describe( 'fetchLinkSuggestions', () => {
title: 'Limit Case',
url: 'http://wordpress.local/limit-case/',
type: 'page',
kind: 'post-type',
},
] )
);
Expand All @@ -211,6 +218,7 @@ describe( 'fetchLinkSuggestions', () => {
title: 'Page Case',
url: 'http://wordpress.local/page-case/',
type: 'page',
kind: 'post-type',
},
] )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ exports[`Navigation allows an empty navigation block to be created and manually
"<!-- wp:navigation {\\"orientation\\":\\"horizontal\\"} -->
<!-- wp:navigation-link {\\"label\\":\\"WP\\",\\"id\\":\\"https://wordpress.org\\",\\"url\\":\\"https://wordpress.org\\"} /-->
<!-- wp:navigation-link {\\"label\\":\\"Contact\\",\\"id\\":1,\\"url\\":\\"https://this/is/a/test/search/get-in-touch\\"} /-->
<!-- wp:navigation-link {\\"label\\":\\"Contact\\",\\"type\\":\\"page\\",\\"id\\":1,\\"url\\":\\"https://this/is/a/test/search/get-in-touch\\",\\"kind\\":\\"post-type\\"} /-->
<!-- /wp:navigation -->"
`;

exports[`Navigation allows pages to be created from the navigation block and their links added to menu 1`] = `
"<!-- wp:navigation {\\"orientation\\":\\"horizontal\\"} -->
<!-- wp:navigation-link {\\"label\\":\\"A really long page name that will not exist\\",\\"id\\":1,\\"url\\":\\"https://this/is/a/test/create/page/my-new-page\\"} /-->
<!-- wp:navigation-link {\\"label\\":\\"A really long page name that will not exist\\",\\"type\\":\\"page\\",\\"id\\":1,\\"url\\":\\"https://this/is/a/test/create/page/my-new-page\\",\\"kind\\":\\"post-type\\"} /-->
<!-- /wp:navigation -->"
`;

0 comments on commit 8de9985

Please sign in to comment.