Make WordPress Core

Ticket #22112: 22112.diff

File 22112.diff, 6.6 KB (added by kovshenin, 11 years ago)
  • src/wp-includes/link-template.php

     
    14771477        $current_post_date = $post->post_date;
    14781478
    14791479        $join = '';
    1480         $posts_in_ex_terms_sql = '';
     1480        $where = '';
     1481
    14811482        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    14821483                $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";
     1484                $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    14831485
     1486                if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
     1487                        // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     1488                        if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1489                                _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1490                                $excluded_terms = explode( ' and ', $excluded_terms );
     1491                        } else {
     1492                                $excluded_terms = explode( ',', $excluded_terms );
     1493                        }
     1494
     1495                        $excluded_terms = array_map( 'intval', $excluded_terms );
     1496                }
     1497
    14841498                if ( $in_same_term ) {
    14851499                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    14861500                                return '';
    14871501                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1502
     1503                        // Remove any exclusions from the term array to include.
     1504                        $term_array = array_diff( $term_array, (array) $excluded_terms );
     1505                        $term_array = array_map( 'intval', $term_array );
     1506
    14881507                        if ( ! $term_array || is_wp_error( $term_array ) )
    14891508                                return '';
    1490                         $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
     1509
     1510                        $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
    14911511                }
    14921512
    1493                 $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    14941513                if ( ! empty( $excluded_terms ) ) {
    1495                         if ( ! is_array( $excluded_terms ) ) {
    1496                                 // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
    1497                                 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
    1498                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
    1499                                         $excluded_terms = explode( ' and ', $excluded_terms );
    1500                                 } else {
    1501                                         $excluded_terms = explode( ',', $excluded_terms );
    1502                                 }
    1503                         }
    1504 
    1505                         $excluded_terms = array_map( 'intval', $excluded_terms );
    1506 
    1507                         if ( ! empty( $term_array ) ) {
    1508                                 $excluded_terms = array_diff( $excluded_terms, $term_array );
    1509                                 $posts_in_ex_terms_sql = '';
    1510                         }
    1511 
    1512                         if ( ! empty( $excluded_terms ) ) {
    1513                                 $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
    1514                         }
     1514                        $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )';
    15151515                }
    15161516        }
    15171517
     
    15451545         * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
    15461546         * @param array  $excluded_terms Array of excluded term IDs.
    15471547         */
    1548         $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 );
     1548        $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' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
    15491549
    15501550        /**
    15511551         * Filter the ORDER BY clause in the SQL for an adjacent post query.
  • tests/phpunit/tests/link.php

     
    188188                $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
    189189                $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
    190190        }
     191
     192        /**
     193        * @ticket 22112
     194        */
     195        function test_get_adjacent_post_exclude_self_term() {
     196                $include = $this->factory->category->create();
     197                $exclude = $this->factory->category->create();
     198
     199                $one = $this->factory->post->create_and_get( array(
     200                        'post_date' => '2012-01-01 12:00:00',
     201                        'post_category' => array( $include, $exclude ),
     202                ) );
     203
     204                $two = $this->factory->post->create_and_get( array(
     205                        'post_date' => '2012-01-02 12:00:00',
     206                        'post_category' => array(),
     207                ) );
     208
     209                $three = $this->factory->post->create_and_get( array(
     210                        'post_date' => '2012-01-03 12:00:00',
     211                        'post_category' => array( $include, $exclude ),
     212                ) );
     213
     214                $four = $this->factory->post->create_and_get( array(
     215                        'post_date' => '2012-01-04 12:00:00',
     216                        'post_category' => array( $include ),
     217                ) );
     218
     219                $five = $this->factory->post->create_and_get( array(
     220                        'post_date' => '2012-01-05 12:00:00',
     221                        'post_category' => array( $include, $exclude ),
     222                ) );
     223
     224                // First post
     225                $this->go_to( get_permalink( $one ) );
     226                $this->assertEquals( $two, get_adjacent_post( false, array(), false ) );
     227                $this->assertEquals( $three, get_adjacent_post( true, array(), false ) );
     228                $this->assertEquals( $two, get_adjacent_post( false, array( $exclude ), false ) );
     229                $this->assertEquals( $four, get_adjacent_post( true, array( $exclude ), false ) );
     230                $this->assertEmpty( get_adjacent_post( false, array(), true ) );
     231
     232                // Fourth post
     233                $this->go_to( get_permalink( $four ) );
     234                $this->assertEquals( $five, get_adjacent_post( false, array(), false ) );
     235                $this->assertEquals( $five, get_adjacent_post( true, array(), false ) );
     236                $this->assertEmpty( get_adjacent_post( false, array( $exclude ), false ) );
     237                $this->assertEmpty( get_adjacent_post( true, array( $exclude ), false ) );
     238
     239                $this->assertEquals( $three, get_adjacent_post( false, array(), true ) );
     240                $this->assertEquals( $three, get_adjacent_post( true, array(), true ) );
     241                $this->assertEquals( $two, get_adjacent_post( false, array( $exclude ), true ) );
     242                $this->assertEmpty( get_adjacent_post( true, array( $exclude ), true ) );
     243
     244                // Last post
     245                $this->go_to( get_permalink( $five ) );
     246                $this->assertEquals( $four, get_adjacent_post( false, array(), true ) );
     247                $this->assertEquals( $four, get_adjacent_post( true, array(), true ) );
     248                $this->assertEquals( $four, get_adjacent_post( false, array( $exclude ), true ) );
     249                $this->assertEquals( $four, get_adjacent_post( true, array( $exclude ), true ) );
     250                $this->assertEmpty( get_adjacent_post( false, array(), false ) );
     251        }
    191252}
     253 No newline at end of file