Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: Remove the ref from pattern content #550

Merged
merged 2 commits into from
Jan 27, 2023
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
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;
}