Skip to content

Commit

Permalink
Refactor blocks for core loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jul 31, 2024
1 parent 5519d7e commit f6726e8
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 180 deletions.
4 changes: 4 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This page lists the blocks included in the block-library package.
Accordion item content ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-content))

- **Name:** core/accordion-content
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** border, color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight)
Expand All @@ -23,6 +24,7 @@ Accordion item content ([Source](https://github.com/WordPress/gutenberg/tree/tru
A group of headers and associated expandable content. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-group))

- **Name:** core/accordion-group
- **Experimental:** true
- **Category:** design
- **Allowed Blocks:** core/accordion-item
- **Supports:** align (full, wide), background (backgroundImage, backgroundSize), color (background, gradient, text), interactivity, layout, shadow, spacing (blockGap, margin, padding), ~~html~~
Expand All @@ -33,6 +35,7 @@ A group of headers and associated expandable content. ([Source](https://github.c
A single accordion that displays a header and expandable content. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-item))

- **Name:** core/accordion-item
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-group
- **Allowed Blocks:** core/accordion-trigger, core/accordion-content
Expand All @@ -44,6 +47,7 @@ A single accordion that displays a header and expandable content. ([Source](http
Accordion item trigger. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/accordion-trigger))

- **Name:** core/accordion-trigger
- **Experimental:** true
- **Category:** design
- **Parent:** core/accordion-item
- **Supports:** anchor, border, color (background, gradient, text), interactivity, layout, shadow, spacing (margin, padding), typography (fontSize, textAlign), ~~align~~
Expand Down
6 changes: 2 additions & 4 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ function gutenberg_reregister_core_block_types() {
$blocks_dirs = array(
__DIR__ . '/../build/block-library/blocks/' => array(
'block_folders' => array(
'accordion-content',
'accordion-group',
'accordion-item',
'accordion-trigger',
'accordion-content',
'audio',
'button',
'buttons',
Expand Down Expand Up @@ -49,8 +47,8 @@ function gutenberg_reregister_core_block_types() {
'embed',
),
'block_names' => array(
'accordion-group.php' => 'core/accordion-group',
'accordion-item.php' => 'core/accordion-item',
'accordion-group.php' => 'core/accordion-group',
'archives.php' => 'core/archives',
'avatar.php' => 'core/avatar',
'block.php' => 'core/block',
Expand Down
8 changes: 7 additions & 1 deletion lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-patterns-tab', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutPatternsTab = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-bindings-ui', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalBlockBindingsUI = true', 'before' );
}
}

add_action( 'admin_init', 'gutenberg_enable_experiments' );
Expand Down Expand Up @@ -61,4 +67,4 @@ function gutenberg_enable_block_experiments() {
}
}

add_action( 'admin_init', 'gutenberg_enable_block_experiments' );
add_action( 'admin_init', 'gutenberg_enable_block_experiments' );
2 changes: 1 addition & 1 deletion lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function gutenberg_initialize_experiments_settings() {
'id' => 'gutenberg-block-experiments',
)
);

add_settings_field(
'gutenberg-form-blocks',
__( 'Form and input blocks ', 'gutenberg' ),
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/accordion-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"category": "design",
"description": "Accordion item content",
"example": {},
"__experimental": true,
"parent": [ "core/accordion-item" ],
"supports": {
"color": {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/accordion-content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SVG, Path } from '@wordpress/components';
/**
* Internal dependencies
*/
import Edit from './edit';
import edit from './edit';
import save from './save';
import metadata from './block.json';
import initBlock from '../utils/init-block';
Expand Down Expand Up @@ -34,7 +34,7 @@ export { metadata, name };
export const settings = {
icon,
example: {},
Edit,
edit,
save,
};

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/accordion-group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "core/accordion-group",
"version": "0.1.0",
"title": "Accordion Group",
"category": "design",
"description": "A group of headers and associated expandable content.",
"example": {},
"__experimental": true,
"supports": {
"html": false,
"align": [ "wide", "full" ],
Expand Down Expand Up @@ -45,5 +45,5 @@
},
"allowedBlocks": [ "core/accordion-item" ],
"textdomain": "default",
"viewScriptModule": "file:./view.min.js"
"style": "wp-block-accordion-group"
}
4 changes: 2 additions & 2 deletions packages/block-library/src/accordion-group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SVG, Path } from '@wordpress/components';
/**
* Internal dependencies
*/
import Edit from './edit';
import edit from './edit';
import save from './save';
import metadata from './block.json';
import initBlock from '../utils/init-block';
Expand Down Expand Up @@ -42,7 +42,7 @@ export { metadata, name };
export const settings = {
icon,
example: {},
Edit,
edit,
save,
};

Expand Down
63 changes: 39 additions & 24 deletions packages/block-library/src/accordion-group/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,43 @@
*
* @package WordPress
* @since 6.6.0
*
*
* @param array $attributes The block attributes.
* @param string $content The block content.
*
* @return string Returns the updated markup.
*/

function block_core_accordion_group_render( $attributes, $content ) {
if ( ! $content ) {
return $content;
}

$p = new WP_HTML_Tag_Processor( $content );
$autoclose = $attributes['autoclose'];

while ( $p->next_tag() ){
if ( $p->has_class( 'wp-block-accordion-group') ) {
$p->set_attribute( 'data-wp-interactive', 'core/accordion' );
$p->set_attribute( 'data-wp-context', '{"isOpen":[],"autoclose":"' . $autoclose . '"}' );
}
}

return $p->get_updated_html();
function render_block_core_accordion_group( $attributes, $content ) {
$suffix = wp_scripts_get_suffix();
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
$module_url = gutenberg_url( '/build/interactivity/accordionGroup.min.js' );
}

wp_register_script_module(
'@wordpress/block-library/accordion-group',
isset( $module_url ) ? $module_url : includes_url( "blocks/accordion-group/view{$suffix}.js" ),
array( '@wordpress/interactivity' ),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);

wp_enqueue_script_module( '@wordpress/block-library/accordion-group' );

if ( ! $content ) {
return $content;
}

$p = new WP_HTML_Tag_Processor( $content );
$autoclose = $attributes['autoclose'];

while ( $p->next_tag() ) {
if ( $p->has_class( 'wp-block-accordion-group' ) ) {
$p->set_attribute( 'data-wp-interactive', 'core/accordion' );
$p->set_attribute( 'data-wp-context', '{"isOpen":[],"autoclose":"' . $autoclose . '"}' );
}
}

return $p->get_updated_html();
}

/**
Expand All @@ -35,10 +49,11 @@ function block_core_accordion_group_render( $attributes, $content ) {
* @since 6.6.0
*/
function register_block_core_accordion_group() {
register_block_type_from_metadata(
__DIR__ . '/accordion-group',
array(
'render_callback' => 'block_core_accordion_group_render',
)
);
}
register_block_type_from_metadata(
__DIR__ . '/accordion-group',
array(
'render_callback' => 'render_block_core_accordion_group',
)
);
}
add_action( 'init', 'register_block_core_accordion_group' );
17 changes: 9 additions & 8 deletions packages/block-library/src/accordion-group/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { iconPosition } = attributes;

const className = clsx( {
'icon-position-left': iconPosition === 'left',
} );
const blockProps = useBlockProps.save();
const className = clsx(
{
'icon-position-left': iconPosition === 'left',
},
blockProps.className
);

return (
<div { ...useBlockProps.save( { className } ) }>
<InnerBlocks.Content />
</div>
<div { ...useInnerBlocksProps.save( { ...blockProps, className } ) } />
);
}
Loading

0 comments on commit f6726e8

Please sign in to comment.