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

Media Summary: Setup postdata for obtaining the excerpt #9348

Merged
merged 4 commits into from
Apr 21, 2018
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
40 changes: 30 additions & 10 deletions _inc/lib/class.media-summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static function get( $post_id, $blog_id = 0, $args = array() ) {
);

if ( empty( $post->post_password ) ) {
$return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'] );
$return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'] , $post);
$return['count']['word'] = self::get_word_count( $post->post_content );
$return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] );
$return['count']['link'] = self::get_link_count( $post->post_content );
Expand Down Expand Up @@ -298,22 +298,42 @@ static function clean_text( $text ) {
);
}

static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256 ) {
/**
* Retrieve an excerpt for the post summary.
*
* This function works around a suspected problem with Core. If resolved, this function should be simplified.
* @link https://github.com/Automattic/jetpack/pull/8510
* @link https://core.trac.wordpress.org/ticket/42814
*
* @param string $post_content The post's content.
* @param string $post_excerpt The post's excerpt. Empty if none was explicitly set.
* @param int $max_words Maximum number of words for the excerpt. Used on wp.com. Default 16.
* @param int $max_chars Maximum characters in the excerpt. Used on wp.com. Default 256.
* @param WP_Post $requested_post The post object.
* @return string Post excerpt.
**/
static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256, $requested_post = null ) {
global $post;
$original_post = $post; // Saving the global for later use.
if ( function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) {
return self::clean_text( wpcom_enhanced_excerpt_extract_excerpt( array(
'text' => $post_content,
'excerpt_only' => true,
'show_read_more' => false,
'max_words' => $max_words,
'max_chars' => $max_chars,
'text' => $post_content,
'excerpt_only' => true,
'show_read_more' => false,
'max_words' => $max_words,
'max_chars' => $max_chars,
'read_more_threshold' => 25,
) ) );
} else {

} elseif ( $requested_post instanceof WP_Post ) {
$post = $requested_post; // setup_postdata does not set the global.
setup_postdata( $post );
/** This filter is documented in core/src/wp-includes/post-template.php */
$post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt );
$post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt, $post );
$post = $original_post; // wp_reset_postdata uses the $post global.
wp_reset_postdata();
return self::clean_text( $post_excerpt );
}
return '';
}

static function get_word_count( $post_content ) {
Expand Down