Make WordPress Core

Ticket #34544: 34544.diff

File 34544.diff, 6.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/taxonomy-functions.php

    diff --git src/wp-includes/taxonomy-functions.php src/wp-includes/taxonomy-functions.php
    index 706caf1..ad5de6b 100644
    function get_terms( $taxonomies, $args = '' ) { 
    15561556 * @param mixed  $meta_value Metadata value.
    15571557 * @param bool   $unique     Optional. Whether to bail if an entry with the same key is found for the term.
    15581558 *                           Default false.
    1559  * @return int|bool Meta ID on success, false on failure.
     1559 * @return int|WP_Error|bool Meta ID on success. WP_Error when term_id is ambiguous between taxonomies.
     1560 *                           False on failure.
    15601561 */
    15611562function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
    15621563        // Bail if term meta table is not installed.
    function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) { 
    15641565                return false;
    15651566        }
    15661567
     1568        if ( wp_term_is_shared( $term_id ) ) {
     1569                return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
     1570        }
     1571
    15671572        $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
    15681573
    15691574        // Bust term query cache.
    function get_term_meta( $term_id, $key = '', $single = false ) { 
    16331638 * @param string $meta_key   Metadata key.
    16341639 * @param mixed  $meta_value Metadata value.
    16351640 * @param mixed  $prev_value Optional. Previous value to check before removing.
    1636  * @return int|bool Meta ID if the key didn't previously exist. True on successful update. False on failure.
     1641 * @return int|WP_Error|bool Meta ID if the key didn't previously exist. True on successful update.
     1642 *                           WP_Error when term_id is ambiguous between taxonomies. False on failure.
    16371643 */
    16381644function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
    16391645        // Bail if term meta table is not installed.
    function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) 
    16411647                return false;
    16421648        }
    16431649
     1650        if ( wp_term_is_shared( $term_id ) ) {
     1651                return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
     1652        }
     1653
    16441654        $updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
    16451655
    16461656        // Bust term query cache.
    function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { 
    39853995                update_option( '_split_terms', $split_term_data );
    39863996        }
    39873997
     3998        // If we've just split the final shared terms, set the "finished" flag.
     3999        $shared_terms_exist = $wpdb->get_results(
     4000                "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
     4001                 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
     4002                 GROUP BY t.term_id
     4003                 HAVING term_tt_count > 1
     4004                 LIMIT 1"
     4005        );
     4006        if ( ! $shared_terms_exist ) {
     4007                update_option( 'finished_splitting_shared_terms', true );
     4008        }
     4009
    39884010        /**
    39894011         * Fires after a previously shared taxonomy term is split into two separate terms.
    39904012         *
    function wp_get_split_term( $old_term_id, $taxonomy ) { 
    42334255}
    42344256
    42354257/**
     4258 * Determine whether a term is shared between multiple taxonomies.
     4259 *
     4260 * Shared taxonomy terms began to be split in 4.3, but failed cron tasks or other delays in upgrade routines may cause
     4261 * shared terms to remain.
     4262 *
     4263 * @since 4.4.0
     4264 *
     4265 * @param int $term_id
     4266 * @return bool
     4267 */
     4268function wp_term_is_shared( $term_id ) {
     4269        global $wpdb;
     4270
     4271        if ( get_option( 'finished_splitting_shared_terms' ) ) {
     4272                return false;
     4273        }
     4274
     4275        $tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
     4276
     4277        return $tt_count > 1;
     4278}
     4279
     4280/**
    42364281 * Generate a permalink for a taxonomy term archive.
    42374282 *
    42384283 * @since 2.5.0
  • tests/phpunit/tests/term/meta.php

    diff --git tests/phpunit/tests/term/meta.php tests/phpunit/tests/term/meta.php
    index 16e4527..6c34cab 100644
    class Tests_Term_Meta extends WP_UnitTestCase { 
    307307                $this->assertEqualSets( array( $terms[0] ), $found );
    308308        }
    309309
     310        /**
     311         * @ticket 34544
     312         */
     313        public function test_add_term_meta_should_return_error_when_term_id_is_shared() {
     314                global $wpdb;
     315
     316                update_option( 'finished_splitting_shared_terms', false );
     317
     318                register_taxonomy( 'wptests_tax', 'post' );
     319                register_taxonomy( 'wptests_tax_2', 'post' );
     320                register_taxonomy( 'wptests_tax_3', 'post' );
     321
     322                $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     323                $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     324                $t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
     325
     326                // Manually modify because shared terms shouldn't naturally occur.
     327                $wpdb->update( $wpdb->term_taxonomy,
     328                        array( 'term_id' => $t1['term_id'] ),
     329                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     330                        array( '%d' ),
     331                        array( '%d' )
     332                );
     333
     334                $wpdb->update( $wpdb->term_taxonomy,
     335                        array( 'term_id' => $t1['term_id'] ),
     336                        array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     337                        array( '%d' ),
     338                        array( '%d' )
     339                );
     340
     341                $found = add_term_meta( $t1['term_id'], 'bar', 'baz' );
     342                $this->assertWPError( $found );
     343                $this->assertSame( 'ambiguous_term_id', $found->get_error_code() );
     344        }
     345
     346        /**
     347         * @ticket 34544
     348         */
     349        public function test_update_term_meta_should_return_error_when_term_id_is_shared() {
     350                global $wpdb;
     351
     352                update_option( 'finished_splitting_shared_terms', false );
     353
     354                register_taxonomy( 'wptests_tax', 'post' );
     355                $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     356                add_term_meta( $t1, 'foo', 'bar' );
     357
     358                register_taxonomy( 'wptests_tax_2', 'post' );
     359                register_taxonomy( 'wptests_tax_3', 'post' );
     360
     361                $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     362                $t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
     363
     364                // Manually modify because shared terms shouldn't naturally occur.
     365                $wpdb->update( $wpdb->term_taxonomy,
     366                        array( 'term_id' => $t1['term_id'] ),
     367                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     368                        array( '%d' ),
     369                        array( '%d' )
     370                );
     371
     372                $wpdb->update( $wpdb->term_taxonomy,
     373                        array( 'term_id' => $t1['term_id'] ),
     374                        array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     375                        array( '%d' ),
     376                        array( '%d' )
     377                );
     378
     379                $found = update_term_meta( $t1['term_id'], 'foo', 'baz' );
     380                $this->assertWPError( $found );
     381                $this->assertSame( 'ambiguous_term_id', $found->get_error_code() );
     382        }
     383
    310384        public static function set_cache_results( $q ) {
    311385                $q->set( 'cache_results', true );
    312386        }