Ticket #22112: 22112.2.patch
| File 22112.2.patch, 6.1 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/link-template.php
1136 1136 $current_post_date = $post->post_date; 1137 1137 1138 1138 $join = ''; 1139 $ posts_in_ex_terms_sql= '';1139 $same_terms_or_exclude_where = ''; 1140 1140 if ( $in_same_term || ! empty( $excluded_terms ) ) { 1141 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"; 1142 1142 1143 1143 if ( $in_same_term ) { 1144 1144 if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) 1145 1145 return ''; 1146 $ term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );1147 if ( ! $ term_array || is_wp_error( $term_array ) )1146 $same_term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 1147 if ( ! $same_term_array || is_wp_error( $same_term_array ) ) 1148 1148 return ''; 1149 $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 1149 } else { 1150 $same_term_array = array(); 1150 1151 } 1151 1152 1152 $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );1153 1153 if ( ! empty( $excluded_terms ) ) { 1154 1154 if ( ! is_array( $excluded_terms ) ) { 1155 1155 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " … … 1162 1162 } 1163 1163 1164 1164 $excluded_terms = array_map( 'intval', $excluded_terms ); 1165 } else { 1166 $excluded_terms = array(); 1167 } 1165 1168 1166 if ( ! empty( $term_array ) ) { 1167 $excluded_terms = array_diff( $excluded_terms, $term_array ); 1168 $posts_in_ex_terms_sql = ''; 1169 if ( ! empty( $same_term_array ) || ! empty ( $excluded_terms ) ) { 1170 $same_term_array = array_diff( $same_term_array, $excluded_terms ); 1171 if ( !empty ( $same_term_array ) ) { 1172 // if there are still same terms, we can assume that all $excluded_terms 1173 // have been removed from them. therefore, since AND is exclusive, 1174 // we don't need to worry about the remaining excluded terms and 1175 // can do IN 1176 $same_terms_or_exclude_where = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $same_term_array ) ) . ")", $taxonomy ); 1177 } else if ( ! empty ( $excluded_terms ) ) { 1178 // if there are no same terms, but there are excluded terms, 1179 // we need to do a NOT IN 1180 $same_terms_or_exclude_where = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 1169 1181 } 1170 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 );1173 }1174 1182 } 1175 1183 } 1176 1184 … … 1179 1187 $order = $previous ? 'DESC' : 'ASC'; 1180 1188 1181 1189 $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 );1190 $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' $same_terms_or_exclude_where", $current_post_date, $post->post_type), $in_same_term, $excluded_terms ); 1183 1191 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 1184 1192 1185 1193 $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; -
tests/phpunit/tests/link.php
167 167 $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) ); 168 168 $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) ); 169 169 } 170 171 /** 172 * @ticket 22112 173 */ 174 function test_get_adjacent_post_exclude_self_term() { 175 // Need some sample posts to test adjacency when looking at the 176 // next post 177 $post_one = $this->factory->post->create_and_get( array( 178 'post_title' => 'First', 179 'post_date' => '2012-01-01 12:00:00' 180 ) ); 181 182 $post_two = $this->factory->post->create_and_get( array( 183 'post_title' => 'Second', 184 'post_date' => '2012-02-01 12:00:00' 185 ) ); 186 187 $post_three = $this->factory->post->create_and_get( array( 188 'post_title' => 'Third', 189 'post_date' => '2012-03-01 12:00:00' 190 ) ); 191 192 // Assign some terms 193 wp_set_object_terms( $post_one->ID, 'good', 'category', false ); 194 wp_set_object_terms( $post_one->ID, 'bad', 'category', false ); 195 196 wp_set_object_terms( $post_two->ID, 'bad', 'category', false ); 197 198 wp_set_object_terms( $post_three->ID, 'good', 'category', false ); 199 200 $exclude_term = get_term_by('name', 'bad', 'category'); 201 202 // Test that if the next post has the same term *and* is in the 203 // has a term in the excluded_terms array, it is skipped 204 $this->go_to( get_permalink( $post_one->ID ) ); 205 $this->assertEquals( $post_three, get_adjacent_post( true, "$exclude_term->term_id", false ) ); 206 207 208 // Need some sample posts to test adjacency when looking at the 209 // previous post 210 $post_four = $this->factory->post->create_and_get( array( 211 'post_title' => 'Fourth', 212 'post_date' => '2012-04-01 12:00:00' 213 ) ); 214 215 $post_five = $this->factory->post->create_and_get( array( 216 'post_title' => 'Fifth', 217 'post_date' => '2012-05-01 12:00:00' 218 ) ); 219 220 wp_set_object_terms( $post_four->ID, 'bad', 'category', false ); 221 222 wp_set_object_terms( $post_five->ID, 'good', 'category', false ); 223 wp_set_object_terms( $post_five->ID, 'bad', 'category', false ); 224 225 // Test that if the previous post has the same term *and* is in the 226 // has a term in the excluded_terms array, it is skipped 227 $this->go_to( get_permalink( $post_five->ID ) ); 228 $this->assertEquals( $post_three, get_adjacent_post( true, "$exclude_term->term_id", true ) ); 229 } 170 230 } 231 No newline at end of file