Make WordPress Core

Ticket #43521: 43521.1.diff

File 43521.1.diff, 4.6 KB (added by soulseekah, 7 years ago)
  • src/wp-includes/link-template.php

    diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php
    index 9a27404..13619d2 100644
    function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo 
    16821682        $where    = '';
    16831683        $adjacent = $previous ? 'previous' : 'next';
    16841684
    1685         if ( $in_same_term || ! empty( $excluded_terms ) ) {
    1686                 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
    1687                         // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
    1688                         if ( false !== strpos( $excluded_terms, ' and ' ) ) {
    1689                                 _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
    1690                                 $excluded_terms = explode( ' and ', $excluded_terms );
    1691                         } else {
    1692                                 $excluded_terms = explode( ',', $excluded_terms );
    1693                         }
    1694 
    1695                         $excluded_terms = array_map( 'intval', $excluded_terms );
     1685        if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
     1686                // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     1687                if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1688                        _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1689                        $excluded_terms = explode( ' and ', $excluded_terms );
     1690                } else {
     1691                        $excluded_terms = explode( ',', $excluded_terms );
    16961692                }
    16971693
     1694                $excluded_terms = array_map( 'intval', $excluded_terms );
     1695        }
     1696
     1697        /**
     1698         * Filters the IDs of terms excluded from adjacent post queries.
     1699         *
     1700         * The dynamic portion of the hook name, `$adjacent`, refers to the type
     1701         * of adjacency, 'next' or 'previous'.
     1702         *
     1703         * @since 4.4.0
     1704         *
     1705         * @param string $excluded_terms Array of excluded term IDs.
     1706         */
     1707        $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
     1708
     1709        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    16981710                if ( $in_same_term ) {
    16991711                        $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";
    17001712                        $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );
    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;