Ticket #17807: 17807.10.patch
File 17807.10.patch, 28.1 KB (added by , 10 years ago) |
---|
-
src/wp-includes/link-template.php
1091 1091 * 1092 1092 * @since 1.5.0 1093 1093 * 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. 1097 1098 */ 1098 function get_previous_post( $in_same_cat = false, $excluded_categories = '') {1099 return get_adjacent_post( $in_same_cat, $excluded_categories);1099 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1100 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 1100 1101 } 1101 1102 1102 1103 /** … … 1104 1105 * 1105 1106 * @since 1.5.0 1106 1107 * 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. 1110 1112 */ 1111 function get_next_post( $in_same_cat = false, $excluded_categories = '') {1112 return get_adjacent_post( $in_same_cat, $excluded_categories, false);1113 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1114 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 1113 1115 } 1114 1116 1115 1117 /** … … 1119 1121 * 1120 1122 * @since 2.5.0 1121 1123 * 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. 1126 1129 */ 1127 function get_adjacent_post( $in_same_ cat = false, $excluded_categories = '', $previous = true) {1130 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1128 1131 global $wpdb; 1129 1132 1130 if ( ! $post = get_post() )1133 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) 1131 1134 return null; 1132 1135 1133 1136 $current_post_date = $post->post_date; 1134 1137 1135 1138 $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 ) ) { 1138 1141 $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"; 1139 1142 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 ) ) 1142 1145 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 ) ) 1145 1148 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 ); 1147 1150 } 1148 1151 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 beIDs 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 ); 1156 1159 } else { 1157 $excluded_ categories = explode( ',', $excluded_categories );1160 $excluded_terms = explode( ',', $excluded_terms ); 1158 1161 } 1159 1162 } 1160 1163 1161 $excluded_ categories = array_map( 'intval', $excluded_categories );1164 $excluded_terms = array_map( 'intval', $excluded_terms ); 1162 1165 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 = ''; 1166 1169 } 1167 1170 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 ); 1170 1173 } 1171 1174 } 1172 1175 } … … 1175 1178 $op = $previous ? '<' : '>'; 1176 1179 $order = $previous ? 'DESC' : 'ASC'; 1177 1180 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 ); 1180 1183 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 1181 1184 1182 1185 $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' ); 1185 1188 if ( false !== $result ) { 1186 1189 if ( $result ) 1187 1190 $result = get_post( $result ); … … 1192 1195 if ( null === $result ) 1193 1196 $result = ''; 1194 1197 1195 wp_cache_set( $query_key, $result, 'counts');1198 wp_cache_set( $query_key, $result, 'counts' ); 1196 1199 1197 1200 if ( $result ) 1198 1201 $result = get_post( $result ); … … 1207 1210 * 1208 1211 * @since 2.8.0 1209 1212 * 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'. 1214 1218 * @return string 1215 1219 */ 1216 function get_adjacent_post_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {1220 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1217 1221 if ( $previous && is_attachment() && $post = get_post() ) 1218 1222 $post = get_post( $post->post_parent ); 1219 1223 else 1220 $post = get_adjacent_post( $in_same_ cat, $excluded_categories, $previous);1224 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 1221 1225 1222 if ( empty( $post) )1226 if ( empty( $post ) ) 1223 1227 return; 1224 1228 1225 1229 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 1226 1230 1227 1231 if ( empty( $post_title ) ) 1228 $post_title = $previous ? __( 'Previous Post') : __('Next Post');1232 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 1229 1233 1230 $date = mysql2date( get_option('date_format'), $post->post_date);1234 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 1231 1235 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 ); 1234 1238 1235 1239 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 1236 1240 $link .= esc_attr( $title ); 1237 $link .= "' href='" . get_permalink( $post) . "' />\n";1241 $link .= "' href='" . get_permalink( $post ) . "' />\n"; 1238 1242 1239 1243 $adjacent = $previous ? 'previous' : 'next'; 1240 1244 return apply_filters( "{$adjacent}_post_rel_link", $link ); … … 1245 1249 * 1246 1250 * @since 2.8.0 1247 1251 * 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'. 1251 1256 */ 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);1257 function 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 ); 1255 1260 } 1256 1261 1257 1262 /** … … 1272 1277 * 1273 1278 * @since 2.8.0 1274 1279 * 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'. 1278 1284 */ 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);1285 function 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 ); 1281 1287 } 1282 1288 1283 1289 /** … … 1285 1291 * 1286 1292 * @since 2.8.0 1287 1293 * 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'. 1291 1298 */ 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);1299 function 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 ); 1294 1301 } 1295 1302 1296 1303 /** 1297 1304 * Retrieve boundary post. 1298 1305 * 1299 1306 * 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. 1301 1308 * 1302 1309 * @since 2.8.0 1303 1310 * 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'. 1307 1315 * @return object 1308 1316 */ 1309 function get_boundary_post( $in_same_ cat = false, $excluded_categories = '', $start = true) {1317 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 1310 1318 $post = get_post(); 1311 if ( ! $post || ! is_single() || is_attachment() )1319 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 1312 1320 return null; 1313 1321 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 ); 1317 1328 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(); 1321 1330 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 } 1325 1337 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; 1330 1350 } 1351 1352 $query_args[ 'tax_query' ] = array( array( 1353 'taxonomy' => $taxonomy, 1354 'terms' => array_merge( $term_array, $excluded_terms ) 1355 ) ); 1331 1356 } 1332 1357 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 ); 1338 1359 } 1339 1360 1340 1361 /* … … 1342 1363 * 1343 1364 * @since 3.7.0 1344 1365 * 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'. 1349 1371 * @return string 1350 1372 */ 1351 function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_ categories = '' ) {1352 return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_ categories, true);1373 function get_previous_post_link( $format = '« %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 ); 1353 1375 } 1354 1376 1355 1377 /** … … 1358 1380 * @since 1.5.0 1359 1381 * @uses get_previous_post_link() 1360 1382 * 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'. 1365 1388 */ 1366 function previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_ categories = '' ) {1367 echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_ categories);1389 function previous_post_link( $format = '« %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 ); 1368 1391 } 1369 1392 1370 1393 /** … … 1373 1396 * @since 3.7.0 1374 1397 * @uses get_next_post_link() 1375 1398 * 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'. 1380 1404 * @return string 1381 1405 */ 1382 function get_next_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_ categories = '' ) {1383 return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_ categories, false);1406 function get_next_post_link( $format = '« %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 ); 1384 1408 } 1385 1409 1386 1410 /** … … 1388 1412 * 1389 1413 * @since 1.5.0 1390 1414 * 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'. 1395 1420 */ 1396 function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_ categories = '' ) {1397 echo get_next_post_link( $format, $link, $in_same_cat, $excluded_ categories);1421 function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) { 1422 echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy ); 1398 1423 } 1399 1424 1400 1425 /** … … 1404 1429 * 1405 1430 * @since 3.7.0 1406 1431 * 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'. 1412 1438 * @return string 1413 1439 */ 1414 function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_ categories = '', $previous = true) {1440 function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1415 1441 if ( $previous && is_attachment() ) 1416 1442 $post = get_post( get_post()->post_parent ); 1417 1443 else 1418 $post = get_adjacent_post( $in_same_cat, $excluded_ categories, $previous);1444 $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy ); 1419 1445 1420 1446 if ( ! $post ) { 1421 1447 $output = ''; … … 1451 1477 * @since 2.5.0 1452 1478 * @uses get_adjacent_post_link() 1453 1479 * 1454 * @param string $formatLink anchor format.1455 * @param string $linkLink permalink format.1456 * @param bool $in_same_catOptional. 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. 1459 1485 * @return string 1460 1486 */ 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 );1487 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true ) { 1488 echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous ); 1463 1489 } 1464 1490 1465 1491 /** -
tests/phpunit/tests/link.php
96 96 $wp_rewrite->flush_rules(); 97 97 } 98 98 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 } 99 170 } 171 No newline at end of file