Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin: Extract block editor styles replacement as filter #13625

Merged
merged 3 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,48 @@ function gutenberg_get_available_image_sizes() {
return $all_sizes;
}

/**
* Extends block editor settings to include Gutenberg's `editor-styles.css` as
* taking precedent those styles shipped with core.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_block_editor_styles( $settings ) {
if ( empty( $settings['styles'] ) ) {
$settings['styles'] = array();
} else {
/*
* By handling this filter at an earlier-than-default priority and with
* an understanding that plugins should concatenate (not unshift) their
* own custom styles, it's assumed that the first entry of the styles
* setting should be the default (core) stylesheet.
*/
array_shift( $settings['styles'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's simpler to just loop over the styles are replace the core editor styles if found (checking the path or something).

Copy link
Member Author

@aduth aduth Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace the core editor styles if found (checking the path or something).

The problem is that the array contains only the CSS string, no file paths.

It looks something like:

$settings = array(
	'styles' => array(
		array( 'css' => 'body { /* ... */ }' )
	)
);

So we've got nothing to work from to know whether it's the core style, unless we explicitly (re-)read the ABSPATH . WPINC . '/css/dist/editor/editor-styles.css' file from disk and compare it with the array style contents.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just do that.

}

/*
* The default styles array would include the core `editor-styles.css` and
* a separate style for the locale font family. To avoid overriding the
* localized value, ensure that Gutenberg's editor styles assume the same
* order as core styles (earlier than the font style assignment).
*
* See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L181
*/
array_unshift(
$settings['styles'],
array(
'css' => file_get_contents(
gutenberg_dir_path() . 'build/editor/editor-styles.css'
),
)
);

return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles', 9 );

/**
* Scripts & Styles.
*
Expand Down Expand Up @@ -1037,7 +1079,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
$styles = array(
array(
'css' => file_get_contents(
gutenberg_dir_path() . 'build/editor/editor-styles.css'
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
),
),
);
Expand Down
88 changes: 88 additions & 0 deletions phpunit/class-extend-styles-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Test `gutenberg_extend_block_editor_styles`.
*
* @package Gutenberg
*/

class Extend_Styles_Test extends WP_UnitTestCase {

/**
* Path of the `editor-styles.css` stub file created, or null if one had
* already existed on the filesystem.
*
* @var string|null
*/
protected static $stub_file;

/**
* Contents of the `editor-styles.css` file.
*
* @var string
*/
protected static $style_contents;

public static function wpSetUpBeforeClass() {
self::ensure_editor_styles();
}

public static function wpTearDownAfterClass() {
if ( self::$stub_file ) {
unlink( self::$stub_file );
}
}

/**
* Verifies that an `editor-styles.css` file exists, creating it otherwise,
* and assigning the `style_contents` and `stub_file` class properties.
*/
protected static function ensure_editor_styles() {
$path = gutenberg_dir_path() . 'build/editor/editor-styles.css';

if ( file_exists( $path ) ) {
self::$style_contents = file_get_contents( $path );
self::$stub_file = null;
} else {
self::$style_contents = '';
file_put_contents( $path, self::$style_contents );
self::$stub_file = $path;
}
}

/**
* Tests an unset `styles` setting.
*/
function test_unset_editor_settings_style() {
$settings = array();

$settings = gutenberg_extend_block_editor_styles( $settings );

$this->assertEquals(
array( array( 'css' => self::$style_contents ) ),
$settings['styles']
);
}

/**
* Tests a default `styles` setting.
*/
function test_default_editor_settings_style() {
$settings = array(
'styles' => array(
array( 'css' => 'original' ),
array( 'css' => 'someother' ),
),
);

$settings = gutenberg_extend_block_editor_styles( $settings );

$this->assertEquals(
array(
array( 'css' => self::$style_contents ),
array( 'css' => 'someother' ),
),
$settings['styles']
);
}

}