diff --git a/docs/reference-guides/block-api/block-metadata.md b/docs/reference-guides/block-api/block-metadata.md index 0e5c26d42af65..041bfcd2aef89 100644 --- a/docs/reference-guides/block-api/block-metadata.md +++ b/docs/reference-guides/block-api/block-metadata.md @@ -461,7 +461,7 @@ Block type frontend and editor script definition. It will be enqueued both in th Block type frontend script definition. It will be enqueued only when viewing the content on the front of the site. -_Note: An option to pass also an array of view scripts exists since WordPress `6.0.0`._ +_Note: An option to pass also an array of view scripts exists since WordPress `6.1.0`._ ### Editor Style diff --git a/lib/compat/wordpress-6.0/blocks.php b/lib/compat/wordpress-6.0/blocks.php index 379c470a94d3e..547d3246319fd 100644 --- a/lib/compat/wordpress-6.0/blocks.php +++ b/lib/compat/wordpress-6.0/blocks.php @@ -123,161 +123,6 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { return $query; } -/** - * Registers view scripts for core blocks if handling is missing in WordPress core. - * - * This is a temporary solution until the Gutenberg plugin sets - * the required WordPress version to 6.0. - * - * @param array $settings Array of determined settings for registering a block type. - * @param array $metadata Metadata provided for registering a block type. - * - * @return array Array of settings for registering a block type. - */ -function gutenberg_block_type_metadata_view_script( $settings, $metadata ) { - if ( - ! isset( $metadata['viewScript'] ) || - ! empty( $settings['view_script'] ) || - ! isset( $metadata['file'] ) || - strpos( $metadata['file'], gutenberg_dir_path() ) !== 0 - ) { - return $settings; - } - - $view_script_path = realpath( dirname( $metadata['file'] ) . '/' . remove_block_asset_path_prefix( $metadata['viewScript'] ) ); - - if ( file_exists( $view_script_path ) ) { - $view_script_id = str_replace( array( '.min.js', '.js' ), '', basename( remove_block_asset_path_prefix( $metadata['viewScript'] ) ) ); - $view_script_handle = str_replace( 'core/', 'wp-block-', $metadata['name'] ) . '-' . $view_script_id; - wp_deregister_script( $view_script_handle ); - - // Replace suffix and extension with `.asset.php` to find the generated dependencies file. - $view_asset_file = substr( $view_script_path, 0, -( strlen( '.js' ) ) ) . '.asset.php'; - $view_asset = file_exists( $view_asset_file ) ? require( $view_asset_file ) : null; - $view_script_dependencies = isset( $view_asset['dependencies'] ) ? $view_asset['dependencies'] : array(); - $view_script_version = isset( $view_asset['version'] ) ? $view_asset['version'] : false; - $result = wp_register_script( - $view_script_handle, - gutenberg_url( str_replace( gutenberg_dir_path(), '', $view_script_path ) ), - $view_script_dependencies, - $view_script_version - ); - if ( $result ) { - $settings['view_script'] = $view_script_handle; - - if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $view_script_dependencies, true ) ) { - wp_set_script_translations( $view_script_handle, $metadata['textdomain'] ); - } - } - } - return $settings; -} -add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_view_script', 10, 2 ); - -if ( ! function_exists( 'wp_enqueue_block_view_script' ) ) { - /** - * Enqueues a frontend script for a specific block. - * - * Scripts enqueued using this function will only get printed - * when the block gets rendered on the frontend. - * - * @since 6.0.0 - * - * @param string $block_name The block name, including namespace. - * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. - * - * @return void - */ - function wp_enqueue_block_view_script( $block_name, $args ) { - $args = wp_parse_args( - $args, - array( - 'handle' => '', - 'src' => '', - 'deps' => array(), - 'ver' => false, - 'in_footer' => false, - - // Additional arg to allow translations for the script's textdomain. - 'textdomain' => '', - ) - ); - - /** - * Callback function to register and enqueue scripts. - * - * @param string $content When the callback is used for the render_block filter, - * the content needs to be returned so the function parameter - * is to ensure the content exists. - * @return string Block content. - */ - $callback = static function( $content, $block ) use ( $args, $block_name ) { - - // Sanity check. - if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { - return $content; - } - - // Register the stylesheet. - if ( ! empty( $args['src'] ) ) { - wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); - } - - // Enqueue the stylesheet. - wp_enqueue_script( $args['handle'] ); - - // If a textdomain is defined, use it to set the script translations. - if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { - wp_set_script_translations( $args['handle'], $args['textdomain'] ); - } - - return $content; - }; - - /* - * The filter's callback here is an anonymous function because - * using a named function in this case is not possible. - * - * The function cannot be unhooked, however, users are still able - * to dequeue the script registered/enqueued by the callback - * which is why in this case, using an anonymous function - * was deemed acceptable. - */ - add_filter( 'render_block', $callback, 10, 2 ); - } -} - -/** - * Allow multiple view scripts per block. - * - * Filters the metadata provided for registering a block type. - * - * @since 6.0.0 - * - * @param array $metadata Metadata for registering a block type. - * - * @return array - */ -function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { - - // Early return if viewScript is empty, or not an array. - if ( ! isset( $metadata['viewScript'] ) || ! is_array( $metadata['viewScript'] ) ) { - return $metadata; - } - - // Register all viewScript items. - foreach ( $metadata['viewScript'] as $view_script ) { - $item_metadata = $metadata; - $item_metadata['viewScript'] = $view_script; - gutenberg_block_type_metadata_view_script( array(), $item_metadata ); - } - - // Proceed with the default behavior. - $metadata['viewScript'] = $metadata['viewScript'][0]; - return $metadata; -} -add_filter( 'block_type_metadata', 'gutenberg_block_type_metadata_multiple_view_scripts' ); - if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) { /** * Helper function that constructs a comment query vars array from the passed block properties. diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php new file mode 100644 index 0000000000000..dbfd4f49b7373 --- /dev/null +++ b/lib/compat/wordpress-6.1/blocks.php @@ -0,0 +1,160 @@ + '', + 'src' => '', + 'deps' => array(), + 'ver' => false, + 'in_footer' => false, + + // Additional arg to allow translations for the script's textdomain. + 'textdomain' => '', + ) + ); + + /** + * Callback function to register and enqueue scripts. + * + * @param string $content When the callback is used for the render_block filter, + * the content needs to be returned so the function parameter + * is to ensure the content exists. + * @return string Block content. + */ + $callback = static function( $content, $block ) use ( $args, $block_name ) { + + // Sanity check. + if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { + return $content; + } + + // Register the stylesheet. + if ( ! empty( $args['src'] ) ) { + wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); + } + + // Enqueue the stylesheet. + wp_enqueue_script( $args['handle'] ); + + // If a textdomain is defined, use it to set the script translations. + if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { + wp_set_script_translations( $args['handle'], $args['textdomain'] ); + } + + return $content; + }; + + /* + * The filter's callback here is an anonymous function because + * using a named function in this case is not possible. + * + * The function cannot be unhooked, however, users are still able + * to dequeue the script registered/enqueued by the callback + * which is why in this case, using an anonymous function + * was deemed acceptable. + */ + add_filter( 'render_block', $callback, 10, 2 ); + } +} + +/** + * Allow multiple view scripts per block. + * + * Filters the metadata provided for registering a block type. + * + * @since 6.1.0 + * + * @param array $metadata Metadata for registering a block type. + * + * @return array + */ +function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { + + // Early return if viewScript is empty, or not an array. + if ( ! isset( $metadata['viewScript'] ) || ! is_array( $metadata['viewScript'] ) ) { + return $metadata; + } + + // Register all viewScript items. + foreach ( $metadata['viewScript'] as $view_script ) { + $item_metadata = $metadata; + $item_metadata['viewScript'] = $view_script; + gutenberg_block_type_metadata_view_script( array(), $item_metadata ); + } + + // Proceed with the default behavior. + $metadata['viewScript'] = $metadata['viewScript'][0]; + return $metadata; +} +add_filter( 'block_type_metadata', 'gutenberg_block_type_metadata_multiple_view_scripts' ); diff --git a/lib/experimental/experiments-page.php b/lib/experiments-page.php similarity index 100% rename from lib/experimental/experiments-page.php rename to lib/experiments-page.php diff --git a/lib/load.php b/lib/load.php index 5d9969c409ffa..20ec36d6a7a30 100644 --- a/lib/load.php +++ b/lib/load.php @@ -74,6 +74,9 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/experimental/rest-api.php'; } +require __DIR__ . '/experimental/editor-settings.php'; + +// WordPress 5.9 compat. require __DIR__ . '/compat/wordpress-5.9/block-gallery.php'; require __DIR__ . '/compat/wordpress-5.9/widget-render-api-endpoint/index.php'; require __DIR__ . '/compat/wordpress-5.9/blocks.php'; @@ -83,10 +86,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; require __DIR__ . '/compat/wordpress-5.9/register-global-styles-cpt.php'; require __DIR__ . '/compat/wordpress-5.9/script-loader.php'; -require __DIR__ . '/compat/wordpress-6.0/block-editor-settings.php'; -require __DIR__ . '/experimental/block-editor-settings-mobile.php'; -require __DIR__ . '/compat/wordpress-6.0/get-global-styles-and-settings.php'; -require __DIR__ . '/compat/wordpress-6.0/render-svg-filters.php'; require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; require __DIR__ . '/compat/wordpress-5.9/global-styles-css-custom-properties.php'; @@ -94,7 +93,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/templates.php'; require __DIR__ . '/compat/wordpress-5.9/template-parts.php'; require __DIR__ . '/compat/wordpress-5.9/theme-templates.php'; -require __DIR__ . '/experimental/editor-settings.php'; require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-schema-gutenberg.php'; require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-5-9.php'; require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-resolver-5-9.php'; @@ -105,6 +103,13 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/wp-theme-get-post-templates.php'; require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php'; require __DIR__ . '/compat/wordpress-5.9/move-theme-editor-menu-item.php'; +require __DIR__ . '/compat/wordpress-5.9/navigation.php'; +require __DIR__ . '/compat/wordpress-5.9/kses.php'; + +// WordPress 6.0 compat. +require __DIR__ . '/compat/wordpress-6.0/block-editor-settings.php'; +require __DIR__ . '/compat/wordpress-6.0/get-global-styles-and-settings.php'; +require __DIR__ . '/compat/wordpress-6.0/render-svg-filters.php'; require __DIR__ . '/compat/wordpress-6.0/post-lock.php'; require __DIR__ . '/compat/wordpress-6.0/blocks.php'; require __DIR__ . '/compat/wordpress-6.0/block-template-utils.php'; @@ -114,6 +119,14 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.0/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.0/block-template.php'; require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php'; +require __DIR__ . '/compat/wordpress-6.0/block-patterns-update.php'; +require __DIR__ . '/compat/wordpress-6.0/client-assets.php'; + +// WordPress 6.1 compat. +require __DIR__ . '/compat/wordpress-6.1/blocks.php'; + +// Experimental features. +require __DIR__ . '/experimental/block-editor-settings-mobile.php'; require __DIR__ . '/experimental/register-webfonts-from-theme-json.php'; require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/experimental/class-wp-webfonts.php'; @@ -121,17 +134,14 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/experimental/class-wp-webfonts-provider-local.php'; require __DIR__ . '/experimental/webfonts.php'; require __DIR__ . '/experimental/blocks.php'; +require __DIR__ . '/experimental/navigation-theme-opt-in.php'; +require __DIR__ . '/experimental/navigation-page.php'; +// Plugin specific code. require __DIR__ . '/blocks.php'; -require __DIR__ . '/compat/wordpress-6.0/block-patterns-update.php'; require __DIR__ . '/client-assets.php'; -require __DIR__ . '/compat/wordpress-6.0/client-assets.php'; require __DIR__ . '/demo.php'; -require __DIR__ . '/compat/wordpress-5.9/navigation.php'; -require __DIR__ . '/experimental/navigation-theme-opt-in.php'; -require __DIR__ . '/experimental/navigation-page.php'; -require __DIR__ . '/experimental/experiments-page.php'; -require __DIR__ . '/compat/wordpress-5.9/kses.php'; +require __DIR__ . '/experiments-page.php'; require __DIR__ . '/pwa.php'; // Copied package PHP files. @@ -139,6 +149,7 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php'; } +// Block supports overrides. require __DIR__ . '/block-supports/utils.php'; require __DIR__ . '/block-supports/elements.php'; require __DIR__ . '/block-supports/colors.php';