diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 0e746becb0..be39e36279 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1687,6 +1687,18 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where = ''; $adjacent = $previous ? 'previous' : 'next'; + if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { + // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " + if ( false !== strpos( $excluded_terms, ' and ' ) ) { + _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); + $excluded_terms = explode( ' and ', $excluded_terms ); + } else { + $excluded_terms = explode( ',', $excluded_terms ); + } + + $excluded_terms = array_map( 'intval', $excluded_terms ); + } + /** * Filters the IDs of terms excluded from adjacent post queries. * @@ -1695,23 +1707,11 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo * * @since 4.4.0 * - * @param string $excluded_terms Array of excluded term IDs. + * @param array $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 " - if ( false !== strpos( $excluded_terms, ' and ' ) ) { - _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); - $excluded_terms = explode( ' and ', $excluded_terms ); - } else { - $excluded_terms = explode( ',', $excluded_terms ); - } - - $excluded_terms = array_map( 'intval', $excluded_terms ); - } - if ( $in_same_term ) { $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"; $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy ); diff --git a/tests/phpunit/tests/link/getAdjacentPost.php b/tests/phpunit/tests/link/getAdjacentPost.php index 83ff42c3fc..649d1547f4 100644 --- a/tests/phpunit/tests/link/getAdjacentPost.php +++ b/tests/phpunit/tests/link/getAdjacentPost.php @@ -312,6 +312,40 @@ class Tests_Link_GetAdjacentPost extends WP_UnitTestCase { $this->assertSame( $p3, $found->ID ); } + /** + * @ticket 43521 + */ + public function test_excluded_terms_filter_empty() { + 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;