-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Experimental: try new "Widget Group" block to act as container for Widgets #33881
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
528568b
Register new Widget box block for Widgets screen only
getdave 64d5bdc
Better mirror legacy markup for Widgets
getdave a8b172e
Remove hardcoded class as not needed
getdave 43372b8
Move Widget Box block to widgets package and update registration
getdave 765c72a
Register block in Widget Customizer and allow to appear
getdave ceb16cf
Utilise block appender style
getdave 3f89a7f
Remove unnecessary /edit subfolder
getdave f0fef9b
Add transform for any block to allow it to be wrapped in a Widget Box
getdave 1c5d054
Initial attempt at RichText
getdave 1a64c57
Rename misnamed method on filter
getdave c692d54
Add onReplace and identifier props to RichText in widget box block
talldan a8cac1b
Improving self documenting code for allowing only single line for Wid…
getdave 83d199d
Remove restriction to limit Widget Box to being child of Widget Area
getdave 69351c1
Rename `widgetTitle` to `title` and update the block.json
getdave 69a9ddc
Update transform to convert preexisting heading blocks to the widget …
getdave c83a223
Allow richtext to split without overwritng the Widget Box
getdave a9cd341
Attempt to make block dynamic
getdave 1babfd7
Register the WIdget Box server side
getdave ff602a7
Render inner blocks on server side
tellthemachines 6db4b04
Rename to Widget Group, render before_title and after_title dynamically
noisysocks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "core/widget-box", | ||
"category": "widgets", | ||
"attributes": { | ||
"title": { | ||
"type": "string" | ||
} | ||
}, | ||
"supports": { | ||
"html": false, | ||
"inserter": true, | ||
"customClassName": true, | ||
"reusable": false | ||
}, | ||
"editorStyle": "wp-block-widget-box-editor", | ||
"style": "wp-block-widget-box" | ||
} |
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,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export const HEADING_PLACEHOLDER = [ | ||
'core/heading', | ||
{ | ||
placeholder: __( 'Add your Widget title' ), | ||
className: 'widget-title', | ||
}, | ||
]; |
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,65 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalUseInnerBlocksProps as useInnerBlocksProps, | ||
useBlockProps, | ||
RichText, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
||
export default function Edit( { | ||
attributes, | ||
setAttributes, | ||
clientId, | ||
onReplace, | ||
mergeBlocks, | ||
} ) { | ||
const { getBlock } = useSelect( blockEditorStore ); | ||
const { replaceInnerBlocks } = useDispatch( blockEditorStore ); | ||
|
||
const { innerBlocks } = getBlock( clientId ); | ||
|
||
const innerBlocksProps = useInnerBlocksProps( { | ||
className: 'wp-widget-box__inner-blocks', | ||
} ); | ||
|
||
const blockProps = useBlockProps(); | ||
|
||
/** | ||
* Split RichText on ENTER by manually creating a new paragraph block | ||
* within the innerBlocks of the **existing** Widget Box block. | ||
* If we don't do this then RichText will be split into heading + para | ||
* thereby entirely removint the Widget Box block altogether. | ||
*/ | ||
function allowSingleLineOnly() { | ||
replaceInnerBlocks( | ||
clientId, | ||
[ createBlock( 'core/paragraph', {} ), ...innerBlocks ], | ||
true | ||
); | ||
} | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<RichText | ||
identifier="content" | ||
className="widget-title" | ||
tagName="h2" | ||
aria-label={ __( 'Widget title' ) } | ||
placeholder={ __( 'Add title' ) } | ||
value={ attributes.title } | ||
onChange={ ( value ) => setAttributes( { title: value } ) } | ||
onReplace={ onReplace } | ||
onMerge={ mergeBlocks } | ||
onSplit={ allowSingleLineOnly } | ||
onRemove={ () => onReplace( [] ) } | ||
{ ...blockProps } | ||
/> | ||
<div { ...innerBlocksProps } /> | ||
</div> | ||
); | ||
} |
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,78 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { group as icon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
// eslint-disable-next-line no-unused-vars | ||
import save from './save'; | ||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Widget Group' ), | ||
description: __( 'A widget container.' ), | ||
icon, | ||
__experimentalLabel: ( { name: label } ) => label, | ||
edit, | ||
save, | ||
transforms: { | ||
from: [ | ||
{ | ||
type: 'block', | ||
isMultiBlock: true, | ||
blocks: [ '*' ], | ||
__experimentalConvert( blocks ) { | ||
// Avoid transforming existing `widget-box` blocks. | ||
const blocksContainWidgetBox = !! blocks.filter( | ||
( block ) => block.name === 'core/widget-box' | ||
)?.length; | ||
|
||
if ( blocksContainWidgetBox ) { | ||
return; | ||
} | ||
|
||
// Put the selected blocks inside the new Widget Box's innerBlocks. | ||
let innerBlocks = [ | ||
...blocks.map( ( block ) => { | ||
return createBlock( | ||
block.name, | ||
block.attributes, | ||
block.innerBlocks | ||
); | ||
} ), | ||
]; | ||
|
||
// If the first block is a heading then assume this is intended | ||
// to be the Widget's "title". | ||
const firstHeadingBlock = | ||
innerBlocks[ 0 ].name === 'core/heading' | ||
? innerBlocks[ 0 ] | ||
: null; | ||
|
||
// Remove the first heading block as we're copying | ||
// it's content into the Widget Box's title attribute. | ||
innerBlocks = innerBlocks.filter( | ||
( block ) => block !== firstHeadingBlock | ||
); | ||
|
||
return createBlock( | ||
'core/widget-box', | ||
{ | ||
...( firstHeadingBlock && { | ||
title: firstHeadingBlock.attributes.content, | ||
} ), | ||
}, | ||
innerBlocks | ||
); | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
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,59 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/widget-box` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
|
||
function render_block_core_widget_box( $attributes, $content, $block ) { | ||
global $wp_registered_sidebars, $_sidebar_being_rendered; | ||
|
||
if ( isset( $wp_registered_sidebars[ $_sidebar_being_rendered ] ) ) { | ||
$before_title = $wp_registered_sidebars[ $_sidebar_being_rendered ]['before_title']; | ||
$after_title = $wp_registered_sidebars[ $_sidebar_being_rendered ]['after_title']; | ||
} else { | ||
$before_title = '<h2 class="widget-title">'; | ||
$after_title = '</h2>'; | ||
} | ||
|
||
$html = ''; | ||
|
||
if ( ! empty( $attributes['title'] ) ) { | ||
$html .= $before_title . $attributes['title'] . $after_title; | ||
} | ||
|
||
$html .= '<div class="wp-widget-box__inner-blocks">'; | ||
foreach ( $block->inner_blocks as $inner_block ) { | ||
$html .= $inner_block->render(); | ||
} | ||
$html .= '</div>'; | ||
|
||
return $html; | ||
} | ||
|
||
/** | ||
* Registers the 'core/widget-box' block. | ||
*/ | ||
function register_block_core_widget_box() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/widget-box', | ||
array( | ||
'render_callback' => 'render_block_core_widget_box', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', 'register_block_core_widget_box' ); | ||
|
||
function note_sidebar_being_rendered( $index ) { | ||
global $_sidebar_being_rendered; | ||
$_sidebar_being_rendered = $index; | ||
} | ||
add_action( 'dynamic_sidebar_before', 'note_sidebar_being_rendered' ); | ||
|
||
function discard_sidebar_being_rendered( $index ) { | ||
global $_sidebar_being_rendered; | ||
unset( $_sidebar_being_rendered ); | ||
} | ||
add_action( 'dynamic_sidebar_after', 'discard_sidebar_being_rendered' ); |
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,17 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks, RichText } from '@wordpress/block-editor'; | ||
|
||
export default function save( { attributes } ) { | ||
return ( | ||
<> | ||
<RichText.Content | ||
tagName="h2" | ||
className="widget-title" | ||
value={ attributes.title } | ||
/> | ||
<InnerBlocks.Content /> | ||
</> | ||
); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we allow this? I guess we ought to. There's no harm right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I would not allow HTML of blocks to be changed in any block in the entire widgets editor.