Ticket #17807: 17807.7.patch
File 17807.7.patch, 19.6 KB (added by , 11 years ago) |
---|
-
wp-includes/link-template.php
1089 1089 * 1090 1090 * @since 1.5.0 1091 1091 * 1092 * @param bool $in_same_cat Optional. Whether post should be in a same category. 1093 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1092 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 1093 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1094 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1094 1095 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 1095 1096 */ 1096 function get_previous_post( $in_same_cat = false, $excluded_categories = '') {1097 return get_adjacent_post( $in_same_cat, $excluded_categories);1097 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1098 return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 1098 1099 } 1099 1100 1100 1101 /** … … 1102 1103 * 1103 1104 * @since 1.5.0 1104 1105 * 1105 * @param bool $in_same_cat Optional. Whether post should be in a same category. 1106 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1106 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 1107 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1108 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1107 1109 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 1108 1110 */ 1109 function get_next_post( $in_same_cat = false, $excluded_categories = '') {1110 return get_adjacent_post( $in_same_cat, $excluded_categories, false);1111 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1112 return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 1111 1113 } 1112 1114 1113 1115 /** … … 1117 1119 * 1118 1120 * @since 2.5.0 1119 1121 * 1120 * @param bool $in_same_ cat Optional. Whether post should be in a same category.1121 * @param array|string $excluded_ categories Optional. Array or comma-separated list of excluded categoryIDs.1122 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 1123 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1122 1124 * @param bool $previous Optional. Whether to retrieve previous post. 1125 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1123 1126 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 1124 1127 */ 1125 function get_adjacent_post( $in_same_ cat = false, $excluded_categories = '', $previous = true) {1128 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1126 1129 global $wpdb; 1127 1130 1128 if ( ! $post = get_post() )1131 if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) 1129 1132 return null; 1130 1133 1131 1134 $current_post_date = $post->post_date; 1132 1135 1133 1136 $join = ''; 1134 $posts_in_ex_ cats_sql = '';1135 if ( $in_same_ cat || ! empty( $excluded_categories ) ) {1137 $posts_in_ex_terms_sql = ''; 1138 if ( $in_same_term || ! empty( $excluded_terms ) ) { 1136 1139 $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"; 1137 1140 1138 if ( $in_same_ cat) {1139 if ( ! is_object_in_taxonomy( $post->post_type, 'category') )1141 if ( $in_same_term ) { 1142 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) 1140 1143 return ''; 1141 $ cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));1142 if ( ! $ cat_array || is_wp_error( $cat_array ) )1144 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 1145 if ( ! $term_array || is_wp_error( $term_array ) ) 1143 1146 return ''; 1144 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";1147 $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 1145 1148 } 1146 1149 1147 $posts_in_ex_ cats_sql = "AND tt.taxonomy = 'category'";1148 if ( ! empty( $excluded_ categories ) ) {1149 if ( ! is_array( $excluded_ categories ) ) {1150 // back-compat, $excluded_ categories used to beIDs separated by " and "1151 if ( strpos( $excluded_categories, ' and ' ) !== false) {1152 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );1153 $excluded_ categories = explode( ' and ', $excluded_categories );1150 $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 1151 if ( ! empty( $excluded_terms ) ) { 1152 if ( ! is_array( $excluded_terms ) ) { 1153 // back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and " 1154 if ( false !== strpos( $excluded_terms, ' and ' ) ) { 1155 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 1156 $excluded_terms = explode( ' and ', $excluded_terms ); 1154 1157 } else { 1155 $excluded_ categories = explode( ',', $excluded_categories );1158 $excluded_terms = explode( ',', $excluded_terms ); 1156 1159 } 1157 1160 } 1158 1161 1159 $excluded_ categories = array_map( 'intval', $excluded_categories );1162 $excluded_terms = array_map( 'intval', $excluded_terms ); 1160 1163 1161 if ( ! empty( $ cat_array ) ) {1162 $excluded_ categories = array_diff($excluded_categories, $cat_array);1163 $posts_in_ex_ cats_sql = '';1164 if ( ! empty( $term_array ) ) { 1165 $excluded_terms = array_diff( $excluded_terms, $term_array ); 1166 $posts_in_ex_terms_sql = ''; 1164 1167 } 1165 1168 1166 if ( ! empty($excluded_categories) ) {1167 $posts_in_ex_ cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';1169 if ( ! empty( $excluded_terms ) ) { 1170 $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 1168 1171 } 1169 1172 } 1170 1173 } … … 1173 1176 $op = $previous ? '<' : '>'; 1174 1177 $order = $previous ? 'DESC' : 'ASC'; 1175 1178 1176 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_ cat, $excluded_categories );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_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );1179 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms ); 1180 $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 ); 1178 1181 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 1179 1182 1180 1183 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; 1181 $query_key = 'adjacent_post_' . md5( $query);1182 $result = wp_cache_get( $query_key, 'counts');1184 $query_key = 'adjacent_post_' . md5( $query ); 1185 $result = wp_cache_get( $query_key, 'counts' ); 1183 1186 if ( false !== $result ) { 1184 1187 if ( $result ) 1185 1188 $result = get_post( $result ); … … 1190 1193 if ( null === $result ) 1191 1194 $result = ''; 1192 1195 1193 wp_cache_set( $query_key, $result, 'counts');1196 wp_cache_set( $query_key, $result, 'counts' ); 1194 1197 1195 1198 if ( $result ) 1196 1199 $result = get_post( $result ); … … 1206 1209 * @since 2.8.0 1207 1210 * 1208 1211 * @param string $title Optional. Link title format. 1209 * @param bool $in_same_ cat Optional. Whether link should be in a same category.1210 * @param array|string $excluded_ categories Optional. Array or comma-separated list of excluded categoryIDs.1212 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 1213 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1211 1214 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 1215 * @param string $taxonony Options, default is category. Taxonomy, if $in_same_term is true. 1212 1216 * @return string 1213 1217 */ 1214 function get_adjacent_post_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {1218 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1215 1219 if ( $previous && is_attachment() && $post = get_post() ) 1216 1220 $post = get_post( $post->post_parent ); 1217 1221 else 1218 $post = get_adjacent_post( $in_same_ cat, $excluded_categories, $previous);1222 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 1219 1223 1220 if ( empty( $post) )1224 if ( empty( $post ) ) 1221 1225 return; 1222 1226 1223 1227 $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 1224 1228 1225 1229 if ( empty( $post_title ) ) 1226 $post_title = $previous ? __( 'Previous Post') : __('Next Post');1230 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 1227 1231 1228 $date = mysql2date( get_option('date_format'), $post->post_date);1232 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 1229 1233 1230 $title = str_replace( '%title', $post_title, $title);1231 $title = str_replace( '%date', $date, $title);1234 $title = str_replace( '%title', $post_title, $title ); 1235 $title = str_replace( '%date', $date, $title ); 1232 1236 1233 1237 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 1234 1238 $link .= esc_attr( $title ); 1235 $link .= "' href='" . get_permalink( $post) . "' />\n";1239 $link .= "' href='" . get_permalink( $post ) . "' />\n"; 1236 1240 1237 1241 $adjacent = $previous ? 'previous' : 'next'; 1238 1242 return apply_filters( "{$adjacent}_post_rel_link", $link ); … … 1244 1248 * @since 2.8.0 1245 1249 * 1246 1250 * @param string $title Optional. Link title format. 1247 * @param bool $in_same_cat Optional. Whether link should be in a same category. 1248 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1251 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 1252 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1253 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1249 1254 */ 1250 function adjacent_posts_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '') {1251 echo get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories = '', true);1252 echo get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories = '', false);1255 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1256 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 1257 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 1253 1258 } 1254 1259 1255 1260 /** … … 1271 1276 * @since 2.8.0 1272 1277 * 1273 1278 * @param string $title Optional. Link title format. 1274 * @param bool $in_same_cat Optional. Whether link should be in a same category. 1275 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1279 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 1280 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1281 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1276 1282 */ 1277 function next_post_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '') {1278 echo get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories = '', false);1283 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1284 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 1279 1285 } 1280 1286 1281 1287 /** … … 1283 1289 * 1284 1290 * @since 2.8.0 1285 1291 * 1286 * @param string $title Optional. Link title format. 1287 * @param bool $in_same_cat Optional. Whether link should be in a same category. 1288 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1292 * @param string $title (optional) Link title format. 1293 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term. 1294 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs. 1295 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 1289 1296 */ 1290 function prev_post_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '') {1291 echo get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories = '', true);1297 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1298 echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 1292 1299 } 1293 1300 1294 1301 /** 1295 1302 * Retrieve boundary post. 1296 1303 * 1297 1304 * Boundary being either the first or last post by publish date within the constraints specified 1298 * by $in_same_ cat or $excluded_categories.1305 * by $in_same_term or $excluded_terms. 1299 1306 * 1300 1307 * @since 2.8.0 1301 1308 * 1302 * @param bool $in_same_cat Optional. Whether returned post should be in a same category. 1303 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1304 * @param bool $start Optional. Whether to retrieve first or last post. 1309 * @param bool $in_same_term (optional) Whether returned post should be in a same taxonomy term. 1310 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs. 1311 * @param bool $start (optional) Whether to retrieve first or last post. 1312 * @param $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 1305 1313 * @return object 1306 1314 */ 1307 function get_boundary_post( $in_same_ cat = false, $excluded_categories = '', $start = true) {1315 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 1308 1316 $post = get_post(); 1309 if ( ! $post || ! is_single() || is_attachment() )1317 if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 1310 1318 return null; 1311 1319 1312 $cat_array = array(); 1313 if( ! is_array( $excluded_categories ) ) 1314 $excluded_categories = explode( ',', $excluded_categories ); 1320 $query_args = array( 1321 'posts_per_page' => 1, 1322 'order' => $start ? 'ASC' : 'DESC', 1323 'update_post_term_cache' => false, 1324 'update_post_meta_cache' => false 1325 ); 1315 1326 1316 if ( $in_same_cat || ! empty( $excluded_categories ) ) { 1317 if ( $in_same_cat ) 1318 $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ); 1327 $term_array = array(); 1328 if ( ! is_array( $excluded_terms ) ) 1329 if ( ! empty( $excluded_terms ) ) 1330 $excluded_terms = explode( ',', $excluded_terms ); 1331 else 1332 $excluded_terms = array(); 1319 1333 1320 if ( ! empty( $excluded_categories ) ) {1321 $excluded_categories = array_map( 'intval', $excluded_categories );1322 $ excluded_categories = array_diff( $excluded_categories, $cat_array);1334 if ( $in_same_term || ! empty( $excluded_terms ) ) { 1335 if ( $in_same_term ) 1336 $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 1323 1337 1324 $inverse_cats = array(); 1325 foreach ( $excluded_categories as $excluded_category ) 1326 $inverse_cats[] = $excluded_category * -1; 1327 $excluded_categories = $inverse_cats; 1338 if ( ! empty( $excluded_terms ) ) { 1339 $excluded_terms = array_map( 'intval', $excluded_terms ); 1340 $excluded_terms = array_diff( $excluded_terms, $term_array ); 1341 1342 $inverse_terms = array(); 1343 foreach ( $excluded_terms as $excluded_term ) 1344 $inverse_terms[] = $excluded_term * -1; 1345 $excluded_terms = $inverse_terms; 1328 1346 } 1347 1348 $query_args[ 'tax_query' ] = array( array( 1349 'taxonomy' => $taxonomy, 1350 'terms' => array_merge( $term_array, $excluded_terms ) 1351 ) ); 1329 1352 } 1330 1353 1331 $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) ); 1332 1333 $order = $start ? 'ASC' : 'DESC'; 1334 1335 return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) ); 1354 return get_posts( $query_args ); 1336 1355 } 1337 1356 1338 1357 /** … … 1342 1361 * 1343 1362 * @param string $format Optional. Link anchor format. 1344 1363 * @param string $link Optional. Link permalink format. 1345 * @param bool $in_same_cat Optional. Whether link should be in a same category. 1346 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1364 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 1365 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1366 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1347 1367 */ 1348 function previous_post_link( $format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {1349 adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true);1368 function previous_post_link( $format='« %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1369 adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 1350 1370 } 1351 1371 1352 1372 /** … … 1356 1376 * 1357 1377 * @param string $format Optional. Link anchor format. 1358 1378 * @param string $link Optional. Link permalink format. 1359 * @param bool $in_same_cat Optional. Whether link should be in a same category. 1360 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 1379 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 1380 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 1381 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1361 1382 */ 1362 function next_post_link( $format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {1363 adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false);1383 function next_post_link( $format='%link »', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1384 adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 1364 1385 } 1365 1386 1366 1387 /** … … 1372 1393 * 1373 1394 * @param string $format Link anchor format. 1374 1395 * @param string $link Link permalink format. 1375 * @param bool $in_same_ cat Optional. Whether link should be in a same category.1376 * @param array|string $excluded_ categories Optional. Array or comma-separated list of excluded categoryIDs.1396 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term. 1397 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded terms IDs. 1377 1398 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 1399 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 1378 1400 */ 1379 function adjacent_post_link( $format, $link, $in_same_ cat = false, $excluded_categories = '', $previous = true) {1401 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1380 1402 if ( $previous && is_attachment() ) 1381 1403 $post = get_post( get_post()->post_parent ); 1382 1404 else 1383 $post = get_adjacent_post( $in_same_ cat, $excluded_categories, $previous);1405 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 1384 1406 1385 1407 if ( ! $post ) { 1386 1408 $output = '';