File size: 1,867 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { createBlock } from '@wordpress/blocks';
import { select, dispatch } from '@wordpress/data';
import { isEditorReadyWithBlocks } from '../../utils';

/**
 * Checks if a given block object has content.
 *
 * Makes sure that we don't delete content when block auto-fix gives us empty
 * block content. For example, if a paragraph block structure is invalid, its
 * content attribute may be an empty string. However, depending on the error,
 * the block could still be fixed via the code editor. This way, we keep blocks
 * which do not auto-fix into a somewhat reasonable shape so that they can be
 * manually fixed.
 *
 * Note that we return 'true' if we don't understand how to validate the block.
 * This way, we continue auto-fixing other blocks if we can.
 * @param {Object} block The block to check for content.
 * @returns bool True if the block has content. False otherwise.
 */
function blockHasContent( block ) {
	// There is no content if there is no block.
	if ( ! block ) {
		return false;
	}
	switch ( block.name ) {
		case 'core/paragraph':
			return block.attributes?.content?.length > 0;
		case 'core/image':
			return block.attributes?.url?.length > 0;
		case 'core/quote':
			return block.attributes?.value?.length > 0;
		default:
			return true;
	}
}

async function fixInvalidBlocks() {
	const editorHasBlocks = await isEditorReadyWithBlocks();
	if ( ! editorHasBlocks ) {
		return;
	}

	// If any blocks have validation issues auto-fix them for now, until core is less strict.
	select( 'core/editor' )
		.getBlocks()
		.filter( ( block ) => ! block.isValid )
		.forEach( ( { clientId, name, attributes, innerBlocks } ) => {
			const replacement = createBlock( name, attributes, innerBlocks );
			if ( blockHasContent( replacement ) ) {
				dispatch( 'core/editor' ).replaceBlock( clientId, replacement );
			}
		} );
}

fixInvalidBlocks();