Skip to content

Commit

Permalink
Image block: UI updates for the image lightbox (redo) (#54509)
Browse files Browse the repository at this point in the history
* Begin removing theme.json dependency in block UI

* Remove the useHasBehaviorsPanel hook

* Fix  declaration to actually retrieve from user data

* Restructured use of global behaviors

Simplified the `__experimentalUseGlobalBehaviors` function by removing the 'source' parameter. Now, the function directly uses 'userConfig' for getting raw data and 'mergedConfig' for variable value.

* More removal of behaviors.

* Remove the reference to behaviors in Global styles and first iteration of updates to the lightbox UI

* Remove behaviors altogether and everywhere

* Fix linter & sniffer PHP errors

* Adjust schema properties count assertion

Previously, the test was checking whether schema properties array had exactly 10 elements
We're now checking for exactly 9 elements instead.

* Add initial implementation of image settings panel

* Add the lightbox attribute

* Remove unnecessary space

* Remove unnecessary code and fix reset functionality

* Add UI to image block inspector

* Add `lightbox` to the valid `theme.json` settings

* Fix a bug with image selector and integrate with global styles

* Update `theme.json` schema

* Added the `@since`annotation

* Refactor image settings panel and screen block

Simplified the ImageSettingsPanel and ScreenBlock components. More specific changes:
 - Removed `name` and `settings` from the ImageSettingsPanel
 - Use `userSettings` instead of `settings` in the ImageSettingsPanel
 - Modified `onChangeLightbox` logic, it now takes a new setting instead of a boolean and directly passes to `onChange`
 - Updated the ScreenBlock component to account for this refactor

* Add showUI option to lightbox settings

A new option has been added to the lightbox settings in the Theme JSON reference guide and Gutenberg class. This `showUI` option allows users to toggle whether the Lightbox UI is displayed in the block editor or not. Also, updated the JSON schema accordingly to reflect these changes in theme.json files.

* Add defaults for the `lightbox` to the GB `theme.json`

* Change the falsy checks in image.js

* Add filters; add legacy support for behaviors syntax

I moved the logic to determine whether the lightbox should display
or not to two render_block_data filters.

One of these filters is inside of the index.php
so that itc can exist in WP core, the other inside of blocks.php
in order to offer legacy support for the Behaviors syntax in
the Gutenberg plugin.

Using the render_block_data instead of render_block allows us to
store a 'lightboxEnabled' value on the block, which we can use to
determine whether the lightbox should be rendered in these two
separate locations relatively cleanly without needing to touch
the markup.

I added behaviors back to the valid top-level keys so that we can
read it to offer legacy support.

Lastly, I set the lightbox.enabled attribute to NULL by default
so that we can determine whether the Behaviors syntax should override
it or not.

* Fix linter errors & add more expansive comments.

* If no value is set for the lightbox in the Global Styles, then the block editor UI should inherit the value from `theme.json`.

Likewise, if no value is set in the block attributes, the block editor UI should inherit the value from the Global Styles of the Image.

* Rename `showUI` to `allowEditing`

* Fix the `theme.json` schema

* Use the globalPath for the settings on the PHP side

* Add backwards support for enabling fade animation via the legacy syntax

* Fix PHP linter errors

* Fix error when checking lightbox['enabled'] value in global settings

* Empty commit

* Remove the default `false` value for `lightbox.enabled`attribute.

* Add deprecation notice for 'behaviors' in image block

I needed to add 'behaviors' back to the block.json attributes
in order to read them on the JavaScript side in the editor
to fire the deprecation notice.

* Add clarifying comment regarding skipped tests

* Do not remove `behaviors` attribute from Image block's block.json.
Behaviors are deprecated for 2 more releases and will be removed then.

* Revert "Do not remove `behaviors` attribute from Image block's block.json."

This reverts commit cabe31b.

* Add deprecation for  attribute in image block

* Remove obsolete code now that block deprecation is in place

* Add support for 'lightbox: true' syntax

* Fix lightbox 'checked' attribute being read improperly

* Add conditional display for settings panel at image block level

* Fix an error with the theme.json schema.

* Update docs with `npm run build:docs`

* Copy `class-wp-theme-json-schema.php` from core
 into `class-wp-theme-json-schema-gutenberg.php`

* Revert deletion of behaviors.php

* Add a theme.json migration to v3 away from behaviors and to a new, simpler syntax used by the lightbox.

* Remove the `null` value from lightbox.enabled in `lib/theme.json`

* Remove `behaviors` from VALID_TOP_LEVEL_KEYS & VALID_SETTINGS

* Revise backwards compatibility for behaviors; add deprecation to block_supports

* Remove outdated comment

* Update outdated comment

* Update comment and shuffle the lines so the diff is easier on the eyes.

* Update comment to explain why we use userSettings in image-settings-panel.js

* Remove support for legacy fade configuration

* Resolve lint error

* Add clarifying comment regarding lightbox markup

* Rename the migrate function to reflect that it's not a v3 migration

* Add a `@since` in `gutenberg_should_render_lightbox` docblock

* Add support for reading top-level 'lightbox' setting in editor

By default, we read the lightbox settings underneath the 'core/image'
in theme.json; however, the 'enabled' property there is undefined by default,
which means it should be possible to declare a top-level setting for the lightbox
that overrides an undefined block-level setting.

While this appeared to be working on the PHP side, the UI wasn't accurately
reflecting this inheritance structure, so this commit fixes that.

Users should now be able to define a top-level lightbox setting as either
'lightbox: true' or 'lightbox: { enabled: true }' that will be used
if the block-level lightbox setting for 'enabled' is undefined.

* Revert "Add support for reading top-level 'lightbox' setting in editor"

This reverts commit 2f5f122.

* Add correct deprecation mentioning the Gutenberg version

* Move 'allowEditing' to top-level settings

* Fix top-level lightbox setting not being read properly

* Fix 'false' values in theme.json not being stored in settings object

* Fix error wherein 'undefined' was being passed to input component

* Fix bug wherein lightbox UI would disappear if user value was set

* Remove inheritance when determining whether to enable lightbox

Rather than trying to check if the 'enabled' has been set or not
and falling back to other levels of the theme.json inheritance
structure, I decided to just read and use the settings as defined
in theme.json. This is the expected behavior in Gutenberg from
what I understand and has less edge cases.

* Update comment

* Update whitespace in theme.json

Co-authored-by: Alex Lende <[email protected]>

* Add docblocks to clarify that `class-wp-theme-json-schema-gutenberg.php` is only put in GB as a temporary migration.

* Add comments to clarify that behaviors.php is temporarily added to GB an will be removed in a future version.

* Added integration fixtures for the lightbox

* Fix incorrect reading of global lightbox settings

* Clarify the comment getting the global lightbox settings

* Fix PHPCS parenthesis error 🤦‍♂️

* Move lightbox settings to the block level

* Remove support for shorthand

* Remove 'lightbox' from hooks.js and add `.enabled` & `.allowEditing` to class-wp-theme-json-gutenberg.php

* Fix another phpcs error

* Fix phpcs error

---------

Co-authored-by: Michal Czaplinski <[email protected]>
Co-authored-by: Alex Lende <[email protected]>
  • Loading branch information
3 people authored Sep 15, 2023
1 parent fa33487 commit 900439e
Show file tree
Hide file tree
Showing 21 changed files with 758 additions and 50 deletions.
9 changes: 4 additions & 5 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

This page lists the blocks included in the block-library package.

- Items marked with a strikeout (~~strikeout~~) are explicitly disabled.
- Blocks marked with **Experimental:** true are only available when Gutenberg is active.
- Blocks marked with **Experimental:** fse are only available in the Site Editor.

- Items marked with a strikeout (~~strikeout~~) are explicitly disabled.
- Blocks marked with **Experimental:** true are only available when Gutenberg is active.
- Blocks marked with **Experimental:** fse are only available in the Site Editor.

<!-- START TOKEN Autogenerated - DO NOT EDIT -->

Expand Down Expand Up @@ -340,7 +339,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments

Expand Down
11 changes: 11 additions & 0 deletions docs/reference-guides/theme-json-reference/theme-json-living.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ Settings related to layout.

---

### lightbox

Settings related to the lightbox.

| Property | Type | Default | Props |
| --- | --- | --- |--- |
| enabled | boolean | | |
| allowEditing | boolean | | |

---

### position

Settings related to position.
Expand Down
16 changes: 15 additions & 1 deletion lib/block-supports/behaviors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
/**
* Behaviors block support flag.
*
* This file will NOT be backported to Core. It exists to provide a
* migration path for theme.json files that used the deprecated "behaviors".
* This file will be removed from Gutenberg in version 17.0.0.
*
* @package gutenberg
*/

Expand Down Expand Up @@ -37,13 +41,23 @@ function gutenberg_register_behaviors_support( $block_type ) {

/**
* Add the directives and layout needed for the lightbox behavior.
* This functions shouldn't be in this file. It should be moved to a package (or somewhere else), where all the behaviors logic is defined.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {

// We've deprecated the lightbox implementation via behaviors.
// While we may continue to explore behaviors in the future, the lightbox
// logic seems very specific to the image and will likely never be a part
// of behaviors, even in the future. With that in mind, we've rewritten the lightbox
// to be a feature of the image block and will also soon remove the block_supports.
// *Note: This logic for generating the lightbox markup has been duplicated and moved
// to the image block's index.php.*
// See https://github.com/WordPress/gutenberg/issues/53403.
_deprecated_function( 'gutenberg_render_behaviors_support_lightbox', 'Gutenberg 17.0.0', '' );

$link_destination = isset( $block['attrs']['linkDestination'] ) ? $block['attrs']['linkDestination'] : 'none';
// Get the lightbox setting from the block attributes.
if ( isset( $block['attrs']['behaviors']['lightbox'] ) ) {
Expand Down
29 changes: 29 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,33 @@ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $si

return $value;
}

add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );

/**
* Complements the lightbox implementation for the 'core/image' block.
*
* This function is INTENTIONALLY left out of core as it only provides
* backwards compatibility for the legacy lightbox syntax that was only
* introduced in Gutenberg. The legacy syntax was using the `behaviors` key in
* the block attrbutes and the `theme.json` file.
*
* @since 16.7.0
*
* @param array $block The block to check.
* @return array The block with the legacyLightboxSettings set if available.
*/
function gutenberg_should_render_lightbox( $block ) {

if ( 'core/image' !== $block['blockName'] ) {
return $block;
}

if ( isset( $block['attrs']['behaviors']['lightbox'] ) ) {
$block['legacyLightboxSettings'] = $block['attrs']['behaviors']['lightbox'];
}

return $block;
}

add_filter( 'render_block_data', 'gutenberg_should_render_lightbox', 15, 1 );
9 changes: 7 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ class WP_Theme_JSON_Gutenberg {
* `position.fixed` and `position.sticky`.
* @since 6.3.0 Removed `layout.definitions`. Added `typography.writingMode`.
* @since 6.4.0 Added `layout.allowEditing`.
* @since 6.4.0 Added `lightbox`.
* @var array
*/
const VALID_SETTINGS = array(
Expand Down Expand Up @@ -386,6 +387,10 @@ class WP_Theme_JSON_Gutenberg {
'wideSize' => null,
'allowEditing' => null,
),
'lightbox' => array(
'enabled' => null,
'allowEditing' => null,
),
'position' => array(
'fixed' => null,
'sticky' => null,
Expand Down Expand Up @@ -616,7 +621,7 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) {
$origin = 'theme';
}

$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json );
$registry = WP_Block_Type_Registry::get_instance();
$valid_block_names = array_keys( $registry->get_all_registered() );
$valid_element_names = array_keys( static::ELEMENTS );
Expand Down Expand Up @@ -2859,7 +2864,7 @@ protected static function filter_slugs( $node, $slugs ) {
public static function remove_insecure_properties( $theme_json ) {
$sanitized = array();

$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
$theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json );

$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
Expand Down
202 changes: 202 additions & 0 deletions lib/class-wp-theme-json-schema-gutenberg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* WP_Theme_JSON_Schema_Gutenberg class
*
* This class/file will NOT be backported to Core. It exists to provide a
* migration path for theme.json files that used the deprecated "behaviors".
* This file will be removed from Gutenberg in version 17.0.0.
*
* @package gutenberg
* @since 16.7.0
*/

if ( class_exists( 'WP_Theme_JSON_Schema_Gutenberg' ) ) {
return;
}

/**
* Class that migrates a given theme.json structure to the latest schema.
*
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
* This is a low-level API that may need to do breaking changes. Please,
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
*
* @since 5.9.0
* @access private
*/
#[AllowDynamicProperties]
class WP_Theme_JSON_Schema_Gutenberg {

/**
* Maps old properties to their new location within the schema's settings.
* This will be applied at both the defaults and individual block levels.
*/
const V1_TO_V2_RENAMED_PATHS = array(
'border.customRadius' => 'border.radius',
'spacing.customMargin' => 'spacing.margin',
'spacing.customPadding' => 'spacing.padding',
'typography.customLineHeight' => 'typography.lineHeight',
);

/**
* Function that migrates a given theme.json structure to the last version.
*
* @since 5.9.0
*
* @param array $theme_json The structure to migrate.
*
* @return array The structure in the last version.
*/
public static function migrate( $theme_json ) {
if ( ! isset( $theme_json['version'] ) ) {
$theme_json = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
);
}

if ( 1 === $theme_json['version'] ) {
$theme_json = self::migrate_v1_to_v2( $theme_json );
}

if ( 2 === $theme_json['version'] ) {
$theme_json = self::migrate_deprecated_lightbox_behaviors( $theme_json );
}

return $theme_json;
}

/**
* Removes the custom prefixes for a few properties
* that were part of v1:
*
* 'border.customRadius' => 'border.radius',
* 'spacing.customMargin' => 'spacing.margin',
* 'spacing.customPadding' => 'spacing.padding',
* 'typography.customLineHeight' => 'typography.lineHeight',
*
* @since 5.9.0
*
* @param array $old Data to migrate.
*
* @return array Data without the custom prefixes.
*/
private static function migrate_v1_to_v2( $old ) {
// Copy everything.
$new = $old;

// Overwrite the things that changed.
if ( isset( $old['settings'] ) ) {
$new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS );
}

// Set the new version.
$new['version'] = 2;

return $new;
}


/**
* Migrate away from the previous syntax that used a top-level "behaviors" key
* in the `theme.json` to a new "lightbox" setting.
*
* This function SHOULD NOT be ported to Core!!!
*
* It is a temporary migration that will be removed in Gutenberg 17.0.0
*
* @since 16.7.0
*
* @param array $old Data with (potentially) behaviors.
* @return array Data with behaviors removed.
*/
private static function migrate_deprecated_lightbox_behaviors( $old ) {
// Copy everything.
$new = $old;

// Migrate the old behaviors syntax to the new "lightbox" syntax.
if ( isset( $old['behaviors']['blocks']['core/image']['lightbox']['enabled'] ) ) {
_wp_array_set(
$new,
array( 'settings', 'blocks', 'core/image', 'lightbox', 'enabled' ),
$old['behaviors']['blocks']['core/image']['lightbox']['enabled']
);
}

// Migrate the behaviors setting to the new syntax. This setting controls
// whether the Lightbox UI shows up in the block editor.
if ( isset( $old['settings']['blocks']['core/image']['behaviors']['lightbox'] ) ) {
_wp_array_set(
$new,
array( 'settings', 'blocks', 'core/image', 'lightbox', 'allowEditing' ),
$old['settings']['blocks']['core/image']['behaviors']['lightbox']
);
}

return $new;
}

/**
* Processes the settings subtree.
*
* @since 5.9.0
*
* @param array $settings Array to process.
* @param array $paths_to_rename Paths to rename.
*
* @return array The settings in the new format.
*/
private static function rename_paths( $settings, $paths_to_rename ) {
$new_settings = $settings;

// Process any renamed/moved paths within default settings.
self::rename_settings( $new_settings, $paths_to_rename );

// Process individual block settings.
if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) {
foreach ( $new_settings['blocks'] as &$block_settings ) {
self::rename_settings( $block_settings, $paths_to_rename );
}
}

return $new_settings;
}

/**
* Processes a settings array, renaming or moving properties.
*
* @since 5.9.0
*
* @param array $settings Reference to settings either defaults or an individual block's.
* @param array $paths_to_rename Paths to rename.
*/
private static function rename_settings( &$settings, $paths_to_rename ) {
foreach ( $paths_to_rename as $original => $renamed ) {
$original_path = explode( '.', $original );
$renamed_path = explode( '.', $renamed );
$current_value = _wp_array_get( $settings, $original_path, null );

if ( null !== $current_value ) {
_wp_array_set( $settings, $renamed_path, $current_value );
self::unset_setting_by_path( $settings, $original_path );
}
}
}

/**
* Removes a property from within the provided settings by its path.
*
* @since 5.9.0
*
* @param array $settings Reference to the current settings array.
* @param array $path Path to the property to be removed.
*/
private static function unset_setting_by_path( &$settings, $path ) {
$tmp_settings = &$settings; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$last_key = array_pop( $path );
foreach ( $path as $key ) {
$tmp_settings = &$tmp_settings[ $key ];
}

unset( $tmp_settings[ $last_key ] );
}
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/global-styles-and-settings.php';
require __DIR__ . '/class-wp-theme-json-data-gutenberg.php';
require __DIR__ . '/class-wp-theme-json-gutenberg.php';
require __DIR__ . '/class-wp-theme-json-schema-gutenberg.php';
require __DIR__ . '/class-wp-theme-json-resolver-gutenberg.php';
require __DIR__ . '/class-wp-duotone-gutenberg.php';
require __DIR__ . '/blocks.php';
Expand Down
5 changes: 5 additions & 0 deletions lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
"radius": true
}
},
"core/image": {
"lightbox": {
"allowEditing": true
}
},
"core/pullquote": {
"border": {
"color": true,
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const VALID_SETTINGS = [
'layout.contentSize',
'layout.definitions',
'layout.wideSize',
'lightbox.enabled',
'lightbox.allowEditing',
'position.fixed',
'position.sticky',
'spacing.customSpacingSize',
Expand Down
Loading

0 comments on commit 900439e

Please sign in to comment.