WordPress.org

Make WordPress Core

Ticket #17807: 17807.11.patch

File 17807.11.patch, 28.2 KB (added by ethitter, 5 years ago)
  • src/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.
    1096  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     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. Taxonomy, if $in_same_term is true. Default 'category'.
     1097 * @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.
    1109  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     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. Taxonomy, if $in_same_term is true. Default 'category'.
     1111 * @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 $previous Optional. Whether to retrieve previous post.
    1125  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     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.
     1126 * @param bool         $previous       Optional. Whether to retrieve previous post.
     1127 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1128 * @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 $wpdb;
    11291132
    1130         if ( ! $post = get_post() )
     1133        if ( ( ! $post = get_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                         if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
     1143                if ( $in_same_term ) {
     1144                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    11421145                                return '';
    1143                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
    1144                         if ( ! $cat_array || is_wp_error( $cat_array ) )
     1146                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1147                        if ( ! $term_array || is_wp_error( $term_array ) )
    11451148                                return '';
    1146                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
     1149                        $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
    11471150                }
    11481151
    1149                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    1150                 if ( ! empty( $excluded_categories ) ) {
    1151                         if ( ! is_array( $excluded_categories ) ) {
    1152                                 // back-compat, $excluded_categories used to be IDs separated by " and "
    1153                                 if ( strpos( $excluded_categories, ' and ' ) !== false ) {
    1154                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
    1155                                         $excluded_categories = explode( ' and ', $excluded_categories );
     1152                $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     1153                if ( ! empty( $excluded_terms ) ) {
     1154                        if ( ! is_array( $excluded_terms ) ) {
     1155                                // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     1156                                if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1157                                        _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1158                                        $excluded_terms = explode( ' and ', $excluded_terms );
    11561159                                } else {
    1157                                         $excluded_categories = explode( ',', $excluded_categories );
     1160                                        $excluded_terms = explode( ',', $excluded_terms );
    11581161                                }
    11591162                        }
    11601163
    1161                         $excluded_categories = array_map( 'intval', $excluded_categories );
     1164                        $excluded_terms = array_map( 'intval', $excluded_terms );
    11621165
    1163                         if ( ! empty( $cat_array ) ) {
    1164                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
    1165                                 $posts_in_ex_cats_sql = '';
     1166                        if ( ! empty( $term_array ) ) {
     1167                                $excluded_terms = array_diff( $excluded_terms, $term_array );
     1168                                $posts_in_ex_terms_sql = '';
    11661169                        }
    11671170
    1168                         if ( !empty($excluded_categories) ) {
    1169                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
     1171                        if ( ! empty( $excluded_terms ) ) {
     1172                                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
    11701173                        }
    11711174                }
    11721175        }
     
    11751178        $op = $previous ? '<' : '>';
    11761179        $order = $previous ? 'DESC' : 'ASC';
    11771180
    1178         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    1179         $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 );
     1181        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
     1182        $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 );
    11801183        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    11811184
    11821185        $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
    1183         $query_key = 'adjacent_post_' . md5($query);
    1184         $result = wp_cache_get($query_key, 'counts');
     1186        $query_key = 'adjacent_post_' . md5( $query );
     1187        $result = wp_cache_get( $query_key, 'counts' );
    11851188        if ( false !== $result ) {
    11861189                if ( $result )
    11871190                        $result = get_post( $result );
     
    11921195        if ( null === $result )
    11931196                $result = '';
    11941197
    1195         wp_cache_set($query_key, $result, 'counts');
     1198        wp_cache_set( $query_key, $result, 'counts' );
    11961199
    11971200        if ( $result )
    11981201                $result = get_post( $result );
     
    12071210 *
    12081211 * @since 2.8.0
    12091212 *
    1210  * @param string $title Optional. Link title format.
    1211  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1212  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1213  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1213 * @param string       $title          Optional. Link title format.
     1214 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1215 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1216 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1217 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    12141218 * @return string
    12151219 */
    1216 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
     1220function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    12171221        if ( $previous && is_attachment() && $post = get_post() )
    12181222                $post = get_post( $post->post_parent );
    12191223        else
    1220                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
     1224                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    12211225
    1222         if ( empty($post) )
     1226        if ( empty( $post ) )
    12231227                return;
    12241228
    12251229        $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
    12261230
    12271231        if ( empty( $post_title ) )
    1228                 $post_title = $previous ? __('Previous Post') : __('Next Post');
     1232                $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
    12291233
    1230         $date = mysql2date(get_option('date_format'), $post->post_date);
     1234        $date = mysql2date( get_option( 'date_format' ), $post->post_date );
    12311235
    1232         $title = str_replace('%title', $post_title, $title);
    1233         $title = str_replace('%date', $date, $title);
     1236        $title = str_replace( '%title', $post_title, $title );
     1237        $title = str_replace( '%date', $date, $title );
    12341238
    12351239        $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
    12361240        $link .= esc_attr( $title );
    1237         $link .= "' href='" . get_permalink($post) . "' />\n";
     1241        $link .= "' href='" . get_permalink( $post ) . "' />\n";
    12381242
    12391243        $adjacent = $previous ? 'previous' : 'next';
    12401244        return apply_filters( "{$adjacent}_post_rel_link", $link );
     
    12451249 *
    12461250 * @since 2.8.0
    12471251 *
    1248  * @param string $title Optional. Link title format.
    1249  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1250  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1252 * @param string       $title          Optional. Link title format.
     1253 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1254 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1255 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    12511256 */
    1252 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1253         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
    1254         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1257function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1258        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
     1259        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12551260}
    12561261
    12571262/**
     
    12721277 *
    12731278 * @since 2.8.0
    12741279 *
    1275  * @param string $title Optional. Link title format.
    1276  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1277  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1280 * @param string       $title          Optional. Link title format.
     1281 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1282 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1283 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    12781284 */
    1279 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1280         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1285function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1286        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12811287}
    12821288
    12831289/**
     
    12851291 *
    12861292 * @since 2.8.0
    12871293 *
    1288  * @param string $title Optional. Link title format.
    1289  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1290  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1294 * @param string       $title          Optional. Link title format.
     1295 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1296 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
     1297 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    12911298 */
    1292 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1293         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     1299function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1300        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
    12941301}
    12951302
    12961303/**
    12971304 * Retrieve boundary post.
    12981305 *
    12991306 * Boundary being either the first or last post by publish date within the constraints specified
    1300  * by $in_same_cat or $excluded_categories.
     1307 * by $in_same_term or $excluded_terms.
    13011308 *
    13021309 * @since 2.8.0
    13031310 *
    1304  * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
    1305  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1306  * @param bool $start Optional. Whether to retrieve first or last post.
     1311 * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
     1312 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1313 * @param bool         $start          Optional. Whether to retrieve first or last post.
     1314 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13071315 * @return object
    13081316 */
    1309 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
     1317function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
    13101318        $post = get_post();
    1311         if ( ! $post || ! is_single() || is_attachment() )
     1319        if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
    13121320                return null;
    13131321
    1314         $cat_array = array();
    1315         if( ! is_array( $excluded_categories ) )
    1316                 $excluded_categories = explode( ',', $excluded_categories );
     1322        $query_args = array(
     1323                'posts_per_page' => 1,
     1324                'order' => $start ? 'ASC' : 'DESC',
     1325                'update_post_term_cache' => false,
     1326                'update_post_meta_cache' => false
     1327        );
    13171328
    1318         if ( $in_same_cat || ! empty( $excluded_categories ) ) {
    1319                 if ( $in_same_cat )
    1320                         $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
     1329        $term_array = array();
    13211330
    1322                 if ( ! empty( $excluded_categories ) ) {
    1323                         $excluded_categories = array_map( 'intval', $excluded_categories );
    1324                         $excluded_categories = array_diff( $excluded_categories, $cat_array );
     1331        if ( ! is_array( $excluded_terms ) ) {
     1332                if ( ! empty( $excluded_terms ) )
     1333                        $excluded_terms = explode( ',', $excluded_terms );
     1334                else
     1335                        $excluded_terms = array();
     1336        }
    13251337
    1326                         $inverse_cats = array();
    1327                         foreach ( $excluded_categories as $excluded_category )
    1328                                 $inverse_cats[] = $excluded_category * -1;
    1329                         $excluded_categories = $inverse_cats;
     1338        if ( $in_same_term || ! empty( $excluded_terms ) ) {
     1339                if ( $in_same_term )
     1340                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1341
     1342                if ( ! empty( $excluded_terms ) ) {
     1343                        $excluded_terms = array_map( 'intval', $excluded_terms );
     1344                        $excluded_terms = array_diff( $excluded_terms, $term_array );
     1345
     1346                        $inverse_terms = array();
     1347                        foreach ( $excluded_terms as $excluded_term )
     1348                                $inverse_terms[] = $excluded_term * -1;
     1349                        $excluded_terms = $inverse_terms;
    13301350                }
     1351
     1352                $query_args[ 'tax_query' ] = array( array(
     1353                        'taxonomy' => $taxonomy,
     1354                        'terms' => array_merge( $term_array, $excluded_terms )
     1355                ) );
    13311356        }
    13321357
    1333         $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
    1334 
    1335         $order = $start ? 'ASC' : 'DESC';
    1336 
    1337         return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
     1358        return get_posts( $query_args );
    13381359}
    13391360
    13401361/*
     
    13421363 *
    13431364 * @since 3.7.0
    13441365 *
    1345  * @param string $format Optional. Link anchor format.
    1346  * @param string $link Optional. Link permalink format.
    1347  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1348  * @param string $excluded_categories Optional. Excluded categories IDs.
     1366 * @param string       $format         Optional. Link anchor format.
     1367 * @param string       $link           Optional. Link permalink format.
     1368 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1369 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1370 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13491371 * @return string
    13501372 */
    1351 function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1352         return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true );
     1373function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1374        return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
    13531375}
    13541376
    13551377/**
     
    13581380 * @since 1.5.0
    13591381 * @uses get_previous_post_link()
    13601382 *
    1361  * @param string $format Optional. Link anchor format.
    1362  * @param string $link Optional. Link permalink format.
    1363  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1364  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1383 * @param string       $format         Optional. Link anchor format.
     1384 * @param string       $link           Optional. Link permalink format.
     1385 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1386 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1387 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13651388 */
    1366 function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1367         echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1389function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1390        echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
    13681391}
    13691392
    13701393/**
     
    13731396 * @since 3.7.0
    13741397 * @uses get_next_post_link()
    13751398 *
    1376  * @param string $format Optional. Link anchor format.
    1377  * @param string $link Optional. Link permalink format.
    1378  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1379  * @param string $excluded_categories Optional. Excluded categories IDs.
     1399 * @param string       $format         Optional. Link anchor format.
     1400 * @param string       $link           Optional. Link permalink format.
     1401 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1402 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1403 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13801404 * @return string
    13811405 */
    1382 function get_next_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1383         return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false );
     1406function get_next_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1407        return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, false, $taxonomy );
    13841408}
    13851409
    13861410/**
     
    13881412 *
    13891413 * @since 1.5.0
    13901414 *
    1391  * @param string $format Optional. Link anchor format.
    1392  * @param string $link Optional. Link permalink format.
    1393  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1394  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1415 * @param string       $format         Optional. Link anchor format.
     1416 * @param string       $link           Optional. Link permalink format.
     1417 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1418 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1419 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13951420 */
    1396 function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1397          echo get_next_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1421function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1422         echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
    13981423}
    13991424
    14001425/**
     
    14041429 *
    14051430 * @since 3.7.0
    14061431 *
    1407  * @param string $format Link anchor format.
    1408  * @param string $link Link permalink format.
    1409  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1410  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1411  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1432 * @param string       $format         Link anchor format.
     1433 * @param string       $link           Link permalink format.
     1434 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1435 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
     1436 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1437 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    14121438 * @return string
    14131439 */
    1414 function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1440function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    14151441        if ( $previous && is_attachment() )
    14161442                $post = get_post( get_post()->post_parent );
    14171443        else
    1418                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
     1444                $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
    14191445
    14201446        if ( ! $post ) {
    14211447                $output = '';
     
    14511477 * @since 2.5.0
    14521478 * @uses get_adjacent_post_link()
    14531479 *
    1454  * @param string $format Link anchor format.
    1455  * @param string $link Link permalink format.
    1456  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1457  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1458  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1480 * @param string       $format         Link anchor format.
     1481 * @param string       $link           Link permalink format.
     1482 * @param bool         $in_same_cat    Optional. Whether link should be in a same category.
     1483 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
     1484 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1485 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    14591486 * @return string
    14601487 */
    1461 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
    1462         echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, $previous );
     1488function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
     1489        echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
    14631490}
    14641491
    14651492/**
  • tests/phpunit/tests/link.php

     
    9696                $wp_rewrite->flush_rules();
    9797        }
    9898
     99        /**
     100         * @ticket 17807
     101         */
     102        function test_get_adjacent_post() {
     103                // Need some sample posts to test adjacency
     104                $post_one = $this->factory->post->create_and_get( array(
     105                        'post_title' => 'First',
     106                        'post_date' => '2012-01-01 12:00:00'
     107                ) );
     108
     109                $post_two = $this->factory->post->create_and_get( array(
     110                        'post_title' => 'Second',
     111                        'post_date' => '2012-02-01 12:00:00'
     112                ) );
     113
     114                $post_three = $this->factory->post->create_and_get( array(
     115                        'post_title' => 'Third',
     116                        'post_date' => '2012-03-01 12:00:00'
     117                ) );
     118
     119                $post_four = $this->factory->post->create_and_get( array(
     120                        'post_title' => 'Fourth',
     121                        'post_date' => '2012-04-01 12:00:00'
     122                ) );
     123
     124                // Assign some terms
     125                wp_set_object_terms( $post_one->ID, 'wordpress', 'category', false );
     126                wp_set_object_terms( $post_three->ID, 'wordpress', 'category', false );
     127
     128                wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false );
     129                wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false );
     130
     131                // Test normal post adjacency
     132                $this->go_to( get_permalink( $post_two->ID ) );
     133
     134                $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) );
     135                $this->assertEquals( $post_three, get_adjacent_post( false, '', false ) );
     136
     137                $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) );
     138                $this->assertNotEquals( $post_two, get_adjacent_post( false, '', false ) );
     139
     140                // Test category adjacency
     141                $this->go_to( get_permalink( $post_one->ID ) );
     142
     143                $this->assertEquals( '', get_adjacent_post( true, '', true, 'category' ) );
     144                $this->assertEquals( $post_three, get_adjacent_post( true, '', false, 'category' ) );
     145
     146                // Test tag adjacency
     147                $this->go_to( get_permalink( $post_two->ID ) );
     148
     149                $this->assertEquals( '', get_adjacent_post( true, '', true, 'post_tag' ) );
     150                $this->assertEquals( $post_four, get_adjacent_post( true, '', false, 'post_tag' ) );
     151
     152                // Test normal boundary post
     153                $this->go_to( get_permalink( $post_two->ID ) );
     154
     155                $this->assertEquals( array( $post_one ), get_boundary_post( false, '', true ) );
     156                $this->assertEquals( array( $post_four ), get_boundary_post( false, '', false ) );
     157
     158                // Test category boundary post
     159                $this->go_to( get_permalink( $post_one->ID ) );
     160
     161                $this->assertEquals( array( $post_one ), get_boundary_post( true, '', true, 'category' ) );
     162                $this->assertEquals( array( $post_three ), get_boundary_post( true, '', false, 'category' ) );
     163
     164                // Test tag boundary post
     165                $this->go_to( get_permalink( $post_two->ID ) );
     166
     167                $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
     168                $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
     169        }
    99170}
     171 No newline at end of file