Changeset 50132
- Timestamp:
- 02/02/2021 12:38:40 AM (4 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/canonical.php
r49924 r50132 78 78 $redirect = $original; 79 79 $redirect_url = false; 80 $redirect_obj = false; 80 81 81 82 // Notice fixing. … … 103 104 if ( is_feed() && $post_id ) { 104 105 $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); 106 $redirect_obj = get_post( $post_id ); 105 107 106 108 if ( $redirect_url ) { … … 127 129 128 130 $redirect_url = get_permalink( $post_id ); 131 $redirect_obj = get_post( $post_id ); 129 132 130 133 if ( $redirect_url ) { … … 151 154 if ( $post_type_obj && $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) { 152 155 $redirect_url = get_permalink( $redirect_post ); 156 $redirect_obj = get_post( $redirect_post ); 153 157 154 158 $redirect['query'] = _remove_qs_args_if_not_in_url( … … 198 202 if ( $post_id ) { 199 203 $redirect_url = get_permalink( $post_id ); 204 $redirect_obj = get_post( $post_id ); 200 205 201 206 $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); … … 224 229 if ( ! empty( $_GET['attachment_id'] ) ) { 225 230 $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); 231 $redirect_obj = get_post( get_query_var( 'attachment_id' ) ); 226 232 227 233 if ( $redirect_url ) { … … 230 236 } else { 231 237 $redirect_url = get_attachment_link(); 238 $redirect_obj = get_post(); 232 239 } 233 240 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { 234 241 $redirect_url = get_permalink( get_query_var( 'p' ) ); 242 $redirect_obj = get_post( get_query_var( 'p' ) ); 235 243 236 244 if ( $redirect_url ) { … … 239 247 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { 240 248 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 249 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 241 250 242 251 if ( $redirect_url ) { … … 245 254 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { 246 255 $redirect_url = get_permalink( get_query_var( 'page_id' ) ); 256 $redirect_obj = get_post( get_query_var( 'page_id' ) ); 247 257 248 258 if ( $redirect_url ) { … … 257 267 ) { 258 268 $redirect_url = get_permalink( get_option( 'page_for_posts' ) ); 269 $redirect_obj = get_post( get_option( 'page_for_posts' ) ); 259 270 260 271 if ( $redirect_url ) { … … 311 322 ) { 312 323 $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); 324 $redirect_obj = $author; 313 325 314 326 if ( $redirect_url ) { … … 386 398 ) { 387 399 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 400 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 388 401 } 389 402 } … … 396 409 if ( ! $redirect_url ) { 397 410 $redirect_url = get_permalink( get_queried_object_id() ); 411 $redirect_obj = get_post( get_queried_object_id() ); 398 412 } 399 413 … … 741 755 } 742 756 757 if ( $redirect_obj instanceof WP_Post ) { 758 $post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) ); 759 /* 760 * Unset the redirect object and URL if they are not readable by the user. 761 * This condition is a little confusing as the condition needs to pass if 762 * the post is not readable by the user. That's why there are ! (not) conditions 763 * throughout. 764 */ 765 if ( 766 // Private post statuses only redirect if the user can read them. 767 ! ( 768 $post_status_obj->private && 769 current_user_can( 'read_post', $redirect_obj->ID ) 770 ) && 771 // For other posts, only redirect if publicly viewable. 772 ! is_post_publicly_viewable( $redirect_obj ) 773 ) { 774 $redirect_obj = false; 775 $redirect_url = false; 776 } 777 } 778 743 779 /** 744 780 * Filters the canonical redirect URL. -
trunk/src/wp-includes/capabilities.php
r50131 r50132 246 246 } 247 247 248 $status_obj = get_post_status_object( $post->post_status);248 $status_obj = get_post_status_object( get_post_status( $post ) ); 249 249 if ( ! $status_obj ) { 250 250 /* translators: 1: Post status, 2: Capability name. */ 251 _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post status %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post with that status.' ), $post->post_status, $cap ), '5.4.0' );251 _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post status %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post with that status.' ), get_post_status( $post ), $cap ), '5.4.0' ); 252 252 $caps[] = 'edit_others_posts'; 253 253 break; -
trunk/src/wp-includes/link-template.php
r49789 r50132 88 88 break; 89 89 } 90 } 91 92 /** 93 * Determine whether post should always use an ugly permalink structure. 94 * 95 * @since 5.7.0 96 * 97 * @param WP_Post|int|null $post Optional. Post ID or post object. Defaults to global $post. 98 * @param bool|null $sample Optional. Whether to force consideration based on sample links. 99 * If omitted, a sample link is generated if a post object is passed 100 * with the filter property set to 'sample'. 101 * @return bool Whether to use an ugly permalink structure. 102 */ 103 function wp_force_ugly_post_permalink( $post = null, $sample = null ) { 104 if ( 105 null === $sample && 106 is_object( $post ) && 107 isset( $post->filter ) && 108 'sample' === $post->filter 109 ) { 110 $sample = true; 111 } else { 112 $post = get_post( $post ); 113 $sample = null !== $sample ? $sample : false; 114 } 115 116 if ( ! $post ) { 117 return true; 118 } 119 120 $post_status_obj = get_post_status_object( get_post_status( $post ) ); 121 $post_type_obj = get_post_type_object( get_post_type( $post ) ); 122 123 if ( ! $post_status_obj || ! $post_type_obj ) { 124 return true; 125 } 126 127 if ( 128 // Publicly viewable links never have ugly permalinks. 129 is_post_status_viewable( $post_status_obj ) || 130 ( 131 // Private posts don't have ugly links if the user can read them. 132 $post_status_obj->private && 133 current_user_can( 'read_post', $post->ID ) 134 ) || 135 // Protected posts don't have ugly links if getting a sample URL. 136 ( $post_status_obj->protected && $sample ) 137 ) { 138 return false; 139 } 140 141 return true; 90 142 } 91 143 … … 167 219 if ( 168 220 $permalink && 169 ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future', 'trash' ), true)221 ! wp_force_ugly_post_permalink( $post ) 170 222 ) { 171 223 … … 278 330 $slug = $post->post_name; 279 331 280 $ draft_or_pending = get_post_status( $post ) && in_array( get_post_status( $post ), array( 'draft', 'pending', 'auto-draft', 'future' ), true);332 $force_ugly_link = wp_force_ugly_post_permalink( $post ); 281 333 282 334 $post_type = get_post_type_object( $post->post_type ); … … 286 338 } 287 339 288 if ( ! empty( $post_link ) && ( ! $ draft_or_pending|| $sample ) ) {340 if ( ! empty( $post_link ) && ( ! $force_ugly_link || $sample ) ) { 289 341 if ( ! $leavename ) { 290 342 $post_link = str_replace( "%$post->post_type%", $slug, $post_link ); … … 292 344 $post_link = home_url( user_trailingslashit( $post_link ) ); 293 345 } else { 294 if ( $post_type->query_var && ( isset( $post->post_status ) && ! $ draft_or_pending) ) {346 if ( $post_type->query_var && ( isset( $post->post_status ) && ! $force_ugly_link ) ) { 295 347 $post_link = add_query_arg( $post_type->query_var, $slug, '' ); 296 348 } else { … … 374 426 $post = get_post( $post ); 375 427 376 $ draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ), true);428 $force_ugly_link = wp_force_ugly_post_permalink( $post ); 377 429 378 430 $link = $wp_rewrite->get_page_permastruct(); 379 431 380 if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $ draft_or_pending) || $sample ) ) {432 if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $force_ugly_link ) || $sample ) ) { 381 433 if ( ! $leavename ) { 382 434 $link = str_replace( '%pagename%', get_page_uri( $post ), $link ); … … 418 470 $link = false; 419 471 420 $post = get_post( $post ); 421 $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false; 422 if ( $parent && ! in_array( $parent->post_type, get_post_types(), true ) ) { 423 $parent = false; 424 } 425 426 if ( $wp_rewrite->using_permalinks() && $parent ) { 472 $post = get_post( $post ); 473 $force_ugly_link = wp_force_ugly_post_permalink( $post ); 474 $parent_id = $post->post_parent; 475 $parent = $parent_id ? get_post( $parent_id ) : false; 476 $parent_valid = true; // Default for no parent. 477 if ( 478 $parent_id && 479 ( 480 $post->post_parent === $post->ID || 481 ! $parent || 482 ! is_post_type_viewable( get_post_type( $parent ) ) 483 ) 484 ) { 485 // Post is either its own parent or parent post unavailable. 486 $parent_valid = false; 487 } 488 489 if ( $force_ugly_link || ! $parent_valid ) { 490 $link = false; 491 } elseif ( $wp_rewrite->using_permalinks() && $parent ) { 427 492 if ( 'page' === $parent->post_type ) { 428 493 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front. -
trunk/tests/phpunit/tests/link.php
r48937 r50132 205 205 } 206 206 207 $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) ); 207 $this->assertSame( home_url( "/?attachment_id={$attachment->ID}" ), get_permalink( $attachment_id ) ); 208 // Visit permalink. 209 $this->go_to( get_permalink( $attachment_id ) ); 210 $this->assertQueryTrue( 'is_attachment', 'is_single', 'is_singular' ); 208 211 } 209 212 } -
trunk/tests/phpunit/tests/media.php
r49974 r50132 3123 3123 * 3124 3124 * @param string $post_key Post as keyed in the shared fixture array. 3125 * @param string $expected Expected result.3125 * @param string $expected_url Expected permalink. 3126 3126 * @param bool $expected_404 Whether the page is expected to return a 404 result. 3127 3127 * 3128 3128 */ 3129 function test_attachment_permalinks_based_on_parent_status( $post_key, $expected , $expected_404 ) {3129 function test_attachment_permalinks_based_on_parent_status( $post_key, $expected_url, $expected_404 ) { 3130 3130 $this->set_permalink_structure( '/%postname%' ); 3131 3131 $post = get_post( self::$post_ids[ $post_key ] ); … … 3135 3135 * post object IDs are placeholders that needs to be replaced. 3136 3136 */ 3137 $expected = home_url( str_replace( '%ID%', $post->ID, $expected ) ); 3138 3139 $this->assertSame( $expected, get_permalink( $post ) ); 3137 $expected_url = home_url( str_replace( '%ID%', $post->ID, $expected_url ) ); 3138 3140 3139 $this->go_to( get_permalink( $post ) ); 3141 $this->assertSame( $expected_404, is_404() ); 3140 $this->assertSame( $expected_url, get_permalink( $post ) ); 3141 if ( $expected_404 ) { 3142 $this->assertQueryTrue( 'is_404' ); 3143 } else { 3144 $this->assertQueryTrue( 'is_attachment', 'is_single', 'is_singular' ); 3145 } 3146 $this->assertSame( 'attachment', $post->post_type ); 3142 3147 } 3143 3148 … … 3147 3152 * @return array[] { 3148 3153 * @type string $post_key Post as keyed in the shared fixture array. 3149 * @type string $expected Expected result.3154 * @type string $expected_url Expected permalink. 3150 3155 * $type bool $expected_404 Whether the page is expected to return a 404 result. 3151 3156 * }
Note: See TracChangeset
for help on using the changeset viewer.