Make WordPress Core

Ticket #18660: canonical.6.patch

File canonical.6.patch, 9.8 KB (added by joostdevalk, 14 years ago)

Canonical Patch v7

  • wp-includes/default-filters.php

     
    203203add_action( 'wp_head',             'feed_links_extra',                3     );
    204204add_action( 'wp_head',             'rsd_link'                               );
    205205add_action( 'wp_head',             'wlwmanifest_link'                       );
    206 add_action( 'wp_head',             'adjacent_posts_rel_link_wp_head', 10, 0 );
     206add_action( 'wp_head',             'adjacent_rel_links',                          10, 0 );
    207207add_action( 'wp_head',             'locale_stylesheet'                      );
    208208add_action( 'publish_future_post', 'check_and_publish_future_post',   10, 1 );
    209209add_action( 'wp_head',             'noindex',                          1    );
  • wp-includes/link-template.php

     
    11631163}
    11641164
    11651165/**
    1166  * Get adjacent post relational link.
    1167  *
    1168  * Can either be next or previous post relational link.
    1169  *
    1170  * @since 2.8.0
    1171  *
    1172  * @param string $title Optional. Link title format.
    1173  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1174  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1175  * @param bool $previous Optional, default is true. Whether display link to previous post.
    1176  * @return string
    1177  */
    1178 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
    1179         if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) )
    1180                 $post = & get_post($GLOBALS['post']->post_parent);
    1181         else
    1182                 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
    1183 
    1184         if ( empty($post) )
    1185                 return;
    1186 
    1187         if ( empty($post->post_title) )
    1188                 $post->post_title = $previous ? __('Previous Post') : __('Next Post');
    1189 
    1190         $date = mysql2date(get_option('date_format'), $post->post_date);
    1191 
    1192         $title = str_replace('%title', $post->post_title, $title);
    1193         $title = str_replace('%date', $date, $title);
    1194         $title = apply_filters('the_title', $title, $post->ID);
    1195 
    1196         $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
    1197         $link .= esc_attr( $title );
    1198         $link .= "' href='" . get_permalink($post) . "' />\n";
    1199 
    1200         $adjacent = $previous ? 'previous' : 'next';
    1201         return apply_filters( "{$adjacent}_post_rel_link", $link );
    1202 }
    1203 
    1204 /**
    1205  * Display relational links for the posts adjacent to the current post.
    1206  *
    1207  * @since 2.8.0
    1208  *
    1209  * @param string $title Optional. Link title format.
    1210  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1211  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1212  */
    1213 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1214         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
    1215         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
    1216 }
    1217 
    1218 /**
    1219  * Display relational links for the posts adjacent to the current post for single post pages.
    1220  *
    1221  * This is meant to be attached to actions like 'wp_head'.  Do not call this directly in plugins or theme templates.
    1222  * @since 3.0.0
    1223  *
    1224  */
    1225 function adjacent_posts_rel_link_wp_head() {
    1226         if ( !is_singular() || is_attachment() )
    1227                 return;
    1228         adjacent_posts_rel_link();
    1229 }
    1230 
    1231 /**
    1232  * Display relational link for the next post adjacent to the current post.
    1233  *
    1234  * @since 2.8.0
    1235  *
    1236  * @param string $title Optional. Link title format.
    1237  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1238  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1239  */
    1240 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1241         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
    1242 }
    1243 
    1244 /**
    1245  * Display relational link for the previous post adjacent to the current post.
    1246  *
    1247  * @since 2.8.0
    1248  *
    1249  * @param string $title Optional. Link title format.
    1250  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1251  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1252  */
    1253 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1254         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
    1255 }
    1256 
    1257 /**
    12581166 * Retrieve boundary post.
    12591167 *
    12601168 * Boundary being either the first or last post by publish date within the constraints specified
     
    22452153}
    22462154
    22472155/**
    2248  * Output rel=canonical for singular queries
     2156 * Get the current archive link
     2157 *
     2158 * @param $paged boolean whether or not to return a link with the current page in the archive, default true
     2159 * @since 3.3
     2160 */
     2161function get_current_archive_link( $paged = true ) {
     2162        $link = false;
     2163       
     2164        if ( is_front_page() ) {
     2165                $link = home_url( '/' );
     2166        } else if ( is_home() && "page" == get_option('show_on_front') ) {
     2167                $link = get_permalink( get_option( 'page_for_posts' ) );
     2168        } else if ( is_tax() || is_tag() || is_category() ) {
     2169                $term = get_queried_object();
     2170                $link = get_term_link( $term, $term->taxonomy );
     2171        } else if ( is_post_type_archive() ) {
     2172                $link = get_post_type_archive_link( get_post_type() );
     2173        } else if ( is_author() ) {
     2174                $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
     2175        } else if ( is_archive() ) {
     2176                if ( is_date() ) {
     2177                        if ( is_day() ) {
     2178                                $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
     2179                        } else if ( is_month() ) {
     2180                                $link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
     2181                        } else if ( is_year() ) {
     2182                                $link = get_year_link( get_query_var('year') );
     2183                        }                                               
     2184                }
     2185        }
     2186       
     2187        if ( $paged && $link && get_query_var('paged') > 1 ) {
     2188                global $wp_rewrite;
     2189                if ( !$wp_rewrite->using_permalinks() ) {
     2190                        $link = add_query_arg( 'paged', get_query_var('paged'), $link );
     2191                } else {
     2192                        $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
     2193                }
     2194        }
     2195        return $link;
     2196}
     2197
     2198/**
     2199 * Output rel=canonical
    22492200 *
    22502201 * @package WordPress
    22512202 * @since 2.9.0
    22522203*/
    22532204function rel_canonical() {
    2254         if ( !is_singular() )
    2255                 return;
     2205        $link = false;
     2206       
     2207        if ( is_singular() ) {
     2208                global $wp_rewrite;
    22562209
    2257         global $wp_the_query;
    2258         if ( !$id = $wp_the_query->get_queried_object_id() )
    2259                 return;
     2210                $link = get_permalink( get_queried_object() );
     2211                if ( get_query_var( 'page' ) > 1 ) {
     2212                        if ( !$wp_rewrite->using_permalinks() )
     2213                                $link = add_query_arg( 'page', get_query_var( 'page' ), $link );
     2214                        else
     2215                                $link = user_trailingslashit( trailingslashit( $link ) . get_query_var( 'page' ), 'single' );
     2216                }
     2217        } else {
     2218                $link = get_current_archive_link();
     2219        }
    22602220
    2261         $link = get_permalink( $id );
    2262         echo "<link rel='canonical' href='$link' />\n";
     2221        $link = apply_filters( 'rel_canonical', $link );
     2222       
     2223        if ( $link )
     2224                echo "<link rel='canonical' href='" . esc_url( $link, 'canonical' ) . "' />\n";
    22632225}
    22642226
    22652227/**
     2228 * Output rel=next and rel=prev links
     2229 * on archives pages these go to next and prev archive pages, when available
     2230 * on singular paginated posts & pages these go to the next page within the post or page
     2231 *
     2232 * @since 3.3
     2233 */
     2234function adjacent_rel_links() {
     2235        global $wp_query;
     2236
     2237        if ( !is_singular() ) {
     2238                $url = get_current_archive_link( false );
     2239
     2240                if ( $url ) {
     2241                        $paged = get_query_var('paged');
     2242                       
     2243                        if ( 0 == $paged )
     2244                                $paged = 1;
     2245
     2246                        if ( $paged > 1 )
     2247                                echo get_adjacent_rel_link( "prev", $url, $paged-1, true, 'category' );
     2248
     2249                        if ( $paged < $wp_query->max_num_pages )
     2250                                echo get_adjacent_rel_link( "next", $url, $paged+1, true, 'category' );
     2251                }
     2252        } else {
     2253                $numpages = substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1;
     2254                if ( $numpages > 1 ) {         
     2255                        $page = get_query_var('page');
     2256                        if ( !$page )
     2257                                $page = 1;
     2258
     2259                        $url = get_permalink( $wp_query->post->ID );
     2260
     2261                        // If the current page is the frontpage, pagination should use /base/
     2262                        if ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $wp_query->post->ID )
     2263                                $usebase = true;
     2264                        else
     2265                                $usebase = false;
     2266                               
     2267                        if ( $page > 1 )
     2268                                echo get_adjacent_rel_link( "prev", $url, $page-1, $usebase, 'single_paged' );
     2269                        if ( $page < $numpages )
     2270                                echo get_adjacent_rel_link( "next", $url, $page+1, $usebase, 'single_paged' );
     2271                }
     2272        }       
     2273}
     2274
     2275/**
     2276 * Get adjacent pages link for archives
     2277 *
     2278 * @param string $rel Link relationship, prev or next.
     2279 * @param string $url the unpaginated URL of the current archive.
     2280 * @param string $page the page number to add on to $url for the $link tag.
     2281 * @param boolean $incl_pagination_base whether or not to include /page/ or not.
     2282 * @param string $context context of the adjacent links, passed on to user_trailingslashit
     2283 * @return string $link link element
     2284 *
     2285 * @since 3.3
     2286 */
     2287function get_adjacent_rel_link( $rel, $url, $page, $incl_pagination_base, $context ) {
     2288        global $wp_rewrite;
     2289        if ( !$wp_rewrite->using_permalinks() ) {
     2290                if ( $page > 1 )
     2291                        $url = add_query_arg( 'paged', $page, $url );
     2292        } else {
     2293                if ( $page > 1 ) {
     2294                        $base = '';
     2295                        if ( $incl_pagination_base )
     2296                                $base = trailingslashit( $wp_rewrite->pagination_base );
     2297                        $url = user_trailingslashit( trailingslashit( $url ) . $base . $page, $context );
     2298                }
     2299        }
     2300        $link = "<link rel='$rel' href='$url' />\n";
     2301        return apply_filters( $rel."_rel_link", $link );       
     2302}
     2303
     2304/**
    22662305 * Return a shortlink for a post, page, attachment, or blog.
    22672306 *
    22682307 * This function exists to provide a shortlink tag that all themes and plugins can target.  A plugin must hook in to