Ticket #36934: 36934.3.diff
File 36934.3.diff, 17.8 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-post.php
diff --git src/wp-includes/class-wp-post.php src/wp-includes/class-wp-post.php index edaf149a3f..c439a5eba1 100644
12 12 * 13 13 * @since 3.5.0 14 14 * 15 * @property string $page_template15 * @property string $page_template The post type template specified for this post. 16 16 * 17 * @property-read array $ancestors 18 * @property-read int $post_category17 * @property-read array $ancestors The post's ancestors. 18 * @property-read array $post_category The categories associated with the post. 19 19 * @property-read string $tag_input 20 * 20 * @property-read array $pages Array of all pages in post/page. Each array element contains part of the content 21 * separated by the <!--nextpage--> tag. 22 * @property-read int $multipage Boolean indicator for whether multiple pages are in play. 21 23 */ 22 24 final class WP_Post { 23 25 … … final class WP_Post { 249 251 * @return bool 250 252 */ 251 253 public function __isset( $key ) { 252 if ( 'ancestors' == $key ) 253 return true; 254 255 if ( 'page_template' == $key ) 256 return true; 257 258 if ( 'post_category' == $key ) 259 return true; 260 261 if ( 'tags_input' == $key ) 262 return true; 263 264 return metadata_exists( 'post', $this->ID, $key ); 254 switch ( $key ) { 255 case 'ancestors': 256 case 'page_template': 257 case 'post_category': 258 case 'tags_input': 259 case 'pages': 260 case 'multipage': 261 return true; 262 default: 263 return metadata_exists( 'post', $this->ID, $key ); 264 } 265 265 } 266 266 267 267 /** … … final class WP_Post { 295 295 return wp_list_pluck( $terms, 'name' ); 296 296 } 297 297 298 if ( 'pages' === $key ) { 299 $content = $this->post_content; 300 if ( false !== strpos( $content, '<!--nextpage-->' ) ) { 301 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 302 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 303 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 304 305 // Ignore nextpage at the beginning of the content. 306 if ( 0 === strpos( $content, '<!--nextpage-->' ) ) 307 $content = substr( $content, 15 ); 308 309 $pages = explode('<!--nextpage-->', $content); 310 } else { 311 $pages = array( $this->post_content ); 312 } 313 314 /** 315 * Filters the "pages" derived from splitting the post content. 316 * 317 * "Pages" are determined by splitting the post content based on the presence 318 * of `<!-- nextpage -->` tags. 319 * 320 * @since 4.4.0 321 * 322 * @param array $pages Array of "pages" derived from the post content. 323 * of `<!-- nextpage -->` tags.. 324 * @param WP_Post $post Current post object. 325 */ 326 return apply_filters( 'content_pagination', $pages, $this ); 327 } 328 329 if ( 'multipage' === $key ) { 330 $numpages = count( $this->pages ); 331 332 return ( $numpages > 1 ) ? 1 : 0; 333 } 334 298 335 // Rest of the values need filtering. 299 336 if ( 'ancestors' == $key ) 300 337 $value = get_post_ancestors( $this ); -
src/wp-includes/class-wp-query.php
diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php index baefec75b4..e75e154109 100644
class WP_Query { 4003 4003 $numpages = 1; 4004 4004 $multipage = 0; 4005 4005 $page = $this->get( 'page' ); 4006 if ( ! $page ) 4006 4007 if ( ! $page || $post->ID !== get_queried_object_id() ) { 4007 4008 $page = 1; 4009 } 4008 4010 4009 4011 /* 4010 4012 * Force full post content when viewing the permalink for the $post, … … class WP_Query { 4018 4020 $more = 0; 4019 4021 } 4020 4022 4021 $content = $post->post_content; 4022 if ( false !== strpos( $content, '<!--nextpage-->' ) ) { 4023 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 4024 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 4025 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 4026 4027 // Ignore nextpage at the beginning of the content. 4028 if ( 0 === strpos( $content, '<!--nextpage-->' ) ) 4029 $content = substr( $content, 15 ); 4030 4031 $pages = explode('<!--nextpage-->', $content); 4032 } else { 4033 $pages = array( $post->post_content ); 4034 } 4035 4036 /** 4037 * Filters the "pages" derived from splitting the post content. 4038 * 4039 * "Pages" are determined by splitting the post content based on the presence 4040 * of `<!-- nextpage -->` tags. 4041 * 4042 * @since 4.4.0 4043 * 4044 * @param array $pages Array of "pages" derived from the post content. 4045 * of `<!-- nextpage -->` tags.. 4046 * @param WP_Post $post Current post object. 4047 */ 4048 $pages = apply_filters( 'content_pagination', $pages, $post ); 4049 4023 $pages = $post->pages; 4050 4024 $numpages = count( $pages ); 4025 $multipage = $post->multipage; 4051 4026 4052 if ( $numpages > 1 ) { 4053 if ( $page > 1 ) { 4054 $more = 1; 4055 } 4056 $multipage = 1; 4057 } else { 4058 $multipage = 0; 4027 if ( $numpages > 1 && $page > 1 ) { 4028 $more = 1; 4059 4029 } 4060 4030 4061 4031 /** -
src/wp-includes/default-filters.php
diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php index 9b45abc9fd..d95ffce693 100644
add_filter( 'the_content', 'shortcode_unautop' ); 140 140 add_filter( 'the_content', 'prepend_attachment' ); 141 141 add_filter( 'the_content', 'wp_make_content_images_responsive' ); 142 142 143 add_filter( 'the_excerpt', 'wptexturize' );144 add_filter( 'the_excerpt', 'convert_smilies' );145 add_filter( 'the_excerpt', 'convert_chars' );146 add_filter( 'the_excerpt', 'wpautop' );147 add_filter( 'the_excerpt', 'shortcode_unautop' );148 add_filter( 'get_the_excerpt', 'wp_trim_excerpt' 143 add_filter( 'the_excerpt', 'wptexturize' ); 144 add_filter( 'the_excerpt', 'convert_smilies' ); 145 add_filter( 'the_excerpt', 'convert_chars' ); 146 add_filter( 'the_excerpt', 'wpautop' ); 147 add_filter( 'the_excerpt', 'shortcode_unautop' ); 148 add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 ); 149 149 150 150 add_filter( 'the_post_thumbnail_caption', 'wptexturize' ); 151 151 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 e4a4db1ddf..9176852c89 100644
function human_time_diff( $from, $to = '' ) { 3282 3282 * The ' […]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter 3283 3283 * 3284 3284 * @since 1.5.0 3285 * @since 4.7.0 Introduced the `$post` parameter. 3285 3286 * 3286 3287 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. 3288 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 3287 3289 * @return string The excerpt. 3288 3290 */ 3289 function wp_trim_excerpt( $text = '' ) {3291 function wp_trim_excerpt( $text = '', $post = null ) { 3290 3292 $raw_excerpt = $text; 3293 3291 3294 if ( '' == $text ) { 3292 $text = get_the_content( '');3295 $text = get_the_content( $post, array( 'more_link_text' => '' ) ); 3293 3296 3294 3297 $text = strip_shortcodes( $text ); 3295 3298 -
src/wp-includes/post-template.php
diff --git src/wp-includes/post-template.php src/wp-includes/post-template.php index 924e3996fb..f918391b38 100644
function the_content( $more_link_text = null, $strip_teaser = false) { 253 253 * @global array $pages Array of all pages in post/page. Each array element contains part of the content separated by the <!--nextpage--> tag. 254 254 * @global int $multipage Boolean indicator for whether multiple pages are in play. 255 255 * 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. 256 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 257 * @param array $args { 258 * Array of arguments. 259 * 260 * @type string $more_link_text Optional. Content for when there is more text. Default null. 261 * @type bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 262 * } 258 263 * @return string 259 264 */ 260 function get_the_content( $more_link_text = null, $strip_teaser = false ) { 261 global $page, $more, $preview, $pages, $multipage; 265 function get_the_content( $post = null, $args = null ) { 266 global $page, $more, $preview; 267 268 $more_link_text = sprintf( 269 '<span aria-label="%1$s">%2$s</span>', 270 sprintf( 271 /* translators: %s: Name of current post */ 272 __( 'Continue reading %s' ), 273 the_title_attribute( array( 'echo' => false ) ) 274 ), 275 __( '(more…)' ) 276 ); 262 277 263 $post = get_post(); 278 $defaults = array( 279 'more_link_text' => $more_link_text, 280 'strip_teaser' => false, 281 ); 264 282 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…)' ) 283 // Backward compatibility for the old function signature. 284 if ( is_bool( $args ) || is_string( $post ) ) { 285 $args = array( 286 'more_link_text' => $post, 287 'strip_teaser' => $args, 274 288 ); 289 $post = null; 275 290 } 276 291 292 $args = wp_parse_args( $args, $defaults ); 293 294 $post = get_post( $post ); 295 277 296 $output = ''; 278 297 $has_teaser = false; 279 298 … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 281 300 if ( post_password_required( $post ) ) 282 301 return get_the_password_form( $post ); 283 302 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 303 setup_postdata( $post ); 304 305 if ( $page > count( $post->pages ) ) // if the requested page doesn't exist 306 $page = count( $post->pages ); // give them the highest numbered page that DOES exist 286 307 287 $content = $p ages[$page - 1];308 $content = $post->pages[ $page - 1 ]; 288 309 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 289 310 $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] ) ) );311 if ( ! empty( $matches[1] ) && ! empty( $args['more_link_text'] ) ) 312 $args['more_link_text'] = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); 292 313 293 314 $has_teaser = true; 294 315 } else { 295 316 $content = array( $content ); 296 317 } 297 318 298 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) ) 299 $strip_teaser = true; 319 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $post->multipage || $page == 1 ) ) { 320 $args['strip_teaser'] = true; 321 } 300 322 301 323 $teaser = $content[0]; 302 324 303 if ( $more && $ strip_teaser && $has_teaser )325 if ( $more && $args['strip_teaser'] && $has_teaser ) { 304 326 $teaser = ''; 327 } 305 328 306 329 $output .= $teaser; 307 330 … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 309 332 if ( $more ) { 310 333 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1]; 311 334 } else { 312 if ( ! empty( $more_link_text ) ) 335 if ( ! empty( $args['more_link_text'] ) ) { 336 $more_link = sprintf( 337 ' <a href="%s#more-%d" class="more-link">%s</a>', 338 get_permalink(), 339 $post->ID, 340 $args['more_link_text'] 341 ); 313 342 314 343 /** 315 344 * Filters the Read More link text. … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 319 348 * @param string $more_link_element Read More link element. 320 349 * @param string $more_link_text Read More text. 321 350 */ 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 ); 351 $output .= apply_filters( 'the_content_more_link', $more_link, $args['more_link_text'] ); 352 } 323 353 $output = force_balance_tags( $output ); 324 354 } 325 355 } … … function get_the_content( $more_link_text = null, $strip_teaser = false ) { 327 357 if ( $preview ) // Preview fix for JavaScript bug with foreign languages. 328 358 $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output ); 329 359 360 wp_reset_postdata(); 361 330 362 return $output; 331 363 } 332 364 -
tests/phpunit/tests/formatting/WpTrimExcerpt.php
diff --git tests/phpunit/tests/formatting/WpTrimExcerpt.php tests/phpunit/tests/formatting/WpTrimExcerpt.php index da508c7712..48b1fc7641 100644
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 tests/phpunit/tests/post/getTheContent.php tests/phpunit/tests/post/getTheContent.php new file mode 100644 index 0000000000..9e1da323b5
- + 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 tests/phpunit/tests/post/getTheExcerpt.php tests/phpunit/tests/post/getTheExcerpt.php index 805333606e..481c1359b9 100644
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 }