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: limit excerpt lengths even for custom excerpts #874

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions includes/class-newspack-blocks-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public static function posts_endpoint( $request ) {
'showExcerpt' => $params['show_excerpt'],
'excerptLength' => $params['excerpt_length'],
];
Newspack_Blocks::filter_excerpt_length( $block_attributes );
Newspack_Blocks::filter_excerpt( $block_attributes );
}

$query = new WP_Query();
Expand Down Expand Up @@ -402,7 +402,7 @@ public static function posts_endpoint( $request ) {
$posts[] = array_merge( $data, $add_ons );
}

Newspack_Blocks::remove_excerpt_length_filter();
Newspack_Blocks::remove_excerpt_filter();
Newspack_Blocks::remove_filter_posts_clauses_when_co_authors_filter();

return new \WP_REST_Response( $posts );
Expand Down
56 changes: 55 additions & 1 deletion includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,15 @@ public static function get_sponsor_logos( $sponsors = null, $id = null ) {
/**
* Closure for excerpt filtering that can be added and removed.
*
* @var newspack_blocks_excerpt_length_closure
* @var Closure
*/
public static $newspack_blocks_excerpt_closure = null;

/**
* Closure for excerpt length filtering that can be added and removed.
*
* @var Closure
* @deprecated
*/
public static $newspack_blocks_excerpt_length_closure = null;

Expand All @@ -752,6 +760,47 @@ public static function remove_wc_memberships_excerpt_limit() {
*
* @param array $attributes The block's attributes.
*/
public static function filter_excerpt( $attributes ) {
if ( empty( $attributes['excerptLength'] ) || ! $attributes['showExcerpt'] ) {
return;
}

self::$newspack_blocks_excerpt_closure = function( $text = '', $post = null ) use ( $attributes ) {
$raw_excerpt = $text;

$post = get_post( $post );
$text = get_the_content( '', false, $post );

$text = strip_shortcodes( $text );
$text = excerpt_remove_blocks( $text );

/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text ); // phpcs:ignore
$text = str_replace( ']]>', ']]>', $text );
adekbadek marked this conversation as resolved.
Show resolved Hide resolved
$text = wp_trim_words( $text, $attributes['excerptLength'], static::more_excerpt() );

/** This filter is documented in wp-includes/post-template.php */
return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt ); // phpcs:ignore
};

add_filter( 'get_the_excerpt', self::$newspack_blocks_excerpt_closure, 11, 2 );
}

/**
* Remove excerpt filter after Homepage Posts block loop.
*/
public static function remove_excerpt_filter() {
if ( static::$newspack_blocks_excerpt_closure ) {
remove_filter( 'get_the_excerpt', static::$newspack_blocks_excerpt_closure, 11 );
}
}

/**
* Filter for excerpt length.
*
* @deprectated
* @param array $attributes The block's attributes.
*/
public static function filter_excerpt_length( $attributes ) {
// If showing excerpt, filter the length using the block attribute.
if ( isset( $attributes['excerptLength'] ) && $attributes['showExcerpt'] ) {
Expand All @@ -771,6 +820,8 @@ function() use ( $attributes ) {

/**
* Remove excerpt length filter after Homepage Posts block loop.
*
* @deprectated
mreishus marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function remove_excerpt_length_filter() {
if ( self::$newspack_blocks_excerpt_length_closure ) {
Expand All @@ -793,6 +844,7 @@ public static function more_excerpt() {
/**
* Filter for excerpt ellipsis.
*
* @deprectated
* @param array $attributes The block's attributes.
*/
public static function filter_excerpt_more( $attributes ) {
Expand All @@ -804,6 +856,8 @@ public static function filter_excerpt_more( $attributes ) {

/**
* Remove excerpt ellipsis filter after Homepage Posts block loop.
*
* @deprectated
*/
public static function remove_excerpt_more_filter() {
remove_filter( 'excerpt_more', [ __CLASS__, 'more_excerpt' ], 999 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ public function get_items( $request ) {
$ids = [];
$next_url = '';

Newspack_Blocks::filter_excerpt_length( $attributes );
Newspack_Blocks::filter_excerpt_more( $attributes );
Newspack_Blocks::filter_excerpt( $attributes );

// The Loop.
while ( $article_query->have_posts() ) {
Expand All @@ -222,8 +221,7 @@ public function get_items( $request ) {
$ids[] = get_the_ID();
}

Newspack_Blocks::remove_excerpt_length_filter();
Newspack_Blocks::remove_excerpt_more_filter();
Newspack_Blocks::remove_excerpt_filter();

// Provide next URL if there are more pages.
$show_next_button = ! empty( $exclude_ids ) ? $article_query->max_num_pages > 1 : $article_query->max_num_pages > $next_page;
Expand Down
6 changes: 2 additions & 4 deletions src/blocks/homepage-articles/templates/articles-loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ function( $data ) {
global $newspack_blocks_post_id;
do_action( 'newspack_blocks_homepage_posts_before_render' );

Newspack_Blocks::filter_excerpt_length( $attributes );
Newspack_Blocks::filter_excerpt_more( $attributes );
Newspack_Blocks::filter_excerpt( $attributes );

while ( $article_query->have_posts() ) {
$article_query->the_post();
$newspack_blocks_post_id[ get_the_ID() ] = true;
echo Newspack_Blocks::template_inc( __DIR__ . '/article.php', array( 'attributes' => $attributes ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Newspack_Blocks::remove_excerpt_length_filter();
Newspack_Blocks::remove_excerpt_more_filter();
Newspack_Blocks::remove_excerpt_filter();

do_action( 'newspack_blocks_homepage_posts_after_render' );
wp_reset_postdata();
Expand Down