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