Changeset 50132 for trunk/src/wp-includes/link-template.php
- Timestamp:
- 02/02/2021 12:38:40 AM (5 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/link-template.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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.
Note: See TracChangeset
for help on using the changeset viewer.