Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site Editor: Don't allow creating template part on the Patterns page for non-block themes #52656

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/edit-site/src/components/add-new-pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DropdownMenu } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { plus, symbol, symbolFilled } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
Expand All @@ -14,6 +15,7 @@ import CreatePatternModal from '../create-pattern-modal';
import CreateTemplatePartModal from '../create-template-part-modal';
import SidebarButton from '../sidebar-button';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';

const { useHistory } = unlock( routerPrivateApis );

Expand All @@ -22,6 +24,10 @@ export default function AddNewPattern() {
const [ showPatternModal, setShowPatternModal ] = useState( false );
const [ showTemplatePartModal, setShowTemplatePartModal ] =
useState( false );
const isTemplatePartsMode = useSelect( ( select ) => {
const settings = select( editSiteStore ).getSettings();
return !! settings.supportsTemplatePartsMode;
}, [] );

function handleCreatePattern( { pattern, categoryId } ) {
setShowPatternModal( false );
Expand Down Expand Up @@ -55,7 +61,7 @@ export default function AddNewPattern() {
<>
<DropdownMenu
controls={ [
{
! isTemplatePartsMode && {
icon: symbolFilled,
onClick: () => setShowTemplatePartModal( true ),
title: __( 'Create template part' ),
Expand All @@ -65,7 +71,7 @@ export default function AddNewPattern() {
onClick: () => setShowPatternModal( true ),
title: __( 'Create pattern' ),
},
] }
].filter( Boolean ) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use conditional spread and get rid of the filter.

 [
	...( ! isTemplatePartsMode
		? [
				{
					icon: symbolFilled,
					onClick: () =>
						setShowTemplatePartModal( true ),
					title: __( 'Create template part' ),
				},
				]
		: [] ),
	{
		icon: symbol,
		onClick: () => setShowPatternModal( true ),
		title: __( 'Create pattern' ),
	},
]

toggleProps={ {
as: SidebarButton,
} }
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/specs/site-editor/hybrid-theme.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Hybrid theme', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptyhybrid' );
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );

test( 'should not show the Add Template Part button', async ( {
admin,
page,
} ) => {
await admin.visitAdminPage(
'/site-editor.php',
'postType=wp_template_part&path=/wp_template_part/all'
);
t-hamano marked this conversation as resolved.
Show resolved Hide resolved

await expect(
page.locator( 'role=button[name="Add New Template Part"i]' )
).toBeHidden();

// Back to Patterns page.
await page.click(
'role=region[name="Navigation"i] >> role=button[name="Back"i]'
);

await page.click(
'role=region[name="Navigation"i] >> role=button[name="Create pattern"i]'
);

await expect(
page.locator( 'role=menuitem[name="Create template part"i]' )
).toBeHidden();
} );
} );