Ticket #17807: 17807.6.patch
File 17807.6.patch, 19.3 KB (added by , 12 years ago) |
---|
-
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. 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. 1096 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. 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. 1109 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 categoryIDs.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. 1124 1126 * @param bool $previous Optional. Whether to retrieve previous post. 1127 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 1125 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 $post, $wpdb; 1129 1132 1130 if ( empty( $post ) )1133 if ( empty( $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 $ 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 ); 1143 1146 } 1144 1147 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 ); 1152 1155 } else { 1153 $excluded_ categories = explode( ',', $excluded_categories );1156 $excluded_terms = explode( ',', $excluded_terms ); 1154 1157 } 1155 1158 } 1156 1159 1157 $excluded_ categories = array_map( 'intval', $excluded_categories );1160 $excluded_terms = array_map( 'intval', $excluded_terms ); 1158 1161 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 = ''; 1162 1165 } 1163 1166 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 ); 1167 1169 } 1168 1170 } 1169 1171 … … 1171 1173 $op = $previous ? '<' : '>'; 1172 1174 $order = $previous ? 'DESC' : 'ASC'; 1173 1175 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 ); 1176 1178 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 1177 1179 1178 1180 $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' ); 1181 1183 if ( false !== $result ) 1182 1184 return $result; 1183 1185 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" ); 1185 1187 if ( null === $result ) 1186 1188 $result = ''; 1187 1189 1188 wp_cache_set( $query_key, $result, 'counts');1190 wp_cache_set( $query_key, $result, 'counts' ); 1189 1191 return $result; 1190 1192 } 1191 1193 … … 1197 1199 * @since 2.8.0 1198 1200 * 1199 1201 * @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 categoryIDs.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. 1202 1204 * @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. 1203 1206 * @return string 1204 1207 */ 1205 function get_adjacent_post_rel_link( $title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {1208 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1206 1209 if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) 1207 1210 $post = & get_post($GLOBALS['post']->post_parent); 1208 1211 else 1209 $post = get_adjacent_post( $in_same_cat,$excluded_categories,$previous);1212 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 1210 1213 1211 1214 if ( empty($post) ) 1212 1215 return; 1213 1216 1214 if ( empty( $post->post_title) )1217 if ( empty( $post->post_title ) ) 1215 1218 $post->post_title = $previous ? __('Previous Post') : __('Next Post'); 1216 1219 1217 $date = mysql2date( get_option('date_format'), $post->post_date);1220 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 1218 1221 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 ); 1222 1225 1223 1226 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 1224 1227 $link .= esc_attr( $title ); 1225 $link .= "' href='" . get_permalink( $post) . "' />\n";1228 $link .= "' href='" . get_permalink( $post ) . "' />\n"; 1226 1229 1227 1230 $adjacent = $previous ? 'previous' : 'next'; 1228 1231 return apply_filters( "{$adjacent}_post_rel_link", $link ); … … 1234 1237 * @since 2.8.0 1235 1238 * 1236 1239 * @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. 1239 1243 */ 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);1244 function 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 ); 1243 1247 } 1244 1248 1245 1249 /** … … 1261 1265 * @since 2.8.0 1262 1266 * 1263 1267 * @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. 1266 1271 */ 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);1272 function 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 ); 1269 1274 } 1270 1275 1271 1276 /** … … 1274 1279 * @since 2.8.0 1275 1280 * 1276 1281 * @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. 1279 1285 */ 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);1286 function 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 ); 1282 1288 } 1283 1289 1284 1290 /** 1285 1291 * Retrieve boundary post. 1286 1292 * 1287 1293 * 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. 1289 1295 * 1290 1296 * @since 2.8.0 1291 1297 * 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 categoryIDs.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. 1294 1300 * @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. 1295 1302 * @return object 1296 1303 */ 1297 function get_boundary_post( $in_same_ cat = false, $excluded_categories = '', $start = true) {1304 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 1298 1305 global $post; 1299 1306 1300 if ( empty( $post) || ! is_single() || is_attachment() )1307 if ( empty( $post ) || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 1301 1308 return null; 1302 1309 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 ); 1306 1316 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(); 1310 1318 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 } 1314 1325 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; 1319 1339 } 1340 1341 $query_args[ 'tax_query' ] = array( array( 1342 'taxonomy' => $taxonomy, 1343 'terms' => array_merge( $term_array, $excluded_terms ) 1344 ) ); 1320 1345 } 1321 1346 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 ); 1327 1348 } 1328 1349 1329 1350 /** … … 1333 1354 * 1334 1355 * @param string $format Optional. Link anchor format. 1335 1356 * @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. 1338 1360 */ 1339 function previous_post_link( $format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {1340 adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true);1361 function previous_post_link( $format='« %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1362 adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 1341 1363 } 1342 1364 1343 1365 /** … … 1347 1369 * 1348 1370 * @param string $format Optional. Link anchor format. 1349 1371 * @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. 1352 1375 */ 1353 function next_post_link( $format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {1354 adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false);1376 function next_post_link( $format='%link »', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 1377 adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 1355 1378 } 1356 1379 1357 1380 /** … … 1363 1386 * 1364 1387 * @param string $format Link anchor format. 1365 1388 * @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 categoryIDs.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. 1368 1391 * @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. 1369 1393 */ 1370 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {1394 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1371 1395 if ( $previous && is_attachment() ) 1372 1396 $post = & get_post($GLOBALS['post']->post_parent); 1373 1397 else 1374 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous);1398 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 1375 1399 1376 1400 if ( !$post ) 1377 1401 return;