-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
FSE: Fall back to next best template in hierarchy when querying through REST API (Take Two) #41848
Changes from 5 commits
19c9a97
e5ce7f3
d414e0e
c9ca559
62953df
cdc7395
d1ee1f7
e0ff8b5
f090473
c062ef5
0d92f7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -165,6 +165,22 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t | |||||
return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); | ||||||
} | ||||||
|
||||||
function gutenberg_get_template_slugs( $template ) { | ||||||
$limit = 2; | ||||||
if ( strpos( $template, 'single-' ) === 0 || strpos( $template, 'taxonomy-' ) === 0 ) { | ||||||
// E.g. single-post-mypost or taxonomy-recipes-vegetarian. | ||||||
$limit = 3; | ||||||
} | ||||||
$parts = explode( '-', $template, $limit ); | ||||||
$type = array_shift( $parts ); | ||||||
$slugs = array( $type ); | ||||||
|
||||||
foreach ( $parts as $part ) { | ||||||
array_unshift( $slugs, $slugs[0] . '-' . $part ); | ||||||
} | ||||||
return $slugs; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Retrieves a single unified template object using its id. | ||||||
* | ||||||
|
@@ -195,34 +211,58 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { | |||||
if ( count( $parts ) < 2 ) { | ||||||
return null; | ||||||
} | ||||||
list( $theme, $slug ) = $parts; | ||||||
$wp_query_args = array( | ||||||
'post_name__in' => array( $slug ), | ||||||
'post_type' => $template_type, | ||||||
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), | ||||||
'posts_per_page' => 1, | ||||||
'no_found_rows' => true, | ||||||
'tax_query' => array( | ||||||
array( | ||||||
'taxonomy' => 'wp_theme', | ||||||
'field' => 'name', | ||||||
'terms' => $theme, | ||||||
), | ||||||
), | ||||||
); | ||||||
$template_query = new WP_Query( $wp_query_args ); | ||||||
$posts = $template_query->posts; | ||||||
list( , $slug ) = $parts; | ||||||
|
||||||
if ( count( $posts ) > 0 ) { | ||||||
$template = gutenberg_build_block_template_result_from_post( $posts[0] ); | ||||||
$templates = gutenberg_get_template_slugs( $slug ); | ||||||
$block_template = resolve_block_template( $slug, $templates, '' ); | ||||||
if ( ! $block_template ) { | ||||||
$block_template = resolve_block_template( 'index', array(), '' ); | ||||||
} | ||||||
// This might give us a fallback template with a different ID, | ||||||
// so we have to override it to make sure it's correct. | ||||||
$block_template->id = $id; | ||||||
$block_template->slug = $slug; | ||||||
$default_template_types = get_default_block_template_types(); | ||||||
|
||||||
$slug_parts = explode( '-', $slug, 3 ); | ||||||
if ( count( $slug_parts ) > 1 ) { | ||||||
if ( 'single' === $slug_parts [0] ) { | ||||||
// Get CPT labels | ||||||
$post_type = get_post_type_object( $slug_parts[1] ); | ||||||
$labels = $post_type->labels; | ||||||
|
||||||
if ( ! is_wp_error( $template ) ) { | ||||||
return $template; | ||||||
if ( count( $slug_parts ) > 2 ) { | ||||||
// Now we look for the CPT with slug as defined in $slug_parts[2] | ||||||
$post = get_page_by_path( $slug_parts[2], OBJECT, $slug_parts[1] ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also can't we just do this?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid that won't work -- If it's ( |
||||||
$block_template->title = sprintf( | ||||||
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress" | ||||||
__( '%1$s: %2$s' ), | ||||||
$labels->singular_name, | ||||||
$post->post_title | ||||||
); | ||||||
$block_template->description = sprintf( | ||||||
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress" | ||||||
__( 'Template for %1$s' ), | ||||||
$block_template->title | ||||||
); | ||||||
} else { | ||||||
$block_template->title = sprintf( | ||||||
// translators: %s: Name of the post type e.g: "Post". | ||||||
__( 'Single item: %s' ), | ||||||
$labels->singular_name | ||||||
); | ||||||
$block_template->description = sprintf( | ||||||
// translators: %s: Name of the post type e.g: "Post". | ||||||
__( 'Displays a single item: %s.' ), | ||||||
$labels->singular_name | ||||||
); | ||||||
} | ||||||
} | ||||||
} elseif ( array_key_exists( $slug, $default_template_types ) ) { | ||||||
$block_template->title = $default_template_types[ $slug ]['title']; | ||||||
$block_template->description = $default_template_types[ $slug ]['description']; | ||||||
} | ||||||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
$block_template = get_block_file_template( $id, $template_type ); | ||||||
|
||||||
/** | ||||||
* Filters the queried block template object after it's been fetched. | ||||||
* | ||||||
|
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.
Why use this function over a WP_Query?
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.
WP_Query
is probably the better option;get_page_by_path
was the stop-gap to make it work 👍