Skip to content

Commit

Permalink
Getting URI from themes API response
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Apr 29, 2024
1 parent cf77710 commit dab63c1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lib/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ function gutenberg_get_block_editor_settings( $settings ) {
}
}

$current_theme = wp_get_theme();
$settings['currentTheme'] = array(
// Directory of the current theme. Will be the child theme directory if a child theme is active.
'directoryURI' => $current_theme->get_stylesheet_directory_uri(),
// `__unstableIsBlockBasedTheme` could also be relocated here, e.g., 'isBlockTheme' => $current_theme->is_block_theme().
);

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

$settings['__experimentalFeatures'] = gutenberg_get_global_settings();
Expand Down
73 changes: 73 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,76 @@ function wp_api_template_access_controller( $args, $post_type ) {
}
}
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 );


if ( ! function_exists( 'gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field' ) ) {
/**
* Adds `stylesheet_uri` fields to WP_REST_Themes_Controller class.
* Core ticket: https://core.trac.wordpress.org/ticket/61021
*/
function gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field() {
register_rest_field(
'theme',
'stylesheet_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_stylesheet_directory_uri();
} else {
return $theme->get_stylesheet_directory_uri();
}
}

return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field' );

if ( ! function_exists( 'gutenberg_register_wp_rest_themes_template_directory_uri_field' ) ) {
/**
* Adds `template_uri` fields to WP_REST_Themes_Controller class.
* Core ticket: https://core.trac.wordpress.org/ticket/61021
*/
function gutenberg_register_wp_rest_themes_template_directory_uri_field() {
register_rest_field(
'theme',
'template_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_template_directory_uri();
} else {
return $theme->get_template_directory_uri();
}
}

return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_template_directory_uri_field' );
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
const { themeDirURI, disableLayoutStyles } = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeDirURI: _settings?.currentTheme?.directoryURI,
themeDirURI: _settings?.currentTheme?.stylesheetURI,
disableLayoutStyles: !! _settings.disableLayoutStyles,
};
} );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ function useBlockProps( { name, style } ) {
const { themeDirURI } = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeDirURI: _settings?.currentTheme?.directoryURI,
themeDirURI: _settings?.currentTheme?.stylesheetURI,
};
} );
const blockElementsContainerIdentifier = `wp-elements-${ useInstanceId(
Expand Down
8 changes: 8 additions & 0 deletions packages/core-data/src/entity-types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ declare module './base-entity-records' {
* The theme's stylesheet. This uniquely identifies the theme.
*/
stylesheet: string;
/**
* The uri for the theme's stylesheet directory
*/
stylesheet_uri: string;
/**
* The theme's template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme's stylesheet.
*/
template: string;
/**
* The uri for the theme's template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme's stylesheet directory.
*/
temnplate_uri: string;
/**
* The theme author.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
const {
allowRightClickOverrides,
blockTypes,
currentTheme,
focusMode,
hasFixedToolbar,
isDistractionFree,
Expand All @@ -123,7 +124,9 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
getEntityRecord,
getUserPatternCategories,
getBlockPatternCategories,
getCurrentTheme,
} = select( coreStore );
const _currentTheme = getCurrentTheme();
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const { getBlocksByName, getBlockAttributes } =
Expand Down Expand Up @@ -156,6 +159,9 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
postType,
postId
)?._links?.hasOwnProperty( 'wp:action-unfiltered-html' ),
currentTheme: {
stylesheetURI: _currentTheme?.stylesheet_uri,
},
focusMode: get( 'core', 'focusMode' ),
hasFixedToolbar:
get( 'core', 'fixedToolbar' ) || ! isLargeViewport,
Expand Down Expand Up @@ -262,6 +268,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
),
allowedBlockTypes,
allowRightClickOverrides,
currentTheme,
focusMode: focusMode && ! forceDisableFocusMode,
hasFixedToolbar,
isDistractionFree,
Expand Down

0 comments on commit dab63c1

Please sign in to comment.