-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for allowed blocks recursively in patterns (#30366)
Currently, patterns show up in the inserter even if they contain a block that's not on the allowed blocks list. Blocks aren't checked recursively in the patterns, only top level blocks. Even though they show up, we can't insert them since the blocks are disallowed. This PR checks recursively for disallowed blocks.
- Loading branch information
1 parent
d21299e
commit 6a843df
Showing
8 changed files
with
233 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/e2e-tests/plugins/allowed-patterns-disable-blocks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Test Allowed Patterns Disable Blocks | ||
* Plugin URI: https://github.com/WordPress/gutenberg | ||
* Author: Gutenberg Team | ||
* | ||
* @package gutenberg-test-allowed-patterns-disable-blocks | ||
*/ | ||
|
||
/** | ||
* Restrict the allowed blocks in the editor. | ||
* | ||
* @param Array $allowed_block_types An array of strings containing the previously allowed blocks. | ||
* @param WP_Post $post The current post object. | ||
* @return Array An array of strings containing the new allowed blocks after the filter is applied. | ||
*/ | ||
function my_plugin_allowed_block_types( $allowed_block_types, $post ) { | ||
if ( 'post' !== $post->post_type ) { | ||
return $allowed_block_types; | ||
} | ||
return array( 'core/heading', 'core/columns', 'core/column', 'core/image', 'core/spacer' ); | ||
} | ||
|
||
add_filter( 'allowed_block_types', 'my_plugin_allowed_block_types', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Test Allowed Patterns | ||
* Plugin URI: https://github.com/WordPress/gutenberg | ||
* Author: Gutenberg Team | ||
* | ||
* @package gutenberg-test-allowed-patterns | ||
*/ | ||
|
||
register_block_pattern( | ||
'test-allowed-patterns/lone-heading', | ||
array( | ||
'title' => 'Test: Single heading', | ||
'content' => '<!-- wp:heading --><h2>Hello!</h2><!-- /wp:heading -->', | ||
) | ||
); | ||
|
||
register_block_pattern( | ||
'test-allowed-patterns/lone-paragraph', | ||
array( | ||
'title' => 'Test: Single paragraph', | ||
'content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', | ||
) | ||
); | ||
|
||
register_block_pattern( | ||
'test-allowed-patterns/paragraph-inside-group', | ||
array( | ||
'title' => 'Test: Paragraph inside group', | ||
'content' => '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph --></div><!-- /wp:group -->', | ||
) | ||
); |
74 changes: 74 additions & 0 deletions
74
packages/e2e-tests/specs/editor/various/allowed-patterns.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
activatePlugin, | ||
createNewPost, | ||
deactivatePlugin, | ||
searchForPattern, | ||
toggleGlobalBlockInserter, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
const checkPatternExistence = async ( name, available = true ) => { | ||
await searchForPattern( name ); | ||
const patternElement = await page.waitForXPath( | ||
`//div[@role = 'option']//div[contains(text(), '${ name }')]`, | ||
{ timeout: 5000, visible: available, hidden: ! available } | ||
); | ||
const patternExists = !! patternElement; | ||
await toggleGlobalBlockInserter(); | ||
return patternExists; | ||
}; | ||
|
||
const TEST_PATTERNS = [ | ||
[ 'Test: Single heading', true ], | ||
[ 'Test: Single paragraph', false ], | ||
[ 'Test: Paragraph inside group', false ], | ||
]; | ||
|
||
describe( 'Allowed Patterns', () => { | ||
beforeAll( async () => { | ||
await activatePlugin( 'gutenberg-test-allowed-patterns' ); | ||
await createNewPost(); | ||
} ); | ||
afterAll( async () => { | ||
await deactivatePlugin( 'gutenberg-test-allowed-patterns' ); | ||
} ); | ||
|
||
describe( 'Disable blocks plugin disabled', () => { | ||
for ( const [ patternName ] of TEST_PATTERNS ) { | ||
it( `should show test pattern "${ patternName }"`, async () => { | ||
expect( await checkPatternExistence( patternName, true ) ).toBe( | ||
true | ||
); | ||
} ); | ||
} | ||
} ); | ||
|
||
describe( 'Disable blocks plugin enabled', () => { | ||
beforeAll( async () => { | ||
await activatePlugin( | ||
'gutenberg-test-allowed-patterns-disable-blocks' | ||
); | ||
await createNewPost(); | ||
} ); | ||
afterAll( async () => { | ||
await deactivatePlugin( | ||
'gutenberg-test-allowed-patterns-disable-blocks' | ||
); | ||
} ); | ||
|
||
for ( const [ patternName, shouldBeAvailable ] of TEST_PATTERNS ) { | ||
it( `should${ | ||
shouldBeAvailable ? '' : ' not' | ||
} show test "pattern ${ patternName }"`, async () => { | ||
expect( | ||
await checkPatternExistence( | ||
patternName, | ||
shouldBeAvailable | ||
) | ||
).toBe( shouldBeAvailable ); | ||
} ); | ||
} | ||
} ); | ||
} ); |