Skip to content

Commit

Permalink
Rename to Widget Group, render before_title and after_title dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Aug 12, 2021
1 parent ff602a7 commit 6db4b04
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
14 changes: 4 additions & 10 deletions packages/widgets/src/blocks/widget-box/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
useBlockProps,
InnerBlocks,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
Expand All @@ -24,14 +23,9 @@ export default function Edit( {

const { innerBlocks } = getBlock( clientId );

const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-widget-box__inner-blocks',
},
{
renderAppender: InnerBlocks.ButtonBlockAppender,
}
);
const innerBlocksProps = useInnerBlocksProps( {
className: 'wp-widget-box__inner-blocks',
} );

const blockProps = useBlockProps();

Expand All @@ -56,7 +50,7 @@ export default function Edit( {
className="widget-title"
tagName="h2"
aria-label={ __( 'Widget title' ) }
placeholder={ __( 'Add a Widget title' ) }
placeholder={ __( 'Add title' ) }
value={ attributes.title }
onChange={ ( value ) => setAttributes( { title: value } ) }
onReplace={ onReplace }
Expand Down
4 changes: 3 additions & 1 deletion packages/widgets/src/blocks/widget-box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { group as icon } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -15,8 +16,9 @@ const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Widget Box' ),
title: __( 'Widget Group' ),
description: __( 'A widget container.' ),
icon,
__experimentalLabel: ( { name: label } ) => label,
edit,
save,
Expand Down
39 changes: 29 additions & 10 deletions packages/widgets/src/blocks/widget-box/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@


function render_block_core_widget_box( $attributes, $content, $block ) {
$inner_blocks = $block->inner_blocks;
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( $inner_blocks ) ) {
return '';
if ( ! empty( $attributes['title'] ) ) {
$html .= $before_title . $attributes['title'] . $after_title;
}

$inner_blocks_html = '';
$html .= '<div class="wp-widget-box__inner-blocks">';
foreach ( $block->inner_blocks as $inner_block ) {
$inner_blocks_html .= $inner_block->render();
$html .= $inner_block->render();
}
$html .= '</div>';

return sprintf(
'<div>%1$s</div>',
$inner_blocks_html
);

return $html;
}

/**
Expand All @@ -38,3 +45,15 @@ function register_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' );

0 comments on commit 6db4b04

Please sign in to comment.