diff --git a/lib/block-supports/behaviors.php b/lib/block-supports/behaviors.php deleted file mode 100644 index 383f4b4e6f9279..00000000000000 --- a/lib/block-supports/behaviors.php +++ /dev/null @@ -1,260 +0,0 @@ -attributes ) { - $block_type->attributes = array(); - } - - $block_type->attributes['behaviors'] = array( - 'type' => 'object', - ); - - // If it supports the lightbox behavior, add the hook to that block. - // In the future, this should be a loop with all the behaviors. - $has_lightbox_support = block_has_support( $block_type, array( 'behaviors', 'lightbox' ), false ); - if ( $has_lightbox_support ) { - // Use priority 15 to run this hook after other hooks/plugins. - // They could use the `render_block_{$this->name}` filter to modify the markup. - add_filter( 'render_block_' . $block_type->name, 'gutenberg_render_behaviors_support_lightbox', 15, 2 ); - } -} - -/** - * Add the directives and layout needed for the lightbox behavior. - * - * @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'] ) ) { - $lightbox_settings = $block['attrs']['behaviors']['lightbox']; - // If the lightbox setting is not set in the block attributes, get it from the theme.json file. - } else { - $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_data(); - if ( isset( $theme_data['behaviors']['blocks'][ $block['blockName'] ]['lightbox'] ) ) { - $lightbox_settings = $theme_data['behaviors']['blocks'][ $block['blockName'] ]['lightbox']; - } else { - $lightbox_settings = null; - } - } - - if ( isset( $lightbox_settings['enabled'] ) && false === $lightbox_settings['enabled'] ) { - return $block_content; - } - - if ( ! $lightbox_settings || 'none' !== $link_destination ) { - return $block_content; - } - - $processor = new WP_HTML_Tag_Processor( $block_content ); - - $aria_label = __( 'Enlarge image', 'gutenberg' ); - - $processor->next_tag( 'img' ); - $alt_attribute = $processor->get_attribute( 'alt' ); - - // An empty alt attribute `alt=""` is valid for decorative images. - if ( is_string( $alt_attribute ) ) { - $alt_attribute = trim( $alt_attribute ); - } - - // It only makes sense to append the alt text to the button aria-label when the alt text is non-empty. - if ( $alt_attribute ) { - /* translators: %s: Image alt text. */ - $aria_label = sprintf( __( 'Enlarge image: %s', 'gutenberg' ), $alt_attribute ); - } - - // If we don't set a default, it won't work if Lightbox is set to enabled by default. - $lightbox_animation = 'zoom'; - if ( isset( $lightbox_settings['animation'] ) && '' !== $lightbox_settings['animation'] ) { - $lightbox_animation = $lightbox_settings['animation']; - } - - // Note: We want to store the `src` in the context so we - // can set it dynamically when the lightbox is opened. - if ( isset( $block['attrs']['id'] ) ) { - $img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] ); - $img_metadata = wp_get_attachment_metadata( $block['attrs']['id'] ); - $img_width = $img_metadata['width'] ?? 'none'; - $img_height = $img_metadata['height'] ?? 'none'; - } else { - $img_uploaded_src = $processor->get_attribute( 'src' ); - $img_width = 'none'; - $img_height = 'none'; - } - - if ( isset( $block['attrs']['scale'] ) ) { - $scale_attr = $block['attrs']['scale']; - } else { - $scale_attr = false; - } - - $w = new WP_HTML_Tag_Processor( $block_content ); - $w->next_tag( 'figure' ); - $w->add_class( 'wp-lightbox-container' ); - $w->set_attribute( 'data-wp-interactive', true ); - - $w->set_attribute( - 'data-wp-context', - sprintf( - '{ "core": - { "image": - { "imageLoaded": false, - "initialized": false, - "lightboxEnabled": false, - "hideAnimationEnabled": false, - "preloadInitialized": false, - "lightboxAnimation": "%s", - "imageUploadedSrc": "%s", - "imageCurrentSrc": "", - "targetWidth": "%s", - "targetHeight": "%s", - "scaleAttr": "%s" - } - } - }', - $lightbox_animation, - $img_uploaded_src, - $img_width, - $img_height, - $scale_attr - ) - ); - $w->next_tag( 'img' ); - $w->set_attribute( 'data-wp-init', 'effects.core.image.setCurrentSrc' ); - $w->set_attribute( 'data-wp-on--load', 'actions.core.image.handleLoad' ); - $w->set_attribute( 'data-wp-effect', 'effects.core.image.setButtonStyles' ); - $body_content = $w->get_updated_html(); - - // Wrap the image in the body content with a button. - $img = null; - preg_match( '/]+>/', $body_content, $img ); - - $button = - $img[0] - . ''; - - $body_content = preg_replace( '/]+>/', $button, $body_content ); - - // We need both a responsive image and an enlarged image to animate - // the zoom seamlessly on slow internet connections; the responsive - // image is a copy of the one in the body, which animates immediately - // as the lightbox is opened, while the enlarged one is a full-sized - // version that will likely still be loading as the animation begins. - $m = new WP_HTML_Tag_Processor( $block_content ); - $m->next_tag( 'figure' ); - $m->add_class( 'responsive-image' ); - $m->next_tag( 'img' ); - // We want to set the 'src' attribute to an empty string in the responsive image - // because otherwise, as of this writing, the wp_filter_content_tags() function in - // WordPress will automatically add a 'srcset' attribute to the image, which will at - // times cause the incorrectly sized image to be loaded in the lightbox on Firefox. - // Because of this, we bind the 'src' attribute explicitly the current src to reliably - // use the exact same image as in the content when the lightbox is first opened while - // we wait for the larger image to load. - $m->set_attribute( 'src', '' ); - $m->set_attribute( 'data-wp-bind--src', 'context.core.image.imageCurrentSrc' ); - $m->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' ); - $initial_image_content = $m->get_updated_html(); - - $q = new WP_HTML_Tag_Processor( $block_content ); - $q->next_tag( 'figure' ); - $q->add_class( 'enlarged-image' ); - $q->next_tag( 'img' ); - - // We set the 'src' attribute to an empty string to prevent the browser from loading the image - // on initial page load, then bind the attribute to a selector that returns the full-sized image src when - // the lightbox is opened. We could use 'loading=lazy' in combination with the 'hidden' attribute to - // accomplish the same behavior, but that approach breaks progressive loading of the image in Safari - // and Chrome (see https://github.com/WordPress/gutenberg/pull/52765#issuecomment-1674008151). Until that - // is resolved, manually setting the 'src' seems to be the best solution to load the large image on demand. - $q->set_attribute( 'src', '' ); - $q->set_attribute( 'data-wp-bind--src', 'selectors.core.image.enlargedImgSrc' ); - $q->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' ); - $enlarged_image_content = $q->get_updated_html(); - - $background_color = esc_attr( wp_get_global_styles( array( 'color', 'background' ) ) ); - - $close_button_icon = ''; - $close_button_color = esc_attr( wp_get_global_styles( array( 'color', 'text' ) ) ); - $dialog_label = esc_attr__( 'Enlarged image', 'gutenberg' ); - $close_button_label = esc_attr__( 'Close', 'gutenberg' ); - - $lightbox_html = << - - - -
- -HTML; - - return str_replace( '', $lightbox_html . '', $body_content ); -} - -// Register the block support. -WP_Block_Supports::get_instance()->register( - 'behaviors', - array( - 'register_attribute' => 'gutenberg_register_behaviors_support', - ) -); diff --git a/lib/blocks.php b/lib/blocks.php index 698b5d6873748f..d5283afeb7f999 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -439,33 +439,6 @@ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $si 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 ); /** * Registers the metadata block attribute for all block types. diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 43a3772a1c3af0..57ca9ab59eacf1 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -695,7 +695,7 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { $origin = 'theme'; } - $this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); + $this->theme_json = WP_Theme_JSON_Schema::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 ); @@ -2989,7 +2989,7 @@ protected static function filter_slugs( $node, $slugs ) { public static function remove_insecure_properties( $theme_json ) { $sanitized = array(); - $theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json ); + $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php deleted file mode 100644 index d11545751af362..00000000000000 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ /dev/null @@ -1,202 +0,0 @@ - '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 ] ); - } -} diff --git a/lib/load.php b/lib/load.php index 59fb75541ac41e..5fa30b44420dc2 100644 --- a/lib/load.php +++ b/lib/load.php @@ -219,7 +219,6 @@ function () { 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'; @@ -251,7 +250,6 @@ function () { require __DIR__ . '/block-supports/duotone.php'; require __DIR__ . '/block-supports/shadow.php'; require __DIR__ . '/block-supports/background.php'; -require __DIR__ . '/block-supports/behaviors.php'; require __DIR__ . '/block-supports/pattern.php'; // Data views. diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index 85cf3de57b1275..add8e5989ab7de 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -97,12 +97,6 @@ function block_core_image_get_lightbox_settings( $block ) { // Get the lightbox setting from the block attributes. if ( isset( $block['attrs']['lightbox'] ) ) { $lightbox_settings = $block['attrs']['lightbox']; - // If the lightbox setting is not set in the block attributes, - // check the legacy lightbox settings that are set using the - // `gutenberg_should_render_lightbox` filter. - // We can remove this elseif statement when the legacy lightbox settings are removed. - } elseif ( isset( $block['legacyLightboxSettings'] ) ) { - $lightbox_settings = $block['legacyLightboxSettings']; } if ( ! isset( $lightbox_settings ) ) {