From e85ccb5b500690d87bb276a33a1cf35a52ba984f Mon Sep 17 00:00:00 2001
From: BE-Webdesign
instead of in the
. Default 'false'. Default value: false. + * + * @return array Array of asset data for block. + */ +function gutenberg_add_block_editor_script( $name, $handle, $src, $deps = array(), $version = false, $in_footer = false ) { + $script = array( + 'handle' => $handle, + 'src' => $src, + 'deps' => $deps, + 'ver' => $version, + 'in_footer' => $in_footer, + ); + + return gutenberg_register_block_editor_scripts( $name, array( $script ) ); +} + +/** + * Adds a block script to the theme. + * + * Should use the same function signature as wp_register_script() after $name. + * + * @param string $name Block name to register to. + * @param string $handle (Required) Name of the script. Should be unique. + * @param string $src (Required) Full URL of the script, + * or path of the script relative to the WordPress root directory. + * @param array $deps (Optional) An array of registered script + * handles this script depends on. Default value: array(). + * @param string|bool|null $version (Optional) String specifying script + * version number, if it has one, which is added to the URL as a query string + * for cache busting purposes. If version is set to false, a version number is + * automatically added equal to current installed WordPress version. + * If set to null, no version is added. Default value: false. + * @param bool $in_footer (Optional) Whether to enqueue the script + * before instead of in the
. Default 'false'. Default value: false. + * + * @return array Array of asset data for block. + */ +function gutenberg_add_block_theme_script( $name, $handle, $src, $deps = array(), $version = false, $in_footer = false ) { + $script = array( + 'handle' => $handle, + 'src' => $src, + 'deps' => $deps, + 'ver' => $version, + 'in_footer' => $in_footer, + ); + + return gutenberg_register_block_theme_scripts( $name, array( $script ) ); +} diff --git a/lib/blocks/quote.php b/lib/blocks/quote.php new file mode 100644 index 00000000000000..128be3a4ed343a --- /dev/null +++ b/lib/blocks/quote.php @@ -0,0 +1,8 @@ + $settings ) { + // If there are assets registered see if any theme assets are registered. + if ( isset( $settings['assets'] ) && isset( $settings['assets'][ $location ] ) ) { + $location_assets = $settings['assets'][ $location ]; + + // Handle scripts. + if ( isset( $location_assets['scripts'] ) && is_array( $location_assets['scripts'] ) ) { + foreach ( $location_assets['scripts'] as $script ) { + wp_enqueue_style( $script['handle'], $script['src'], $script['deps'], $script['ver'], $script['in_footer'] ); + } + } + + // Handle styles. + if ( isset( $location_assets['styles'] ) && is_array( $location_assets['styles'] ) ) { + foreach ( $location_assets['styles'] as $style ) { + wp_enqueue_style( $style['handle'], $style['src'], $style['deps'], $style['ver'], $style['media'] ); + } + } + } + } +} diff --git a/phpunit/class-asset-registration-test.php b/phpunit/class-asset-registration-test.php new file mode 100644 index 00000000000000..82a3189435a5e8 --- /dev/null +++ b/phpunit/class-asset-registration-test.php @@ -0,0 +1,492 @@ +assertFalse( $result ); + } + + /** + * Test registering to a block with empty assets. + */ + function test_registering_assets_to_registered_block_without_assets() { + $assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $result = register_block_assets( 'core/text', $assets ); + + global $wp_registered_blocks; + + $this->assertEquals( $assets, $wp_registered_blocks['core/text']['assets'] ); + } + + /** + * Test registering to a block with already existing assets. + */ + function test_registering_assets_to_registered_block_with_existing_assets() { + $assets_1 = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + register_block_assets( 'core/text', $assets_1 ); + + $assets_2 = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $result = register_block_assets( 'core/text', $assets_2 ); + + $expected = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $this->assertEquals( $expected, $result ); + } + + /** + * Should add an editor style asset for the block. + */ + function test_gutenberg_add_block_editor_style() { + $expected = array( + 'editor' => array( + 'styles' => array( + array( + 'handle' => 'my-handle', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => false, + 'media' => 'all', + ), + ), + ), + ); + gutenberg_add_block_editor_style( 'core/text', 'my-handle', 'https://wordpress.org/is-the-best' ); + + global $wp_registered_blocks; + + $this->assertEquals( $expected, $wp_registered_blocks['core/text']['assets'] ); + } + + /** + * Should add an editor style asset for the block. + */ + function test_gutenberg_add_block_theme_style() { + $expected = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'my-handle', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => false, + 'media' => 'all', + ), + ), + ), + ); + gutenberg_add_block_theme_style( 'core/text', 'my-handle', 'https://wordpress.org/is-the-best' ); + + global $wp_registered_blocks; + + $this->assertEquals( $expected, $wp_registered_blocks['core/text']['assets'] ); + } + + /** + * Should add an editor script asset for the block. + */ + function test_gutenberg_add_block_editor_script() { + $expected = array( + 'editor' => array( + 'scripts' => array( + array( + 'handle' => 'my-handle', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => false, + 'in_footer' => false, + ), + ), + ), + ); + gutenberg_add_block_editor_script( 'core/text', 'my-handle', 'https://wordpress.org/is-the-best' ); + + global $wp_registered_blocks; + + $this->assertEquals( $expected, $wp_registered_blocks['core/text']['assets'] ); + } + + /** + * Should add an editor script asset for the block. + */ + function test_gutenberg_add_block_theme_script() { + $expected = array( + 'theme' => array( + 'scripts' => array( + array( + 'handle' => 'my-handle', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => false, + 'in_footer' => false, + ), + ), + ), + ); + gutenberg_add_block_theme_script( 'core/text', 'my-handle', 'https://wordpress.org/is-the-best' ); + + global $wp_registered_blocks; + + $this->assertEquals( $expected, $wp_registered_blocks['core/text']['assets'] ); + } + + /** + * Should merge asset data into array. + */ + function test_merging_to_empty_assets() { + $current_assets = array(); + $assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $result = gutenberg_merge_assets( $current_assets, $assets ); + $expected = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'core/does-not-exist', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $this->assertEquals( $expected, $result ); + } + + /** + * Should merge asset data into existing data, creating multiple styles to be enqueued. + */ + function test_merging_to_existing_assets() { + $current_assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $result = gutenberg_merge_assets( $current_assets, $assets ); + $expected = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $this->assertEquals( $expected, $result ); + } + + /** + * Should merge scripts and style assets to match the expected output. + */ + function test_merging_new_scripts_and_styles() { + $current_assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $assets = array( + 'theme' => array( + 'scripts' => array( + array( + 'handle' => 'script 1', + 'src' => 'https://wordpress.org/is-neat', + 'deps' => array(), + 'ver' => null, + 'in_footer' => null, + ), + ), + 'styles' => array( + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $result = gutenberg_merge_assets( $current_assets, $assets ); + $expected = array( + 'theme' => array( + 'scripts' => array( + array( + 'handle' => 'script 1', + 'src' => 'https://wordpress.org/is-neat', + 'deps' => array(), + 'ver' => null, + 'in_footer' => null, + ), + ), + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $this->assertEquals( $expected, $result ); + } + + /** + * Should merge scripts and style assets across multiple calls to match the expected output. + */ + function test_merging_across_multiple_calls() { + $current_assets = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $assets_1 = array( + 'theme' => array( + 'scripts' => array( + array( + 'handle' => 'script 1', + 'src' => 'https://wordpress.org/is-neat', + 'deps' => array(), + 'ver' => null, + 'in_footer' => null, + ), + ), + 'styles' => array( + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + $assets_2 = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 3', + 'src' => 'https://wordpress.org/is-goofy', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + 'editor' => array( + 'styles' => array( + array( + 'handle' => 'style 4', + 'src' => 'https://wordpress.org/is-free', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $first_merge = gutenberg_merge_assets( $current_assets, $assets_1 ); + $result = gutenberg_merge_assets( $first_merge, $assets_2 ); + $expected = array( + 'theme' => array( + 'styles' => array( + array( + 'handle' => 'style 1', + 'src' => 'https://wordpress.org/is-the-best', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + array( + 'handle' => 'style 2', + 'src' => 'https://wordpress.org/is-wonderful', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + array( + 'handle' => 'style 3', + 'src' => 'https://wordpress.org/is-goofy', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + 'scripts' => array( + array( + 'handle' => 'script 1', + 'src' => 'https://wordpress.org/is-neat', + 'deps' => array(), + 'ver' => null, + 'in_footer' => null, + ), + ), + ), + 'editor' => array( + 'styles' => array( + array( + 'handle' => 'style 4', + 'src' => 'https://wordpress.org/is-free', + 'deps' => array(), + 'ver' => null, + 'media' => null, + ), + ), + ), + ); + + $this->assertEquals( $expected, $result ); + } +} diff --git a/webpack.config.js b/webpack.config.js index c20462f582084c..bcb064298a75a1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -30,6 +30,16 @@ entryPointNames.forEach( entryPointName => { }; } ); +// Main CSS loader. +const MainCSSExtractTextPlugin = new ExtractTextPlugin( { + filename: './[name]/build/style.css', +} ); + +// CSS loader for front end style building. +const BlockCSSExtractTextPlugin = new ExtractTextPlugin( { + filename: './blocks/build/blocks.css', +} ); + const config = { entry: entryPointNames.reduce( ( memo, entryPointName ) => { memo[ entryPointName ] = './' + entryPointName + '/index.js'; @@ -64,9 +74,31 @@ const config = { exclude: /node_modules/, use: 'babel-loader', }, + { + test: /blocks\.s?css$/, + include: [ + /blocks/, + ], + use: BlockCSSExtractTextPlugin.extract( { + use: [ + { loader: 'raw-loader' }, + { loader: 'postcss-loader' }, + { + loader: 'sass-loader', + query: { + outputStyle: 'production' === process.env.NODE_ENV ? + 'compressed' : 'nested', + }, + }, + ], + } ), + }, { test: /\.s?css$/, - use: ExtractTextPlugin.extract( { + exclude: [ + /blocks\.s?css$/, + ], + use: MainCSSExtractTextPlugin.extract( { use: [ { loader: 'raw-loader' }, { loader: 'postcss-loader' }, @@ -88,9 +120,8 @@ const config = { new webpack.DefinePlugin( { 'process.env.NODE_ENV': JSON.stringify( process.env.NODE_ENV || 'development' ), } ), - new ExtractTextPlugin( { - filename: './[name]/build/style.css', - } ), + BlockCSSExtractTextPlugin, + MainCSSExtractTextPlugin, new webpack.LoaderOptionsPlugin( { minimize: process.env.NODE_ENV === 'production', debug: process.env.NODE_ENV !== 'production',