Make WordPress Core

Ticket #30335: 30335.9.patch

File 30335.9.patch, 7.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/category-template.php

    diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
    index 0169145..ec40e6c 100644
    function wp_dropdown_categories( $args = '' ) { 
    402402                        $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
    403403                }
    404404
     405                if ( $r['selected'] ) {
     406                        // Check if the 'selected' term was split
     407                        $selected_term = get_term( $r['selected'], $r['taxonomy'] );
     408                        if ( $selected_term && ! is_wp_error( $selected_term ) && intval( $r['selected'] ) != $selected_term->term_id ) {
     409                                $r['selected'] = $selected_term->term_id;
     410                        }
     411                }
     412
    405413                if ( $r['hierarchical'] ) {
    406414                        $depth = $r['depth'];  // Walk the full depth.
    407415                } else {
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 012c020..8ccc847 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 = wp_get_split_term( $term_id, $query['taxonomy'] ) ) {
     1241                                                $terms[ $term_index ] = $split_term;
     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 && $old_term_id = wp_get_split_term( $term, $taxonomy ) ) {
     1331                                return get_term( $old_term_id, $taxonomy, $output, $filter );
     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 = wp_get_split_term( $passed_term_id, $taxonomy ) ) {
     1710                                        $passed_term_ids[ $passed_term_index ] = $split_term;
     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 *
     3958 * @param int    $old_term_id Old, pre-split term_id.
     3959 * @param string $taxonomy    Taxonomy name.
     3960 */
     3961function wp_get_split_term( $old_term_id, $taxonomy ) {
     3962        $split_terms = get_option( '_split_terms_' . $taxonomy );
     3963
     3964        return ! empty( $split_terms[ $old_term_id ] ) ? $split_terms[ $old_term_id ] : null;
     3965}
     3966
     3967/**
    39063968 * Add count of children to parent count.
    39073969 *
    39083970 * Recalculates term counts by including items from child terms. Assumes all
  • tests/phpunit/tests/term/splitSharedTerm.php

    diff --git tests/phpunit/tests/term/splitSharedTerm.php tests/phpunit/tests/term/splitSharedTerm.php
    index 901ab14..994d341 100644
    class Tests_Term_SplitSharedTerm extends WP_UnitTestCase { 
    125125        /**
    126126         * @ticket 30335
    127127         */
     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        }
     184
     185        /**
     186         * @ticket 30335
     187         */
     188        public function test_should_remain_available_at_old_term_id_using_wp_dropdown_categories() {
     189                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     190                $terms = get_terms( 'wptests_tax_2', array( 'hide_empty' => false ) );
     191                $dropdown = wp_dropdown_categories( array(
     192                        'echo'       => false,
     193                        'taxonomy'   => 'wptests_tax_2',
     194                        'hide_empty' => false,
     195                        'selected'   => $this->terms['t1']['term_id'],
     196                ) );
     197
     198                $this->assertRegExp( "/<option[^>]+value=\"{$t2_term->term_id}\" selected=\"selected\">/", $dropdown );
     199        }
     200
     201        /**
     202         * @ticket 30335
     203         */
    128204        public function test_should_update_default_category_on_term_split() {
    129205                global $wpdb;
    130206                $t1 = wp_insert_term( 'Foo Default', 'category' );