Skip to content

Commit

Permalink
Try setting the wp_theme term again
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 21, 2024
1 parent f714ec0 commit 205296d
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/compat/wordpress-6.8/template-activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,28 @@ function gutenberg_pre_get_block_templates( $output, $query, $template_type ) {
return $output;
}

// Whenever templates are queried, include all templates, not just the ones with
// a matching theme. See:
// https://github.com/WordPress/wordpress-develop/blob/94a798b54b4be308cfafb2db09bbed9c3ea9ff34/src/wp-includes/block-template-utils.php#L1111-L1117
// Whenever templates are queried by slug, never return any user templates.
// We are handling that in gutenberg_pre_get_block_templates.
function gutenberg_remove_tax_query_for_templates( $query ) {
if ( isset( $query->query['post_type'] ) && 'wp_template' === $query->query['post_type'] ) {
$query->set( 'tax_query', array() );
// We don't have templates with this status, that's the point. We want

Check failure on line 94 in lib/compat/wordpress-6.8/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
// this query to not return any user templates.

Check failure on line 95 in lib/compat/wordpress-6.8/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
$query->set( 'post_status', array( 'pending' ) );
}
}

add_filter( 'pre_get_block_templates', 'gutenberg_tax_pre_get_block_templates', 10, 3 );
add_action( 'pre_get_block_template', 'gutenberg_tax_pre_get_block_templates', 10, 3 );
function gutenberg_tax_pre_get_block_templates( $output, $query, $template_type ) {
// Do not remove the tax query when querying for a specific slug.
if ( 'wp_template' === $template_type && ( is_numeric( $query ) || empty( $query['slug__in'] ) ) ) {
if ( 'wp_template' === $template_type && ! empty( $query['slug__in'] ) ) {
add_action( 'pre_get_posts', 'gutenberg_remove_tax_query_for_templates' );
}
return $output;
}

add_filter( 'get_block_templates', 'gutenberg_tax_get_block_templates', 10, 3 );
add_action( 'get_block_template', 'gutenberg_tax_get_block_templates', 10, 3 );
function gutenberg_tax_get_block_templates( $output, $query, $template_type ) {
if ( 'wp_template' === $template_type && ( is_numeric( $query ) || empty( $query['slug__in'] ) ) ) {
if ( 'wp_template' === $template_type && ! empty( $query['slug__in'] ) ) {
remove_action( 'pre_get_posts', 'gutenberg_remove_tax_query_for_templates' );
}
return $output;
Expand All @@ -133,3 +132,19 @@ function gutenberg_get_the_terms( $terms, $object_id, $taxonomy ) {
}
return $terms;
}


// We need to set the theme for the template when it's created. See:
// https://github.com/WordPress/wordpress-develop/blob/b2c8d8d2c8754cab5286b06efb4c11e2b6aa92d5/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php#L571-L578
function gutenberg_set_active_template_theme( $changes, $request ) {
$template = $request['id'] ? get_block_template( $request['id'], 'wp_template' ) : null;
if ( $template ) {
return $changes;
}
$changes->tax_input = array(
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : get_stylesheet(),
);
return $changes;
}

add_action( 'rest_pre_insert_wp_template', 'gutenberg_set_active_template_theme', 10, 2 );

0 comments on commit 205296d

Please sign in to comment.