Ticket #42814: 42814.9.diff
File 42814.9.diff, 16.3 KB (added by , 6 years ago) |
---|
-
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 02f2406479..13bea5133a 100644
a b 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 a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 1319826212..631525d0e2 100644
a b 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 a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index b5c4982141..b740634144 100644
a b 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 a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 3cf0c7c48f..c356880ccb 100644
a b 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 a/src/wp-includes/query.php b/src/wp-includes/query.php index 9d2db7408e..2e1fa507d1 100644
a b 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 } -
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 0000000000..7bb14a1f93
- + 1 <?php 2 3 /** 4 * @group post 5 * @group formatting 6 */ 7 class Tests_Post_GetTheContent extends WP_UnitTestCase { 8 /** 9 * @ticket 42814 10 */ 11 public function test_argument_back_compat_more_link_text() { 12 $text = 'Foo<!--more-->Bar'; 13 $p = self::factory()->post->create( array( 'post_content' => $text ) ); 14 15 $q = new WP_Query( array( 'p' => $p ) ); 16 while ( $q->have_posts() ) { 17 $q->the_post(); 18 19 $found = get_the_content( 'Ping' ); 20 } 21 22 $this->assertContains( '>Ping<', $found ); 23 } 24 25 /** 26 * @ticket 42814 27 */ 28 public function test_argument_back_compat_strip_teaser() { 29 $text = 'Foo<!--more-->Bar'; 30 $p = self::factory()->post->create( array( 'post_content' => $text ) ); 31 32 $this->go_to( get_permalink( $p ) ); 33 34 $q = new WP_Query( array( 'p' => $p ) ); 35 while ( $q->have_posts() ) { 36 $q->the_post(); 37 38 $found = get_the_content( null, true ); 39 } 40 41 $this->assertNotContains( 'Foo', $found ); 42 } 43 44 /** 45 * @ticket 42814 46 */ 47 public function test_content_other_post() { 48 $text_1 = 'Foo<!--nextpage-->Bar<!--nextpage-->Baz'; 49 $post_1 = self::factory()->post->create_and_get( array( 'post_content' => $text_1 ) ); 50 51 $text_2 = 'Bing<!--nextpage-->Bang<!--nextpage-->Boom'; 52 $post_2 = self::factory()->post->create_and_get( array( 'post_content' => $text_2 ) ); 53 setup_postdata( $post_1 ); 54 $found = get_the_content( null, true, $post_2 ); 55 56 $this->assertSame( 'Bing', $found ); 57 } 58 59 /** 60 * @ticket 42814 61 */ 62 public function test_should_respect_pagination_of_inner_post() { 63 $text_1 = 'Foo<!--nextpage-->Bar<!--nextpage-->Baz'; 64 $post_1 = self::factory()->post->create_and_get( array( 'post_content' => $text_1 ) ); 65 66 $text_2 = 'Bing<!--nextpage-->Bang<!--nextpage-->Boom'; 67 $post_2 = self::factory()->post->create_and_get( array( 'post_content' => $text_2 ) ); 68 $go_to = add_query_arg( 'page', '2', get_permalink( $post_1->ID ) ); 69 $this->go_to( $go_to ); 70 71 while ( have_posts() ) { 72 the_post(); 73 $found = get_the_content( '', false, $post_2 ); 74 } 75 76 $this->assertSame( 'Bang', $found ); 77 } 78 } -
tests/phpunit/tests/post/getTheExcerpt.php
diff --git a/tests/phpunit/tests/post/getTheExcerpt.php b/tests/phpunit/tests/post/getTheExcerpt.php index 65e5bb0f03..7ede00186a 100644
a b 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 } -
new file tests/phpunit/tests/query/generatePostdata.php
diff --git a/tests/phpunit/tests/query/generatePostdata.php b/tests/phpunit/tests/query/generatePostdata.php new file mode 100644 index 0000000000..a8bcb7fee7
- + 1 <?php 2 3 /** 4 * @group query 5 * @covers ::generate_postdata 6 */ 7 class Tests_Query_GeneratePostdata extends WP_UnitTestCase { 8 9 /** 10 * @ticket 42814 11 */ 12 public function test_setup_by_id() { 13 $p = self::factory()->post->create_and_get(); 14 $data = generate_postdata( $p->ID ); 15 $this->assertSame( $p->ID, $data['id'] ); 16 } 17 18 /** 19 * @ticket 42814 20 */ 21 public function test_setup_by_fake_post() { 22 $fake = new stdClass; 23 $fake->ID = 98765; 24 $data = generate_postdata( $fake->ID ); 25 26 // Fails because there's no post with this ID. 27 $this->assertNotSame( $fake->ID, $data['id'] ); 28 } 29 30 /** 31 * @ticket 42814 32 */ 33 public function test_setup_by_postish_object() { 34 $p = self::factory()->post->create(); 35 36 $post = new stdClass(); 37 $post->ID = $p; 38 $data = generate_postdata( $p ); 39 40 $this->assertSame( $p, $data['id'] ); 41 } 42 43 /** 44 * @ticket 42814 45 */ 46 public function test_authordata() { 47 $u = self::factory()->user->create_and_get(); 48 $p = self::factory()->post->create_and_get( 49 array( 50 'post_author' => $u->ID, 51 ) 52 ); 53 $data = generate_postdata( $p ); 54 55 $this->assertNotEmpty( $data['authordata'] ); 56 $this->assertEquals( $u, $data['authordata'] ); 57 } 58 59 /** 60 * @ticket 42814 61 */ 62 public function test_currentday() { 63 $p = self::factory()->post->create_and_get( 64 array( 65 'post_date' => '1980-09-09 06:30:00', 66 ) 67 ); 68 $data = generate_postdata( $p ); 69 70 $this->assertSame( '09.09.80', $data['currentday'] ); 71 } 72 73 public function test_currentmonth() { 74 $p = self::factory()->post->create_and_get( 75 array( 76 'post_date' => '1980-09-09 06:30:00', 77 ) 78 ); 79 $data = generate_postdata( $p ); 80 81 $this->assertSame( '09', $data['currentmonth'] ); 82 } 83 84 /** 85 * @ticket 42814 86 */ 87 public function test_single_page() { 88 $post = self::factory()->post->create_and_get( 89 array( 90 'post_content' => 'Page 0', 91 ) 92 ); 93 $data = generate_postdata( $post ); 94 95 $this->assertSame( 0, $data['multipage'] ); 96 $this->assertSame( 1, $data['numpages'] ); 97 $this->assertEquals( array( 'Page 0' ), $data['pages'] ); 98 } 99 100 /** 101 * @ticket 42814 102 */ 103 public function test_multi_page() { 104 $post = self::factory()->post->create_and_get( 105 array( 106 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', 107 ) 108 ); 109 $data = generate_postdata( $post ); 110 111 $this->assertSame( 1, $data['multipage'] ); 112 $this->assertSame( 4, $data['numpages'] ); 113 $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $data['pages'] ); 114 } 115 116 /** 117 * @ticket 42814 118 */ 119 public function test_nextpage_at_start_of_content() { 120 $post = self::factory()->post->create_and_get( 121 array( 122 'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', 123 ) 124 ); 125 $data = generate_postdata( $post ); 126 127 $this->assertSame( 1, $data['multipage'] ); 128 $this->assertSame( 3, $data['numpages'] ); 129 $this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $data['pages'] ); 130 } 131 132 /** 133 * @ticket 42814 134 */ 135 public function test_trim_nextpage_linebreaks() { 136 $post = self::factory()->post->create_and_get( 137 array( 138 'post_content' => "Page 0\n<!--nextpage-->\nPage 1\nhas a line break\n<!--nextpage-->Page 2<!--nextpage-->\n\nPage 3", 139 ) 140 ); 141 $data = generate_postdata( $post ); 142 143 $this->assertEquals( array( 'Page 0', "Page 1\nhas a line break", 'Page 2', "\nPage 3" ), $data['pages'] ); 144 } 145 }