From 7639ed87bfd99a13e8d943a9b7c314897aaf39fc Mon Sep 17 00:00:00 2001 From: Vipul Gupta <55375170+vipul0425@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:44:36 +0530 Subject: [PATCH] Blocks: Adds check for parent type before showing convert to pattern button (#66158) Co-authored-by: vipul0425 Co-authored-by: gziolo Co-authored-by: ntsekouras Co-authored-by: Mamaduka Co-authored-by: mtias Co-authored-by: aristath Co-authored-by: jordesign Co-authored-by: fabiankaegy --- .../src/components/pattern-convert-button.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/patterns/src/components/pattern-convert-button.js b/packages/patterns/src/components/pattern-convert-button.js index d670cd85946aa9..4573a6a5de4e82 100644 --- a/packages/patterns/src/components/pattern-convert-button.js +++ b/packages/patterns/src/components/pattern-convert-button.js @@ -6,6 +6,7 @@ import { isReusableBlock, createBlock, serialize, + getBlockType, } from '@wordpress/blocks'; import { store as blockEditorStore } from '@wordpress/block-editor'; import { useState, useCallback } from '@wordpress/element'; @@ -60,6 +61,15 @@ export default function PatternConvertButton( { const blocks = getBlocksByClientId( clientIds ) ?? []; + // Check if the block has reusable support defined. + const hasReusableBlockSupport = ( blockName ) => { + const blockType = getBlockType( blockName ); + const hasParent = blockType && 'parent' in blockType; + + // If the block has a parent, check with false as default, otherwise with true. + return hasBlockSupport( blockName, 'reusable', ! hasParent ); + }; + const isReusable = blocks.length === 1 && blocks[ 0 ] && @@ -82,7 +92,7 @@ export default function PatternConvertButton( { // Hide on invalid blocks. block.isValid && // Hide when block doesn't support being made into a pattern. - hasBlockSupport( block.name, 'reusable', true ) + hasReusableBlockSupport( block.name ) ) && // Hide when current doesn't have permission to do that. // Blocks refers to the wp_block post type, this checks the ability to create a post of that type.