Skip to content

Commit

Permalink
Fix for tags being stripped from custom excerpts.
Browse files Browse the repository at this point in the history
This commit does two things: 1) It allows inline tags in the Post Excerpt block when a user has manually created an excerpt and 2) It removes the excerpt length limit for manual excerpts.

See: WordPress/gutenberg#49449
  • Loading branch information
justintadlock committed Jul 17, 2024
1 parent 3ed407b commit 1e87fa2
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Block/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ public function boot(): void
$this->hookMethods();
}

/**
* Before rendering the Post Excerpt block, add a custom filter to
* `wp_trim_words` so that we can handle the output of custom excerpts.
*
* @hook pre_render_block
* @since 1.0.0
* @link https://github.com/WordPress/gutenberg/issues/49449
*/
public function preRenderCorePostExcerpt(
?string $pre_render,
array $block,
?WP_Block $parent_block
): ?string {
if (
'core/post-excerpt' === $block['blockName']
&& is_null($pre_render)
&& ! is_null($parent_block)
&& isset($parent_block->context['postId'])
&& has_excerpt($parent_block->context['postId'])
) {
add_filter('wp_trim_words', [$this, 'formatManualExcerpt'], 10, 4);
}

return $pre_render;
}

/**
* Disables the enhanced pagination feature for the Query Loop block.
* There is currently no `theme.json`-supported method of disabling it,
Expand Down Expand Up @@ -171,6 +197,24 @@ public function renderCoreLoginout(string $content, array $block): string
return $processor->get_updated_html();
}

/**
* Removes the filter on `wp_trim_words` if it was added on the earlier
* `pre_render_block` hook.
*
* @hook render_block_core/post-excerpt first
* @since 1.0.0
* @link https://github.com/WordPress/gutenberg/issues/49449
* @see Render::preRenderCorePostExcerpt()
*/
public function renderCorePostExcerpt(string $content): string
{
if ($priority = has_filter('wp_trim_words', [$this, 'formatManualExcerpt'])) {
remove_filter('wp_trim_words', [$this, 'formatManualExcerpt'], $priority);
}

return $content;
}

/**
* WordPress doesn't add the taxonomy name to the tag cloud wrapper. In
* order for taxonomy-based block styles to work, the theme is adding
Expand Down Expand Up @@ -281,4 +325,34 @@ public function setWidgetArchivesArgs(array $args): array

return $args;
}

/**
* Filters `wp_trim_words` to allow the original text but removes
* unwanted tags. This is meant to be used only when the user has
* written a custom excerpt.
*
* @since 1.0.0
* @link https://github.com/WordPress/gutenberg/issues/49449
* @see Render::preRenderCorePostExcerpt()
*/
public function formatManualExcerpt(
string $text,
int $num_words,
string $more,
string $original_text
): string {
return wp_kses($original_text, [
'a' => [ 'href' => true, 'title' => true, 'class' => true ],
'abbr' => [ 'title' => true ],
'acronym' => [ 'title' => true ],
'bold' => [ 'class' => true ],
'code' => [ 'class' => true ],
'em' => [ 'class' => true ],
'i' => [ 'class' => true ],
'mark' => [ 'class' => true ],
'small' => [ 'class' => true ],
'span' => [ 'class' => true ],
'strong' => [ 'class' => true ]
]);
}
}

0 comments on commit 1e87fa2

Please sign in to comment.