Skip to content

Commit

Permalink
Patterns: Remove the ref from pattern content (#550)
Browse files Browse the repository at this point in the history
* Mock Blocks: Remove ref from navigation blocks

Removing the ref from navigation blocks before they're rendered so that the block always falls back to the mocked nav menu.

* Patterns: Remove the ref from raw content

This prevents the ref from appearing in the copyable code.
  • Loading branch information
ryelle authored Jan 27, 2023
1 parent 8ac046d commit a28ff10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
add_action( 'render_block_core/archives', __NAMESPACE__ . '\render_archives', 10, 3 );
add_action( 'render_block_core/latest-comments', __NAMESPACE__ . '\render_latest_comments', 10, 3 );
add_filter( 'block_core_navigation_render_fallback', __NAMESPACE__ . '\provide_fallback_nav_items' );
add_filter( 'render_block_data', __NAMESPACE__ . '\update_block_data' );
add_filter( 'get_custom_logo', __NAMESPACE__ . '\provide_mock_logo' );

/**
Expand Down Expand Up @@ -257,6 +258,20 @@ function provide_fallback_nav_items( $fallback_blocks ) {
);
}

/**
* Remove the `ref` from the navigation block to ensure it renders the fallback.
*
* @param array $parsed_block The block being rendered.
* @return array Updated block data.
*/
function update_block_data( $parsed_block ) {
if ( 'core/navigation' === $parsed_block['blockName'] ) {
unset( $parsed_block['attrs']['ref'] );
}

return $parsed_block;
}

/**
* Replace the custom logo output with the WordPress W.
* This serves to provide a placeholder for the Site Logo block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ function( $post ) {
);

/**
* Process post content, replacing broken encoding.
* Process post content, replacing broken encoding & removing refs.
*
* Some image URLs have &s, which are double-encoded and sanitized to become malformed,
* for example, `https://img.rawpixel.com/s3fs-private/rawpixel_images/website_content/a010-markuss-0964.jpg?w=1200\u0026amp;h=1200\u0026amp;fit=clip\u0026amp;crop=default\u0026amp;dpr=1\u0026amp;q=75\u0026amp;vib=3\u0026amp;con=3\u0026amp;usm=15\u0026amp;cs=srgb\u0026amp;bg=F4F4F3\u0026amp;ixlib=js-2.2.1\u0026amp;s=7d494bd5db8acc2a34321c15ed18ace5`.
Expand All @@ -974,5 +974,8 @@ function( $post ) {
*/
function decode_pattern_content( $content ) {
// Sometimes the initial `\` is missing, so look for both versions.
return str_replace( [ '\u0026amp;', 'u0026amp;' ], '&', $content );
$content = str_replace( [ '\u0026amp;', 'u0026amp;' ], '&', $content );
// Remove `ref` from all content.
$content = preg_replace( '/"ref":\d+,?/', '', $content );
return $content;
}

0 comments on commit a28ff10

Please sign in to comment.