WordPress.org

Make WordPress Core

Ticket #5411: 5411.diff

File 5411.diff, 6.8 KB (added by xknown, 6 years ago)
  • wp-includes/link-template.php

     
    356356// Navigation links 
    357357 
    358358function get_previous_post($in_same_cat = false, $excluded_categories = '') { 
    359         global $post, $wpdb; 
    360  
    361         if( empty($post) || !is_single() || is_attachment() ) 
    362                 return null; 
    363  
    364         $current_post_date = $post->post_date; 
    365  
    366         $join = ''; 
    367         if ( $in_same_cat ) { 
    368                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id "; 
    369                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids'); 
    370                 $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]); 
    371                 for ( $i = 1; $i < (count($cat_array)); $i++ ) { 
    372                         $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = %d', $cat_array[$i]); 
    373                 } 
    374                 $join .= ')'; 
    375         } 
    376  
    377         $sql_exclude_cats = ''; 
    378         if ( !empty($excluded_categories) ) { 
    379                 $blah = explode(' and ', $excluded_categories); 
    380                 $posts_in_ex_cats = get_objects_in_term($blah, 'category'); 
    381                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; 
    382         } 
    383  
    384         $join  = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories ); 
    385         $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 ); 
    386         $sort  = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' ); 
    387  
    388         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort"); 
     359        return get_adjacent_post($in_same_cat, $excluded_categories); 
    389360} 
    390361 
    391362function get_next_post($in_same_cat = false, $excluded_categories = '') { 
     363        return get_adjacent_post($in_same_cat, $excluded_categories, false); 
     364} 
     365 
     366function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) { 
    392367        global $post, $wpdb; 
    393368 
    394369        if( empty($post) || !is_single() || is_attachment() ) 
     
    397372        $current_post_date = $post->post_date; 
    398373 
    399374        $join = ''; 
    400         if ( $in_same_cat ) { 
    401                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id "; 
    402                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids'); 
    403                 $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]); 
    404                 for ( $i = 1; $i < (count($cat_array)); $i++ ) { 
    405                         $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = $d', $cat_array[$i]); 
     375        if ( $in_same_cat || !empty($excluded_categories) ) { 
     376                $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"; 
     377 
     378                if ( $in_same_cat ) { 
     379                        $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids'); 
     380                        $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')'; 
    406381                } 
    407                 $join .= ')'; 
    408         } 
    409382 
    410         $sql_exclude_cats = ''; 
    411         if ( !empty($excluded_categories) ) { 
    412                 $blah = explode(' and ', $excluded_categories); 
    413                 $posts_in_ex_cats = get_objects_in_term($blah, 'category'); 
    414                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; 
     383                $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; 
     384                if ( !empty($excluded_categories) ) { 
     385                        $excluded_categories = array_map('intval', explode(' and ', $excluded_categories)); 
     386                        if ( !empty($cat_array) ) { 
     387                                $excluded_categories = array_diff($excluded_categories, $cat_array); 
     388                                $posts_in_ex_cats_sql = ''; 
     389                        } 
     390 
     391                        if ( !empty($excluded_categories) ) { 
     392                                $posts_in_ex_cats_sql = " AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
     393                        } 
     394                } 
    415395        } 
    416396 
    417         $join  = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories ); 
    418         $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 ); 
    419         $sort  = apply_filters( 'get_next_post_sort', 'ORDER BY p.post_date ASC LIMIT 1' ); 
     397        $adjacent = $previous ? 'previous' : 'next'; 
     398        $op = $previous ? '<' : '>'; 
     399        $order = $previous ? 'DESC' : 'ASC'; 
    420400 
    421         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort"); 
     401        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); 
     402        $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 ); 
     403        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 
     404 
     405        return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); 
    422406} 
    423407 
    424  
    425408function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
     409        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true); 
     410} 
    426411 
    427         if ( is_attachment() ) 
     412function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
     413        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false); 
     414} 
     415 
     416function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     417        if ( $previous && is_attachment() ) 
    428418                $post = & get_post($GLOBALS['post']->post_parent); 
    429419        else 
    430                 $post = get_previous_post($in_same_cat, $excluded_categories); 
     420                $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); 
    431421 
    432422        if ( !$post ) 
    433423                return; 
     
    435425        $title = $post->post_title; 
    436426 
    437427        if ( empty($post->post_title) ) 
    438                 $title = __('Previous Post'); 
     428                $title = $previous ? __('Previous Post') : __('Next Post'); 
    439429 
    440430        $title = apply_filters('the_title', $title, $post); 
    441         $string = '<a href="'.get_permalink($post->ID).'">'; 
     431        $string = '<a href="'.get_permalink($post).'">'; 
    442432        $link = str_replace('%title', $title, $link); 
    443         $link = $pre . $string . $link . '</a>'; 
     433        $link = $string . $link . '</a>'; 
    444434 
    445435        $format = str_replace('%link', $link, $format); 
    446436 
    447437        echo $format; 
    448438} 
    449439 
    450 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    451         $post = get_next_post($in_same_cat, $excluded_categories); 
    452  
    453         if ( !$post ) 
    454                 return; 
    455  
    456         $title = $post->post_title; 
    457  
    458         if ( empty($post->post_title) ) 
    459                 $title = __('Next Post'); 
    460  
    461         $title = apply_filters('the_title', $title, $post); 
    462         $string = '<a href="'.get_permalink($post->ID).'">'; 
    463         $link = str_replace('%title', $title, $link); 
    464         $link = $string . $link . '</a>'; 
    465         $format = str_replace('%link', $link, $format); 
    466  
    467         echo $format; 
    468 } 
    469  
    470440function get_pagenum_link($pagenum = 1) { 
    471441        global $wp_rewrite; 
    472442