Make WordPress Core

Ticket #35082: adjacent-post-in-term.diff

File adjacent-post-in-term.diff, 4.6 KB (added by WazzaJB, 10 years ago)

Patch with unit test

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

    diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php
    index 8476314..1ce5c08 100644
    function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo 
    15831583                }
    15841584
    15851585                if ( $in_same_term ) {
     1586
    15861587                        $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";
    15871588                        $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    15881589
    15891590                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    15901591                                return '';
    1591                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1592
     1593                        // Get all fields rather than just IDs, so we have access to parent ID
     1594                        $term_objects = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'all' ) );
     1595
     1596                        // Seperated WP error check from empty check as needs to run earlier
     1597                        if( is_wp_error( $term_objects ) )
     1598                                return '';
     1599
     1600                        // Loop over our terms and store the IDs
     1601                        $term_ids = array();
     1602                        foreach( $term_objects as $term )
     1603                                $term_ids[] = (int) $term->term_id;
     1604
     1605                        // Loop over the terms and if a terms parent exists, remove the parent
     1606                        foreach( $term_objects as $term ) {
     1607                                if( $term_parent = in_array($term->parent, $term_ids) )
     1608                                        unset($term_ids[$term_parent]);
     1609                        }
    15921610
    15931611                        // Remove any exclusions from the term array to include.
    1594                         $term_array = array_diff( $term_array, (array) $excluded_terms );
    1595                         $term_array = array_map( 'intval', $term_array );
     1612                        $term_ids = array_diff( $term_ids, (array) $excluded_terms );
    15961613
    1597                         if ( ! $term_array || is_wp_error( $term_array ) )
     1614                        // If the term objects array is now empty
     1615                        if ( ! $term_objects )
    15981616                                return '';
    15991617
    1600                         $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
     1618                        $where .= " AND tt.term_id IN (" . implode( ',', $term_ids ) . ")";
    16011619                }
    16021620
    16031621                if ( ! empty( $excluded_terms ) ) {
  • tests/phpunit/tests/link.php

    diff --git tests/phpunit/tests/link.php tests/phpunit/tests/link.php
    index ebec6c1..ea3e12c 100644
    class Tests_Link extends WP_UnitTestCase { 
    305305        }
    306306
    307307        /**
     308         * @ticket 35082
     309         */
     310        public function test_get_adjacent_post_in_same_term() {
     311                // Need some sample posts to test adjacency
     312                $post_one = self::factory()->post->create_and_get( array(
     313                        'post_title' => 'First',
     314                        'post_date' => '2012-01-01 12:00:00'
     315                ) );
     316
     317                $post_two = self::factory()->post->create_and_get( array(
     318                        'post_title' => 'Second',
     319                        'post_date' => '2012-02-01 12:00:00'
     320                ) );
     321
     322                $post_three = self::factory()->post->create_and_get( array(
     323                        'post_title' => 'Third',
     324                        'post_date' => '2012-03-01 12:00:00'
     325                ) );
     326
     327                $post_four = self::factory()->post->create_and_get( array(
     328                        'post_title' => 'Fourth',
     329                        'post_date' => '2012-04-01 12:00:00'
     330                ) );
     331
     332                // Create terms
     333                $term_one = wp_insert_term('parent', 'category');
     334                $term_two = wp_insert_term('child', 'category', array('parent' => $term_one['term_id']));
     335
     336                // Assign some terms
     337                wp_set_object_terms( $post_one->ID, array('parent', 'child'), 'category', false );
     338                wp_set_object_terms( $post_two->ID, 'parent', 'category', false );
     339
     340                wp_set_object_terms( $post_three->ID, array('parent', 'child'), 'category', false );
     341                wp_set_object_terms( $post_four->ID, array('child'), 'category', false );
     342
     343                // Post one should have no prior posts and next post should be three
     344                $this->go_to( get_permalink( $post_one->ID ) );
     345                $this->assertEquals( '', get_adjacent_post( true, '', true ) );
     346                $this->assertEquals( $post_three, get_adjacent_post( true, '', false ) );
     347
     348                // Post three previous should be one and next should be four
     349                $this->go_to( get_permalink( $post_three->ID ) );
     350                $this->assertEquals( $post_one, get_adjacent_post( true, '', true ) );
     351                $this->assertEquals( $post_four, get_adjacent_post( true, '', false ) );
     352
     353                // Post two should have next post of three and previous of one
     354                $this->go_to( get_permalink( $post_two->ID ) );
     355                $this->assertEquals( $post_one, get_adjacent_post( true, '', true ) );
     356                $this->assertEquals( $post_three, get_adjacent_post( true, '', false ) );
     357
     358                // Post four should have next post of one and previous of three
     359                $this->go_to( get_permalink( $post_four->ID ) );
     360                $this->assertEquals( $post_three, get_adjacent_post( true, '', true ) );
     361                $this->assertEquals( '', get_adjacent_post( true, '', false ) );
     362        }
     363
     364        /**
    308365         * @ticket 30910
    309366         */
    310367        public function test_get_permalink_should_not_reveal_post_name_for_post_with_post_status_future() {