WordPress.org

Make WordPress Core

Ticket #30335: 30335.2.patch

File 30335.2.patch, 10.2 KB (added by boonebgorges, 4 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index c60f1df..3d8015e 100644
    function wp_update_term( $term_id, $taxonomy, $args = array() ) { 
    33863386
    33873387        $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
    33883388
     3389        // Check whether this is a shared term that needs splitting.
     3390        $_term_id = _split_shared_term( $term_id, $tt_id );
     3391        if ( ! is_wp_error( $_term_id ) ) {
     3392                $term_id = $_term_id;
     3393        }
     3394
    33893395        /**
    33903396         * Fires immediately before the given terms are edited.
    33913397         *
    function _update_generic_term_count( $terms, $taxonomy ) { 
    40404046}
    40414047
    40424048/**
     4049 * Create a new term for a term_taxonomy item that currently shares its term.
     4050 *
     4051 * @since 4.1.0
     4052 * @access private
     4053 *
     4054 * @param int   $term_id          ID of the shared term.
     4055 * @param int   $term_taxonomy_id ID of the term taxonomy item to receive a new term.
     4056 * @param array $shared_tts       Sibling term taxonomies, used for busting caches.
     4057 * @return int  Term ID.
     4058 */
     4059function _split_shared_term( $term_id, $term_taxonomy_id ) {
     4060        global $wpdb;
     4061
     4062        // Don't try to split terms if database schema does not support shared slugs.
     4063        $current_db_version = get_option( 'db_version' );
     4064        if ( $current_db_version < 30133 ) {
     4065                return $term_id;
     4066        }
     4067
     4068        // If there are no shared term_taxonomy rows, there's nothing to do here.
     4069        $shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );
     4070        if ( ! $shared_tt_count ) {
     4071                return $term_id;
     4072        }
     4073
     4074        // Pull up data about the currently shared slug, which we'll use to populate the new one.
     4075        $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
     4076
     4077        $new_term_data = array(
     4078                'name' => $shared_term->name,
     4079                'slug' => $shared_term->slug,
     4080                'term_group' => $shared_term->term_group,
     4081        );
     4082
     4083        if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
     4084                return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
     4085        }
     4086
     4087        $new_term_id = (int) $wpdb->insert_id;
     4088
     4089        // Update the existing term_taxonomy to point to the newly created term.
     4090        $wpdb->update( $wpdb->term_taxonomy,
     4091                array( 'term_id' => $new_term_id ),
     4092                array( 'term_taxonomy_id' => $term_taxonomy_id )
     4093        );
     4094
     4095        // Reassign child terms to the new parent.
     4096        $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
     4097        $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
     4098
     4099        foreach ( $children_tt_ids as $child_tt_id ) {
     4100                $wpdb->update( $wpdb->term_taxonomy,
     4101                        array( 'parent' => $new_term_id ),
     4102                        array( 'term_taxonomy_id' => $child_tt_id )
     4103                );
     4104                clean_term_cache( $term_id, $term_taxonomy->taxonomy );
     4105        }
     4106
     4107        // Clean the cache for term taxonomies formerly shared with the current term.
     4108        $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
     4109        foreach ( (array) $shared_term_taxonomies as $shared_term_taxonomy ) {
     4110                clean_term_cache( $term_id, $shared_term_taxonomy );
     4111        }
     4112
     4113        /**
     4114         * Fires after a previously shared taxonomy term is split into two separate terms.
     4115         *
     4116         * @since 4.1.0
     4117         *
     4118         * @param int $new_term_id      ID of the new term created for the $term_taxonomy_id.
     4119         * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
     4120         * @param int $term_id          ID of the formerly shared term.
     4121         */
     4122        do_action( 'split_shared_term', $new_term_id, $term_taxonomy_id, $term_id );
     4123
     4124        return $new_term_id;
     4125}
     4126
     4127/**
    40434128 * Generate a permalink for a taxonomy term archive.
    40444129 *
    40454130 * @since 2.5.0
  • tests/phpunit/tests/term.php

    diff --git tests/phpunit/tests/term.php tests/phpunit/tests/term.php
    index 2cf5fb2..1258c4b 100644
    class Tests_Term extends WP_UnitTestCase { 
    690690                $this->assertSame( 'duplicate_term_slug', $updated->get_error_code() );
    691691        }
    692692
     693        /**
     694         * @ticket 5809
     695         */
     696        public function test_wp_update_term_should_split_shared_term() {
     697                global $wpdb;
     698
     699                register_taxonomy( 'wptests_tax', 'post' );
     700                register_taxonomy( 'wptests_tax_2', 'post' );
     701
     702                $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     703                $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     704
     705                // Manually modify because split terms shouldn't naturally occur.
     706                $wpdb->update( $wpdb->term_taxonomy,
     707                        array( 'term_id' => $t1['term_id'] ),
     708                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     709                        array( '%d' ),
     710                        array( '%d' )
     711                );
     712
     713                $posts = $this->factory->post->create_many( 2 );
     714                wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );
     715                wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );
     716
     717                // Verify that the terms are shared.
     718                $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
     719                $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
     720                $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
     721
     722                wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(
     723                        'name' => 'New Foo',
     724                ) );
     725
     726                $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
     727                $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
     728                $this->assertNotEquals( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
     729        }
     730
     731        /**
     732         * @ticket 5809
     733         */
     734        public function test_wp_update_term_should_not_split_shared_term_before_410_schema_change() {
     735                global $wpdb;
     736
     737                $db_version = get_option( 'db_version' );
     738                update_option( 'db_version', 30055 );
     739
     740                register_taxonomy( 'wptests_tax', 'post' );
     741                register_taxonomy( 'wptests_tax_2', 'post' );
     742
     743                $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     744                $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     745
     746                // Manually modify because split terms shouldn't naturally occur.
     747                $wpdb->update( $wpdb->term_taxonomy,
     748                        array( 'term_id' => $t1['term_id'] ),
     749                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     750                        array( '%d' ),
     751                        array( '%d' )
     752                );
     753
     754                $posts = $this->factory->post->create_many( 2 );
     755                wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );
     756                wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );
     757
     758                // Verify that the term is shared.
     759                $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
     760                $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
     761                $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
     762
     763                wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(
     764                        'name' => 'New Foo',
     765                ) );
     766
     767                // Term should still be shared.
     768                $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
     769                $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
     770                $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
     771
     772                update_option( 'db_version', $db_version );
     773        }
     774
    693775        public function test_wp_update_term_alias_of_no_term_group() {
    694776                register_taxonomy( 'wptests_tax', 'post' );
    695777                $t1 = $this->factory->term->create( array(
  • new file tests/phpunit/tests/term/splitSharedTerm.php

    diff --git tests/phpunit/tests/term/splitSharedTerm.php tests/phpunit/tests/term/splitSharedTerm.php
    new file mode 100644
    index 0000000..50ef1c9
    - +  
     1<?php
     2
     3/**
     4 * @group taxonomy
     5 */
     6class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
     7        protected $tt_ids = array();
     8
     9        public function setUp() {
     10                global $wpdb;
     11
     12                parent::setUp();
     13
     14                register_taxonomy( 'wptests_tax', 'post' );
     15                register_taxonomy( 'wptests_tax_2', 'post', array(
     16                        'hierarchical' => true,
     17                ) );
     18                register_taxonomy( 'wptests_tax_3', 'post' );
     19
     20                $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     21                $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     22                $t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
     23
     24                // Manually modify because split terms shouldn't naturally occur.
     25                $wpdb->update( $wpdb->term_taxonomy,
     26                        array( 'term_id' => $t1['term_id'] ),
     27                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     28                        array( '%d' ),
     29                        array( '%d' )
     30                );
     31
     32                $wpdb->update( $wpdb->term_taxonomy,
     33                        array( 'term_id' => $t1['term_id'] ),
     34                        array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     35                        array( '%d' ),
     36                        array( '%d' )
     37                );
     38
     39                $t2_child = wp_insert_term( 'Foo Child', 'wptests_tax_2', array(
     40                        'parent' => $t1['term_id'],
     41                ) );
     42
     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'],
     48                );
     49
     50                _split_shared_term( $t1['term_id'], $t2['term_taxonomy_id'] );
     51                _split_shared_term( $t1['term_id'], $t3['term_taxonomy_id'] );
     52        }
     53
     54        /**
     55         * @ticket 5809
     56         */
     57        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' );
     61
     62                $this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
     63                $this->assertNotEquals( $t1_term->term_id, $t3_term->term_id );
     64                $this->assertNotEquals( $t2_term->term_id, $t3_term->term_id );
     65        }
     66
     67        /**
     68         * @ticket 5809
     69         */
     70        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' );
     72                $children = get_terms( 'wptests_tax_2', array(
     73                        'parent' => $t2_term->term_id,
     74                        'hide_empty' => false,
     75                ) );
     76
     77                $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     78        }
     79
     80        /**
     81         * @ticket 5809
     82         */
     83        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' );
     85                $children = get_terms( 'wptests_tax_2', array(
     86                        'child_of' => $t2_term->term_id,
     87                        'hide_empty' => false,
     88                ) );
     89
     90                $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     91        }
     92}