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

Edit Site: Extract gutenberg_find_template_post helper, use in edit-site-page #21959

Merged
merged 7 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
25 changes: 11 additions & 14 deletions lib/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ function gutenberg_get_editor_styles() {
*/
function gutenberg_edit_site_init( $hook ) {
global
$_wp_current_template_id,
$_wp_current_template_name,
$_wp_current_template_content,
$_wp_current_template_hierarchy,
$_wp_current_template_part_ids,
$current_screen;
Expand Down Expand Up @@ -171,30 +168,30 @@ function gutenberg_edit_site_init( $hook ) {
$template_ids = array();
$template_part_ids = array();
foreach ( $template_getters as $template_getter ) {
call_user_func( $template_getter );
apply_filters( 'template_include', null );
if ( isset( $_wp_current_template_id ) ) {
$template_ids[ $_wp_current_template_name ] = $_wp_current_template_id;
call_user_func( $template_getter ); // This sets $_wp_current_template_hierarchy.

$current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy );
if ( isset( $current_template_post ) ) {
$template_ids[ $current_template_post->post_name ] = $current_template_post->ID;
}
if ( isset( $_wp_current_template_part_ids ) ) {
$template_part_ids = $template_part_ids + $_wp_current_template_part_ids;
}
$_wp_current_template_id = null;
$_wp_current_template_name = null;
$_wp_current_template_content = null;

$_wp_current_template_hierarchy = null;
$_wp_current_template_part_ids = null;
}
get_front_page_template();
get_index_template();
apply_filters( 'template_include', null );
$template_ids[ $_wp_current_template_name ] = $_wp_current_template_id;
$current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy );
$template_ids[ $current_template_post->post_name ] = $current_template_post->ID;
if ( isset( $_wp_current_template_part_ids ) ) {
$template_part_ids = $template_part_ids + $_wp_current_template_part_ids;
}
$settings['templateId'] = $_wp_current_template_id;
$settings['templateId'] = $current_template_post->ID;
$settings['templateType'] = 'wp_template';
$settings['templateIds'] = array_values( $template_ids );
$settings['templateIdsAll'] = $template_ids;
ockham marked this conversation as resolved.
Show resolved Hide resolved
$settings['templatePartIds'] = array_values( $template_part_ids );
$settings['styles'] = gutenberg_get_editor_styles();

Expand All @@ -210,7 +207,7 @@ function gutenberg_edit_site_init( $hook ) {
'/wp/v2/types?context=edit',
'/wp/v2/taxonomies?per_page=-1&context=edit',
'/wp/v2/themes?status=active',
sprintf( '/wp/v2/templates/%s?context=edit', $_wp_current_template_id ),
sprintf( '/wp/v2/templates/%s?context=edit', $current_template_post->ID ),
array( '/wp/v2/media', 'OPTIONS' ),
);
$preload_data = array_reduce(
Expand Down
50 changes: 34 additions & 16 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function gutenberg_add_template_loader_filters() {
add_filter( $template_type . '_template', 'gutenberg_override_query_template', 20, 3 );
}

add_filter( 'template_include', 'gutenberg_find_template', 20 );
add_filter( 'template_include', 'gutenberg_template_include_filter', 20 );
}
add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );

Expand All @@ -53,7 +53,7 @@ function gutenberg_add_template_loader_filters() {
* The method returns an empty result for every template so that a 'wp_template' post
* is used instead.
*
* @see gutenberg_find_template
* @see gutenberg_template_include_filter
*
* @param string $template Path to the template. See locate_template().
* @param string $type Sanitized filename without extension.
Expand Down Expand Up @@ -159,18 +159,45 @@ function create_auto_draft_for_template_part_block( $block ) {
* @param string $template_file Original template file. Will be overridden.
* @return string Path to the canvas file to include.
*/
function gutenberg_find_template( $template_file ) {
global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy;
function gutenberg_template_include_filter( $template_file ) {
global $_wp_current_template_id, $_wp_current_template_content, $_wp_current_template_hierarchy;

// Bail if no relevant template hierarchy was determined, or if the template file
// was overridden another way.
if ( ! $_wp_current_template_hierarchy || $template_file ) {
return $template_file;
}

$current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy );

if ( $current_template_post ) {
$_wp_current_template_id = $current_template_post->ID;
$_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content;
}

// Add extra hooks for template canvas.
add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );

// This file will be included instead of the theme's template file.
return gutenberg_dir_path() . 'lib/template-canvas.php';
}

/**
* Return the correct 'wp_template' post for the current template hierarchy.
*
* @param string[] $slugs The current template hierarchy, ordered by priority.
ockham marked this conversation as resolved.
Show resolved Hide resolved
* @return WP_Post|null A template post object, or null if none could be found.
ockham marked this conversation as resolved.
Show resolved Hide resolved
*/
function gutenberg_find_template_post( $template_hierarchy ) {
if ( ! $template_hierarchy ) {
return null;
}

$slugs = array_map(
'gutenberg_strip_php_suffix',
$_wp_current_template_hierarchy
$template_hierarchy
);

// Find most specific 'wp_template' post matching the hierarchy.
Expand Down Expand Up @@ -257,18 +284,9 @@ function gutenberg_find_template( $template_file ) {
create_auto_draft_for_template_part_block( $block );
}
}
$_wp_current_template_id = $current_template_post->ID;
$_wp_current_template_name = $current_template_post->post_name;
$_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content;
return $current_template_post;
}

// Add extra hooks for template canvas.
add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 );
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', 'gutenberg_render_title_tag', 1 );

// This file will be included instead of the theme's template file.
return gutenberg_dir_path() . 'lib/template-canvas.php';
return null;
}

/**
Expand Down