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