Changeset 45590 for trunk/src/wp-includes/canonical.php
- Timestamp:
- 07/02/2019 11:41:16 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/canonical.php
r45133 r45590 94 94 } 95 95 96 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { 97 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { 96 $id = get_query_var( 'p' ); 97 98 if ( is_feed() && $id ) { 99 $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ); 100 if ( $redirect_url ) { 98 101 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url ); 99 102 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); … … 101 104 } 102 105 103 if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) )) {106 if ( is_singular() && 1 > $wp_query->post_count && $id ) { 104 107 105 108 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) ); 106 109 107 if ( isset( $vars[0] ) && $vars = $vars[0] ) { 110 if ( ! empty( $vars[0] ) ) { 111 $vars = $vars[0]; 108 112 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) { 109 113 $id = $vars->post_parent; 110 114 } 111 115 112 if ( $redirect_url = get_permalink( $id ) ) { 116 $redirect_url = get_permalink( $id ); 117 if ( $redirect_url ) { 113 118 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); 114 119 } … … 120 125 121 126 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's 122 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 123 if ( $id && $redirect_post = get_post( $id ) ) { 127 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 128 $redirect_post = $id ? get_post( $id ) : false; 129 if ( $redirect_post ) { 124 130 $post_type_obj = get_post_type_object( $redirect_post->post_type ); 125 131 if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { … … 144 150 145 151 if ( ! $redirect_url ) { 146 if ( $redirect_url = redirect_guess_404_permalink() ) { 152 $redirect_url = redirect_guess_404_permalink(); 153 if ( $redirect_url ) { 147 154 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); 148 155 } … … 169 176 } 170 177 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { 171 if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) { 178 $redirect_url = get_permalink( get_query_var( 'p' ) ); 179 if ( $redirect_url ) { 172 180 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); 173 181 } 174 182 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { 175 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) { 183 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 184 if ( $redirect_url ) { 176 185 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); 177 186 } 178 187 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { 179 if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) { 188 $redirect_url = get_permalink( get_query_var( 'page_id' ) ); 189 if ( $redirect_url ) { 180 190 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 181 191 } … … 183 193 $redirect_url = home_url( '/' ); 184 194 } elseif ( is_home() && ! empty( $_GET['page_id'] ) && 'page' == get_option( 'show_on_front' ) && get_query_var( 'page_id' ) == get_option( 'page_for_posts' ) && ! $redirect_url ) { 185 if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) { 195 $redirect_url = get_permalink( get_option( 'page_for_posts' ) ); 196 if ( $redirect_url ) { 186 197 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 187 198 } … … 204 215 // now moving on to non ?m=X year/month/day links 205 216 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) { 206 if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) { 217 $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ); 218 if ( $redirect_url ) { 207 219 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); 208 220 } 209 221 } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) { 210 if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) { 222 $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ); 223 if ( $redirect_url ) { 211 224 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); 212 225 } 213 226 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { 214 if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) { 227 $redirect_url = get_year_link( get_query_var( 'year' ) ); 228 if ( $redirect_url ) { 215 229 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); 216 230 } … … 218 232 $author = get_userdata( get_query_var( 'author' ) ); 219 233 if ( ( false !== $author ) && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) { 220 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) { 234 $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); 235 if ( $redirect_url ) { 221 236 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); 222 237 } … … 230 245 231 246 $obj = $wp_query->get_queried_object(); 232 if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) { 233 if ( ! empty( $redirect['query'] ) ) { 234 // Strip taxonomy query vars off the url. 235 $qv_remove = array( 'term', 'taxonomy' ); 236 if ( is_category() ) { 237 $qv_remove[] = 'category_name'; 238 $qv_remove[] = 'cat'; 239 } elseif ( is_tag() ) { 240 $qv_remove[] = 'tag'; 241 $qv_remove[] = 'tag_id'; 242 } else { // Custom taxonomies will have a custom query var, remove those too: 243 $tax_obj = get_taxonomy( $obj->taxonomy ); 244 if ( false !== $tax_obj->query_var ) { 245 $qv_remove[] = $tax_obj->query_var; 247 if ( $term_count <= 1 && ! empty( $obj->term_id ) ) { 248 $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ); 249 if ( $tax_url && ! is_wp_error( $tax_url ) ) { 250 if ( ! empty( $redirect['query'] ) ) { 251 // Strip taxonomy query vars off the url. 252 $qv_remove = array( 'term', 'taxonomy' ); 253 if ( is_category() ) { 254 $qv_remove[] = 'category_name'; 255 $qv_remove[] = 'cat'; 256 } elseif ( is_tag() ) { 257 $qv_remove[] = 'tag'; 258 $qv_remove[] = 'tag_id'; 259 } else { // Custom taxonomies will have a custom query var, remove those too: 260 $tax_obj = get_taxonomy( $obj->taxonomy ); 261 if ( false !== $tax_obj->query_var ) { 262 $qv_remove[] = $tax_obj->query_var; 263 } 246 264 } 247 } 248 249 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 250 251 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET252 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's 253 254 // Create the destination url for this taxonomy255 $tax_url = parse_url( $tax_url );256 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..257 parse_str( $tax_url['query'], $query_vars);258 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );259 } else { // Taxonomy is accessible via a "pretty-URL"260 $redirect['path'] = $tax_url['path'];261 } 262 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite263 foreach ( $qv_remove as $_qv) {264 if ( isset( $rewrite_vars[ $_qv ] ) ) {265 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );265 266 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 267 268 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET 269 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's 270 271 // Create the destination url for this taxonomy 272 $tax_url = parse_url( $tax_url ); 273 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. 274 parse_str( $tax_url['query'], $query_vars ); 275 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); 276 } else { // Taxonomy is accessible via a "pretty-URL" 277 $redirect['path'] = $tax_url['path']; 278 } 279 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite 280 foreach ( $qv_remove as $_qv ) { 281 if ( isset( $rewrite_vars[ $_qv ] ) ) { 282 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); 283 } 266 284 } 267 285 } … … 269 287 } 270 288 } 271 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { 272 $category = get_category_by_path( $cat ); 273 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { 274 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 289 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) { 290 $cat = get_query_var( 'category_name' ); 291 if ( $cat ) { 292 $category = get_category_by_path( $cat ); 293 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { 294 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 295 } 275 296 } 276 297 }
Note: See TracChangeset
for help on using the changeset viewer.