From f46d2229489e6cec0e43b48ecd40f56c41bd3872 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 18 Apr 2018 08:53:20 -0500 Subject: [PATCH 1/4] Use 4.5-era arg for get_excerpt filter. Fixes #9209 --- _inc/lib/class.media-summary.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_inc/lib/class.media-summary.php b/_inc/lib/class.media-summary.php index d129f5e688961..0ce838c7585b1 100644 --- a/_inc/lib/class.media-summary.php +++ b/_inc/lib/class.media-summary.php @@ -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 ); @@ -298,7 +298,7 @@ static function clean_text( $text ) { ); } - static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256 ) { + static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256, $post = null ) { if ( function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) { return self::clean_text( wpcom_enhanced_excerpt_extract_excerpt( array( 'text' => $post_content, @@ -311,7 +311,7 @@ static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max } else { /** 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 ); return self::clean_text( $post_excerpt ); } } From 0300c4a16dc467b756aa018eb3498bff5da46f25 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 18 Apr 2018 11:30:46 -0500 Subject: [PATCH 2/4] Setup postdata to avoid get_except related errors in PHP 7.2 --- _inc/lib/class.media-summary.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/_inc/lib/class.media-summary.php b/_inc/lib/class.media-summary.php index 0ce838c7585b1..a1fda6364e544 100644 --- a/_inc/lib/class.media-summary.php +++ b/_inc/lib/class.media-summary.php @@ -298,7 +298,9 @@ static function clean_text( $text ) { ); } - static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256, $post = null ) { + 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, @@ -308,12 +310,16 @@ static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max '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 ); + $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 ) { From 6f955f3d63a5ea55b0d660caac1e36fbd2cdc588 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Wed, 18 Apr 2018 11:38:21 -0500 Subject: [PATCH 3/4] Add docblock and coding standard compliance. --- _inc/lib/class.media-summary.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/_inc/lib/class.media-summary.php b/_inc/lib/class.media-summary.php index a1fda6364e544..de2d96b06f76f 100644 --- a/_inc/lib/class.media-summary.php +++ b/_inc/lib/class.media-summary.php @@ -298,16 +298,26 @@ static function clean_text( $text ) { ); } + /** + * Retrieve an excerpt for the post summary. + * + * @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, ) ) ); } elseif ( $requested_post instanceof WP_Post ) { From 467eb0eda43a5de9eb5ce013c5c8eda2687d15b5 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Fri, 20 Apr 2018 14:46:40 -0500 Subject: [PATCH 4/4] Add note regarding future todo pending Core. --- _inc/lib/class.media-summary.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_inc/lib/class.media-summary.php b/_inc/lib/class.media-summary.php index de2d96b06f76f..cf1bc0506ffb6 100644 --- a/_inc/lib/class.media-summary.php +++ b/_inc/lib/class.media-summary.php @@ -301,6 +301,10 @@ static function clean_text( $text ) { /** * 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.