Make WordPress Core


Ignore:
Timestamp:
09/20/2024 02:05:50 AM (14 months 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/rest-api/endpoints/class-wp-rest-templates-controller.php

    r58707 r59073  
    327327     */
    328328    public function get_item( $request ) {
    329         if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
     329        if ( isset( $request['source'] ) && ( 'theme' === $request['source'] || 'plugin' === $request['source'] ) ) {
    330330            $template = get_block_file_template( $request['id'], $this->post_type );
    331331        } else {
     
    777777        }
    778778
     779        if ( rest_is_field_included( 'plugin', $fields ) ) {
     780            $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
     781            if ( $registered_template ) {
     782                $data['plugin'] = $registered_template->plugin;
     783            }
     784        }
     785
    779786        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    780787        $data    = $this->add_additional_fields_to_object( $data, $request );
     
    832839
    833840            // Added by plugin.
    834             if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) {
     841            if ( 'plugin' === $template_object->origin ) {
    835842                return 'plugin';
    836843            }
     
    866873                return empty( $theme_name ) ? $template_object->theme : $theme_name;
    867874            case 'plugin':
    868                 $plugins = get_plugins();
    869                 $plugin  = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ];
    870                 return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name'];
     875                if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'get_plugin_data' ) ) {
     876                    require_once ABSPATH . 'wp-admin/includes/plugin.php';
     877                }
     878                if ( isset( $template_object->plugin ) ) {
     879                    $plugins = wp_get_active_and_valid_plugins();
     880
     881                    foreach ( $plugins as $plugin_file ) {
     882                        $plugin_basename = plugin_basename( $plugin_file );
     883                        // Split basename by '/' to get the plugin slug.
     884                        list( $plugin_slug, ) = explode( '/', $plugin_basename );
     885
     886                        if ( $plugin_slug === $template_object->plugin ) {
     887                            $plugin_data = get_plugin_data( $plugin_file );
     888
     889                            if ( ! empty( $plugin_data['Name'] ) ) {
     890                                return $plugin_data['Name'];
     891                            }
     892
     893                            break;
     894                        }
     895                    }
     896                }
     897
     898                /*
     899                 * Fall back to the theme name if the plugin is not defined. That's needed to keep backwards
     900                 * compatibility with templates that were registered before the plugin attribute was added.
     901                 */
     902                $plugins         = get_plugins();
     903                $plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) );
     904                if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
     905                    return $plugins[ $plugin_basename ]['Name'];
     906                }
     907                return isset( $template_object->plugin ) ?
     908                    $template_object->plugin :
     909                    $template_object->theme;
    871910            case 'site':
    872911                return get_bloginfo( 'name' );
     
    11351174                'readonly'    => true,
    11361175            );
     1176            $schema['properties']['plugin']    = array(
     1177                'type'        => 'string',
     1178                'description' => __( 'Plugin that registered the template.' ),
     1179                'readonly'    => true,
     1180                'context'     => array( 'view', 'edit', 'embed' ),
     1181            );
    11371182        }
    11381183
Note: See TracChangeset for help on using the changeset viewer.