Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
# blocks. Keep them out of the archive so WordPress.org does not list them as blocks.
/build/*/block.json export-ignore

# The form is a registered block and requires its metadata in release archives.
/build/form/block.json -export-ignore

# GitHub linguist attributes
/build/** linguist-generated
/package-lock.json linguist-generated
9 changes: 3 additions & 6 deletions includes/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Block {
public static function init() {
self::register_blocks();

// Only hook the form block into block themes, and only if the form is enabled.
// Only hook the form block into block themes.
\add_filter( 'hooked_block_types', array( self::class, 'hooked_block_types' ) );

// Add editor plugin.
Expand All @@ -34,17 +34,14 @@ public static function register_blocks() {
}

/**
* Remove the form block from the hooked blocks, if it should not be added automatically.
* Remove the form block from the hooked blocks when the classic form is used.
*
* @param string[] $hooked_block_types The hooked block types.
*
* @return string[] The filtered hooked block types.
*/
public static function hooked_block_types( $hooked_block_types ) {
if (
webmention_use_block_hooks() &&
1 === (int) \get_option( 'webmention_show_comment_form', 1 )
) {
if ( webmention_use_block_hooks() ) {
return $hooked_block_types;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ function get_webmention_form( $args = array() ) {
* @return bool True if the block is hooked, false if the classic form is used.
*/
function webmention_use_block_hooks() {
$use_block_hooks = site_supports_blocks() && function_exists( 'get_hooked_blocks' ) && wp_is_block_theme();
$use_block_hooks = site_supports_blocks() && function_exists( 'get_hooked_blocks' ) && wp_is_block_theme() && WP_Block_Type_Registry::get_instance()->is_registered( 'webmention/form' );

/**
* Filter whether the Webmention form block is added through Block Hooks.
Expand All @@ -697,12 +697,12 @@ function webmention_use_block_hooks() {
* @since 3.8.9
*/
function webmention_comment_form() {
if ( 1 !== (int) get_option( 'webmention_show_comment_form', 1 ) ) {
// Block themes get the block through Block Hooks.
if ( webmention_use_block_hooks() ) {
return;
}

// Block themes get the block through Block Hooks.
if ( webmention_use_block_hooks() ) {
if ( 1 !== (int) get_option( 'webmention_show_comment_form', 1 ) ) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions includes/wp-admin/class-settings-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ public static function render_approve_domains_field() {
public static function render_comment_settings_field() {
?>
<fieldset>
<?php if ( ! function_exists( 'wp_is_block_theme' ) || ! \wp_is_block_theme() ) : ?>
<p>
<label for="webmention_show_comment_form">
<input type="checkbox" name="webmention_show_comment_form" id="webmention_show_comment_form" value="1" <?php \checked( true, \get_option( 'webmention_show_comment_form' ) ); ?> />
<?php \esc_html_e( 'Show a Webmention form at the comment section, to allow anyone to notify you of a mention.', 'webmention' ); ?>
</label>
</p>
<?php endif; ?>

<p>
<textarea name="webmention_comment_form_text" id="webmention_comment_form_text" rows="10" cols="50" class="large-text code" placeholder="<?php echo \esc_attr( \get_default_webmention_form_text() ); ?>"><?php echo \esc_textarea( \get_option( 'webmention_comment_form_text', '' ) ); ?></textarea>
Expand Down
100 changes: 97 additions & 3 deletions tests/phpunit/tests/class-test-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,79 @@
* Test form class.
*/
class Test_Form extends WP_UnitTestCase {
/**
* Test that a missing form block does not disable the classic fallback.
*/
public function test_block_theme_falls_back_when_form_block_is_missing() {
if ( ! site_supports_blocks() || ! function_exists( 'get_hooked_blocks' ) ) {
$this->markTestSkipped( 'Block Hooks are not supported.' );
}

$stylesheet = get_stylesheet();
register_theme_directory( DIR_TESTDATA . '/themedir1' );
switch_theme( 'block-theme' );
update_option( 'webmention_show_comment_form', 1 );
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( 'webmention/form' );

try {
$this->assertTrue( webmention_use_block_hooks() );
$registry->unregister( 'webmention/form' );
$this->assertFalse( webmention_use_block_hooks() );

$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
the_post();

ob_start();
webmention_comment_form();
$this->assertStringContainsString( 'name="source"', ob_get_clean() );
} finally {
$registry->register( $block_type );
switch_theme( $stylesheet );
delete_option( 'webmention_show_comment_form' );
}
}

/**
* Test actual hook insertion and rendering with a block theme and a disabled legacy option.
*/
public function test_block_theme_inserts_form_with_legacy_option_disabled() {
if ( ! site_supports_blocks() || ! function_exists( 'apply_block_hooks_to_content' ) ) {
$this->markTestSkipped( 'Block Hooks content insertion is not supported.' );
}

$stylesheet = get_stylesheet();
register_theme_directory( DIR_TESTDATA . '/themedir1' );
switch_theme( 'block-theme' );
update_option( 'webmention_show_comment_form', 0 );

try {
$this->assertTrue( webmention_use_block_hooks() );

$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
the_post();

$markup = '<!-- wp:comments --><div class="wp-block-comments"><!-- wp:post-comments-form /--></div><!-- /wp:comments -->';
$hooked = apply_block_hooks_to_content( $markup );
$this->assertStringContainsString( '<!-- wp:webmention/form /-->', $hooked );

$html = do_blocks( $hooked );
$this->assertSame( 1, substr_count( $html, 'name="source"' ) );
$this->assertStringContainsString( 'value="' . esc_url( get_permalink( $post_id ) ) . '"', $html );

ob_start();
\Webmention\WP_Admin\Settings_Fields::render_comment_settings_field();
$settings = ob_get_clean();
$this->assertStringNotContainsString( 'name="webmention_show_comment_form"', $settings );
$this->assertStringContainsString( 'name="webmention_comment_form_text"', $settings );
} finally {
switch_theme( $stylesheet );
delete_option( 'webmention_show_comment_form' );
}
}


/**
* Test that the form targets the given post.
Expand Down Expand Up @@ -118,6 +191,27 @@ public function test_classic_form_rendered() {
$this->assertStringContainsString( 'webmention-form', ob_get_clean() );
}

/**
* Test that the checkbox still disables the classic form.
*/
public function test_classic_form_respects_option() {
update_option( 'webmention_show_comment_form', 0 );
add_filter( 'webmention_use_block_hooks', '__return_false' );

$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
the_post();

ob_start();
webmention_comment_form();
$output = ob_get_clean();

remove_filter( 'webmention_use_block_hooks', '__return_false' );
delete_option( 'webmention_show_comment_form' );

$this->assertSame( '', $output );
}

/**
* Test that the classic form is skipped if the block is added through Block Hooks.
*/
Expand All @@ -138,7 +232,7 @@ public function test_classic_form_skipped_with_block_hooks() {
}

/**
* Test that the block is only hooked into block themes with the form enabled.
* Test that the block is hooked into block themes regardless of the legacy option.
*/
public function test_hooked_block_types() {
$hooked = array( 'core/paragraph', 'webmention/form' );
Expand All @@ -147,9 +241,9 @@ public function test_hooked_block_types() {
$this->assertSame( $hooked, \Webmention\Block::hooked_block_types( $hooked ) );

update_option( 'webmention_show_comment_form', 0 );
$this->assertSame( array( 'core/paragraph' ), \Webmention\Block::hooked_block_types( $hooked ) );
$this->assertSame( $hooked, \Webmention\Block::hooked_block_types( $hooked ) );

update_option( 'webmention_show_comment_form', 1 );
delete_option( 'webmention_show_comment_form' );
remove_filter( 'webmention_use_block_hooks', '__return_true' );
$this->assertSame( array( 'core/paragraph' ), \Webmention\Block::hooked_block_types( $hooked ) );
}
Expand Down
Loading