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

Add template lock attribute to column and group #26128

Merged
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
3 changes: 3 additions & 0 deletions packages/block-library/src/column/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"width": {
"type": "string"
},
"templateLock": {
"type": "string"
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a boolean?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @ZebulanStanphill, the template lock attribute can contain values like "all", and "insert" which are strings.

}
},
"supports": {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

function ColumnEdit( {
attributes: { verticalAlignment, width },
attributes: { verticalAlignment, width, templateLock = false },
setAttributes,
clientId,
} ) {
Expand Down Expand Up @@ -60,7 +60,7 @@ function ColumnEdit( {
style: width ? { flexBasis: width } : undefined,
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
templateLock: false,
templateLock,
renderAppender: hasChildBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"tagName": {
"type": "string",
"default": "div"
},
"templateLock": {
"type": "string"
}
},
"supports": {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ function GroupEdit( { attributes, clientId } ) {
[ clientId ]
);
const blockProps = useBlockProps();
const { tagName: TagName = 'div' } = attributes;
const { tagName: TagName = 'div', templateLock } = attributes;
const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-group__inner-container',
},
{
templateLock,
renderAppender: hasInnerBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
Expand Down
76 changes: 76 additions & 0 deletions packages/e2e-tests/plugins/cpt-locking.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,82 @@ function gutenberg_test_cpt_locking() {
'template_lock' => false,
)
);
register_post_type(
'l-post-ul-group',
array(
'public' => true,
'label' => 'Locked Post Unlocked group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(
'templateLock' => false,
),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
),
),
),
'template_lock' => 'all',
)
);
register_post_type(
'l-post-l-group',
array(
'public' => true,
'label' => 'Locked Post Locked group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(
'templateLock' => 'all',
),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
),
),
),
'template_lock' => 'all',
)
);
register_post_type(
'l-post-i-group',
array(
'public' => true,
'label' => 'Locked Post Inherited group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
),
),
),
'template_lock' => 'all',
)
);
}

add_action( 'init', 'gutenberg_test_cpt_locking' );
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ exports[`cpt locking template_lock all should not error when deleting the cotent
<!-- /wp:columns -->"
`;

exports[`cpt locking template_lock all unlocked group should allow blocks to be moved 1`] = `
"<!-- wp:group {\\"templateLock\\":false} -->
<div class=\\"wp-block-group\\"><div class=\\"wp-block-group__inner-container\\"><!-- wp:paragraph {\\"placeholder\\":\\"Add a description\\"} -->
<p>p1</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote --></div></div>
<!-- /wp:group -->"
`;

exports[`cpt locking template_lock all unlocked group should allow blocks to be removed 1`] = `
"<!-- wp:group {\\"templateLock\\":false} -->
<div class=\\"wp-block-group\\"><div class=\\"wp-block-group__inner-container\\"><!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote --></div></div>
<!-- /wp:group -->"
`;

exports[`cpt locking template_lock false should allow blocks to be inserted 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
Expand Down
63 changes: 63 additions & 0 deletions packages/e2e-tests/specs/editor/plugins/cpt-locking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,67 @@ describe( 'cpt locking', () => {

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );

describe( 'template_lock all unlocked group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'l-post-ul-group',
} );
} );

it( 'should allow blocks to be removed', async () => {
await page.type(
'.block-editor-rich-text__editable[data-type="core/paragraph"]',
'p1'
);
await clickBlockToolbarButton( 'More options' );
const [ removeBlock ] = await page.$x(
'//button[contains(text(), "Remove block")]'
);
await removeBlock.click();
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );

describe( 'template_lock all locked group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'l-post-l-group',
} );
} );

it(
'should not allow blocks to be removed',
shouldNotAllowBlocksToBeRemoved
);

it( 'should not allow blocks to be moved', async () => {
await page.click(
'.block-editor-rich-text__editable[data-type="core/paragraph"]'
);
expect( await page.$( 'button[aria-label="Move up"]' ) ).toBeNull();
} );
} );

describe( 'template_lock all inherited group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'l-post-i-group',
} );
} );

it(
'should not allow blocks to be removed',
shouldNotAllowBlocksToBeRemoved
);

it( 'should not allow blocks to be moved', async () => {
await page.click(
'.block-editor-rich-text__editable[data-type="core/paragraph"]'
);
expect( await page.$( 'button[aria-label="Move up"]' ) ).toBeNull();
} );
} );
} );