WordPress.org

Make WordPress Core

Ticket #43521: 43521.diff

File 43521.diff, 3.4 KB (added by soulseekah, 3 years ago)

Patch + tests

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

    diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php
    index 9a27404..365b14b 100644
    function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo 
    16821682        $where    = '';
    16831683        $adjacent = $previous ? 'previous' : 'next';
    16841684
     1685        /**
     1686         * Filters the IDs of terms excluded from adjacent post queries.
     1687         *
     1688         * The dynamic portion of the hook name, `$adjacent`, refers to the type
     1689         * of adjacency, 'next' or 'previous'.
     1690         *
     1691         * @since 4.4.0
     1692         *
     1693         * @param string $excluded_terms Array of excluded term IDs.
     1694         */
     1695        $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
     1696
    16851697        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    16861698                if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
    16871699                        // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
    function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo 
    17151727                        $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
    17161728                }
    17171729
    1718                 /**
    1719                  * Filters the IDs of terms excluded from adjacent post queries.
    1720                  *
    1721                  * The dynamic portion of the hook name, `$adjacent`, refers to the type
    1722                  * of adjacency, 'next' or 'previous'.
    1723                  *
    1724                  * @since 4.4.0
    1725                  *
    1726                  * @param string $excluded_terms Array of excluded term IDs.
    1727                  */
    1728                 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
    1729 
    17301730                if ( ! empty( $excluded_terms ) ) {
    17311731                        $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
    17321732                }
  • tests/phpunit/tests/link/getAdjacentPost.php

    diff --git tests/phpunit/tests/link/getAdjacentPost.php tests/phpunit/tests/link/getAdjacentPost.php
    index 0c38e63..07bdc4d 100644
    class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { 
    276276                $this->assertSame( $p3, $found->ID );
    277277        }
    278278
     279        /**
     280         * @ticket 43521
     281         */
     282        public function test_excluded_terms_filter_empty() {
     283                register_taxonomy( 'wptests_tax', 'post' );
     284
     285                $terms = self::factory()->term->create_many(
     286                        2, array(
     287                                'taxonomy' => 'wptests_tax',
     288                        )
     289                );
     290
     291                $p1 = self::factory()->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
     292                $p2 = self::factory()->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
     293                $p3 = self::factory()->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
     294
     295                wp_set_post_terms( $p1, array( $terms[0], $terms[1] ), 'wptests_tax' );
     296                wp_set_post_terms( $p2, array( $terms[1] ), 'wptests_tax' );
     297                wp_set_post_terms( $p3, array( $terms[0] ), 'wptests_tax' );
     298
     299                $this->go_to( get_permalink( $p1 ) );
     300
     301                $this->exclude_term = $terms[1];
     302                add_filter( 'get_previous_post_excluded_terms', array( $this, 'filter_excluded_terms' ) );
     303
     304                $found = get_adjacent_post( false, array(), true, 'wptests_tax' );
     305
     306                remove_filter( 'get_previous_post_excluded_terms', array( $this, 'filter_excluded_terms' ) );
     307                unset( $this->exclude_term );
     308
     309                $this->assertSame( $p3, $found->ID );
     310        }
     311
    279312        public function filter_excluded_terms( $excluded_terms ) {
    280313                $excluded_terms[] = $this->exclude_term;
    281314                return $excluded_terms;