Skip to content

Commit

Permalink
Blocks: Adds check for parent type before showing convert to pattern …
Browse files Browse the repository at this point in the history
…button (WordPress#66158)

Co-authored-by: vipul0425 <vipulgupta003@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: mtias <matveb@git.wordpress.org>
Co-authored-by: aristath <aristath@git.wordpress.org>
Co-authored-by: jordesign <jordesign@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
  • Loading branch information
9 people authored Nov 20, 2024
1 parent 41f5eb2 commit 7639ed8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/patterns/src/components/pattern-convert-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ] &&
Expand All @@ -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.
Expand Down

0 comments on commit 7639ed8

Please sign in to comment.