Ticket #42814: 42814.6.diff
File 42814.6.diff, 10.3 KB (added by , 6 years ago) |
---|
-
src/wp-includes/class-wp-query.php
diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php index 02f2406479..13bea5133a 100644
class WP_Query { 4155 4155 return; 4156 4156 } 4157 4157 4158 $elements = $this->generate_postdata( $post ); 4159 if ( false === $elements ) { 4160 return; 4161 } 4162 4163 $id = $elements['id']; 4164 $authordata = $elements['authordata']; 4165 $currentday = $elements['currentday']; 4166 $currentmonth = $elements['currentmonth']; 4167 $page = $elements['page']; 4168 $pages = $elements['pages']; 4169 $multipage = $elements['multipage']; 4170 $more = $elements['more']; 4171 $numpages = $elements['numpages']; 4172 4173 return true; 4174 } 4175 4176 /** 4177 * Generate post data. 4178 * 4179 * @since 5.2.0 4180 * 4181 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 4182 * @return array|bool $elements Elements of post or false on failure. 4183 */ 4184 public function generate_postdata( $post ) { 4185 4186 if ( ! ( $post instanceof WP_Post ) ) { 4187 $post = get_post( $post ); 4188 } 4189 4190 if ( ! $post ) { 4191 return false; 4192 } 4193 4158 4194 $id = (int) $post->ID; 4159 4195 4160 4196 $authordata = get_userdata( $post->post_author ); … … class WP_Query { 4235 4271 */ 4236 4272 do_action_ref_array( 'the_post', array( &$post, &$this ) ); 4237 4273 4238 return true; 4274 $elements = compact( 'id','authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); 4275 4276 return $elements; 4239 4277 } 4240 4278 /** 4241 4279 * After looping through a nested query, this function -
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index 1319826212..631525d0e2 100644
add_filter( 'the_excerpt', 'convert_smilies' ); 182 182 add_filter( 'the_excerpt', 'convert_chars' ); 183 183 add_filter( 'the_excerpt', 'wpautop' ); 184 184 add_filter( 'the_excerpt', 'shortcode_unautop' ); 185 add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );185 add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 ); 186 186 187 187 add_filter( 'the_post_thumbnail_caption', 'wptexturize' ); 188 188 add_filter( 'the_post_thumbnail_caption', 'convert_smilies' ); -
src/wp-includes/formatting.php
diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php index b5c4982141..5f33c3197a 100644
function human_time_diff( $from, $to = '' ) { 3676 3676 * The ' […]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter 3677 3677 * 3678 3678 * @since 1.5.0 3679 * @since 5.2.0 Added '$post' param 3679 3680 * 3680 3681 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. 3682 * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default is null. 3681 3683 * @return string The excerpt. 3682 3684 */ 3683 function wp_trim_excerpt( $text = '' ) {3685 function wp_trim_excerpt( $text = '', $post = null ) { 3684 3686 $raw_excerpt = $text; 3685 3687 if ( '' == $text ) { 3686 $text = get_the_content( '' ); 3688 $post = get_post( $post ); 3689 $text = get_the_content( '', false, $post ); 3687 3690 3688 3691 $text = strip_shortcodes( $text ); 3689 3692 $text = excerpt_remove_blocks( $text ); -
src/wp-includes/post-template.php
diff --git src/wp-includes/post-template.php src/wp-includes/post-template.php index 3cf0c7c48f..c356880ccb 100644
function the_content( $more_link_text = null, $strip_teaser = false ) { 253 253 * Retrieve the post content. 254 254 * 255 255 * @since 0.71 256 * @since 5.2.0 Added `$post` parameter. 256 257 * 257 258 * @global int $page Page number of a single post/page. 258 259 * @global int $more Boolean indicator for whether single post/page is being viewed. … … function the_content( $more_link_text = null, $strip_teaser = false ) { 263 264 * 264 265 * @param string $more_link_text Optional. Content for when there is more text. 265 266 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 267 * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default is null. 266 268 * @return string 267 269 */ 268 function get_the_content( $more_link_text = null, $strip_teaser = false ) {270 function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) { 269 271 global $page, $more, $preview, $pages, $multipage; 270 272 271 $post = get_post(); 273 $_post = get_post( $post ); 274 275 if ( ! ( $_post instanceof WP_Post ) ) { 276 return ''; 277 } 278 279 if ( null === $post ) { 280 $elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' ); 281 } else { 282 $elements = generate_postdata( $_post ); 283 } 284 272 285 273 286 if ( null === $more_link_text ) { 274 287 $more_link_text = sprintf( … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 276 289 sprintf( 277 290 /* translators: %s: Name of current post */ 278 291 __( 'Continue reading %s' ), 279 the_title_attribute( array( 'echo' => false ) )292 the_title_attribute( array( 'echo' => false, 'post' => $_post, ) ) 280 293 ), 281 294 __( '(more…)' ) 282 295 ); … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 286 299 $has_teaser = false; 287 300 288 301 // If post password required and it doesn't match the cookie. 289 if ( post_password_required( $ post ) ) {290 return get_the_password_form( $ post );302 if ( post_password_required( $_post ) ) { 303 return get_the_password_form( $_post ); 291 304 } 292 305 293 if ( $ page > count( $pages) ) { // if the requested page doesn't exist294 $ page = count( $pages); // give them the highest numbered page that DOES exist306 if ( $elements['page'] > count( $elements['pages'] ) ) { // if the requested page doesn't exist 307 $elements['page'] = count( $elements['pages'] ); // give them the highest numbered page that DOES exist 295 308 } 296 309 297 $content = $ pages[ $page- 1 ];310 $content = $elements['pages'][ $elements['page'] - 1 ]; 298 311 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 299 312 $content = explode( $matches[0], $content, 2 ); 300 313 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) { … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 306 319 $content = array( $content ); 307 320 } 308 321 309 if ( false !== strpos( $ post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page== 1 ) ) {322 if ( false !== strpos( $_post->post_content, '<!--noteaser-->' ) && ( ! $elements['multipage'] || $elements['page'] == 1 ) ) { 310 323 $strip_teaser = true; 311 324 } 312 325 313 326 $teaser = $content[0]; 314 327 315 if ( $ more&& $strip_teaser && $has_teaser ) {328 if ( $elements['more'] && $strip_teaser && $has_teaser ) { 316 329 $teaser = ''; 317 330 } 318 331 319 332 $output .= $teaser; 320 333 321 334 if ( count( $content ) > 1 ) { 322 if ( $ more) {323 $output .= '<span id="more-' . $ post->ID . '"></span>' . $content[1];335 if ( $elements['more'] ) { 336 $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1]; 324 337 } else { 325 338 if ( ! empty( $more_link_text ) ) { 326 339 … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 332 345 * @param string $more_link_element Read More link element. 333 346 * @param string $more_link_text Read More text. 334 347 */ 335 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( ) . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );348 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); 336 349 } 337 350 $output = force_balance_tags( $output ); 338 351 } -
src/wp-includes/query.php
diff --git src/wp-includes/query.php src/wp-includes/query.php index 9d2db7408e..2e1fa507d1 100644
function setup_postdata( $post ) { 1111 1111 1112 1112 return false; 1113 1113 } 1114 1115 /** 1116 * Generate post data. 1117 * 1118 * @since 5.2.0 1119 * 1120 * @global WP_Query $wp_query Global WP_Query instance. 1121 * 1122 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 1123 * @return array|bool Elements of post, or false on failure. 1124 */ 1125 function generate_postdata( $post ) { 1126 global $wp_query; 1127 1128 if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) { 1129 return $wp_query->generate_postdata( $post ); 1130 } 1131 1132 return false; 1133 } -
tests/phpunit/tests/post/getTheExcerpt.php
diff --git tests/phpunit/tests/post/getTheExcerpt.php tests/phpunit/tests/post/getTheExcerpt.php index 65e5bb0f03..7ede00186a 100644
class Tests_Post_GetTheExcerpt extends WP_UnitTestCase { 57 57 $post_id = self::factory()->post->create( array( 'post_excerpt' => 'Bar' ) ); 58 58 $this->assertSame( 'Bar', get_the_excerpt( $post_id ) ); 59 59 } 60 61 /** 62 * @ticket 42814 63 */ 64 public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_inferred_from_context() { 65 $post_id = self::factory()->post->create( array( 'post_content' => 'Foo', 'post_excerpt' => '' ) ); 66 67 $q = new WP_Query( array( 68 'p' => $post_id, 69 ) ); 70 71 while ( $q->have_posts() ) { 72 $q->the_post(); 73 $found = get_the_excerpt(); 74 } 75 76 $this->assertSame( 'Foo', $found ); 77 } 78 79 /** 80 * @ticket 42814 81 */ 82 public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() { 83 $GLOBALS['post'] = self::factory()->post->create_and_get( array( 84 'post_content' => 'Foo', 85 'post_excerpt' => '' 86 ) ); 87 $this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) ); 88 } 89 90 /** 91 * @ticket 42814 92 */ 93 public function test_should_respect_post_parameter_in_the_loop() { 94 $p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) ); 95 $p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) ); 96 $q = new WP_Query( array( 97 'p' => $p1->ID, 98 ) ); 99 100 while ( $q->have_posts() ) { 101 $q->the_post(); 102 $found = get_the_excerpt( $p2 ); 103 } 104 105 $this->assertSame( 'Bar', $found ); 106 } 107 108 /** 109 * @ticket 42814 110 */ 111 public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() { 112 $p1 = self::factory()->post->create_and_get( array( 'post_content' => 'Foo', 'post_excerpt' => '' ) ); 113 $p2 = self::factory()->post->create_and_get( array( 'post_content' => 'Bar', 'post_excerpt' => '' ) ); 114 $q = new WP_Query( array( 115 'p' => $p1->ID, 116 ) ); 117 118 while ( $q->have_posts() ) { 119 $q->the_post(); 120 $found = get_the_excerpt( $p2 ); 121 } 122 123 $this->assertSame( 'Bar', $found ); 124 } 60 125 }