-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navigation Block: Fix erroneous escaping of ampersands (etc) (#59561)
Fix erroneous escaping of ampersands to `u0026amp;`. This is done by using the [`rest_pre_insert_{$this->post_type}`](https://developer.wordpress.org/reference/hooks/rest_pre_insert_this-post_type/) _filter_ rather than the `rest_insert_wp_navigation` _action_. This avoids calling `wp_update_post` twice, which was the original reason of the issue, as it removed the backslash from the already-encoded entity. Unlinked contributors: kylekelly. Co-authored-by: ockham <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: tjcafferkey <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: draganescu <[email protected]> Co-authored-by: annezazu <[email protected]> Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: getdave <[email protected]> Co-authored-by: swissspidy <[email protected]>
- Loading branch information
1 parent
d00d11b
commit 9f934bb
Showing
2 changed files
with
113 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Navigation block block hooks tests. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
*/ | ||
|
||
/** | ||
* Tests for the Navigation block. | ||
* | ||
* @group blocks | ||
*/ | ||
class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase { | ||
/** | ||
* Original markup. | ||
* | ||
* @var string | ||
*/ | ||
protected static $original_markup; | ||
|
||
/** | ||
* Post object. | ||
* | ||
* @var object | ||
*/ | ||
protected static $navigation_post; | ||
|
||
/** | ||
* Setup method. | ||
*/ | ||
public static function wpSetUpBeforeClass() { | ||
//self::$original_markup = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"https://localhost:8888/?page_id=2","kind":"post-type"} /-->'; | ||
|
||
self::$navigation_post = self::factory()->post->create_and_get( | ||
array( | ||
'post_type' => 'wp_navigation', | ||
'post_title' => 'Navigation Menu', | ||
'post_content' => 'Original content', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Tear down each test method. | ||
*/ | ||
public function tear_down() { | ||
$registry = WP_Block_Type_Registry::get_instance(); | ||
|
||
if ( $registry->is_registered( 'tests/my-block' ) ) { | ||
$registry->unregister( 'tests/my-block' ); | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta | ||
*/ | ||
public function test_block_core_navigation_update_ignore_hooked_blocks_meta_preserves_entities() { | ||
if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { | ||
$this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionalit.' ); | ||
} | ||
|
||
register_block_type( | ||
'tests/my-block', | ||
array( | ||
'block_hooks' => array( | ||
'core/navigation' => 'last_child', | ||
), | ||
) | ||
); | ||
|
||
$original_markup = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"https://localhost:8888/?page_id=2","kind":"post-type"} /-->'; | ||
$post = new stdClass(); | ||
$post->ID = self::$navigation_post->ID; | ||
$post->post_content = $original_markup; | ||
|
||
$post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post ); | ||
|
||
// We expect the '&' character to be replaced with its unicode representation. | ||
$expected_markup = str_replace( '&', '\u0026', $original_markup ); | ||
|
||
$this->assertSame( | ||
$expected_markup, | ||
$post->post_content, | ||
'Post content did not match expected markup with entities escaped.' | ||
); | ||
$this->assertSame( | ||
array( 'tests/my-block' ), | ||
json_decode( get_post_meta( self::$navigation_post->ID, '_wp_ignored_hooked_blocks', true ), true ), | ||
'Block was not added to ignored hooked blocks metadata.' | ||
); | ||
} | ||
} |