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

fix: disable excerpt more filter when excerpt block is used #4205

Merged
merged 2 commits into from
Mar 12, 2024
Merged
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
34 changes: 33 additions & 1 deletion inc/views/template_parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
class Template_Parts extends Base_View {
use Layout;

/**
* Use to temporary disable excerpt functionality.
*
* @var bool
*/
private $disable_excerpt = false;
/**
* Function that is run after instantiation.
*
Expand All @@ -34,6 +40,25 @@ public function init() {
add_action( 'neve_blog_post_template_part_content', array( $this, 'render_post' ) );
add_filter( 'excerpt_more', array( $this, 'link_excerpt_more' ) );
add_filter( 'the_content_more_link', array( $this, 'link_excerpt_more' ) );
add_filter( 'render_block_data', array( $this, 'temporary_disable_excerpt_more' ), -99, 3 );
}

/**
* Checks if a query block has the excerpt more block added to avoid duplicate read more.
*
* @param array $block_data Block data.
* @param array $block_type Block type.
* @param array $attributes Block attributes.
*
* @return array
*/
public function temporary_disable_excerpt_more( $block_data, $block_type, $attributes ) {

if ( 'core/post-excerpt' === $block_type['blockName'] ) {
$this->disable_excerpt = true;
}
return $block_data;

}

/**
Expand Down Expand Up @@ -211,7 +236,7 @@ protected function post_class( $post_id = null, $additional = '' ) {
$class .= ' nv-non-grid-article';
}
}

// Filter the Core classes for missing components.
$is_thumbnail_inactive = ! in_array( 'thumbnail', $this->get_ordered_components(), true );
if ( $is_thumbnail_inactive ) {
Expand Down Expand Up @@ -417,6 +442,13 @@ private function get_excerpt( $post_id = null ) {
* @return string
*/
public function link_excerpt_more( $moretag, $post_id = null ) {


if ( $this->disable_excerpt ) {
$this->disable_excerpt = false;
return $moretag;
}

$new_moretag = '… ';

if ( $moretag !== ' […]' ) {
Expand Down
Loading