Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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: 20 additions & 5 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ function create_auto_draft_for_template_part_block( $block ) {
'post_type' => 'wp_template_part',
'post_status' => array( 'publish', 'auto-draft' ),
'title' => $block['attrs']['slug'],
'meta_key' => 'theme',
'meta_value' => $block['attrs']['theme'],
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'slug',
'terms' => $block['attrs']['theme'],
),
),
'posts_per_page' => 1,
'no_found_rows' => true,
)
Expand Down Expand Up @@ -180,18 +185,28 @@ function create_auto_draft_for_template_part_block( $block ) {
if ( $template_part_post && $template_part_post->post_content === $file_contents ) {
$template_part_id = $template_part_post->ID;
} else {

// Get the term used for this theme.
$wp_theme_term = get_term_by( 'slug', $block['attrs']['theme'], 'wp_theme' );

// Insert the post.
$template_part_id = wp_insert_post(
array(
'post_content' => $file_contents,
'post_title' => $block['attrs']['slug'],
'post_status' => 'auto-draft',
'post_type' => 'wp_template_part',
'post_name' => $block['attrs']['slug'],
'meta_input' => array(
'theme' => $block['attrs']['theme'],
),
)
);

if ( $wp_theme_term ) {
// If the term was found, assign it to this post.
wp_set_object_terms( $template_part_id, $wp_theme_term->term_id, 'wp_theme' );
} else {
// Using a string instead of integer will create the term and assign it to the post.
wp_set_object_terms( $template_part_id, $block['attrs']['theme'], 'wp_theme' );
}
}
}
}
Expand Down
44 changes: 30 additions & 14 deletions lib/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,7 @@ function gutenberg_register_template_part_post_type() {
),
);

$meta_args = array(
'object_subtype' => 'wp_template_part',
'type' => 'string',
'description' => 'The theme that provided the template part, if any.',
'single' => true,
'show_in_rest' => true,
);

register_post_type( 'wp_template_part', $args );
register_meta( 'post', 'theme', $meta_args );
}
add_action( 'init', 'gutenberg_register_template_part_post_type' );

Expand Down Expand Up @@ -203,10 +194,11 @@ function filter_rest_wp_template_part_query( $args, $request ) {
}

if ( $request['theme'] ) {
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
$meta_query[] = array(
'key' => 'theme',
'value' => $request['theme'],
$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
$tax_query[] = array(
'taxonomy' => 'wp_theme',
'field' => 'slug',
'terms' => $request['theme'],
);

// Ensure auto-drafts of all theme supplied template parts are created.
Expand Down Expand Up @@ -252,9 +244,33 @@ function get_template_part_paths( $base_directory ) {
}
};

$args['meta_query'] = $meta_query;
$args['tax_query'] = $tax_query;
}

return $args;
}
add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 );


/**
* Filters the post data for a response.
*
* @param WP_REST_Response $response The response object.
* @return WP_REST_Response
*/
function filter_rest_prepare_wp_template_part( $response ) {
if ( isset( $response->data ) && is_array( $response->data ) && isset( $response->data['id'] ) ) {
$response->data['wp_theme_slug'] = 'false';

// Get the wp_theme terms.
$wp_themes = wp_get_post_terms( $response->data['id'], 'wp_theme' );

// If a theme is assigned, add it to the REST response.
if ( $wp_themes && is_array( $wp_themes ) && isset( $wp_themes[0] ) ) {
$response->data['wp_theme_slug'] = $wp_themes[0]->slug;
}
}

return $response;
}
add_filter( 'rest_prepare_wp_template_part', 'filter_rest_prepare_wp_template_part' );
17 changes: 17 additions & 0 deletions lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ function gutenberg_register_template_post_type() {
}
add_action( 'init', 'gutenberg_register_template_post_type' );

/**
* Registers block editor 'wp_theme' taxonomy.
*/
function gutenberg_register_wp_theme_taxonomy() {

// Register wp_theme taxonomy.
register_taxonomy(
'wp_theme',
array( 'wp_template', 'wp_template_part' ),
array(
'public' => false,
'show_in_rest' => true,
)
);
}
add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' );

/**
* Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function TemplatePartPlaceholder( { setAttributes } ) {
setAttributes( {
postId: templatePart.id,
slug: templatePart.slug,
theme: templatePart.meta.theme,
theme: templatePart.wp_theme_slug,
} );
}, [ setAttributes ] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ function TemplatePartsByTheme( {
composite,
} ) {
const templatePartsByTheme = useMemo( () => {
return Object.values( groupBy( templateParts, 'meta.theme' ) );
return Object.values( groupBy( templateParts, 'wp_theme_slug' ) );
}, [ templateParts ] );
const currentShownTPs = useAsyncList( templateParts );

return templatePartsByTheme.map( ( templatePartList ) => (
<PanelGroup
key={ templatePartList[ 0 ].meta.theme }
title={ templatePartList[ 0 ].meta.theme }
key={ templatePartList[ 0 ].wp_theme_slug }
title={ templatePartList[ 0 ].wp_theme_slug }
>
{ templatePartList.map( ( templatePart ) => {
return currentShownTPs.includes( templatePart ) ? (
Expand Down Expand Up @@ -164,10 +164,10 @@ function TemplatePartSearchResults( {
// Second prioritize index found in theme.
// Since diacritics can be used in theme names, remove them for the comparison.
return (
deburr( a.meta.theme )
deburr( a.wp_theme_slug )
.toLowerCase()
.indexOf( normalizedFilterValue ) -
deburr( b.meta.theme )
deburr( b.wp_theme_slug )
.toLowerCase()
.indexOf( normalizedFilterValue )
);
Expand All @@ -178,7 +178,10 @@ function TemplatePartSearchResults( {
const currentShownTPs = useAsyncList( filteredTPs );

return filteredTPs.map( ( templatePart ) => (
<PanelGroup key={ templatePart.id } title={ templatePart.meta.theme }>
<PanelGroup
key={ templatePart.id }
title={ templatePart.wp_theme_slug }
>
{ currentShownTPs.includes( templatePart ) ? (
<TemplatePartItem
key={ templatePart.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export default function useTemplatePartPost( postId, slug, theme ) {
const foundPosts = posts?.filter(
( post ) =>
post.slug === cleanedSlug &&
post.meta &&
post.meta.theme === theme
post.wp_theme_slug === theme
);
// A published post might already exist if this template part was customized elsewhere
// or if it's part of a customized template.
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ function render_block_core_template_part( $attributes ) {
$template_part_query = new WP_Query(
array(
'post_type' => 'wp_template_part',
'post_status' => 'publish',
'post_status' => array( 'publish', 'auto-draft' ),
'name' => $attributes['slug'],
'meta_key' => 'theme',
'meta_value' => $attributes['theme'],
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'slug',
'terms' => $attributes['theme'],
),
),
'posts_per_page' => 1,
'no_found_rows' => true,
)
Expand Down