Make WordPress Core


Ignore:
Timestamp:
09/20/2024 02:05:50 AM (18 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/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r58227 r59073  
    530530
    531531    /**
     532     * Tests that get_item() returns plugin-registered templates.
     533     *
     534     * @ticket 61804
     535     *
     536     * @covers WP_REST_Templates_Controller::get_item
     537     */
     538    public function test_get_item_from_registry() {
     539        wp_set_current_user( self::$admin_id );
     540
     541        $template_name = 'test-plugin//test-template';
     542        $args          = array(
     543            'content'     => 'Template content',
     544            'title'       => 'Test Template',
     545            'description' => 'Description of test template',
     546            'post_types'  => array( 'post', 'page' ),
     547        );
     548
     549        wp_register_block_template( $template_name, $args );
     550
     551        $request  = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
     552        $response = rest_get_server()->dispatch( $request );
     553
     554        $this->assertNotWPError( $response, "Fetching a registered template shouldn't cause an error." );
     555
     556        $data = $response->get_data();
     557
     558        $this->assertSame( 'default//test-template', $data['id'], 'Template ID mismatch.' );
     559        $this->assertSame( 'default', $data['theme'], 'Template theme mismatch.' );
     560        $this->assertSame( 'Template content', $data['content']['raw'], 'Template content mismatch.' );
     561        $this->assertSame( 'test-template', $data['slug'], 'Template slug mismatch.' );
     562        $this->assertSame( 'plugin', $data['source'], "Template source should be 'plugin'." );
     563        $this->assertSame( 'plugin', $data['origin'], "Template origin should be 'plugin'." );
     564        $this->assertSame( 'test-plugin', $data['author_text'], 'Template author text mismatch.' );
     565        $this->assertSame( 'Description of test template', $data['description'], 'Template description mismatch.' );
     566        $this->assertSame( 'Test Template', $data['title']['rendered'], 'Template title mismatch.' );
     567        $this->assertSame( 'test-plugin', $data['plugin'], 'Plugin name mismatch.' );
     568
     569        wp_unregister_block_template( $template_name );
     570
     571        $request  = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
     572        $response = rest_get_server()->dispatch( $request );
     573
     574        $this->assertNotWPError( $response, "Fetching an unregistered template shouldn't cause an error." );
     575        $this->assertSame( 404, $response->get_status(), 'Fetching an unregistered template should return 404.' );
     576    }
     577
     578    /**
    532579     * @ticket 54507
    533580     * @dataProvider data_sanitize_template_id
     
    864911        $data       = $response->get_data();
    865912        $properties = $data['schema']['properties'];
    866         $this->assertCount( 17, $properties );
     913        $this->assertCount( 18, $properties );
    867914        $this->assertArrayHasKey( 'id', $properties );
    868915        $this->assertArrayHasKey( 'description', $properties );
     
    883930        $this->assertArrayHasKey( 'author_text', $properties );
    884931        $this->assertArrayHasKey( 'original_source', $properties );
     932        $this->assertArrayHasKey( 'plugin', $properties );
    885933    }
    886934
Note: See TracChangeset for help on using the changeset viewer.