From fff6412f9142ddc80e2e3d4de6a69f19ab4536ea Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 23 Dec 2015 19:56:32 +0000 Subject: [PATCH] Move excluded_terms filter in `get_adjacent_post()`. The filter was added in 4.4 [34528] #9571, but in a place where it could not affect the adjacent post query. Fixes #35211. git-svn-id: https://develop.svn.wordpress.org/trunk@36078 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 26 +++++++------- tests/phpunit/tests/link/getAdjacentPost.php | 38 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index cfe6bc9e0d..5d88bd60c3 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1568,6 +1568,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $join = ''; $where = ''; + $adjacent = $previous ? 'previous' : 'next'; if ( $in_same_term || ! empty( $excluded_terms ) ) { if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { @@ -1600,6 +1601,18 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")"; } + /** + * Filter the IDs of terms excluded from adjacent post queries. + * + * The dynamic portion of the hook name, `$adjacent`, refers to the type + * of adjacency, 'next' or 'previous'. + * + * @since 4.4.0 + * + * @param string $excluded_terms Array of excluded term IDs. + */ + $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); + if ( ! empty( $excluded_terms ) ) { $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( $excluded_terms, ',' ) . ') )'; } @@ -1635,22 +1648,9 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where .= " AND p.post_status = 'publish'"; } - $adjacent = $previous ? 'previous' : 'next'; $op = $previous ? '<' : '>'; $order = $previous ? 'DESC' : 'ASC'; - /** - * Filter the excluded term ids - * - * The dynamic portion of the hook name, `$adjacent`, refers to the type - * of adjacency, 'next' or 'previous'. - * - * @since 4.4.0 - * - * @param string $excluded_terms Array of excluded term IDs. - */ - $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); - /** * Filter the JOIN clause in the SQL for an adjacent post query. * diff --git a/tests/phpunit/tests/link/getAdjacentPost.php b/tests/phpunit/tests/link/getAdjacentPost.php index b8155aef91..80a36b1888 100644 --- a/tests/phpunit/tests/link/getAdjacentPost.php +++ b/tests/phpunit/tests/link/getAdjacentPost.php @@ -4,6 +4,8 @@ * @covers ::get_adjacent_link */ class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { + protected $exclude_term; + /** * @ticket 17807 */ @@ -210,4 +212,40 @@ class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { // Should skip $p2, which belongs to $t. $this->assertEquals( $p3, $found->ID ); } + + /** + * @ticket 35211 + */ + public function test_excluded_terms_filter() { + register_taxonomy( 'wptests_tax', 'post' ); + + $terms = self::factory()->term->create_many( 2, array( + 'taxonomy' => 'wptests_tax', + ) ); + + $p1 = self::factory()->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) ); + $p2 = self::factory()->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) ); + $p3 = self::factory()->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) ); + + wp_set_post_terms( $p1, array( $terms[0], $terms[1] ), 'wptests_tax' ); + wp_set_post_terms( $p2, array( $terms[1] ), 'wptests_tax' ); + wp_set_post_terms( $p3, array( $terms[0] ), 'wptests_tax' ); + + $this->go_to( get_permalink( $p1 ) ); + + $this->exclude_term = $terms[1]; + add_filter( 'get_previous_post_excluded_terms', array( $this, 'filter_excluded_terms' ) ); + + $found = get_adjacent_post( true, array(), true, 'wptests_tax' ); + + remove_filter( 'get_previous_post_excluded_terms', array( $this, 'filter_excluded_terms' ) ); + unset( $this->exclude_term ); + + $this->assertSame( $p3, $found->ID ); + } + + public function filter_excluded_terms( $excluded_terms ) { + $excluded_terms[] = $this->exclude_term; + return $excluded_terms; + } }