Make WordPress Core


Ignore:
Timestamp:
09/20/2024 02:05:50 AM (2 years ago)
Author:
noisysocks
Message:

Editor: Add plugin template registration API and improve theme overrides for plugin-registered templates

This commit introduces a new API to allow plugins to easily register block
templates with wp_register_block_template() and the
WP_Block_Templates_Registry class, addressing the complexity of hooking into
multiple filters. It also ensures plugin-registered templates overridden by
themes fall back to the plugin-provided title and description when the theme
doesn't define them.

See https://github.com/WordPress/gutenberg/pull/61577.
See https://github.com/WordPress/gutenberg/pull/64610.

Fixes #61804.
Props aljullu, peterwilsoncc, antonvlasenko, azaozz, youknowriad, noisysocks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r59003 r59073  
    592592        $template->is_custom      = true;
    593593        $template->modified       = null;
     594
     595        if ( 'wp_template' === $template_type ) {
     596                $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
     597                if ( $registered_template ) {
     598                        $template->plugin      = $registered_template->plugin;
     599                        $template->title       = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
     600                        $template->description = empty( $template->description ) ? $registered_template->description : $template->description;
     601                }
     602        }
    594603
    595604        if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
     
    10151024        }
    10161025
     1026        if ( 'wp_template' === $post->post_type ) {
     1027                $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
     1028                if ( $registered_template ) {
     1029                        $template->plugin      = $registered_template->plugin;
     1030                        $template->origin      =
     1031                                'theme' !== $template->origin && 'theme' !== $template->source ?
     1032                                'plugin' :
     1033                                $template->origin;
     1034                        $template->title       = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
     1035                        $template->description = empty( $template->description ) ? $registered_template->description : $template->description;
     1036                }
     1037        }
     1038
    10171039        $hooked_blocks = get_hooked_blocks();
    10181040        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
     
    11581180                        $query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
    11591181                }
     1182
     1183                if ( 'wp_template' === $template_type ) {
     1184                        // Add templates registered in the template registry. Filtering out the ones which have a theme file.
     1185                        $registered_templates          = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
     1186                        $matching_registered_templates = array_filter(
     1187                                $registered_templates,
     1188                                function ( $registered_template ) use ( $template_files ) {
     1189                                        foreach ( $template_files as $template_file ) {
     1190                                                if ( $template_file['slug'] === $registered_template->slug ) {
     1191                                                        return false;
     1192                                                }
     1193                                        }
     1194                                        return true;
     1195                                }
     1196                        );
     1197                        $query_result                  = array_merge( $query_result, $matching_registered_templates );
     1198                }
    11601199        }
    11611200
     
    12881327        list( $theme, $slug ) = $parts;
    12891328
    1290         if ( get_stylesheet() !== $theme ) {
    1291                 /** This filter is documented in wp-includes/block-template-utils.php */
    1292                 return apply_filters( 'get_block_file_template', null, $id, $template_type );
    1293         }
    1294 
    1295         $template_file = _get_block_template_file( $template_type, $slug );
    1296         if ( null === $template_file ) {
    1297                 /** This filter is documented in wp-includes/block-template-utils.php */
    1298                 return apply_filters( 'get_block_file_template', null, $id, $template_type );
    1299         }
    1300 
    1301         $block_template = _build_block_template_result_from_file( $template_file, $template_type );
     1329        if ( get_stylesheet() === $theme ) {
     1330                $template_file = _get_block_template_file( $template_type, $slug );
     1331                if ( null !== $template_file ) {
     1332                        $block_template = _build_block_template_result_from_file( $template_file, $template_type );
     1333
     1334                        /** This filter is documented in wp-includes/block-template-utils.php */
     1335                        return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
     1336                }
     1337        }
     1338
     1339        $block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
    13021340
    13031341        /**
     
    16661704                }
    16671705
    1668                 $content = get_comment_delimited_block_content(
     1706                $content               = get_comment_delimited_block_content(
    16691707                        'core/template-part',
    16701708                        $attributes,
    16711709                        $changes->post_content
    16721710                );
    1673                 $content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
     1711                $content               = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
    16741712                $changes->post_content = remove_serialized_parent_block( $content );
    16751713
Note: See TracChangeset for help on using the changeset viewer.