Skip to content

Commit

Permalink
Editor: Add viewStyle property to block.json for frontend-only bl…
Browse files Browse the repository at this point in the history
…ock styles

Related issue in Gutenberg: WordPress/gutenberg#54491.

For block scripts there was already `script`, `viewScript` and `editorScript`. For block styles there was only `style` and `editorStyle`. This brings the parity.

Props gaambo.
Fixes #59673. 


Built from https://develop.svn.wordpress.org/trunk@57493


git-svn-id: http://core.svn.wordpress.org/trunk@56994 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
gziolo committed Jan 31, 2024
1 parent 2de0031 commit 9eea02e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
'viewScript' => 'view-script',
'editorStyle' => 'editor-style',
'style' => 'style',
'viewStyle' => 'view-style',
);
$asset_handle = str_replace( '/', '-', $block_name ) .
'-' . $field_mappings[ $field_name ];
Expand Down Expand Up @@ -326,6 +327,7 @@ function get_block_metadata_i18n_schema() {
* @since 6.1.0 Added support for `render` field.
* @since 6.3.0 Added `selectors` field.
* @since 6.4.0 Added support for `blockHooks` field.
* @since 6.5.0 Added support for `viewStyle` field.
*
* @param string $file_or_folder Path to the JSON file with metadata definition for
* the block or path to the folder where the `block.json` file is located.
Expand Down Expand Up @@ -503,6 +505,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$style_fields = array(
'editorStyle' => 'editor_style_handles',
'style' => 'style_handles',
'viewStyle' => 'view_style_handles',
);
foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
if ( ! empty( $settings[ $metadata_field_name ] ) ) {
Expand Down
10 changes: 10 additions & 0 deletions wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ class WP_Block_Type {
*/
public $style_handles = array();

/**
* Block type front end only style handles.
*
* @since 6.5.0
* @var string[]
*/
public $view_style_handles = array();

/**
* Deprecated block type properties for script and style handles.
*
Expand Down Expand Up @@ -278,6 +286,7 @@ class WP_Block_Type {
* Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
* @since 6.3.0 Added the `selectors` property.
* @since 6.4.0 Added the `block_hooks` property.
* @since 6.5.0 Added the `view_style_handles` property.
*
* @see register_block_type()
*
Expand Down Expand Up @@ -315,6 +324,7 @@ class WP_Block_Type {
* @type string[] $view_script_handles Block type front end only script handles.
* @type string[] $editor_style_handles Block type editor only style handles.
* @type string[] $style_handles Block type front end and editor style handles.
* @type string[] $view_style_handles Block type front end only style handles.
* }
*/
public function __construct( $block_type, $args = array() ) {
Expand Down
6 changes: 6 additions & 0 deletions wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ public function render( $options = array() ) {
}
}

if ( ( ! empty( $this->block_type->view_style_handles ) ) ) {
foreach ( $this->block_type->view_style_handles as $view_style_handle ) {
wp_enqueue_style( $view_style_handle );
}
}

/**
* Filters the content of a single block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public function prepare_item_for_response( $item, $request ) {
'view_script_handles',
'editor_style_handles',
'style_handles',
'view_style_handles',
'variations',
'block_hooks',
),
Expand Down Expand Up @@ -602,6 +603,16 @@ public function get_item_schema() {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'view_style_handles' => array(
'description' => __( 'Public facing style handles.' ),
'type' => array( 'array' ),
'default' => array(),
'items' => array(
'type' => 'string',
),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'styles' => array(
'description' => __( 'Block style variations.' ),
'type' => 'array',
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57492';
$wp_version = '6.5-alpha-57493';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 9eea02e

Please sign in to comment.