Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class Gutenberg_REST_Old_Templates_Controller extends WP_REST_Templates_Controller {
public function get_template_fallback( $request ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return parent::get_template_fallback( $request );
}
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );

do {
Expand Down
42 changes: 37 additions & 5 deletions lib/compat/wordpress-6.9/template-activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ function gutenberg_get_migrated_active_templates() {
return $active_templates;
}

// Only load template activation system if the experiment is enabled.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return;
}

require_once __DIR__ . '/class-gutenberg-rest-old-templates-controller.php';

// How does this work?
// 1. For wp_template, we remove the custom templates controller, so it becomes
// a normal posts endpoint, modified slightly to allow auto-drafts.
add_filter( 'register_post_type_args', 'gutenberg_modify_wp_template_post_type_args', 10, 2 );
function gutenberg_modify_wp_template_post_type_args( $args, $post_type ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $args;
}
if ( 'wp_template' === $post_type ) {
$args['rest_base'] = 'created-templates';
$args['rest_controller_class'] = 'WP_REST_Posts_Controller';
Expand All @@ -75,6 +74,10 @@ function gutenberg_modify_wp_template_post_type_args( $args, $post_type ) {
* @global array $wp_post_types List of post types.
*/
function gutenberg_maintain_templates_routes() {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return;
}
// This should later be changed in core so we don't need initialize
// WP_REST_Templates_Controller with a post type.
global $wp_post_types;
Expand Down Expand Up @@ -185,6 +188,10 @@ function gutenberg_setup_static_template() {

add_filter( 'pre_wp_unique_post_slug', 'gutenberg_allow_template_slugs_to_be_duplicated', 10, 5 );
function gutenberg_allow_template_slugs_to_be_duplicated( $override, $slug, $post_id, $post_status, $post_type ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $override;
}
return 'wp_template' === $post_type ? $slug : $override;
}

Expand Down Expand Up @@ -267,6 +274,10 @@ function ( $registered_template ) use ( $template_files ) {
}

function gutenberg_get_template( $template, $type, $templates ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $template;
}
$template = locate_template( $templates );
return gutenberg_locate_block_template( $template, $type, $templates );
}
Expand Down Expand Up @@ -444,6 +455,11 @@ static function ( $template_a, $template_b ) use ( $slug_priorities ) {
function gutenberg_locate_block_template( $template, $type, array $templates ) {
global $_wp_current_template_content, $_wp_current_template_id;

// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $template;
}

if ( ! current_theme_supports( 'block-templates' ) ) {
return $template;
}
Expand Down Expand Up @@ -533,6 +549,10 @@ function gutenberg_locate_block_template( $template, $type, array $templates ) {
remove_action( 'rest_pre_insert_wp_template', 'wp_assign_new_template_to_theme', 9 );
add_action( 'rest_pre_insert_wp_template', 'gutenberg_set_active_template_theme', 9, 2 );
function gutenberg_set_active_template_theme( $changes, $request ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $changes;
}
if ( str_starts_with( $request->get_route(), '/wp/v2/templates' ) ) {
return $changes;
}
Expand All @@ -549,6 +569,10 @@ function gutenberg_set_active_template_theme( $changes, $request ) {

add_action( 'save_post_wp_template', 'gutenberg_maybe_update_active_templates' );
function gutenberg_maybe_update_active_templates( $post_id ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return;
}
$post = get_post( $post_id );
$is_inactive_by_default = get_post_meta( $post_id, 'is_inactive_by_default', true );
if ( $is_inactive_by_default ) {
Expand All @@ -565,6 +589,10 @@ function gutenberg_maybe_update_active_templates( $post_id ) {
// This function is a copy of core's, except for the marked section. //
///////////////////////////////////////////////////////////////////////
function gutenberg_get_block_template( $output, $id, $template_type ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $output;
}
if ( 'wp_template' !== $template_type ) {
return $output;
}
Expand Down Expand Up @@ -648,6 +676,10 @@ function gutenberg_get_block_template( $output, $id, $template_type ) {
// This function is a copy of core's, except for the marked section. //
///////////////////////////////////////////////////////////////////////
function gutenberg_get_block_templates( $output, $query = array(), $template_type = 'wp_template' ) {
// Check active_templates experiment status.
if ( ! gutenberg_is_experiment_enabled( 'active_templates' ) ) {
return $output;
}
if ( 'wp_template' !== $template_type ) {
return $output;
}
Expand Down
Loading