Skip to content

Commit

Permalink
Rename wp_register_block_template() to register_block_template() (Wor…
Browse files Browse the repository at this point in the history
…dPress#65958)

Co-authored-by: Aljullu <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ndiego <[email protected]>
Co-authored-by: getdave <[email protected]>
  • Loading branch information
5 people authored Oct 9, 2024
1 parent ec5acbd commit fc62bf6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.7/7543.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7543

* https://github.com/WordPress/gutenberg/pull/65958
49 changes: 46 additions & 3 deletions lib/compat/wordpress-6.7/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package gutenberg
*/

if ( ! function_exists( 'wp_register_block_template' ) ) {
if ( ! function_exists( 'register_block_template' ) ) {
/**
* Register a template.
*
Expand All @@ -22,20 +22,63 @@
* }
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
*/
function wp_register_block_template( $template_name, $args = array() ) {
function register_block_template( $template_name, $args = array() ) {
return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
}
}

if ( ! function_exists( 'unregister_block_template' ) ) {
/**
* Unregister a template.
*
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
* @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if
* the template doesn't exist.
*/
function unregister_block_template( $template_name ) {
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
}
}

if ( ! function_exists( 'wp_register_block_template' ) ) {
/**
* Register a template.
*
* @deprecated 19.4.0 wp_register_block_template is deprecated. Please use register_block_template instead.
*
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
* @param array|string $args {
* Optional. Array or string of arguments for registering a block template.
*
* @type string $title Optional. Title of the template as it will be shown in the Site Editor
* and other UI elements.
* @type string $description Optional. Description of the template as it will be shown in the Site
* Editor.
* @type string $content Optional. Default content of the template that will be used when the
* template is rendered or edited in the editor.
* @type string[] $post_types Optional. Array of post types to which the template should be available.
* @type string $plugin Uri of the plugin that registers the template.
* }
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
*/
function wp_register_block_template( $template_name, $args = array() ) {
_deprecated_function( __FUNCTION__, 'Gutenberg 19.4.0', 'register_block_template' );
register_block_template( $template_name, $args );
}
}

if ( ! function_exists( 'wp_unregister_block_template' ) ) {
/**
* Unregister a template.
*
* @deprecated 19.4.0 wp_unregister_block_template is deprecated. Please use unregister_block_template instead.
*
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
* @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if
* the template doesn't exist.
*/
function wp_unregister_block_template( $template_name ) {
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
_deprecated_function( __FUNCTION__, 'Gutenberg 19.4.0', 'unregister_block_template' );
return unregister_block_template( $template_name );
}
}
12 changes: 6 additions & 6 deletions packages/e2e-tests/plugins/block-template-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'init',
function () {
// Custom template used by most tests.
wp_register_block_template(
register_block_template(
'gutenberg//plugin-template',
array(
'title' => 'Plugin Template',
Expand All @@ -28,7 +28,7 @@ function () {
);

// Custom template overridden by the theme.
wp_register_block_template(
register_block_template(
'gutenberg//custom-template',
array(
'title' => 'Custom Template (overridden by the theme)',
Expand All @@ -39,18 +39,18 @@ function () {
);

// Custom template used to test unregistration.
wp_register_block_template(
register_block_template(
'gutenberg//plugin-unregistered-template',
array(
'title' => 'Plugin Unregistered Template',
'description' => 'A plugin-registered template that is unregistered.',
'content' => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template that is also unregistered.</p><!-- /wp:paragraph --></main><!-- /wp:group -->',
)
);
wp_unregister_block_template( 'gutenberg//plugin-unregistered-template' );
unregister_block_template( 'gutenberg//plugin-unregistered-template' );

// Custom template used to test overriding default WP templates.
wp_register_block_template(
register_block_template(
'gutenberg//page',
array(
'title' => 'Plugin Page Template',
Expand All @@ -60,7 +60,7 @@ function () {
);

// Custom template used to test overriding default WP templates which can be created by the user.
wp_register_block_template(
register_block_template(
'gutenberg//author-admin',
array(
'title' => 'Plugin Author Template',
Expand Down
8 changes: 4 additions & 4 deletions phpunit/block-template-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public function set_up() {
public function test_get_block_templates_from_registry() {
$template_name = 'test-plugin//test-template';

wp_register_block_template( $template_name );
register_block_template( $template_name );

$templates = get_block_templates();

$this->assertArrayHasKey( $template_name, $templates );

wp_unregister_block_template( $template_name );
unregister_block_template( $template_name );
}

public function test_get_block_template_from_registry() {
Expand All @@ -26,12 +26,12 @@ public function test_get_block_template_from_registry() {
'title' => 'Test Template',
);

wp_register_block_template( $template_name, $args );
register_block_template( $template_name, $args );

$template = get_block_template( 'block-theme//test-template' );

$this->assertEquals( 'Test Template', $template->title );

wp_unregister_block_template( $template_name );
unregister_block_template( $template_name );
}
}
4 changes: 2 additions & 2 deletions phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_get_item() {
'post_types' => array( 'post', 'page' ),
);

wp_register_block_template( $template_name, $args );
register_block_template( $template_name, $args );

$request = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
$response = rest_get_server()->dispatch( $request );
Expand All @@ -52,7 +52,7 @@ public function test_get_item() {
$this->assertSame( 'Test Template', $data['title']['rendered'], 'Template title mismatch.' );
$this->assertSame( 'test-plugin', $data['plugin'], 'Plugin name mismatch.' );

wp_unregister_block_template( $template_name );
unregister_block_template( $template_name );

$request = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
$response = rest_get_server()->dispatch( $request );
Expand Down

0 comments on commit fc62bf6

Please sign in to comment.