WordPress.org

Make WordPress Core

Ticket #17807: 17807.6.patch

File 17807.6.patch, 19.3 KB (added by ethitter, 5 years ago)

Refreshed for 3.5

  • wp-includes/link-template.php

     
    10911091 * 
    10921092 * @since 1.5.0 
    10931093 * 
    1094  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1095  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1094 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1095 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1096 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    10961097 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    10971098 */ 
    1098 function get_previous_post($in_same_cat = false, $excluded_categories = '') { 
    1099         return get_adjacent_post($in_same_cat, $excluded_categories); 
     1099function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1100        return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 
    11001101} 
    11011102 
    11021103/** 
     
    11041105 * 
    11051106 * @since 1.5.0 
    11061107 * 
    1107  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1108  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1108 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1109 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1110 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    11091111 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    11101112 */ 
    1111 function get_next_post($in_same_cat = false, $excluded_categories = '') { 
    1112         return get_adjacent_post($in_same_cat, $excluded_categories, false); 
     1113function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1114        return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 
    11131115} 
    11141116 
    11151117/** 
     
    11191121 * 
    11201122 * @since 2.5.0 
    11211123 * 
    1122  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1123  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1124 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1125 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    11241126 * @param bool $previous Optional. Whether to retrieve previous post. 
     1127 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    11251128 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    11261129 */ 
    1127 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { 
     1130function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    11281131        global $post, $wpdb; 
    11291132 
    1130         if ( empty( $post ) ) 
     1133        if ( empty( $post ) || ! taxonomy_exists( $taxonomy ) ) 
    11311134                return null; 
    11321135 
    11331136        $current_post_date = $post->post_date; 
    11341137 
    11351138        $join = ''; 
    1136         $posts_in_ex_cats_sql = ''; 
    1137         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
     1139        $posts_in_ex_terms_sql = ''; 
     1140        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
    11381141                $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"; 
    11391142 
    1140                 if ( $in_same_cat ) { 
    1141                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids')); 
    1142                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; 
     1143                if ( $in_same_term ) { 
     1144                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
     1145                        $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 
    11431146                } 
    11441147 
    1145                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; 
    1146                 if ( ! empty( $excluded_categories ) ) { 
    1147                         if ( ! is_array( $excluded_categories ) ) { 
    1148                                 // back-compat, $excluded_categories used to be IDs separated by " and " 
    1149                                 if ( strpos( $excluded_categories, ' and ' ) !== false ) { 
    1150                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) ); 
    1151                                         $excluded_categories = explode( ' and ', $excluded_categories ); 
     1148                $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 
     1149                if ( ! empty( $excluded_terms ) ) { 
     1150                        if ( ! is_array( $excluded_terms ) ) { 
     1151                                // back-compat, $excluded_terms used to be IDs separated by " and " 
     1152                                if ( strpos( $excluded_terms, ' and ' ) !== false ) { 
     1153                                        _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 
     1154                                        $excluded_terms = explode( ' and ', $excluded_terms ); 
    11521155                                } else { 
    1153                                         $excluded_categories = explode( ',', $excluded_categories ); 
     1156                                        $excluded_terms = explode( ',', $excluded_terms ); 
    11541157                                } 
    11551158                        } 
    11561159 
    1157                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
     1160                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
    11581161 
    1159                         if ( ! empty( $cat_array ) ) { 
    1160                                 $excluded_categories = array_diff($excluded_categories, $cat_array); 
    1161                                 $posts_in_ex_cats_sql = ''; 
     1162                        if ( ! empty( $term_array ) ) { 
     1163                                $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1164                                $posts_in_ex_terms_sql = ''; 
    11621165                        } 
    11631166 
    1164                         if ( !empty($excluded_categories) ) { 
    1165                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
    1166                         } 
     1167                        if ( !empty( $excluded_terms ) ) 
     1168                                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 
    11671169                } 
    11681170        } 
    11691171 
     
    11711173        $op = $previous ? '<' : '>'; 
    11721174        $order = $previous ? 'DESC' : 'ASC'; 
    11731175 
    1174         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); 
    1175         $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories ); 
     1176        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms ); 
     1177        $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms ); 
    11761178        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 
    11771179 
    11781180        $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort"; 
    1179         $query_key = 'adjacent_post_' . md5($query); 
    1180         $result = wp_cache_get($query_key, 'counts'); 
     1181        $query_key = 'adjacent_post_' . md5( $query ); 
     1182        $result = wp_cache_get( $query_key, 'counts' ); 
    11811183        if ( false !== $result ) 
    11821184                return $result; 
    11831185 
    1184         $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); 
     1186        $result = $wpdb->get_row( "SELECT p.* FROM $wpdb->posts AS p $join $where $sort" ); 
    11851187        if ( null === $result ) 
    11861188                $result = ''; 
    11871189 
    1188         wp_cache_set($query_key, $result, 'counts'); 
     1190        wp_cache_set( $query_key, $result, 'counts' ); 
    11891191        return $result; 
    11901192} 
    11911193 
     
    11971199 * @since 2.8.0 
    11981200 * 
    11991201 * @param string $title Optional. Link title format. 
    1200  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1201  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1202 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1203 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    12021204 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1205 * @param string $taxonony Options, default is category. Taxonomy, if $in_same_term is true. 
    12031206 * @return string 
    12041207 */ 
    1205 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     1208function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    12061209        if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) 
    12071210                $post = & get_post($GLOBALS['post']->post_parent); 
    12081211        else 
    1209                 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); 
     1212                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    12101213 
    12111214        if ( empty($post) ) 
    12121215                return; 
    12131216 
    1214         if ( empty($post->post_title) ) 
     1217        if ( empty( $post->post_title ) ) 
    12151218                $post->post_title = $previous ? __('Previous Post') : __('Next Post'); 
    12161219 
    1217         $date = mysql2date(get_option('date_format'), $post->post_date); 
     1220        $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
    12181221 
    1219         $title = str_replace('%title', $post->post_title, $title); 
    1220         $title = str_replace('%date', $date, $title); 
    1221         $title = apply_filters('the_title', $title, $post->ID); 
     1222        $title = str_replace( '%title', $post->post_title, $title ); 
     1223        $title = str_replace( '%date', $date, $title ); 
     1224        $title = apply_filters( 'the_title', $title, $post->ID ); 
    12221225 
    12231226        $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
    12241227        $link .= esc_attr( $title ); 
    1225         $link .= "' href='" . get_permalink($post) . "' />\n"; 
     1228        $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
    12261229 
    12271230        $adjacent = $previous ? 'previous' : 'next'; 
    12281231        return apply_filters( "{$adjacent}_post_rel_link", $link ); 
     
    12341237 * @since 2.8.0 
    12351238 * 
    12361239 * @param string $title Optional. Link title format. 
    1237  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1238  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1240 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1241 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1242 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12391243 */ 
    1240 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1241         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
    1242         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1244function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1245        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
     1246        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12431247} 
    12441248 
    12451249/** 
     
    12611265 * @since 2.8.0 
    12621266 * 
    12631267 * @param string $title Optional. Link title format. 
    1264  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1265  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1268 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1269 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1270 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12661271 */ 
    1267 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1268         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1272function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1273        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12691274} 
    12701275 
    12711276/** 
     
    12741279 * @since 2.8.0 
    12751280 * 
    12761281 * @param string $title Optional. Link title format. 
    1277  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1278  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1282 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1283 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1284 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12791285 */ 
    1280 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1281         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
     1286function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1287        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
    12821288} 
    12831289 
    12841290/** 
    12851291 * Retrieve boundary post. 
    12861292 * 
    12871293 * Boundary being either the first or last post by publish date within the constraints specified 
    1288  * by $in_same_cat or $excluded_categories. 
     1294 * by $in_same_term or $excluded_terms. 
    12891295 * 
    12901296 * @since 2.8.0 
    12911297 * 
    1292  * @param bool $in_same_cat Optional. Whether returned post should be in a same category. 
    1293  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1298 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term. 
     1299 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    12941300 * @param bool $start Optional. Whether to retrieve first or last post. 
     1301 * @param $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12951302 * @return object 
    12961303 */ 
    1297 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) { 
     1304function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 
    12981305        global $post; 
    12991306 
    1300         if ( empty($post) || ! is_single() || is_attachment() ) 
     1307        if ( empty( $post ) || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 
    13011308                return null; 
    13021309 
    1303         $cat_array = array(); 
    1304         if( ! is_array( $excluded_categories ) ) 
    1305                 $excluded_categories = explode( ',', $excluded_categories ); 
     1310        $query_args = array( 
     1311                'posts_per_page' => 1, 
     1312                'order' => $start ? 'ASC' : 'DESC', 
     1313                'update_post_term_cache' => false, 
     1314                'update_post_meta_cache' => false 
     1315        ); 
    13061316 
    1307         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
    1308                 if ( $in_same_cat ) 
    1309                         $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ); 
     1317        $term_array = array(); 
    13101318 
    1311                 if ( ! empty( $excluded_categories ) ) { 
    1312                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
    1313                         $excluded_categories = array_diff( $excluded_categories, $cat_array ); 
     1319        if( ! is_array( $excluded_terms ) ) { 
     1320                if( ! empty( $excluded_terms ) ) 
     1321                        $excluded_terms = explode( ',', $excluded_terms ); 
     1322                else 
     1323                        $excluded_terms = array(); 
     1324        } 
    13141325 
    1315                         $inverse_cats = array(); 
    1316                         foreach ( $excluded_categories as $excluded_category ) 
    1317                                 $inverse_cats[] = $excluded_category * -1; 
    1318                         $excluded_categories = $inverse_cats; 
     1326        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
     1327                if ( $in_same_term ) 
     1328                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
     1329 
     1330                if ( ! empty( $excluded_terms ) ) { 
     1331                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
     1332                        $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1333 
     1334                        $inverse_terms = array(); 
     1335                        foreach ( $excluded_terms as $excluded_term ) { 
     1336                                $inverse_terms[] = $excluded_term * -1; 
     1337                        } 
     1338                        $excluded_terms = $inverse_terms; 
    13191339                } 
     1340 
     1341                $query_args[ 'tax_query' ] = array( array( 
     1342                        'taxonomy' => $taxonomy, 
     1343                        'terms' => array_merge( $term_array, $excluded_terms ) 
     1344                ) ); 
    13201345        } 
    13211346 
    1322         $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) ); 
    1323  
    1324         $order = $start ? 'ASC' : 'DESC'; 
    1325  
    1326         return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) ); 
     1347        return get_posts( $query_args ); 
    13271348} 
    13281349 
    13291350/** 
     
    13331354 * 
    13341355 * @param string $format Optional. Link anchor format. 
    13351356 * @param string $link Optional. Link permalink format. 
    1336  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1337  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1357 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1358 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1359 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13381360 */ 
    1339 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1340         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true); 
     1361function previous_post_link( $format='&laquo; %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1362        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 
    13411363} 
    13421364 
    13431365/** 
     
    13471369 * 
    13481370 * @param string $format Optional. Link anchor format. 
    13491371 * @param string $link Optional. Link permalink format. 
    1350  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1351  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1372 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1373 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1374 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13521375 */ 
    1353 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1354         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false); 
     1376function next_post_link( $format='%link &raquo;', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1377        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 
    13551378} 
    13561379 
    13571380/** 
     
    13631386 * 
    13641387 * @param string $format Link anchor format. 
    13651388 * @param string $link Link permalink format. 
    1366  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1367  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1389 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1390 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. 
    13681391 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1392 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13691393 */ 
    1370 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     1394function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    13711395        if ( $previous && is_attachment() ) 
    13721396                $post = & get_post($GLOBALS['post']->post_parent); 
    13731397        else 
    1374                 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); 
     1398                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    13751399 
    13761400        if ( !$post ) 
    13771401                return;