Blocks: Introduce register_block_type()
, unregister_block_type()
, and get_dynamic_blocks()
functions.
These helper functions allow easy access to the global block registry. Merges [43743] from the 5.0 branch to trunk. See #45109. git-svn-id: https://develop.svn.wordpress.org/trunk@44109 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
9254ae4a72
commit
738609ae9c
@ -7,6 +7,39 @@
|
||||
* @since 5.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers a block type.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
|
||||
* complete WP_Block_Type instance. In case a WP_Block_Type
|
||||
* is provided, the $args parameter will be ignored.
|
||||
* @param array $args {
|
||||
* Optional. Array of block type arguments. Any arguments may be defined, however the
|
||||
* ones described below are supported by default. Default empty array.
|
||||
*
|
||||
* @type callable $render_callback Callback used to render blocks of this block type.
|
||||
* }
|
||||
* @return WP_Block_Type|false The registered block type on success, or false on failure.
|
||||
*/
|
||||
function register_block_type( $name, $args = array() ) {
|
||||
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a block type.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
|
||||
* complete WP_Block_Type instance.
|
||||
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
|
||||
*/
|
||||
function unregister_block_type( $name ) {
|
||||
return WP_Block_Type_Registry::get_instance()->unregister( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a post or content string has blocks.
|
||||
*
|
||||
@ -59,3 +92,23 @@ function has_block( $block_type, $post = null ) {
|
||||
|
||||
return false !== strpos( $post, '<!-- wp:' . $block_type . ' ' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the names of all registered dynamic block types.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @return array Array of dynamic block names.
|
||||
*/
|
||||
function get_dynamic_block_names() {
|
||||
$dynamic_block_names = array();
|
||||
|
||||
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
||||
foreach ( $block_types as $block_type ) {
|
||||
if ( $block_type->is_dynamic() ) {
|
||||
$dynamic_block_names[] = $block_type->name;
|
||||
}
|
||||
}
|
||||
|
||||
return $dynamic_block_names;
|
||||
}
|
||||
|
17
tests/phpunit/data/blocks/do-blocks-expected.html
Normal file
17
tests/phpunit/data/blocks/do-blocks-expected.html
Normal file
@ -0,0 +1,17 @@
|
||||
<p>First Auto Paragraph</p>
|
||||
|
||||
<!--more-->
|
||||
|
||||
<p>First Gutenberg Paragraph</p>
|
||||
|
||||
<p>Second Auto Paragraph</p>
|
||||
|
||||
|
||||
<p>Third Gutenberg Paragraph</p>
|
||||
|
||||
<p>Third Auto Paragraph</p>
|
||||
|
||||
<p>[someshortcode]</p>
|
||||
<p>And some content?!</p>
|
||||
<p>[/someshortcode]</p>
|
||||
|
25
tests/phpunit/data/blocks/do-blocks-original.html
Normal file
25
tests/phpunit/data/blocks/do-blocks-original.html
Normal file
@ -0,0 +1,25 @@
|
||||
<p>First Auto Paragraph</p>
|
||||
|
||||
<!--more-->
|
||||
|
||||
<!-- wp:core/paragraph -->
|
||||
<p>First Gutenberg Paragraph</p>
|
||||
<!-- /wp:core/paragraph -->
|
||||
|
||||
<p>Second Auto Paragraph</p>
|
||||
|
||||
<!-- wp:core/test-self-closing /-->
|
||||
|
||||
<!-- wp:core/paragraph -->
|
||||
<p>Third Gutenberg Paragraph</p>
|
||||
<!-- /wp:core/paragraph -->
|
||||
|
||||
<p>Third Auto Paragraph</p>
|
||||
|
||||
<!-- wp:core/shortcode -->
|
||||
[someshortcode]
|
||||
|
||||
And some content?!
|
||||
|
||||
[/someshortcode]
|
||||
<!-- /wp:core/shortcode -->
|
141
tests/phpunit/tests/blocks/register.php
Normal file
141
tests/phpunit/tests/blocks/register.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* Block registry tests.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 5.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for register_block_type(), unregister_block_type(), get_dynamic_block_names()
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @group blocks
|
||||
*/
|
||||
class WP_Test_Block_Register extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* ID for a test post.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @var int
|
||||
*/
|
||||
protected static $post_id;
|
||||
|
||||
/**
|
||||
* Set up before class.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$post_id = $factory->post->create(
|
||||
array(
|
||||
'post_content' => file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-original.html' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down after class.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public static function wpTearDownAfterClass() {
|
||||
// Also deletes revisions.
|
||||
wp_delete_post( self::$post_id, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty render function for tests to use.
|
||||
*/
|
||||
function render_stub() {}
|
||||
|
||||
/**
|
||||
* Tear down after each test.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
function tearDown() {
|
||||
parent::tearDown();
|
||||
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
|
||||
foreach ( array( 'test-static', 'test-dynamic' ) as $block_name ) {
|
||||
$block_name = 'core/' . $block_name;
|
||||
|
||||
if ( $registry->is_registered( $block_name ) ) {
|
||||
$registry->unregister( $block_name );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 45109
|
||||
*/
|
||||
function test_register_affects_main_registry() {
|
||||
$name = 'core/test-static';
|
||||
$settings = array(
|
||||
'icon' => 'text',
|
||||
);
|
||||
|
||||
register_block_type( $name, $settings );
|
||||
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
$this->assertTrue( $registry->is_registered( $name ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 45109
|
||||
*/
|
||||
function test_unregister_affects_main_registry() {
|
||||
$name = 'core/test-static';
|
||||
$settings = array(
|
||||
'icon' => 'text',
|
||||
);
|
||||
|
||||
register_block_type( $name, $settings );
|
||||
unregister_block_type( $name );
|
||||
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
$this->assertFalse( $registry->is_registered( $name ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 45109
|
||||
*/
|
||||
function test_get_dynamic_block_names() {
|
||||
register_block_type( 'core/test-static', array() );
|
||||
register_block_type( 'core/test-dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) );
|
||||
|
||||
$dynamic_block_names = get_dynamic_block_names();
|
||||
|
||||
$this->assertContains( 'core/test-dynamic', $dynamic_block_names );
|
||||
$this->assertNotContains( 'core/test-static', $dynamic_block_names );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 45109
|
||||
*/
|
||||
function test_has_blocks() {
|
||||
// Test with passing post ID.
|
||||
$this->assertTrue( has_blocks( self::$post_id ) );
|
||||
|
||||
// Test with passing WP_Post object.
|
||||
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );
|
||||
|
||||
// Test with passing content string.
|
||||
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );
|
||||
|
||||
// Test default.
|
||||
$this->assertFalse( has_blocks() );
|
||||
$query = new WP_Query( array( 'post__in' => array( self::$post_id ) ) );
|
||||
$query->the_post();
|
||||
$this->assertTrue( has_blocks() );
|
||||
|
||||
// Test string (without blocks).
|
||||
$content = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-expected.html' );
|
||||
$this->assertFalse( has_blocks( $content ) );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user