Make WordPress Core

Changeset 6359


Ignore:
Timestamp:
12/05/2007 09:25:45 AM (19 years ago)
Author:
ryan
Message:

Refactor next_post_link, previous_post_link. props xknown. fixes #5411

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/link-template.php

    r6352 r6359  
    361361
    362362function get_previous_post($in_same_cat = false, $excluded_categories = '') {
     363        return get_adjacent_post($in_same_cat, $excluded_categories);
     364}
     365
     366function get_next_post($in_same_cat = false, $excluded_categories = '') {
     367        return get_adjacent_post($in_same_cat, $excluded_categories, false);
     368}
     369
     370function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
    363371        global $post, $wpdb;
    364372
    … …  
    369377
    370378        $join = '';
    371         if ( $in_same_cat ) {
    372                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
    373                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
    374                 $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]);
    375                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
    376                         $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = %d', $cat_array[$i]);
    377                 }
    378                 $join .= ')';
    379         }
    380 
    381         $sql_exclude_cats = '';
    382         if ( !empty($excluded_categories) ) {
    383                 $blah = explode(' and ', $excluded_categories);
    384                 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
    385                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
    386         }
    387 
    388         $join  = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories );
    389         $where = apply_filters( 'get_previous_post_where', $wpdb->prepare("WHERE p.post_date < %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
    390         $sort  = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' );
    391 
    392         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
    393 }
    394 
    395 function get_next_post($in_same_cat = false, $excluded_categories = '') {
    396         global $post, $wpdb;
    397 
    398         if( empty($post) || !is_single() || is_attachment() )
    399                 return null;
    400 
    401         $current_post_date = $post->post_date;
    402 
    403         $join = '';
    404         if ( $in_same_cat ) {
    405                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
    406                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
    407                 $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]);
    408                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
    409                         $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = $d', $cat_array[$i]);
    410                 }
    411                 $join .= ')';
    412         }
    413 
    414         $sql_exclude_cats = '';
    415         if ( !empty($excluded_categories) ) {
    416                 $blah = explode(' and ', $excluded_categories);
    417                 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
    418                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
    419         }
    420 
    421         $join  = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories );
    422         $where = apply_filters( 'get_next_post_where', $wpdb->prepare("WHERE p.post_date > %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql AND p.ID != %d", $current_post_date, $post->ID), $in_same_cat, $excluded_categories );
    423         $sort  = apply_filters( 'get_next_post_sort', 'ORDER BY p.post_date ASC LIMIT 1' );
    424 
    425         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
    426 }
    427 
     379        if ( $in_same_cat || !empty($excluded_categories) ) {
     380                $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
     381
     382                if ( $in_same_cat ) {
     383                        $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
     384                        $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')';
     385                }
     386
     387                $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
     388                if ( !empty($excluded_categories) ) {
     389                        $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
     390                        if ( !empty($cat_array) ) {
     391                                $excluded_categories = array_diff($excluded_categories, $cat_array);
     392                                $posts_in_ex_cats_sql = '';
     393                        }
     394
     395                        if ( !empty($excluded_categories) ) {
     396                                $posts_in_ex_cats_sql = " AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
     397                        }
     398                }
     399        }
     400
     401        $adjacent = $previous ? 'previous' : 'next';
     402        $op = $previous ? '<' : '>';
     403        $order = $previous ? 'DESC' : 'ASC';
     404
     405        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
     406        $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
     407        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
     408
     409        return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
     410}
    428411
    429412function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    430 
    431         if ( is_attachment() )
     413        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
     414}
     415
     416function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
     417        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
     418}
     419
     420function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
     421        if ( $previous && is_attachment() )
    432422                $post = & get_post($GLOBALS['post']->post_parent);
    433423        else
    434                 $post = get_previous_post($in_same_cat, $excluded_categories);
     424                $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
    435425
    436426        if ( !$post )
    … …  
    440430
    441431        if ( empty($post->post_title) )
    442                 $title = __('Previous Post');
     432                $title = $previous ? __('Previous Post') : __('Next Post');
    443433
    444434        $title = apply_filters('the_title', $title, $post);
    445         $string = '<a href="'.get_permalink($post->ID).'">';
    446         $link = str_replace('%title', $title, $link);
    447         $link = $pre . $string . $link . '</a>';
    448 
    449         $format = str_replace('%link', $link, $format);
    450 
    451         echo $format;
    452 }
    453 
    454 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    455         $post = get_next_post($in_same_cat, $excluded_categories);
    456 
    457         if ( !$post )
    458                 return;
    459 
    460         $title = $post->post_title;
    461 
    462         if ( empty($post->post_title) )
    463                 $title = __('Next Post');
    464 
    465         $title = apply_filters('the_title', $title, $post);
    466         $string = '<a href="'.get_permalink($post->ID).'">';
     435        $string = '<a href="'.get_permalink($post).'">';
    467436        $link = str_replace('%title', $title, $link);
    468437        $link = $string . $link . '</a>';
     438
    469439        $format = str_replace('%link', $link, $format);
    470440
Note: See TracChangeset for help on using the changeset viewer.