diff --git a/docs/how-to-guides/widgets/legacy-widget-block.md b/docs/how-to-guides/widgets/legacy-widget-block.md index c32136a10e4fef..21d22561d1509c 100644 --- a/docs/how-to-guides/widgets/legacy-widget-block.md +++ b/docs/how-to-guides/widgets/legacy-widget-block.md @@ -123,3 +123,49 @@ function hide_example_widget( $widget_types ) { } add_filter( 'widget_types_to_hide_from_legacy_widget_block', 'hide_example_widget' ); ``` + +## Using the Legacy Widget block in other block editors (Advanced) + +You may optionally allow the Legacy Widget block in other block editors such as +the WordPress post editor. This is not enabled by default. + +First, ensure that any styles and scripts required by the legacy widgets are +loaded onto the page. A convenient way of doing this is to manually perform all +of the hooks that ordinarily run when a user browses to the widgets WP Admin +screen. + +```php +add_action( 'admin_print_styles', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_print_styles-widgets.php' ); + } +} ); +add_action( 'admin_print_scripts', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'load-widgets.php' ); + do_action( 'widgets.php' ); + do_action( 'sidebar_admin_setup' ); + do_action( 'admin_print_scripts-widgets.php' ); + } +} ); +add_action( 'admin_print_footer_scripts', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_print_footer_scripts-widgets.php' ); + } +} ); +add_action( 'admin_footer', function() { + if ( get_current_screen()->is_block_editor() ) { + do_action( 'admin_footer-widgets.php' ); + } +} ); +``` + +Then, register the Legacy Widget block using `registerLegacyWidgetBlock` which +is defined in the `@wordpress/widgets` package. + +```php +add_action( 'enqueue_block_editor_assets', function() { + wp_enqueue_script( 'wp-widgets' ); + wp_add_inline_script( 'wp-widgets', 'wp.widgets.registerLegacyWidgetBlock()' ); +} ); +``` diff --git a/lib/blocks.php b/lib/blocks.php index 8f838b24fff7fb..e6e2968a23fd4d 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -5,24 +5,6 @@ * @package gutenberg */ -/* - * Fixes the priority of register_block_core_legacy_widget(). - * - * This hook was incorrectly added to Core with priority 20. #32300 fixes this - * but causes block registration warnings in the Gutenberg plugin until the - * changes are made in Core. - * - * This temporary fix can be removed after the changes to - * @wordpress/block-library in #32300 have been published to npm and updated in - * Core. - * - * See https://github.com/WordPress/gutenberg/pull/32300. - */ -if ( 20 === has_action( 'init', 'register_block_core_legacy_widget' ) ) { - remove_action( 'init', 'register_block_core_legacy_widget', 20 ); - add_action( 'init', 'register_block_core_legacy_widget', 10 ); -} - /** * Substitutes the implementation of a core-registered block type, if exists, * with the built result from the plugin. @@ -74,7 +56,6 @@ function gutenberg_reregister_core_block_types() { 'file.php' => 'core/file', 'latest-comments.php' => 'core/latest-comments', 'latest-posts.php' => 'core/latest-posts', - 'legacy-widget.php' => 'core/legacy-widget', 'loginout.php' => 'core/loginout', 'navigation.php' => 'core/navigation', 'navigation-link.php' => 'core/navigation-link', @@ -120,8 +101,14 @@ function gutenberg_reregister_core_block_types() { 'block_folders' => array( 'widget-area', ), + 'block_names' => array(), + ), + __DIR__ . '/../build/widgets/blocks/' => array( + 'block_folders' => array( + 'legacy-widget', + ), 'block_names' => array( - 'widget-area.php' => 'core/widget-area', + 'legacy-widget.php' => 'core/legacy-widget', ), ), ); diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 16c41bb15b160a..236afa3ef15999 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -34,6 +34,12 @@ class WP_Theme_JSON_Gutenberg { */ const ROOT_BLOCK_SELECTOR = 'body'; + const VALID_ORIGINS = array( + 'core', + 'theme', + 'user', + ); + const VALID_TOP_LEVEL_KEYS = array( 'customTemplates', 'templateParts', @@ -272,9 +278,14 @@ class WP_Theme_JSON_Gutenberg { /** * Constructor. * - * @param array $theme_json A structure that follows the theme.json schema. + * @param array $theme_json A structure that follows the theme.json schema. + * @param string $origin What source of data this object represents. One of core, theme, or user. Default: theme. */ - public function __construct( $theme_json = array() ) { + public function __construct( $theme_json = array(), $origin = 'theme' ) { + if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) { + $origin = 'theme'; + } + // The old format is not meant to be ported to core. // We can remove it at that point. if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { @@ -284,6 +295,18 @@ public function __construct( $theme_json = array() ) { $valid_block_names = array_keys( self::get_blocks_metadata() ); $valid_element_names = array_keys( self::ELEMENTS ); $this->theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + + // Internally, presets are keyed by origin. + $nodes = self::get_setting_nodes( $this->theme_json ); + foreach ( $nodes as $node ) { + foreach ( self::PRESETS_METADATA as $preset ) { + $path = array_merge( $node['path'], $preset['path'] ); + $preset = _wp_array_get( $this->theme_json, $path, array() ); + if ( ! empty( $preset ) ) { + gutenberg_experimental_set( $this->theme_json, $path, array( $origin => $preset ) ); + } + } + } } /** @@ -641,6 +664,33 @@ private static function append_to_selector( $selector, $to_append ) { return implode( ',', $new_selectors ); } + /** + * Function that given an array of presets keyed by origin + * and the value key of the preset returns an array where each key is + * the a preset slug and each value is the preset value. + * + * @param array $preset_per_origin Array of presets keyed by origin. + * @param string $value_key The property of the preset that contains its value. + * + * @return array Array of presets where each key is a slug and each value is the preset value. + */ + private static function get_merged_preset_by_slug( $preset_per_origin, $value_key ) { + $result = array(); + foreach ( self::VALID_ORIGINS as $origin ) { + if ( ! isset( $preset_per_origin[ $origin ] ) ) { + continue; + } + foreach ( $preset_per_origin[ $origin ] as $preset ) { + // We don't want to use kebabCase here, + // see https://github.com/WordPress/gutenberg/issues/32347 + // However, we need to make sure the generated class or css variable + // doesn't contain spaces. + $result[ preg_replace( '/\s+/', '-', $preset['slug'] ) ] = $preset[ $value_key ]; + } + } + return $result; + } + /** * Given a settings array, it returns the generated rulesets * for the preset classes. @@ -659,19 +709,16 @@ private static function compute_preset_classes( $settings, $selector ) { $stylesheet = ''; foreach ( self::PRESETS_METADATA as $preset ) { - $values = _wp_array_get( $settings, $preset['path'], array() ); - foreach ( $values as $value ) { - foreach ( $preset['classes'] as $class ) { + $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); + $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); + foreach ( $preset['classes'] as $class ) { + foreach ( $preset_by_slug as $slug => $value ) { $stylesheet .= self::to_ruleset( - // We don't want to use kebabCase here, - // see https://github.com/WordPress/gutenberg/issues/32347 - // However, we need to make sure the generated class - // doesn't contain spaces. - self::append_to_selector( $selector, '.has-' . preg_replace( '/\s+/', '-', $value['slug'] ) . '-' . $class['class_suffix'] ), + self::append_to_selector( $selector, '.has-' . $slug . '-' . $class['class_suffix'] ), array( array( 'name' => $class['property_name'], - 'value' => $value[ $preset['value_key'] ] . ' !important', + 'value' => $value . ' !important', ), ) ); @@ -701,11 +748,12 @@ private static function compute_preset_classes( $settings, $selector ) { private static function compute_preset_vars( $settings ) { $declarations = array(); foreach ( self::PRESETS_METADATA as $preset ) { - $values = _wp_array_get( $settings, $preset['path'], array() ); - foreach ( $values as $value ) { + $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); + $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); + foreach ( $preset_by_slug as $slug => $value ) { $declarations[] = array( - 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $value['slug'], - 'value' => $value[ $preset['value_key'] ], + 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $slug, + 'value' => $value, ); } } @@ -1101,86 +1149,35 @@ public function get_stylesheet( $type = 'all' ) { /** * Merge new incoming data. * - * @param WP_Theme_JSON_Gutenberg $incoming Data to merge. - * @param string $update_or_remove Whether update or remove existing colors - * for which the incoming data has a duplicated slug. + * @param WP_Theme_JSON $incoming Data to merge. */ - public function merge( $incoming, $update_or_remove = 'remove' ) { - $incoming_data = $incoming->get_raw_data(); - $existing_data = $this->theme_json; + public function merge( $incoming ) { + $incoming_data = $incoming->get_raw_data(); + $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); // The array_replace_recursive algorithm merges at the leaf level. // For leaf values that are arrays it will use the numeric indexes for replacement. - $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); - - // There are a few cases in which we want to merge things differently - // from what array_replace_recursive does. - - // Some incoming properties should replace the existing. + // In those cases, we want to replace the existing with the incoming value, if it exists. $to_replace = array(); $to_replace[] = array( 'custom' ); $to_replace[] = array( 'spacing', 'units' ); - $to_replace[] = array( 'typography', 'fontSizes' ); - $to_replace[] = array( 'typography', 'fontFamilies' ); - - // Some others should be appended to the existing. - // If the slug is the same than an existing element, - // the $update_or_remove param is used to decide - // what to do with the existing element: - // either remove it and append the incoming, - // or update it with the incoming. - $to_append = array(); - $to_append[] = array( 'color', 'duotone' ); - $to_append[] = array( 'color', 'gradients' ); - $to_append[] = array( 'color', 'palette' ); + $to_replace[] = array( 'color', 'duotone' ); + foreach ( self::VALID_ORIGINS as $origin ) { + $to_replace[] = array( 'color', 'palette', $origin ); + $to_replace[] = array( 'color', 'gradients', $origin ); + $to_replace[] = array( 'typography', 'fontSizes', $origin ); + $to_replace[] = array( 'typography', 'fontFamilies', $origin ); + } $nodes = self::get_setting_nodes( $this->theme_json ); foreach ( $nodes as $metadata ) { - foreach ( $to_replace as $path_to_replace ) { - $path = array_merge( $metadata['path'], $path_to_replace ); + foreach ( $to_replace as $property_path ) { + $path = array_merge( $metadata['path'], $property_path ); $node = _wp_array_get( $incoming_data, $path, array() ); if ( ! empty( $node ) ) { gutenberg_experimental_set( $this->theme_json, $path, $node ); } } - foreach ( $to_append as $path_to_append ) { - $path = array_merge( $metadata['path'], $path_to_append ); - $incoming_node = _wp_array_get( $incoming_data, $path, array() ); - $existing_node = _wp_array_get( $existing_data, $path, array() ); - - if ( empty( $incoming_node ) && empty( $existing_node ) ) { - continue; - } - - $index_table = array(); - $existing_slugs = array(); - $merged = array(); - foreach ( $existing_node as $key => $value ) { - $index_table[ $value['slug'] ] = $key; - $existing_slugs[] = $value['slug']; - $merged[ $key ] = $value; - } - - $to_remove = array(); - foreach ( $incoming_node as $value ) { - if ( ! in_array( $value['slug'], $existing_slugs, true ) ) { - $merged[] = $value; - } elseif ( 'update' === $update_or_remove ) { - $merged[ $index_table[ $value['slug'] ] ] = $value; - } else { - $merged[] = $value; - $to_remove[] = $index_table[ $value['slug'] ]; - } - } - - // Remove the duplicated values and pack the sparsed array. - foreach ( $to_remove as $index ) { - unset( $merged[ $index ] ); - } - $merged = array_values( $merged ); - - gutenberg_experimental_set( $this->theme_json, $path, $merged ); - } } } @@ -1277,14 +1274,26 @@ private static function is_safe_css_declaration( $property_name, $property_value /** * Removes insecure data from theme.json. + * + * @param array $theme_json Structure to sanitize. + * + * @return array Sanitized structure. */ - public function remove_insecure_properties() { + public static function remove_insecure_properties( $theme_json ) { $sanitized = array(); + if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { + $theme_json = WP_Theme_JSON_Schema_V0::parse( $theme_json ); + } + + $valid_block_names = array_keys( self::get_blocks_metadata() ); + $valid_element_names = array_keys( self::ELEMENTS ); + $theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + $blocks_metadata = self::get_blocks_metadata(); - $style_nodes = self::get_style_nodes( $this->theme_json, $blocks_metadata ); + $style_nodes = self::get_style_nodes( $theme_json, $blocks_metadata ); foreach ( $style_nodes as $metadata ) { - $input = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); if ( empty( $input ) ) { continue; } @@ -1295,9 +1304,9 @@ public function remove_insecure_properties() { } } - $setting_nodes = self::get_setting_nodes( $this->theme_json ); + $setting_nodes = self::get_setting_nodes( $theme_json ); foreach ( $setting_nodes as $metadata ) { - $input = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); if ( empty( $input ) ) { continue; } @@ -1309,17 +1318,18 @@ public function remove_insecure_properties() { } if ( empty( $sanitized['styles'] ) ) { - unset( $this->theme_json['styles'] ); + unset( $theme_json['styles'] ); } else { - $this->theme_json['styles'] = $sanitized['styles']; + $theme_json['styles'] = $sanitized['styles']; } if ( empty( $sanitized['settings'] ) ) { - unset( $this->theme_json['settings'] ); + unset( $theme_json['settings'] ); } else { - $this->theme_json['settings'] = $sanitized['settings']; + $theme_json['settings'] = $sanitized['settings']; } + return $theme_json; } /** diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index bcdedda86f30b7..1f6cfe6d30ce85 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -250,7 +250,7 @@ public static function get_core_data() { $config = self::read_json_file( __DIR__ . '/experimental-default-theme.json' ); $config = self::translate( $config ); - self::$core = new WP_Theme_JSON_Gutenberg( $config ); + self::$core = new WP_Theme_JSON_Gutenberg( $config, 'core' ); return self::$core; } @@ -368,7 +368,7 @@ public static function get_user_data() { $json_decoding_error = json_last_error(); if ( JSON_ERROR_NONE !== $json_decoding_error ) { trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); - return new WP_Theme_JSON_Gutenberg( $config ); + return new WP_Theme_JSON_Gutenberg( $config, 'user' ); } // Very important to verify if the flag isGlobalStylesUserThemeJSON is true. @@ -382,7 +382,7 @@ public static function get_user_data() { $config = $decoded_data; } } - self::$user = new WP_Theme_JSON_Gutenberg( $config ); + self::$user = new WP_Theme_JSON_Gutenberg( $config, 'user' ); return self::$user; } @@ -419,7 +419,7 @@ public static function get_merged_data( $settings = array(), $origin = 'user' ) $result->merge( self::get_theme_data( $theme_support_data ) ); if ( 'user' === $origin ) { - $result->merge( self::get_user_data(), 'update' ); + $result->merge( self::get_user_data() ); } return $result; diff --git a/lib/client-assets.php b/lib/client-assets.php index c52c5ae04f2b04..3a366796d6218c 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -447,7 +447,7 @@ function gutenberg_register_packages_styles( $styles ) { $styles, 'wp-edit-widgets', gutenberg_url( 'build/edit-widgets/style.css' ), - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks' ), + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ), $version ); $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' ); @@ -465,7 +465,7 @@ function gutenberg_register_packages_styles( $styles ) { $styles, 'wp-customize-widgets', gutenberg_url( 'build/customize-widgets/style.css' ), - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ), + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ), $version ); $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' ); @@ -478,6 +478,14 @@ function gutenberg_register_packages_styles( $styles ) { $version ); $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' ); + + gutenberg_override_style( + $styles, + 'wp-widgets', + gutenberg_url( 'build/widgets/style.css' ), + array( 'wp-components' ) + ); + $styles->add_data( 'wp-widgets', 'rtl', 'replace' ); } add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); diff --git a/lib/experimental-default-theme.json b/lib/experimental-default-theme.json index dc4dbbaf058529..d92547a1468952 100644 --- a/lib/experimental-default-theme.json +++ b/lib/experimental-default-theme.json @@ -6,148 +6,124 @@ { "name": "Black", "slug": "black", - "color": "#000000", - "origin": "core" + "color": "#000000" }, { "name": "Cyan bluish gray", "slug": "cyan-bluish-gray", - "color": "#abb8c3", - "origin": "core" + "color": "#abb8c3" }, { "name": "White", "slug": "white", - "color": "#ffffff", - "origin": "core" + "color": "#ffffff" }, { "name": "Pale pink", "slug": "pale-pink", - "color": "#f78da7", - "origin": "core" + "color": "#f78da7" }, { "name": "Vivid red", "slug": "vivid-red", - "color": "#cf2e2e", - "origin": "core" + "color": "#cf2e2e" }, { "name": "Luminous vivid orange", "slug": "luminous-vivid-orange", - "color": "#ff6900", - "origin": "core" + "color": "#ff6900" }, { "name": "Luminous vivid amber", "slug": "luminous-vivid-amber", - "color": "#fcb900", - "origin": "core" + "color": "#fcb900" }, { "name": "Light green cyan", "slug": "light-green-cyan", - "color": "#7bdcb5", - "origin": "core" + "color": "#7bdcb5" }, { "name": "Vivid green cyan", "slug": "vivid-green-cyan", - "color": "#00d084", - "origin": "core" + "color": "#00d084" }, { "name": "Pale cyan blue", "slug": "pale-cyan-blue", - "color": "#8ed1fc", - "origin": "core" + "color": "#8ed1fc" }, { "name": "Vivid cyan blue", "slug": "vivid-cyan-blue", - "color": "#0693e3", - "origin": "core" + "color": "#0693e3" }, { "name": "Vivid purple", "slug": "vivid-purple", - "color": "#9b51e0", - "origin": "core" + "color": "#9b51e0" } ], "gradients": [ { "name": "Vivid cyan blue to vivid purple", "gradient": "linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)", - "slug": "vivid-cyan-blue-to-vivid-purple", - "origin": "core" + "slug": "vivid-cyan-blue-to-vivid-purple" }, { "name": "Light green cyan to vivid green cyan", "gradient": "linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)", - "slug": "light-green-cyan-to-vivid-green-cyan", - "origin": "core" + "slug": "light-green-cyan-to-vivid-green-cyan" }, { "name": "Luminous vivid amber to luminous vivid orange", "gradient": "linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%)", - "slug": "luminous-vivid-amber-to-luminous-vivid-orange", - "origin": "core" + "slug": "luminous-vivid-amber-to-luminous-vivid-orange" }, { "name": "Luminous vivid orange to vivid red", "gradient": "linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%)", - "slug": "luminous-vivid-orange-to-vivid-red", - "origin": "core" + "slug": "luminous-vivid-orange-to-vivid-red" }, { "name": "Very light gray to cyan bluish gray", "gradient": "linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%)", - "slug": "very-light-gray-to-cyan-bluish-gray", - "origin": "core" + "slug": "very-light-gray-to-cyan-bluish-gray" }, { "name": "Cool to warm spectrum", "gradient": "linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%)", - "slug": "cool-to-warm-spectrum", - "origin": "core" + "slug": "cool-to-warm-spectrum" }, { "name": "Blush light purple", "gradient": "linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%)", - "slug": "blush-light-purple", - "origin": "core" + "slug": "blush-light-purple" }, { "name": "Blush bordeaux", "gradient": "linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)", - "slug": "blush-bordeaux", - "origin": "core" + "slug": "blush-bordeaux" }, { "name": "Luminous dusk", "gradient": "linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)", - "slug": "luminous-dusk", - "origin": "core" + "slug": "luminous-dusk" }, { "name": "Pale ocean", "gradient": "linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%)", - "slug": "pale-ocean", - "origin": "core" + "slug": "pale-ocean" }, { "name": "Electric grass", "gradient": "linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%)", - "slug": "electric-grass", - "origin": "core" + "slug": "electric-grass" }, { "name": "Midnight", "gradient": "linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%)", - "slug": "midnight", - "origin": "core" + "slug": "midnight" } ], "duotone": [ diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index e6f185bda76bb2..d6ee991fc503cc 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -199,11 +199,21 @@ function set_unique_slug_on_create_template( $post_id ) { * @return void */ function gutenberg_the_skip_link() { + // Early exit if on WP 5.8+. + if ( function_exists( 'the_block_template_skip_link' ) ) { + return; + } - // Early exit if not an FSE theme. + // Early exit if not a block theme. if ( ! gutenberg_supports_block_templates() ) { return; } + + // Early exit if not a block template. + global $_wp_current_template_content; + if ( ! $_wp_current_template_content ) { + return; + } ?> get_settings(); + if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { - $settings['colors'] = $settings['__experimentalFeatures']['color']['palette']; - unset( $settings['__experimentalFeatures']['color']['palette'] ); + $colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; + $settings['colors'] = isset( $colors_by_origin['user'] ) ? + $colors_by_origin['user'] : ( + isset( $colors_by_origin['theme'] ) ? + $colors_by_origin['theme'] : + $colors_by_origin['core'] + ); } + if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { - $settings['gradients'] = $settings['__experimentalFeatures']['color']['gradients']; - unset( $settings['__experimentalFeatures']['color']['gradients'] ); + $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; + $settings['gradients'] = isset( $gradients_by_origin['user'] ) ? + $gradients_by_origin['user'] : ( + isset( $gradients_by_origin['theme'] ) ? + $gradients_by_origin['theme'] : + $gradients_by_origin['core'] + ); + } + + if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { + $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; + $settings['fontSizes'] = isset( $font_sizes_by_origin['user'] ) ? + $font_sizes_by_origin['user'] : ( + isset( $font_sizes_by_origin['theme'] ) ? + $font_sizes_by_origin['theme'] : + $font_sizes_by_origin['core'] + ); } + if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; unset( $settings['__experimentalFeatures']['color']['custom'] ); @@ -152,10 +175,6 @@ function_exists( 'gutenberg_is_edit_site_page' ) && $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; unset( $settings['__experimentalFeatures']['color']['customGradient'] ); } - if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { - $settings['fontSizes'] = $settings['__experimentalFeatures']['typography']['fontSizes']; - unset( $settings['__experimentalFeatures']['typography']['fontSizes'] ); - } if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); @@ -209,9 +228,9 @@ function gutenberg_global_styles_filter_post( $content ) { $decoded_data['isGlobalStylesUserThemeJSON'] ) { unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); - $theme_json = new WP_Theme_JSON_Gutenberg( $decoded_data ); - $theme_json->remove_insecure_properties(); - $data_to_encode = $theme_json->get_raw_data(); + + $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data ); + $data_to_encode['isGlobalStylesUserThemeJSON'] = true; return wp_json_encode( $data_to_encode ); } diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 06376c2c71e396..1a4089eea66fd7 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -103,3 +103,59 @@ function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_bloc function gutenberg_widgets_editor_add_admin_body_classes( $classes ) { return "$classes block-editor-page wp-embed-responsive"; } + +/** + * Emulates the Widgets screen `admin_print_styles` when at the block editor + * screen. + */ +function gutenberg_block_editor_admin_print_styles() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_styles-widgets.php' ); + } +} +add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); + +/** + * Emulates the Widgets screen `admin_print_scripts` when at the block editor + * screen. + */ +function gutenberg_block_editor_admin_print_scripts() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/includes/ajax-actions.php */ + do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** This action is documented in wp-admin/includes/ajax-actions.php */ + do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** This action is documented in wp-admin/widgets.php */ + do_action( 'sidebar_admin_setup' ); + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_scripts-widgets.php' ); + } +} +add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); + +/** + * Emulates the Widgets screen `admin_print_footer_scripts` when at the block + * editor screen. + */ +function gutenberg_block_editor_admin_print_footer_scripts() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_print_footer_scripts-widgets.php' ); + } +} +add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); + +/** + * Emulates the Widgets screen `admin_footer` when at the block editor screen. + */ +function gutenberg_block_editor_admin_footer() { + if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { + /** This action is documented in wp-admin/admin-footer.php */ + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + do_action( 'admin_footer-widgets.php' ); + } +} +add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); diff --git a/lib/widgets.php b/lib/widgets.php index c61d2c1106edc6..314b4f30c2ff8b 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -11,6 +11,12 @@ * @return boolean True if a screen containing the block editor is being loaded. */ function gutenberg_is_block_editor() { + _deprecated_function( + 'gutenberg_is_block_editor', + '10.8', + 'WP_Screen::is_block_editor' + ); + // If get_current_screen does not exist, we are neither in the standard block editor for posts, or the widget block editor. // We can safely return false. if ( ! function_exists( 'get_current_screen' ) ) { @@ -44,79 +50,6 @@ function gutenberg_use_widgets_block_editor() { ); } -/** - * Emulates the Widgets screen `admin_print_styles` when at the block editor - * screen. - */ -function gutenberg_block_editor_admin_print_styles() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_styles-widgets.php' ); - } -} -add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); - -/** - * Emulates the Widgets screen `admin_print_scripts` when at the block editor - * screen. - */ -function gutenberg_block_editor_admin_print_scripts() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/includes/ajax-actions.php */ - do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - /** This action is documented in wp-admin/includes/ajax-actions.php */ - do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - /** This action is documented in wp-admin/widgets.php */ - do_action( 'sidebar_admin_setup' ); - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_scripts-widgets.php' ); - } -} -add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); - -/** - * Emulates the Widgets screen `admin_print_footer_scripts` when at the block - * editor screen. - */ -function gutenberg_block_editor_admin_print_footer_scripts() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_print_footer_scripts-widgets.php' ); - } -} -add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); - -/** - * Emulates the Widgets screen `admin_footer` when at the block editor screen. - */ -function gutenberg_block_editor_admin_footer() { - if ( gutenberg_is_block_editor() ) { - /** This action is documented in wp-admin/admin-footer.php */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - do_action( 'admin_footer-widgets.php' ); - } -} -add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); - -/** - * Adds a save widgets nonce required by the legacy widgets block. - */ -function gutenberg_print_save_widgets_nonce() { - // The function wpWidgets.save needs this nonce to work as expected. - echo implode( - "\n", - array( - '
', - ) - ); -} -add_action( 'admin_footer-widgets.php', 'gutenberg_print_save_widgets_nonce' ); - - /** * Returns the settings required by legacy widgets blocks. * @@ -137,7 +70,6 @@ function gutenberg_get_legacy_widget_settings() { 'media_image', 'media_gallery', 'media_video', - 'meta', 'search', 'text', 'categories', @@ -151,90 +83,11 @@ function gutenberg_get_legacy_widget_settings() { ) ); - // Backwards compatibility. Remove this in or after Gutenberg 10.5. - if ( has_filter( 'widgets_to_exclude_from_legacy_widget_block' ) ) { - /** - * Filters the list of widget classes that should **not** be offered by the legacy widget block. - * - * Returning an empty array will make all the widgets available. - * - * @param array $widgets An array of excluded widgets classnames. - * - * @since 5.6.0 - */ - $widgets_to_exclude_from_legacy_widget_block = apply_filters( - 'widgets_to_exclude_from_legacy_widget_block', - array( - 'WP_Widget_Block', - 'WP_Widget_Pages', - 'WP_Widget_Calendar', - 'WP_Widget_Archives', - 'WP_Widget_Media_Audio', - 'WP_Widget_Media_Image', - 'WP_Widget_Media_Gallery', - 'WP_Widget_Media_Video', - 'WP_Widget_Meta', - 'WP_Widget_Search', - 'WP_Widget_Text', - 'WP_Widget_Categories', - 'WP_Widget_Recent_Posts', - 'WP_Widget_Recent_Comments', - 'WP_Widget_RSS', - 'WP_Widget_Tag_Cloud', - 'WP_Nav_Menu_Widget', - 'WP_Widget_Custom_HTML', - ) - ); - - _deprecated_hook( - 'widgets_to_exclude_from_legacy_widget_block', - '10.3', - "wp.hooks.addFilter( 'legacyWidget.isWidgetTypeHidden', ... )" - ); - - foreach ( $wp_widget_factory->widgets as $widget ) { - if ( - in_array( get_class( $widget ), $widgets_to_exclude_from_legacy_widget_block, true ) && - ! in_array( $widget->id_base, $widget_types_to_hide_from_legacy_widget_block, true ) - ) { - $widget_types_to_hide_from_legacy_widget_block[] = $widget->id_base; - } - } - } - $settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block; return $settings; } -/** - * Extends default editor settings with values supporting legacy widgets. - * - * This can be removed when plugin support requires WordPress 5.8.0+. - * - * @param array $settings Default editor settings. - * - * @return array Filtered editor settings. - */ -function gutenberg_legacy_widget_settings( $settings ) { - return array_merge( $settings, gutenberg_get_legacy_widget_settings() ); -} -// This can be removed when plugin support requires WordPress 5.8.0+. -if ( function_exists( 'get_block_editor_settings' ) ) { - add_filter( 'block_editor_settings_all', 'gutenberg_legacy_widget_settings' ); -} else { - add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' ); -} - -/** - * Function to enqueue admin-widgets as part of the block editor assets. - */ -function gutenberg_enqueue_widget_scripts() { - wp_enqueue_script( 'admin-widgets' ); -} - -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' ); - /** * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in