From 1e54b5a2ae84ab85d1001c68df9dee66b35bc067 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 11 Mar 2018 17:31:04 +0000 Subject: [PATCH] Link Template: Apply `get_{$adjacent}_post_excluded_terms` filter to an empty `excluded_terms` parameter as well. Props soulseekah, zottto. Fixes #43521. git-svn-id: https://develop.svn.wordpress.org/trunk@42828 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 24 +++++++------- tests/phpunit/tests/link/getAdjacentPost.php | 35 +++++++++++++++++++- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 9a27404597..365b14b317 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1682,6 +1682,18 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where = ''; $adjacent = $previous ? 'previous' : 'next'; + /** + * Filters 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 ( $in_same_term || ! empty( $excluded_terms ) ) { if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " @@ -1715,18 +1727,6 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')'; } - /** - * Filters 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( ',', array_map( 'intval', $excluded_terms ) ) . ') )'; } diff --git a/tests/phpunit/tests/link/getAdjacentPost.php b/tests/phpunit/tests/link/getAdjacentPost.php index 0c38e63280..072485bba3 100644 --- a/tests/phpunit/tests/link/getAdjacentPost.php +++ b/tests/phpunit/tests/link/getAdjacentPost.php @@ -246,7 +246,7 @@ class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { /** * @ticket 35211 */ - public function test_excluded_terms_filter() { + public function test_get_adjacent_post_excluded_terms_filter() { register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( @@ -276,6 +276,39 @@ class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { $this->assertSame( $p3, $found->ID ); } + /** + * @ticket 43521 + */ + public function test_get_adjacent_post_excluded_terms_filter_should_apply_to_empty_excluded_terms_parameter() { + 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( false, 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;