Skip to content

Commit

Permalink
Roll back previous test commit. There were build errors that might be…
Browse files Browse the repository at this point in the history
…st be resolved in another PR.
  • Loading branch information
ramonjd committed Mar 28, 2022
1 parent 99b9dbd commit 5e0a9c2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
3 changes: 2 additions & 1 deletion lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
return $attributes;
}

$style_engine = WP_Style_Engine_Gutenberg::get_instance();
$spacing_block_styles = array();
$spacing_block_styles['padding'] = $has_padding_support ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
$spacing_block_styles['margin'] = $has_margin_support ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null;
$inline_styles = WP_Style_Engine_Gutenberg::generate(
$inline_styles = $style_engine->generate(
array( 'spacing' => $spacing_block_styles ),
array(
'inline' => true,
Expand Down
8 changes: 4 additions & 4 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/global-styles.php';
require __DIR__ . '/pwa.php';

// Copied package PHP files .
if ( file_exists( __DIR__ . '/style-engine/class-wp-style-engine-gutenberg.php' ) ) {
require __DIR__ . '/style-engine/class-wp-style-engine-gutenberg.php';
}
// TODO: Before this PR merges, move this to be a part of the style engine package.
// Part of the build process should be to copy the PHP file to the correct location,
// similar to the loading behaviour in `blocks.php`.
require __DIR__ . '/style-engine/class-wp-style-engine-gutenberg.php';

require __DIR__ . '/block-supports/elements.php';
require __DIR__ . '/block-supports/colors.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* WP_Style_Engine class
* WP_Style_Engine_Gutenberg class
*
* Generates classnames and block styles.
*
* @package Gutenberg
*/

if ( class_exists( 'WP_Style_Engine' ) ) {
if ( class_exists( 'WP_Style_Engine_Gutenberg' ) ) {
return;
}

Expand All @@ -19,7 +19,14 @@
*
* @since 6.0.0
*/
class WP_Style_Engine {
class WP_Style_Engine_Gutenberg {
/**
* Container for the main instance of the class.
*
* @var WP_Style_Engine_Gutenberg|null
*/
private static $instance = null;

/**
* Style definitions that contain the instructions to
* parse/output valid Gutenberg styles from a block's attributes.
Expand All @@ -45,6 +52,21 @@ class WP_Style_Engine {
),
);

/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @return WP_Style_Engine_Gutenberg The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Returns a CSS ruleset based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
Expand All @@ -53,7 +75,7 @@ class WP_Style_Engine {
*
* @return array A CSS ruleset compatible with generate().
*/
protected static function get_block_style_css_rules( $style_value, $path ) {
protected function get_block_style_css_rules( $style_value, $path ) {
$style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $path, null );

if ( ! empty( $style_definition ) ) {
Expand All @@ -80,7 +102,7 @@ protected static function get_block_style_css_rules( $style_value, $path ) {
*
* @return string A CSS ruleset formatted to be placed in an HTML `style` attribute.
*/
public static function generate( $block_styles, $options = array() ) {
public function generate( $block_styles, $options = array() ) {
$output = '';

if ( empty( $block_styles ) ) {
Expand All @@ -95,7 +117,7 @@ public static function generate( $block_styles, $options = array() ) {
if ( empty( $style_value ) ) {
return $output;
}
$rules = array_merge( $rules, static::get_block_style_css_rules( $style_value, $options['path'] ) );
$rules = array_merge( $rules, $this->get_block_style_css_rules( $style_value, $options['path'] ) );
} else {
// Otherwise build them all.
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) {
Expand All @@ -104,7 +126,7 @@ public static function generate( $block_styles, $options = array() ) {
if ( empty( $style_value ) ) {
continue;
}
$rules = array_merge( $rules, static::get_block_style_css_rules( $style_value, $style_definition['path'] ) );
$rules = array_merge( $rules, $this->get_block_style_css_rules( $style_value, $style_definition['path'] ) );
}
}
}
Expand Down Expand Up @@ -132,7 +154,7 @@ public static function generate( $block_styles, $options = array() ) {
*
* @return array The class name for the added style.
*/
protected static function get_css_box_rules( $style_value, $style_property ) {
public static function get_css_box_rules( $style_value, $style_property ) {
$rules = array();

if ( ! $style_value ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class WP_Style_Engine_Gutenberg_Test extends WP_UnitTestCase {
* @dataProvider data_block_styles_fixtures
*/
function test_generate_css( $block_styles, $options, $expected_output ) {
$generated_styles = WP_Style_Engine_Gutenberg::generate(
$style_engine = WP_Style_Engine_Gutenberg::get_instance();
$generated_styles = $style_engine->generate(
$block_styles,
$options
);
Expand Down
42 changes: 5 additions & 37 deletions tools/webpack/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ const BUNDLED_PACKAGES = [
'@wordpress/style-engine',
];

// PHP files in packages that have to be copied over to /lib.
const BUNDLED_PACKAGES_PHP = [
{
from: './packages/style-engine/',
to: 'lib/style-engine/',
replaceClasses: [ 'WP_Style_Engine' ],
},
];

const gutenbergPackages = Object.keys( dependencies )
.filter(
( packageName ) =>
Expand Down Expand Up @@ -108,38 +99,15 @@ module.exports = {
...plugins,
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
new CopyWebpackPlugin( {
patterns: [].concat(
gutenbergPackages.map( ( packageName ) => ( {
patterns: gutenbergPackages
.map( ( packageName ) => ( {
from: '*.css',
context: `./packages/${ packageName }/build-style`,
to: `./build/${ packageName }`,
transform: stylesTransform,
noErrorOnMissing: true,
} ) ),
// Packages with PHP files to be parsed and copied to ./lib.
BUNDLED_PACKAGES_PHP.map(
( { from, to, replaceClasses } ) => ( {
from: `${ from }/*.php`,
to: ( { absoluteFilename } ) => {
const [ , filename ] = absoluteFilename.match(
/([\w-]+)(\.php){1,1}$/
);
return join( to, `${ filename }-gutenberg.php` );
},
transform: ( content ) => {
const classSuffix = '_Gutenberg';
content = content.toString();
// Replace class names.
content = content.replace(
new RegExp( replaceClasses.join( '|' ), 'g' ),
( match ) => `${ match }${ classSuffix }`
);
return content;
},
noErrorOnMissing: true,
} )
),
vendorsCopyConfig
),
} ) )
.concat( vendorsCopyConfig ),
} ),
].filter( Boolean ),
};

0 comments on commit 5e0a9c2

Please sign in to comment.