Make WordPress Core

Ticket #30335: 30335.3.patch

File 30335.3.patch, 9.4 KB (added by boonebgorges, 10 years ago)

Backward compatibility for fetching terms by the old term_id

  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index fa450e5..e4d857c 100644
    class WP_Tax_Query { 
    12331233                                " );
    12341234                                break;
    12351235                        default:
    1236                                 $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
     1236                                $terms = $query['terms'];
     1237
     1238                                // If any passed term_ids were previously split, substitute the correct term_id.
     1239                                foreach ( $terms as $term_index => $term_id ) {
     1240                                        if ( $split_term = _get_split_term( $term_id, $query['taxonomy'] ) ) {
     1241                                                $terms[ $term_index ] = $split_term->term_id;
     1242                                        }
     1243                                }
     1244
     1245                                $terms = implode( ',', array_map( 'intval', $terms ) );
     1246
    12371247                                $terms = $wpdb->get_col( "
    12381248                                        SELECT $resulting_field
    12391249                                        FROM $wpdb->term_taxonomy
    function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 
    13151325                        return null;
    13161326                if ( ! $_term = wp_cache_get( $term, $taxonomy . ':terms:' . $incrementor ) ) {
    13171327                        $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );
     1328
     1329                        // If no term is found, check whether this was a previously split term.
     1330                        if ( ! $_term ) {
     1331                                $_term = _get_split_term( $term, $taxonomy );
     1332                        }
     1333
    13181334                        if ( ! $_term )
    13191335                                return null;
    13201336                        wp_cache_add( $term, $_term, $taxonomy . ':terms:' . $incrementor );
    function get_terms( $taxonomies, $args = '' ) { 
    16751691         */
    16761692        $args = apply_filters( 'get_terms_args', $args, $taxonomies );
    16771693
     1694        /*
     1695         * For parameters that accept term_ids, check to see whether any of the IDs passed to the parameter correspond
     1696         * to previously split taxonomy terms. If so, swap out the old term_id for the new one, so the query matches.
     1697         */
     1698        $term_id_params = array( 'exclude', 'exclude_tree', 'include', 'parent', 'child_of' );
     1699        foreach ( $term_id_params as $term_id_param ) {
     1700                if ( empty( $args[ $term_id_param ] ) ) {
     1701                        continue;
     1702                }
     1703
     1704                $param_is_array = is_array( $args[ $term_id_param ] );
     1705
     1706                $passed_term_ids = (array) $args[ $term_id_param ];
     1707                foreach ( $passed_term_ids as $passed_term_index => $passed_term_id ) {
     1708                        foreach ( $taxonomies as $taxonomy ) {
     1709                                if ( $split_term = _get_split_term( $passed_term_id, $taxonomy ) ) {
     1710                                        $passed_term_ids[ $passed_term_index ] = $split_term->term_id;
     1711                                        continue 2;
     1712                                }
     1713                        }
     1714                }
     1715
     1716                if ( $param_is_array ) {
     1717                        $args[ $term_id_param ] = $passed_term_ids;
     1718                } else {
     1719                        $args[ $term_id_param ] = $passed_term_ids[0];
     1720                }
     1721        }
     1722
    16781723        $child_of = $args['child_of'];
    16791724        if ( $child_of ) {
    16801725                $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
    function _get_term_children($term_id, $terms, $taxonomy) { 
    39033948}
    39043949
    39053950/**
     3951 * Look up a previously split taxonomy term by its old term_id.
     3952 *
     3953 * Terms that have been split by _split_shared_term() are stored, so that
     3954 * attempts to retrieve terms by the previous term_id still work.
     3955 *
     3956 * @since 4.1.0
     3957 * @access private
     3958 *
     3959 * @param int    $old_term_id Old, pre-split term_id.
     3960 * @param string $taxonomy    Taxonomy name.
     3961 */
     3962function _get_split_term( $old_term_id, $taxonomy ) {
     3963        $split_terms = get_option( '_split_terms' );
     3964
     3965        if ( empty( $split_terms[ $old_term_id ] ) ) {
     3966                return null;
     3967        }
     3968
     3969        foreach ( $split_terms[ $old_term_id ] as $term_taxonomy_id ) {
     3970                $split_term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy );
     3971                if ( $split_term ) {
     3972                        return $split_term;
     3973                }
     3974        }
     3975}
     3976
     3977/**
    39063978 * Add count of children to parent count.
    39073979 *
    39083980 * Recalculates term counts by including items from child terms. Assumes all
    function _split_shared_term( $term_id, $term_taxonomy_id ) { 
    41154187                clean_term_cache( $term_id, $shared_term_taxonomy );
    41164188        }
    41174189
     4190        // Keep a record of term_taxonomy_ids that have been split, keyed by old term_id.
     4191        $split_term_data = get_option( '_split_terms', array() );
     4192        if ( ! isset( $split_term_data[ $term_id ] ) ) {
     4193                $split_term_data[ $term_id ] = array();
     4194        }
     4195        $split_term_data[ $term_id ][] = $term_taxonomy_id;
     4196        update_option( '_split_terms', $split_term_data );
     4197
    41184198        /**
    41194199         * Fires after a previously shared taxonomy term is split into two separate terms.
    41204200         *
  • tests/phpunit/tests/term/splitSharedTerm.php

    diff --git tests/phpunit/tests/term/splitSharedTerm.php tests/phpunit/tests/term/splitSharedTerm.php
    index ceab947..72f412d 100644
     
    44 * @group taxonomy
    55 */
    66class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
    7         protected $tt_ids = array();
     7        protected $terms = array();
    88
    99        public function setUp() {
    1010                global $wpdb;
    class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { 
    4040                        'parent' => $t1['term_id'],
    4141                ) );
    4242
    43                 $this->tt_ids = array(
    44                         't1' => $t1['term_taxonomy_id'],
    45                         't2' => $t2['term_taxonomy_id'],
    46                         't3' => $t3['term_taxonomy_id'],
    47                         't2_child' => $t2_child['term_taxonomy_id'],
     43                $this->terms = array(
     44                        't1' => $t1,
     45                        't2' => $t2,
     46                        't3' => $t3,
     47                        't2_child' => $t2_child,
    4848                );
    4949
    5050                _split_shared_term( $t1['term_id'], $t2['term_taxonomy_id'] );
    class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { 
    5555         * @ticket 5809
    5656         */
    5757        public function test_should_create_new_term_ids() {
    58                 $t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
    59                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
    60                 $t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
     58                $t1_term = get_term_by( 'term_taxonomy_id', $this->terms['t1']['term_taxonomy_id'], 'wptests_tax' );
     59                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     60                $t3_term = get_term_by( 'term_taxonomy_id', $this->terms['t3']['term_taxonomy_id'], 'wptests_tax_3' );
    6161
    6262                $this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
    6363                $this->assertNotEquals( $t1_term->term_id, $t3_term->term_id );
    class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { 
    6868         * @ticket 5809
    6969         */
    7070        public function test_should_retain_child_terms_when_using_get_terms_parent() {
    71                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     71                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    7272                $children = get_terms( 'wptests_tax_2', array(
    7373                        'parent' => $t2_term->term_id,
    7474                        'hide_empty' => false,
    7575                ) );
    7676
    77                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     77                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    7878        }
    7979
    8080        /**
    8181         * @ticket 5809
    8282         */
    8383        public function test_should_retain_child_terms_when_using_get_terms_child_of() {
    84                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     84                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    8585                $children = get_terms( 'wptests_tax_2', array(
    8686                        'child_of' => $t2_term->term_id,
    8787                        'hide_empty' => false,
    8888                ) );
    8989
    90                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     90                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    9191        }
    9292
    9393        /**
    class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { 
    121121                $t2_children = get_term_children( $t2['term_id'], 'wptests_tax_4' );
    122122                $this->assertEquals( array( $new_term_id ), $t2_children );
    123123        }
     124
     125        /**
     126         * @ticket 30335
     127         */
     128        public function test_should_remain_available_at_old_term_id_using_get_term_by() {
     129                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     130
     131                $this->assertEquals( $t2_term, get_term_by( 'id', $this->terms['t1']['term_id'], 'wptests_tax_2' ) );
     132        }
     133
     134        /**
     135         * @ticket 30335
     136         */
     137        public function test_should_remain_available_at_old_term_id_using_get_term() {
     138                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     139
     140                $this->assertEquals( $t2_term, get_term( intval( $this->terms['t1']['term_id'] ), 'wptests_tax_2' ) );
     141        }
     142
     143        /**
     144         * @ticket 30335
     145         */
     146        public function test_should_remain_available_at_old_term_id_using_get_terms_with_parent_param() {
     147                $found = get_terms( 'wptests_tax_2', array(
     148                        'parent' => $this->terms['t1']['term_id'],
     149                        'hide_empty' => false,
     150                ) );
     151                $this->assertEquals( $this->terms['t2_child']['term_id'], $found[0]->term_id );
     152        }
     153
     154        /**
     155         * @ticket 30335
     156         */
     157        public function test_should_remain_available_at_old_term_id_using_get_terms_with_include_param() {
     158                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     159
     160                $found = get_terms( 'wptests_tax_2', array(
     161                        'include' => array( $this->terms['t1']['term_id'] ),
     162                        'hide_empty' => false,
     163                ) );
     164                $this->assertEquals( $t2_term->term_id, $found[0]->term_id );
     165        }
     166
     167        /**
     168         * @ticket 30335
     169         */
     170        public function test_should_remain_available_at_old_term_id_using_tax_query() {
     171                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     172
     173                $tq = new WP_Tax_Query( array(
     174                        array(
     175                                'field' => 'term_id',
     176                                'taxonomy' => 'wptests_tax_2',
     177                                'terms' => array( $this->terms['t1']['term_id'] ),
     178                        ),
     179                ) );
     180                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
     181
     182                $this->assertEquals( array( $t2_term->term_taxonomy_id ), $tq->queries[0]['terms'] );
     183        }
    124184}