Make WordPress Core

Ticket #22112: 22112.patch

File 22112.patch, 7.1 KB (added by jessepollak, 12 years ago)

22112.patch

  • src/wp-includes/link-template.php

     
    11361136        $current_post_date = $post->post_date;
    11371137
    11381138        $join = '';
    1139         $posts_in_ex_terms_sql = '';
     1139        $same_terms_or_exclude_where = '';
    11401140        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    11411141                $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";
    11421142
    11431143                if ( $in_same_term ) {
    11441144                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    11451145                                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 ) )
    11481148                                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();
    11501151                }
    11511152
    1152                 $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    11531153                if ( ! empty( $excluded_terms ) ) {
    11541154                        if ( ! is_array( $excluded_terms ) ) {
    11551155                                // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     
    11621162                        }
    11631163
    11641164                        $excluded_terms = array_map( 'intval', $excluded_terms );
     1165                } else {
     1166                        $excluded_terms = array();
     1167                }
    11651168
    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 );
    11691181                        }
    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                         }
    11741182                }
    11751183        }
    11761184
     
    11791187        $order = $previous ? 'DESC' : 'ASC';
    11801188
    11811189        $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 );
    11831191        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    11841192
    11851193        $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
     
    13261334                'update_post_meta_cache' => false
    13271335        );
    13281336
    1329         $term_array = array();
     1337        $same_term_array = array();
    13301338
    13311339        if ( ! is_array( $excluded_terms ) ) {
    13321340                if ( ! empty( $excluded_terms ) )
     
    13371345
    13381346        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    13391347                if ( $in_same_term )
    1340                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1348                        $same_term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    13411349
    13421350                if ( ! empty( $excluded_terms ) ) {
    13431351                        $excluded_terms = array_map( 'intval', $excluded_terms );
    1344                         $excluded_terms = array_diff( $excluded_terms, $term_array );
     1352                        $excluded_terms = array_diff( $excluded_terms, $same_term_array );
    13451353
    13461354                        $inverse_terms = array();
    13471355                        foreach ( $excluded_terms as $excluded_term )
     
    13511359
    13521360                $query_args[ 'tax_query' ] = array( array(
    13531361                        'taxonomy' => $taxonomy,
    1354                         'terms' => array_merge( $term_array, $excluded_terms )
     1362                        'terms' => array_merge( $same_term_array, $excluded_terms )
    13551363                ) );
    13561364        }
    13571365
  • tests/phpunit/tests/link.php

     
    167167                $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
    168168                $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
    169169        }
     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        }
    170230}
     231 No newline at end of file