-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block-based template parts for Classic themes #42729
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
91c37c1
WIP: Block-based template parts
Mamaduka 4322444
Only display template parts menu
Mamaduka a9c3643
pinking shears
Mamaduka 4f08c67
Add enqueue_block_editor_assets
Mamaduka 33aeb83
Remove whitespace
Mamaduka 401a457
Hide Global Styles
Mamaduka b19b2a7
Disable template creation
Mamaduka 21dc6d0
Disable make template part action for blocks
Mamaduka 1849a37
Update lib/compat/wordpress-6.1/template-parts-screen.php
Mamaduka 61182c1
Update lib/compat/wordpress-6.1/template-parts-screen.php
Mamaduka 6bc79e8
Update lib/compat/wordpress-6.1/template-parts-screen.php
Mamaduka dac54de
Remove trailing comma
Mamaduka 1ff5e82
Make sure supportsTemplatePartsMode is false for block themes
Mamaduka 666207d
pinking shears
Mamaduka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
<?php | ||
/** | ||
* Bootstrapping the Gutenberg Template Parts screen & integration. | ||
* | ||
* This isn't a new file for wp-admin; just override necessary for `site-editor.php`. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Register Template Parts submenu. | ||
* | ||
* This should be handled directly in wp-admin/menu.php. | ||
* | ||
* @return void | ||
*/ | ||
function gutenberg_template_parts_screen_menu() { | ||
// Only add page for Classic theme that support the feature. | ||
if ( wp_is_block_theme() ) { | ||
return; | ||
} | ||
|
||
if ( ! current_theme_supports( 'block-template-parts' ) ) { | ||
return; | ||
} | ||
|
||
add_theme_page( | ||
__( 'Template Parts', 'gutenberg' ), | ||
__( 'Template Parts', 'gutenberg' ), | ||
'edit_theme_options', | ||
'gutenberg-template-parts', | ||
'gutenberg_template_parts_screen_render' | ||
); | ||
} | ||
add_action( 'admin_menu', 'gutenberg_template_parts_screen_menu' ); | ||
|
||
/** | ||
* The page permissions and redirections. | ||
* | ||
* @return void | ||
*/ | ||
function gutenberg_template_parts_screen_permissions() { | ||
if ( ! current_theme_supports( 'block-template-parts' ) ) { | ||
wp_die( __( 'The theme you are currently using doesn\'t block-based template parts.', 'gutenberg' ) ); | ||
} | ||
|
||
/** | ||
* Should be handled directly in /wp-admin/menu.php. | ||
* Path: `site-editor.php?postType=wp_template_part`. | ||
*/ | ||
if ( ! isset( $_GET['postType'] ) ) { | ||
$redirect_url = add_query_arg( | ||
array( 'postType' => 'wp_template_part' ), | ||
admin_url( 'themes.php?page=gutenberg-template-parts' ) | ||
); | ||
wp_safe_redirect( $redirect_url ); | ||
exit; | ||
} | ||
} | ||
add_action( 'load-appearance_page_gutenberg-template-parts', 'gutenberg_template_parts_screen_permissions' ); | ||
|
||
/** | ||
* Initialize the editor for the screen. Most of this is copied from `site-editor.php`. | ||
* | ||
* Note: Parts that need to be ported back should have inline comments. | ||
* | ||
* @param string $hook Current page hook. | ||
* @return void | ||
*/ | ||
function gutenberg_template_parts_screen_init( $hook ) { | ||
global $current_screen, $editor_styles; | ||
|
||
if ( 'appearance_page_gutenberg-template-parts' !== $hook ) { | ||
return; | ||
} | ||
|
||
// Flag that we're loading the block editor. | ||
$current_screen->is_block_editor( true ); | ||
|
||
// Default to is-fullscreen-mode to avoid jumps in the UI. | ||
add_filter( | ||
'admin_body_class', | ||
static function( $classes ) { | ||
return "$classes is-fullscreen-mode"; | ||
} | ||
); | ||
|
||
$indexed_template_types = array(); | ||
foreach ( get_default_block_template_types() as $slug => $template_type ) { | ||
$template_type['slug'] = (string) $slug; | ||
$indexed_template_types[] = $template_type; | ||
} | ||
|
||
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); | ||
$custom_settings = array( | ||
'siteUrl' => site_url(), | ||
'postsPerPage' => get_option( 'posts_per_page' ), | ||
'styles' => get_block_editor_theme_styles(), | ||
'defaultTemplateTypes' => $indexed_template_types, | ||
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), | ||
'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(), | ||
'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ), | ||
'__unstableHomeTemplate' => gutenberg_resolve_home_template(), | ||
); | ||
|
||
/** | ||
* We don't need home template resolution when block template parts are supported. | ||
* Set the value to true to satisfy the editor initialization guard clause. | ||
*/ | ||
if ( $custom_settings['supportsTemplatePartsMode'] ) { | ||
$custom_settings['__unstableHomeTemplate'] = true; | ||
} | ||
|
||
// Add additional back-compat patterns registered by `current_screen` et al. | ||
$custom_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true ); | ||
$custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true ); | ||
|
||
$editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context ); | ||
|
||
if ( isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ) ) { | ||
$post_type = get_post_type_object( $_GET['postType'] ); | ||
if ( ! $post_type ) { | ||
wp_die( __( 'Invalid post type.', 'gutenberg' ) ); | ||
} | ||
} | ||
|
||
$active_global_styles_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); | ||
$active_theme = wp_get_theme()->get_stylesheet(); | ||
$preload_paths = array( | ||
array( '/wp/v2/media', 'OPTIONS' ), | ||
'/wp/v2/types?context=view', | ||
'/wp/v2/types/wp_template?context=edit', | ||
'/wp/v2/types/wp_template-part?context=edit', | ||
'/wp/v2/templates?context=edit&per_page=-1', | ||
'/wp/v2/template-parts?context=edit&per_page=-1', | ||
'/wp/v2/themes?context=edit&status=active', | ||
'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit', | ||
'/wp/v2/global-styles/' . $active_global_styles_id, | ||
'/wp/v2/global-styles/themes/' . $active_theme, | ||
); | ||
|
||
block_editor_rest_api_preload( $preload_paths, $block_editor_context ); | ||
|
||
wp_add_inline_script( | ||
'wp-edit-site', | ||
sprintf( | ||
'wp.domReady( function() { | ||
wp.editSite.initializeEditor( "site-editor", %s ); | ||
} );', | ||
wp_json_encode( $editor_settings ) | ||
) | ||
); | ||
|
||
// Preload server-registered block schemas. | ||
wp_add_inline_script( | ||
'wp-blocks', | ||
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' | ||
); | ||
|
||
wp_add_inline_script( | ||
'wp-blocks', | ||
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ) | ||
); | ||
|
||
wp_enqueue_script( 'wp-edit-site' ); | ||
wp_enqueue_script( 'wp-format-library' ); | ||
wp_enqueue_style( 'wp-edit-site' ); | ||
wp_enqueue_style( 'wp-format-library' ); | ||
wp_enqueue_media(); | ||
|
||
if ( | ||
current_theme_supports( 'wp-block-styles' ) || | ||
( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) | ||
) { | ||
wp_enqueue_style( 'wp-block-library-theme' ); | ||
} | ||
Mamaduka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** This action is documented in wp-admin/edit-form-blocks.php */ | ||
do_action( 'enqueue_block_editor_assets' ); | ||
} | ||
add_action( 'admin_enqueue_scripts', 'gutenberg_template_parts_screen_init' ); | ||
|
||
/** | ||
* The main entry point for the screen. | ||
* | ||
* @return void | ||
*/ | ||
function gutenberg_template_parts_screen_render() { | ||
echo '<div id="site-editor" class="edit-site"></div>'; | ||
} | ||
|
||
/** | ||
* Register the new theme feature. | ||
* | ||
* Migrates into `create_initial_theme_features` method. | ||
* | ||
* @return void | ||
*/ | ||
function gutenberg_register_template_parts_theme_feature() { | ||
register_theme_feature( | ||
'block-template-parts', | ||
array( | ||
'description' => __( 'Whether a theme uses block-based template parts.', 'gutenberg' ), | ||
'show_in_rest' => true, | ||
) | ||
); | ||
} | ||
add_action( 'setup_theme', 'gutenberg_register_template_parts_theme_feature', 5 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mamaduka @scruffian @fabiankaegy @Soean We should use the
WP_Theme_JSON_Resolver_Gutenberg
class instead of the core one. Fix for this at #49198