Skip to content

Commit

Permalink
Roll back API changes and insert into editor settings under `currentT…
Browse files Browse the repository at this point in the history
…heme` both parent and child dir

Update tests to check for image paths with leading slash

Revert rogue tab
  • Loading branch information
ramonjd committed Apr 11, 2024
1 parent 4a0d107 commit fde4758
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 56 deletions.
7 changes: 7 additions & 0 deletions lib/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ function gutenberg_get_block_editor_settings( $settings ) {
}
}

// Could house also `__unstableIsBlockBasedTheme` as 'isBlockTheme' => $current_theme->is_block_theme().
$current_theme = wp_get_theme();
$settings['currentTheme'] = array(
'parentDirectoryURI' => $current_theme->get_template_directory_uri(),
'directoryURI' => $current_theme->get_stylesheet_directory_uri(),
);

$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );

$settings['__experimentalFeatures'] = gutenberg_get_global_settings();
Expand Down
7 changes: 5 additions & 2 deletions lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ function gutenberg_get_background_support_styles( $background_styles = array() )
/*
* "theme" source implies relative path to the theme directory
*/
if ( ! empty( $background_styles['backgroundImage']['url'] ) && 'theme' === $background_image_source ) {
$background_styles['backgroundImage']['url'] = esc_url( get_theme_file_uri( $background_styles['backgroundImage']['url'] ) );
if ( ! empty( $background_styles['backgroundImage']['url'] ) && is_string( $background_styles['backgroundImage']['url'] ) && 'theme' === $background_image_source ) {
if ( ! str_starts_with( $background_styles['backgroundImage']['url'], '/' ) ) {
$background_styles['backgroundImage']['url'] = '/' . $background_styles['backgroundImage']['url'];
}
$background_styles['backgroundImage']['url'] = esc_url( get_stylesheet_directory_uri() . $background_styles['backgroundImage']['url'] );
}
return gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );
}
Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,7 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
return $result;
}

/**
/**
* Given a styles array, it extracts the style properties
* and adds them to the $declarations array following the format:
*
Expand Down
33 changes: 0 additions & 33 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,3 @@ function wp_api_template_access_controller( $args, $post_type ) {
}
}
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 );


/**
* Registers additional fields for WP_REST_Themes_Controller class.
* https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php
*/
function gutenberg_register_wp_theme_directory_uri_field() {
register_rest_field(
'theme',
'theme_directory_uri',
array(
'get_callback' => function ( $item ) {
/*
* In the rest controller, we can use the WP_Theme object methods to get the theme directory URI.
* See: https://developer.wordpress.org/reference/classes/wp_theme/
*/
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
return $theme->get_stylesheet_directory_uri();
}
return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'URL to the directory of the theme root.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}

add_action( 'rest_api_init', 'gutenberg_register_wp_theme_directory_uri_field' );
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ const getFeatureDeclarations = ( selectors, styles ) => {
* @param {Object} tree A theme.json tree containing layout definitions.
*
* @param {boolean} isTemplate Whether the entity being edited is a full template or a pattern.
* @param {Object} editorSettings Current editor settings.
* @return {Array} An array of style declarations.
*/
export function getStylesDeclarations(
Expand All @@ -323,7 +324,7 @@ export function getStylesDeclarations(
useRootPaddingAlign,
tree = {},
isTemplate = true,
settings
editorSettings
) {
const { kebabCase } = unlock( componentsPrivateApis );
const isRoot = ROOT_BLOCK_SELECTOR === selector;
Expand Down Expand Up @@ -402,7 +403,7 @@ export function getStylesDeclarations(
...blockStyles.background,
...getBackgroundSupportStyles(
blockStyles.background,
settings
editorSettings
),
},
};
Expand Down Expand Up @@ -794,7 +795,7 @@ export const toStyles = (
hasFallbackGapSupport,
disableLayoutStyles = false,
isTemplate = true,
settings = {}
editorSettings = {}
) => {
const nodesWithStyles = getNodesWithStyles( tree, blockSelectors );
const nodesWithSettings = getNodesWithSettings( tree, blockSelectors );
Expand Down Expand Up @@ -905,7 +906,7 @@ export const toStyles = (
styleVariationSelector,
useRootPaddingAlign,
tree,
settings,
editorSettings
);
if ( styleVariationDeclarations.length ) {
ruleset += `${ styleVariationSelector }{${ styleVariationDeclarations.join(
Expand Down Expand Up @@ -954,7 +955,7 @@ export const toStyles = (
useRootPaddingAlign,
tree,
isTemplate,
settings
editorSettings
);
if ( declarations?.length ) {
ruleset += `:where(${ selector }){${ declarations.join(
Expand Down Expand Up @@ -1219,7 +1220,7 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
const { themeDirURI, disableLayoutStyles } = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeDirURI: _settings?.currentTheme?.theme_directory_uri,
themeDirURI: _settings?.currentTheme?.directoryURI,
disableLayoutStyles: !! _settings.disableLayoutStyles,
};
} );
Expand Down
11 changes: 7 additions & 4 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ export function omitStyle( style, paths, preserveReference = false ) {
* @param {Object|string} blockNameOrType Block type.
* @param {Object} attributes Block attributes.
* @param {?Record<string, string[]>} skipPaths An object of keys and paths to skip serialization.
*
* @param {Object} editorSettings Current editor settings.
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps(
props,
blockNameOrType,
attributes,
skipPaths = skipSerializationPathsSave,
settings,
editorSettings
) {
if ( ! hasStyleSupport( blockNameOrType ) ) {
return props;
Expand Down Expand Up @@ -326,7 +326,10 @@ export function addSaveProps(
...style,
background: {
...style.background,
...getBackgroundSupportStyles( style.background, settings ),
...getBackgroundSupportStyles(
style.background,
editorSettings
),
},
};
}
Expand Down Expand Up @@ -391,7 +394,7 @@ function useBlockProps( { name, style } ) {
const { themeDirURI } = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeDirURI: _settings?.currentTheme?.theme_directory_uri,
themeDirURI: _settings?.currentTheme?.directoryURI,
};
} );
const blockElementsContainerIdentifier = `wp-elements-${ useInstanceId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const BLOCK_EDITOR_SETTINGS = [
'clearBlockSelection',
'codeEditingEnabled',
'colors',
'currentTheme',
'disableCustomColors',
'disableCustomFontSizes',
'disableCustomSpacingSizes',
Expand Down Expand Up @@ -96,7 +97,6 @@ function useBlockEditorSettings( settings, postType, postId ) {
const {
allowRightClickOverrides,
blockTypes,
currentTheme,
focusMode,
hasFixedToolbar,
isDistractionFree,
Expand All @@ -117,24 +117,19 @@ function useBlockEditorSettings( settings, postType, postId ) {
getEntityRecord,
getUserPatternCategories,
getBlockPatternCategories,
getCurrentTheme,
} = select( coreStore );
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;
const _currentTheme = getCurrentTheme();

return {
allowRightClickOverrides: get(
'core',
'allowRightClickOverrides'
),
blockTypes: getBlockTypes(),
currentTheme: {
name: _currentTheme?.name,
theme_directory_uri: _currentTheme?.theme_directory_uri,
},
canUseUnfilteredHTML: getRawEntityRecord(
'postType',
postType,
Expand Down Expand Up @@ -245,7 +240,6 @@ function useBlockEditorSettings( settings, postType, postId ) {
),
allowedBlockTypes,
allowRightClickOverrides,
currentTheme,
focusMode: focusMode && ! forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
Expand Down Expand Up @@ -307,7 +301,6 @@ function useBlockEditorSettings( settings, postType, postId ) {
pageForPosts,
postType,
setIsInserterOpened,
currentTheme,
]
);
}
Expand Down
11 changes: 10 additions & 1 deletion phpunit/block-supports/background-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,16 @@ public function test_get_background_support_styles( $background_style, $expected
*/
public function data_get_background_support_styles() {
return array(
'css generated with theme source 2' => array(
'css generated with theme background image path' => array(
'background_style' => array(
'backgroundImage' => array(
'url' => '/assets/image/not_there.png',
'source' => 'theme',
),
),
'expected_css' => "background-image:url('http://localhost:8889/wp-content/plugins/gutenberg/phpunit/data/themedir1/block-theme/assets/image/not_there.png');",
),
'css generated with theme background image path and no preceding slash' => array(
'background_style' => array(
'backgroundImage' => array(
'url' => 'assets/image/not_there.png',
Expand Down

0 comments on commit fde4758

Please sign in to comment.