Skip to content
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

Image block: UI updates for the image lightbox (redo) #54509

Merged
merged 93 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
08bb1ed
Begin removing theme.json dependency in block UI
artemiomorales Aug 21, 2023
c93f1b6
Remove the useHasBehaviorsPanel hook
michalczaplinski Aug 21, 2023
25aa1c6
Fix declaration to actually retrieve from user data
artemiomorales Aug 21, 2023
a8c09c9
Restructured use of global behaviors
michalczaplinski Aug 21, 2023
fac2e3a
More removal of behaviors.
michalczaplinski Aug 28, 2023
dfc1c46
Remove the reference to behaviors in Global styles and first iteratio…
michalczaplinski Aug 28, 2023
51e4802
Remove behaviors altogether and everywhere
michalczaplinski Aug 29, 2023
b416558
Fix linter & sniffer PHP errors
michalczaplinski Aug 30, 2023
321a213
Adjust schema properties count assertion
michalczaplinski Aug 30, 2023
e613a5b
Add initial implementation of image settings panel
artemiomorales Aug 30, 2023
0021116
Add the lightbox attribute
michalczaplinski Aug 30, 2023
6951a86
Remove unnecessary space
michalczaplinski Aug 30, 2023
b0fb3b5
Remove unnecessary code and fix reset functionality
artemiomorales Aug 30, 2023
b76d56c
Add UI to image block inspector
artemiomorales Aug 30, 2023
1872f77
Add `lightbox` to the valid `theme.json` settings
michalczaplinski Aug 31, 2023
3cfcb15
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Aug 31, 2023
d2818af
Fix a bug with image selector and integrate with global styles
michalczaplinski Aug 31, 2023
a32d0f6
Update `theme.json` schema
michalczaplinski Sep 2, 2023
e67634e
Added the `@since`annotation
michalczaplinski Sep 2, 2023
3ddb1b1
Refactor image settings panel and screen block
michalczaplinski Sep 4, 2023
fae6f99
Add showUI option to lightbox settings
michalczaplinski Sep 4, 2023
df9f857
Add defaults for the `lightbox` to the GB `theme.json`
michalczaplinski Sep 4, 2023
df3f1c6
Change the falsy checks in image.js
michalczaplinski Sep 4, 2023
075535b
Add filters; add legacy support for behaviors syntax
artemiomorales Sep 5, 2023
c2a0b3f
Fix linter errors & add more expansive comments.
michalczaplinski Sep 5, 2023
91d130c
If no value is set for the lightbox in the Global Styles, then the bl…
michalczaplinski Sep 5, 2023
c58612c
Rename `showUI` to `allowEditing`
michalczaplinski Sep 5, 2023
c602139
Fix the `theme.json` schema
michalczaplinski Sep 5, 2023
339a715
Use the globalPath for the settings on the PHP side
michalczaplinski Sep 5, 2023
4b43b30
Add backwards support for enabling fade animation via the legacy syntax
artemiomorales Sep 6, 2023
ce3c338
Fix PHP linter errors
artemiomorales Sep 6, 2023
92b084f
Merge branch 'trunk' into update/revise-lightbox-ui
michalczaplinski Sep 6, 2023
7fefb83
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Sep 6, 2023
03e60d7
Fix error when checking lightbox['enabled'] value in global settings
artemiomorales Sep 6, 2023
050c636
Empty commit
michalczaplinski Sep 6, 2023
521d950
Remove the default `false` value for `lightbox.enabled`attribute.
michalczaplinski Sep 7, 2023
1db73c5
Add deprecation notice for 'behaviors' in image block
artemiomorales Sep 7, 2023
232161a
Add clarifying comment regarding skipped tests
artemiomorales Sep 7, 2023
cabe31b
Do not remove `behaviors` attribute from Image block's block.json.
michalczaplinski Sep 7, 2023
715e32c
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Sep 7, 2023
aa4450e
Revert "Do not remove `behaviors` attribute from Image block's block.…
michalczaplinski Sep 7, 2023
41f251d
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Sep 7, 2023
79dad34
Add deprecation for attribute in image block
artemiomorales Sep 9, 2023
871ba45
Remove obsolete code now that block deprecation is in place
artemiomorales Sep 10, 2023
7f2f70f
Add support for 'lightbox: true' syntax
artemiomorales Sep 11, 2023
6da3545
Fix lightbox 'checked' attribute being read improperly
artemiomorales Sep 11, 2023
91b86ef
Merge branch 'trunk' into update/revise-lightbox-ui
michalczaplinski Sep 11, 2023
83bef30
Add conditional display for settings panel at image block level
artemiomorales Sep 11, 2023
52e358d
Fix an error with the theme.json schema.
michalczaplinski Sep 11, 2023
11e96e2
Update docs with `npm run build:docs`
michalczaplinski Sep 11, 2023
8809612
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Sep 11, 2023
59e0dbe
Copy `class-wp-theme-json-schema.php` from core
michalczaplinski Sep 11, 2023
3eeb1a7
Merge branch 'update/lightbox-settings-panels' of github.com:WordPres…
michalczaplinski Sep 11, 2023
f9e48f4
Revert deletion of behaviors.php
artemiomorales Sep 11, 2023
443aa5b
Add a theme.json migration to v3 away from behaviors and to a new, si…
michalczaplinski Sep 11, 2023
6459d56
Remove the `null` value from lightbox.enabled in `lib/theme.json`
michalczaplinski Sep 11, 2023
203649d
Remove `behaviors` from VALID_TOP_LEVEL_KEYS & VALID_SETTINGS
michalczaplinski Sep 11, 2023
5a37499
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
artemiomorales Sep 11, 2023
0e63890
Revise backwards compatibility for behaviors; add deprecation to bloc…
artemiomorales Sep 11, 2023
8b721a7
Remove outdated comment
michalczaplinski Sep 12, 2023
8287c46
Update outdated comment
michalczaplinski Sep 12, 2023
a2c2b94
Update comment and shuffle the lines so the diff is easier on the eyes.
michalczaplinski Sep 12, 2023
4f0709b
Update comment to explain why we use userSettings in image-settings-p…
michalczaplinski Sep 12, 2023
7018ee7
Remove support for legacy fade configuration
artemiomorales Sep 12, 2023
7fd0801
Resolve lint error
artemiomorales Sep 12, 2023
5bb05bc
Add clarifying comment regarding lightbox markup
artemiomorales Sep 12, 2023
a02910c
Rename the migrate function to reflect that it's not a v3 migration
michalczaplinski Sep 12, 2023
b12d386
Add a `@since` in `gutenberg_should_render_lightbox` docblock
michalczaplinski Sep 12, 2023
2f5f122
Add support for reading top-level 'lightbox' setting in editor
artemiomorales Sep 12, 2023
a238d94
Revert "Add support for reading top-level 'lightbox' setting in editor"
artemiomorales Sep 13, 2023
0aba3c4
Add correct deprecation mentioning the Gutenberg version
michalczaplinski Sep 13, 2023
8fa991e
Move 'allowEditing' to top-level settings
artemiomorales Sep 13, 2023
65b173c
Fix top-level lightbox setting not being read properly
artemiomorales Sep 13, 2023
b5f8186
Fix 'false' values in theme.json not being stored in settings object
artemiomorales Sep 13, 2023
7e121da
Fix error wherein 'undefined' was being passed to input component
artemiomorales Sep 13, 2023
fb10384
Fix bug wherein lightbox UI would disappear if user value was set
artemiomorales Sep 13, 2023
ef8f43e
Remove inheritance when determining whether to enable lightbox
artemiomorales Sep 13, 2023
fc8fc47
Update comment
artemiomorales Sep 13, 2023
6ea39f8
Update whitespace in theme.json
michalczaplinski Sep 14, 2023
aaab1ef
Add docblocks to clarify that `class-wp-theme-json-schema-gutenberg.p…
michalczaplinski Sep 14, 2023
61bfb3d
Add comments to clarify that behaviors.php is temporarily added to GB…
michalczaplinski Sep 14, 2023
c18e283
Added integration fixtures for the lightbox
michalczaplinski Sep 14, 2023
8b95f4a
Merge branch 'trunk' into update/revise-lightbox-ui
michalczaplinski Sep 14, 2023
7d30aa6
Merge branch 'update/revise-lightbox-ui' into update/lightbox-setting…
michalczaplinski Sep 14, 2023
dab7f5a
Fix incorrect reading of global lightbox settings
artemiomorales Sep 14, 2023
a5c2f0c
Clarify the comment getting the global lightbox settings
michalczaplinski Sep 14, 2023
485c197
Fix PHPCS parenthesis error 🤦‍♂️
michalczaplinski Sep 15, 2023
d1432bc
Move lightbox settings to the block level
artemiomorales Sep 15, 2023
ff1d2b5
Remove support for shorthand
artemiomorales Sep 15, 2023
ee1dd8c
Remove 'lightbox' from hooks.js and add `.enabled` & `.allowEditing` …
michalczaplinski Sep 15, 2023
c30fd60
Fix another phpcs error
michalczaplinski Sep 15, 2023
1ffe40b
Merge branch 'trunk' into update/lightbox-settings-panels
artemiomorales Sep 15, 2023
8586f29
Fix phpcs error
artemiomorales Sep 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading