Changeset 44941
- Timestamp:
- 03/20/2019 03:48:46 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r44518 r44941 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 … … 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 /** -
trunk/src/wp-includes/default-filters.php
r44714 r44941 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' ); -
trunk/src/wp-includes/formatting.php
r44858 r44941 3679 3679 * 3680 3680 * @since 1.5.0 3681 * 3682 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. 3681 * @since 5.2.0 Added the `$post` parameter. 3682 * 3683 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. 3684 * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default is null. 3683 3685 * @return string The excerpt. 3684 3686 */ 3685 function wp_trim_excerpt( $text = '' ) {3687 function wp_trim_excerpt( $text = '', $post = null ) { 3686 3688 $raw_excerpt = $text; 3687 3689 if ( '' == $text ) { 3688 $text = get_the_content( '' ); 3690 $post = get_post( $post ); 3691 $text = get_the_content( '', false, $post ); 3689 3692 3690 3693 $text = strip_shortcodes( $text ); -
trunk/src/wp-includes/post-template.php
r44471 r44941 254 254 * 255 255 * @since 0.71 256 * @since 5.2.0 Added the `$post` parameter. 256 257 * 257 258 * @global int $page Page number of a single post/page. … … 262 263 * @global int $multipage Boolean indicator for whether multiple pages are in play. 263 264 * 264 * @param string $more_link_text Optional. Content for when there is more text. 265 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 265 * @param string $more_link_text Optional. Content for when there is more text. 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 } 272 284 273 285 if ( null === $more_link_text ) { … … 277 289 /* translators: %s: Name of current post */ 278 290 __( 'Continue reading %s' ), 279 the_title_attribute( array( 'echo' => false ) ) 291 the_title_attribute( 292 array( 293 'echo' => false, 294 'post' => $_post, 295 ) 296 ) 280 297 ), 281 298 __( '(more…)' ) … … 287 304 288 305 // If post password required and it doesn't match the cookie. 289 if ( post_password_required( $post ) ) { 290 return get_the_password_form( $post ); 291 } 292 293 if ( $page > count( $pages ) ) { // if the requested page doesn't exist 294 $page = count( $pages ); // give them the highest numbered page that DOES exist 295 } 296 297 $content = $pages[ $page - 1 ]; 306 if ( post_password_required( $_post ) ) { 307 return get_the_password_form( $_post ); 308 } 309 310 if ( $elements['page'] > count( $elements['pages'] ) ) { // if the requested page doesn't exist 311 $elements['page'] = count( $elements['pages'] ); // give them the highest numbered page that DOES exist 312 } 313 314 $page_no = $elements['page']; 315 $content = $elements['pages'][ $page_no - 1 ]; 298 316 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 299 317 $content = explode( $matches[0], $content, 2 ); … … 307 325 } 308 326 309 if ( false !== strpos( $ post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page== 1 ) ) {327 if ( false !== strpos( $_post->post_content, '<!--noteaser-->' ) && ( ! $elements['multipage'] || $elements['page'] == 1 ) ) { 310 328 $strip_teaser = true; 311 329 } … … 313 331 $teaser = $content[0]; 314 332 315 if ( $ more&& $strip_teaser && $has_teaser ) {333 if ( $elements['more'] && $strip_teaser && $has_teaser ) { 316 334 $teaser = ''; 317 335 } … … 320 338 321 339 if ( count( $content ) > 1 ) { 322 if ( $ more) {323 $output .= '<span id="more-' . $ post->ID . '"></span>' . $content[1];340 if ( $elements['more'] ) { 341 $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1]; 324 342 } else { 325 343 if ( ! empty( $more_link_text ) ) { … … 333 351 * @param string $more_link_text Read More text. 334 352 */ 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 );353 $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 354 } 337 355 $output = force_balance_tags( $output ); -
trunk/src/wp-includes/query.php
r43571 r44941 1112 1112 return false; 1113 1113 } 1114 1115 /** 1116 * Generates 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 } -
trunk/tests/phpunit/tests/post/getTheExcerpt.php
r42343 r44941 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( 66 array( 67 'post_content' => 'Foo', 68 'post_excerpt' => '', 69 ) 70 ); 71 72 $q = new WP_Query( 73 array( 74 'p' => $post_id, 75 ) 76 ); 77 78 while ( $q->have_posts() ) { 79 $q->the_post(); 80 $found = get_the_excerpt(); 81 } 82 83 $this->assertSame( 'Foo', $found ); 84 } 85 86 /** 87 * @ticket 42814 88 */ 89 public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() { 90 $GLOBALS['post'] = self::factory()->post->create_and_get( 91 array( 92 'post_content' => 'Foo', 93 'post_excerpt' => '', 94 ) 95 ); 96 $this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) ); 97 } 98 99 /** 100 * @ticket 42814 101 */ 102 public function test_should_respect_post_parameter_in_the_loop() { 103 $p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) ); 104 $p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) ); 105 $q = new WP_Query( 106 array( 107 'p' => $p1->ID, 108 ) 109 ); 110 111 while ( $q->have_posts() ) { 112 $q->the_post(); 113 $found = get_the_excerpt( $p2 ); 114 } 115 116 $this->assertSame( 'Bar', $found ); 117 } 118 119 /** 120 * @ticket 42814 121 */ 122 public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() { 123 $p1 = self::factory()->post->create_and_get( 124 array( 125 'post_content' => 'Foo', 126 'post_excerpt' => '', 127 ) 128 ); 129 $p2 = self::factory()->post->create_and_get( 130 array( 131 'post_content' => 'Bar', 132 'post_excerpt' => '', 133 ) 134 ); 135 $q = new WP_Query( 136 array( 137 'p' => $p1->ID, 138 ) 139 ); 140 141 while ( $q->have_posts() ) { 142 $q->the_post(); 143 $found = get_the_excerpt( $p2 ); 144 } 145 146 $this->assertSame( 'Bar', $found ); 147 } 60 148 }
Note: See TracChangeset
for help on using the changeset viewer.