Ticket #36934: 36934.2.diff
File 36934.2.diff, 17.0 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-post.php
diff --git a/src/wp-includes/class-wp-post.php b/src/wp-includes/class-wp-post.php index a21776f..3f7ced2 100644
a b 17 17 * @property-read array $ancestors 18 18 * @property-read int $post_category 19 19 * @property-read string $tag_input 20 * 20 * @property-read array $pages 21 * @property-read int $multipage 21 22 */ 22 23 final class WP_Post { 23 24 … … final class WP_Post { 250 251 * @return bool 251 252 */ 252 253 public function __isset( $key ) { 253 if ( 'ancestors' == $key ) 254 return true; 255 256 if ( 'page_template' == $key ) 257 return ( 'page' == $this->post_type ); 258 259 if ( 'post_category' == $key ) 260 return true; 261 262 if ( 'tags_input' == $key ) 263 return true; 264 265 return metadata_exists( 'post', $this->ID, $key ); 254 switch ( $key ) { 255 case 'ancestors' : 256 case 'post_category' : 257 case 'tags_input' : 258 case 'pages' : 259 case 'multipage' : 260 return true; 261 262 case 'page_template' : 263 return ( 'page' == $this->post_type ); 264 265 default : 266 return metadata_exists( 'post', $this->ID, $key ); 267 } 266 268 } 267 269 268 270 /** … … final class WP_Post { 296 298 return wp_list_pluck( $terms, 'name' ); 297 299 } 298 300 301 if ( 'pages' === $key ) { 302 $content = $this->post_content; 303 if ( false !== strpos( $content, '<!--nextpage-->' ) ) { 304 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 305 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 306 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 307 308 // Ignore nextpage at the beginning of the content. 309 if ( 0 === strpos( $content, '<!--nextpage-->' ) ) 310 $content = substr( $content, 15 ); 311 312 $pages = explode('<!--nextpage-->', $content); 313 } else { 314 $pages = array( $this->post_content ); 315 } 316 317 /** 318 * Filters the "pages" derived from splitting the post content. 319 * 320 * "Pages" are determined by splitting the post content based on the presence 321 * of `<!-- nextpage -->` tags. 322 * 323 * @since 4.4.0 324 * 325 * @param array $pages Array of "pages" derived from the post content. 326 * of `<!-- nextpage -->` tags.. 327 * @param WP_Post $post Current post object. 328 */ 329 return apply_filters( 'content_pagination', $pages, $this ); 330 } 331 332 if ( 'multipage' === $key ) { 333 $numpages = count( $this->pages ); 334 335 return ( $numpages > 1 ) ? 1 : 0; 336 } 337 299 338 // Rest of the values need filtering. 300 339 if ( 'ancestors' == $key ) 301 340 $value = get_post_ancestors( $this ); -
src/wp-includes/class-wp-query.php
diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 5a90618..92e0e3d 100644
a b class WP_Query { 3994 3994 $more = 0; 3995 3995 } 3996 3996 3997 $content = $post->post_content; 3998 if ( false !== strpos( $content, '<!--nextpage-->' ) ) { 3999 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 4000 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 4001 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 4002 4003 // Ignore nextpage at the beginning of the content. 4004 if ( 0 === strpos( $content, '<!--nextpage-->' ) ) 4005 $content = substr( $content, 15 ); 4006 4007 $pages = explode('<!--nextpage-->', $content); 4008 } else { 4009 $pages = array( $post->post_content ); 4010 } 4011 4012 /** 4013 * Filters the "pages" derived from splitting the post content. 4014 * 4015 * "Pages" are determined by splitting the post content based on the presence 4016 * of `<!-- nextpage -->` tags. 4017 * 4018 * @since 4.4.0 4019 * 4020 * @param array $pages Array of "pages" derived from the post content. 4021 * of `<!-- nextpage -->` tags.. 4022 * @param WP_Post $post Current post object. 4023 */ 4024 $pages = apply_filters( 'content_pagination', $pages, $post ); 4025 3997 $pages = $post->pages; 4026 3998 $numpages = count( $pages ); 3999 $multipage = $post->multipage; 4027 4000 4028 if ( $numpages > 1 ) { 4029 if ( $page > 1 ) { 4030 $more = 1; 4031 } 4032 $multipage = 1; 4033 } else { 4034 $multipage = 0; 4001 if ( $numpages > 1 && $page > 1 ) { 4002 $more = 1; 4035 4003 } 4036 4004 4037 4005 /** -
src/wp-includes/default-filters.php
diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3402e48..d0e8763 100644
a b add_filter( 'the_content', 'shortcode_unautop' ); 139 139 add_filter( 'the_content', 'prepend_attachment' ); 140 140 add_filter( 'the_content', 'wp_make_content_images_responsive' ); 141 141 142 add_filter( 'the_excerpt', 'wptexturize' );143 add_filter( 'the_excerpt', 'convert_smilies' );144 add_filter( 'the_excerpt', 'convert_chars' );145 add_filter( 'the_excerpt', 'wpautop' );146 add_filter( 'the_excerpt', 'shortcode_unautop' );147 add_filter( 'get_the_excerpt', 'wp_trim_excerpt' 142 add_filter( 'the_excerpt', 'wptexturize' ); 143 add_filter( 'the_excerpt', 'convert_smilies' ); 144 add_filter( 'the_excerpt', 'convert_chars' ); 145 add_filter( 'the_excerpt', 'wpautop' ); 146 add_filter( 'the_excerpt', 'shortcode_unautop' ); 147 add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 ); 148 148 149 149 add_filter( 'the_post_thumbnail_caption', 'wptexturize' ); 150 150 add_filter( 'the_post_thumbnail_caption', 'convert_smilies' ); -
src/wp-includes/formatting.php
diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 5869082..5b3297f 100644
a b function human_time_diff( $from, $to = '' ) { 3270 3270 * The ' […]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter 3271 3271 * 3272 3272 * @since 1.5.0 3273 * @since 4.7.0 Introduced the `$post` parameter. 3273 3274 * 3274 3275 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. 3276 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 3275 3277 * @return string The excerpt. 3276 3278 */ 3277 function wp_trim_excerpt( $text = '' ) {3279 function wp_trim_excerpt( $text = '', $post = null ) { 3278 3280 $raw_excerpt = $text; 3281 3279 3282 if ( '' == $text ) { 3280 $text = get_the_content( '');3283 $text = get_the_content( $post, array( 'more_link_text' => '' ) ); 3281 3284 3282 3285 $text = strip_shortcodes( $text ); 3283 3286 -
src/wp-includes/post-template.php
diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 0c1d1e4..565b2f8 100644
a b function the_content( $more_link_text = null, $strip_teaser = false) { 250 250 * @global int $page 251 251 * @global int $more 252 252 * @global bool $preview 253 * @global array $pages254 * @global int $multipage255 253 * 256 * @param string $more_link_text Optional. Content for when there is more text. 257 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 254 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 255 * @param array $args { 256 * Array of arguments. 257 * 258 * @type string $more_link_text Optional. Content for when there is more text. Default null. 259 * @type bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 260 * } 258 261 * @return string 259 262 */ 260 function get_the_content( $more_link_text = null, $strip_teaser = false ) { 261 global $page, $more, $preview, $pages, $multipage; 263 function get_the_content( $post = null, $args = null ) { 264 global $page, $more, $preview; 265 266 $more_link_text = sprintf( 267 '<span aria-label="%1$s">%2$s</span>', 268 sprintf( 269 /* translators: %s: Name of current post */ 270 __( 'Continue reading %s' ), 271 the_title_attribute( array( 'echo' => false ) ) 272 ), 273 __( '(more…)' ) 274 ); 262 275 263 $post = get_post(); 276 $defaults = array( 277 'more_link_text' => $more_link_text, 278 'strip_teaser' => false, 279 ); 264 280 265 if ( null === $more_link_text ) { 266 $more_link_text = sprintf( 267 '<span aria-label="%1$s">%2$s</span>', 268 sprintf( 269 /* translators: %s: Name of current post */ 270 __( 'Continue reading %s' ), 271 the_title_attribute( array( 'echo' => false ) ) 272 ), 273 __( '(more…)' ) 281 // Backward compatibility for the old function signature. 282 if ( is_bool( $args ) || is_string( $post ) ) { 283 $args = array( 284 'more_link_text' => $post, 285 'strip_teaser' => $args, 274 286 ); 287 $post = null; 275 288 } 276 289 290 $args = wp_parse_args( $args, $defaults ); 291 292 $post = get_post( $post ); 293 277 294 $output = ''; 278 295 $has_teaser = false; 279 296 … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 281 298 if ( post_password_required( $post ) ) 282 299 return get_the_password_form( $post ); 283 300 284 if ( $page > count( $pages ) ) // if the requested page doesn't exist 285 $page = count( $pages ); // give them the highest numbered page that DOES exist 301 setup_postdata( $post ); 302 303 if ( $page > count( $post->pages ) ) // if the requested page doesn't exist 304 $page = count( $post->pages ); // give them the highest numbered page that DOES exist 286 305 287 $content = $p ages[$page - 1];306 $content = $post->pages[ $page - 1 ]; 288 307 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 289 308 $content = explode( $matches[0], $content, 2 ); 290 if ( ! empty( $matches[1] ) && ! empty( $ more_link_text) )291 $ more_link_text= strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );309 if ( ! empty( $matches[1] ) && ! empty( $args['more_link_text'] ) ) 310 $args['more_link_text'] = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); 292 311 293 312 $has_teaser = true; 294 313 } else { 295 314 $content = array( $content ); 296 315 } 297 316 298 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) ) 299 $strip_teaser = true; 317 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $post->multipage || $page == 1 ) ) { 318 $args['strip_teaser'] = true; 319 } 300 320 301 321 $teaser = $content[0]; 302 322 303 if ( $more && $ strip_teaser && $has_teaser )323 if ( $more && $args['strip_teaser'] && $has_teaser ) { 304 324 $teaser = ''; 325 } 305 326 306 327 $output .= $teaser; 307 328 … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 309 330 if ( $more ) { 310 331 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1]; 311 332 } else { 312 if ( ! empty( $more_link_text ) ) 333 if ( ! empty( $args['more_link_text'] ) ) { 334 $more_link = sprintf( 335 ' <a href="%s#more-%d" class="more-link">%s</a>', 336 get_permalink(), 337 $post->ID, 338 $args['more_link_text'] 339 ); 313 340 314 341 /** 315 342 * Filters the Read More link text. … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 319 346 * @param string $more_link_element Read More link element. 320 347 * @param string $more_link_text Read More text. 321 348 */ 322 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); 349 $output .= apply_filters( 'the_content_more_link', $more_link, $args['more_link_text'] ); 350 } 323 351 $output = force_balance_tags( $output ); 324 352 } 325 353 } … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 327 355 if ( $preview ) // Preview fix for JavaScript bug with foreign languages. 328 356 $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output ); 329 357 358 wp_reset_postdata(); 359 330 360 return $output; 331 361 } 332 362 -
tests/phpunit/tests/formatting/WpTrimExcerpt.php
diff --git a/tests/phpunit/tests/formatting/WpTrimExcerpt.php b/tests/phpunit/tests/formatting/WpTrimExcerpt.php index da508c7..48b1fc7 100644
a b class Tests_Formatting_WpTrimExcerpt extends WP_UnitTestCase { 26 26 while ( $q->have_posts() ) { 27 27 $q->the_post(); 28 28 $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() ); 29 $this->assertSame( 'Post 1 Page 1Post 1 Page 2', wp_trim_excerpt( '', $post1 ) ); 29 30 } 30 31 } 31 32 } … … class Tests_Formatting_WpTrimExcerpt extends WP_UnitTestCase { 51 52 while ( $q->have_posts() ) { 52 53 $q->the_post(); 53 54 $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() ); 55 $this->assertSame( 'Post 1 Page 1', wp_trim_excerpt( '', $post1 ) ); 54 56 } 55 57 } 56 58 } 59 60 /** 61 * @ticket 36934 62 */ 63 public function test_should_fall_back_on_current_post_when_text_is_empty() { 64 $GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_content' => 'Foo' ) ); 65 66 $this->assertSame( 'Foo', wp_trim_excerpt( '' ) ); 67 } 68 69 /** 70 * @ticket 36934 71 */ 72 public function test_should_respect_post_param_when_text_is_empty() { 73 $GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_content' => 'Foo' ) ); 74 75 $other_post = self::factory()->post->create_and_get( array( 'post_content' => 'Bar' ) ); 76 $this->assertSame( 'Bar', wp_trim_excerpt( '', $other_post ) ); 77 } 78 79 /** 80 * @ticket 36934 81 */ 82 public function test_should_give_precedence_to_passed_excerpt() { 83 $post = self::factory()->post->create_and_get( array( 'post_content' => 'Foo' ) ); 84 $this->assertSame( 'Foo', wp_trim_excerpt( 'Foo', $post ) ); 85 } 57 86 } -
new file tests/phpunit/tests/post/getTheContent.php
diff --git a/tests/phpunit/tests/post/getTheContent.php b/tests/phpunit/tests/post/getTheContent.php new file mode 100644 index 0000000..9e1da32
- + 1 <?php 2 3 /** 4 * @group post 5 */ 6 class Tests_Post_GetTheContent extends WP_UnitTestCase { 7 /** 8 * @ticket 36934 9 */ 10 public function test_argument_back_compat_more_link_text() { 11 $text = 'Foo<!--more-->Bar'; 12 $p = self::factory()->post->create( array( 'post_content' => $text ) ); 13 14 $q = new WP_Query( array( 'p' => $p ) ); 15 while ( $q->have_posts() ) { 16 $q->the_post(); 17 18 $found = get_the_content( 'Ping' ); 19 } 20 21 $this->assertContains( '>Ping<', $found ); 22 } 23 24 /** 25 * @ticket 36934 26 */ 27 public function test_argument_back_compat_strip_teaser() { 28 $text = 'Foo<!--more-->Bar'; 29 $p = self::factory()->post->create( array( 'post_content' => $text ) ); 30 31 $this->go_to( get_permalink( $p ) ); 32 33 $q = new WP_Query( array( 'p' => $p ) ); 34 while ( $q->have_posts() ) { 35 $q->the_post(); 36 37 $found = get_the_content( null, true ); 38 } 39 40 $this->assertNotContains( 'Foo', $found ); 41 } 42 43 /** 44 * @ticket 36934 45 */ 46 public function test_should_respect_pagination_of_inner_post() { 47 $text_1 = 'Foo<!--nextpage-->Bar<!--nextpage-->Baz'; 48 $post_1 = self::factory()->post->create_and_get( array( 'post_content' => $text_1 ) ); 49 50 $text_2 = 'Bing<!--nextpage-->Bang<!--nextpage-->Boom'; 51 $post_2 = self::factory()->post->create_and_get( array( 'post_content' => $text_2 ) ); 52 $go_to = add_query_arg( 'page', '2', get_permalink( $post_1->ID ) ); 53 $this->go_to( $go_to ); 54 55 while ( have_posts() ) { 56 the_post(); 57 $found = get_the_content( $post_2, array( 'more_link_text' => '' ) ); 58 } 59 60 $this->assertSame( 'Bing', $found ); 61 } 62 } -
tests/phpunit/tests/post/getTheExcerpt.php
diff --git a/tests/phpunit/tests/post/getTheExcerpt.php b/tests/phpunit/tests/post/getTheExcerpt.php index 8053336..481c135 100644
a b class Tests_Post_GetTheExcerpt extends WP_UnitTestCase { 52 52 $post_id = self::factory()->post->create( array( 'post_excerpt' => 'Bar' ) ); 53 53 $this->assertSame( 'Bar', get_the_excerpt( $post_id ) ); 54 54 } 55 56 /** 57 * @ticket 36934 58 */ 59 public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_inferred_from_context() { 60 $post_id = self::factory()->post->create( array( 'post_content' => 'Foo', 'post_excerpt' => '' ) ); 61 $q = new WP_Query( array( 62 'p' => $post_id, 63 ) ); 64 65 while ( $q->have_posts() ) { 66 $q->the_post(); 67 $found = get_the_excerpt(); 68 } 69 70 $this->assertSame( 'Foo', $found ); 71 } 72 73 /** 74 * @ticket 36934 75 */ 76 public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() { 77 $GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_content' => 'Foo', 'post_excerpt' => '' ) ); 78 $this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) ); 79 } 80 81 /** 82 * @ticket 36934 83 */ 84 public function test_should_respect_post_parameter_in_the_loop() { 85 $p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) ); 86 $p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) ); 87 $q = new WP_Query( array( 88 'p' => $p1->ID, 89 ) ); 90 91 while ( $q->have_posts() ) { 92 $q->the_post(); 93 $found = get_the_excerpt( $p2 ); 94 } 95 96 $this->assertSame( 'Bar', $found ); 97 } 98 99 /** 100 * @ticket 36934 101 */ 102 public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() { 103 $p1 = self::factory()->post->create_and_get( array( 'post_content' => 'Foo', 'post_excerpt' => '' ) ); 104 $p2 = self::factory()->post->create_and_get( array( 'post_content' => 'Bar', 'post_excerpt' => '' ) ); 105 $q = new WP_Query( array( 106 'p' => $p1->ID, 107 ) ); 108 109 while ( $q->have_posts() ) { 110 $q->the_post(); 111 $found = get_the_excerpt( $p2 ); 112 } 113 114 $this->assertSame( 'Bar', $found ); 115 } 55 116 }