Skip to content

Commit

Permalink
Navigation Block: Properly decode URL-encoded links (#46435)
Browse files Browse the repository at this point in the history
* fix: issue with already encoded urls

* fix: test

* fix: test

* fix: Always encode a url before saving

* Try to decode only urls that are encoded

* change the naming to match WordPress core naming pattern

* change the naming to match WordPress core naming pattern

* fix: Fix lint issues
  • Loading branch information
kozer authored Jan 5, 2023
1 parent f2963d3 commit 622b829
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ export default function NavigationLinkEdit( {
<TextControl
value={ url || '' }
onChange={ ( urlValue ) => {
setAttributes( { url: urlValue } );
updateAttributes(
{ url: urlValue },
setAttributes,
attributes
);
} }
label={ __( 'URL' ) }
autoComplete="off"
Expand Down
29 changes: 28 additions & 1 deletion packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ function block_core_navigation_link_render_submenu_icon() {
return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
}

/**
* Decodes a url if it's encoded, returning the same url if not.
*
* @param string $url The url to decode.
*
* @return string $url Returns the decoded url.
*/
function block_core_navigation_link_maybe_urldecode( $url ) {
$is_url_encoded = false;
$query = parse_url( $url, PHP_URL_QUERY );
$query_params = wp_parse_args( $query );

foreach ( $query_params as $query_param ) {
if ( rawurldecode( $query_param ) !== $query_param ) {
$is_url_encoded = true;
break;
}
}

if ( $is_url_encoded ) {
return rawurldecode( $url );
}

return $url;
}


/**
* Renders the `core/navigation-link` block.
*
Expand Down Expand Up @@ -171,7 +198,7 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['url'] ) ) {
$html .= ' href="' . esc_url( $attributes['url'] ) . '"';
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $attributes['url'] ) ) . '"';
}

if ( $is_active ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const updateAttributes = (

setAttributes( {
// Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
...( newUrl && { url: encodeURI( safeDecodeURI( newUrl ) ) } ),
...{ url: newUrl ? encodeURI( safeDecodeURI( newUrl ) ) : newUrl },
...( label && { label } ),
...( undefined !== opensInNewTab && { opensInNewTab } ),
...( id && Number.isInteger( id ) && { id } ),
Expand Down
33 changes: 33 additions & 0 deletions phpunit/class-block-library-navigation-link-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,39 @@ public function test_returns_link_for_plain_link() {
);
}

public function test_returns_link_for_decoded_link() {

$urls_before_render = array(
'https://example.com/?id=10&data=lzB%252Fzd%252FZA%253D%253D',
'https://example.com/?id=10&data=lzB%2Fzd%FZA%3D%3D',
'https://example.com/?id=10&data=1234',
);

$urls_after_render = array(
'https://example.com/?id=10&#038;data=lzB%2Fzd%2FZA%3D%3D',
'https://example.com/?id=10&#038;data=lzB%2Fzd%FZA%3D%3D',
'https://example.com/?id=10&#038;data=1234',
);

foreach ( $urls_before_render as $idx => $link ) {
$parsed_blocks = parse_blocks( '<!-- wp:navigation-link {"label":"test label", "url": "' . $link . '"} /-->' );
$this->assertEquals( 1, count( $parsed_blocks ) );
$block = $parsed_blocks[0];
$navigation_link_block = new WP_Block( $block, array() );
$this->assertEquals(
true,
strpos(
gutenberg_render_block_core_navigation_link(
$navigation_link_block->attributes,
array(),
$navigation_link_block
),
$urls_after_render[ $idx ]
) !== false
);
};
}

public function test_returns_empty_when_custom_post_type_draft() {
$page_id = self::$custom_draft->ID;

Expand Down

0 comments on commit 622b829

Please sign in to comment.