Make WordPress Core

Ticket #31149: 31149.diff

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

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 9c24d0b..9ccddb1 100644
    function global_terms_enabled() { 
    38823882        if ( ! is_multisite() )
    38833883                return false;
    38843884
    3885         static $global_terms = null;
    3886         if ( is_null( $global_terms ) ) {
     3885        $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
    38873886
    3888                 /**
    3889                  * Filter whether global terms are enabled.
    3890                  *
    3891                  * Passing a non-null value to the filter will effectively short-circuit the function,
    3892                  * returning the value of the 'global_terms_enabled' site option instead.
    3893                  *
    3894                  * @since 3.0.0
    3895                  *
    3896                  * @param null $anbled Whether global terms are enabled.
    3897                  */
    3898                 $filter = apply_filters( 'global_terms_enabled', null );
    3899                 if ( ! is_null( $filter ) )
    3900                         $global_terms = (bool) $filter;
    3901                 else
    3902                         $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
    3903         }
    3904         return $global_terms;
     3887        /**
     3888         * Filter whether global terms are enabled.
     3889         *
     3890         * Passing a non-null value to the filter will effectively short-circuit the function,
     3891         * returning the value of the 'global_terms_enabled' site option instead.
     3892         *
     3893         * @since 3.0.0
     3894         *
     3895         * @param null $anbled Whether global terms are enabled.
     3896         */
     3897        return apply_filters( 'global_terms_enabled', $global_terms );
    39053898}
    39063899
    39073900/**
  • src/wp-includes/ms-functions.php

    diff --git src/wp-includes/ms-functions.php src/wp-includes/ms-functions.php
    index a1b67a6..6dbcf08 100644
    function global_terms( $term_id, $deprecated = '' ) { 
    18011801                        $global_id = $wpdb->insert_id;
    18021802                }
    18031803        } elseif ( $global_id != $term_id ) {
    1804                 $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
     1804                $local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
    18051805                if ( null != $local_id ) {
    18061806                        global_terms( $local_id );
    18071807                        if ( 10 < $global_terms_recurse ) {
  • new file tests/phpunit/tests/multisite/globalTerms.php

    diff --git tests/phpunit/tests/multisite/globalTerms.php tests/phpunit/tests/multisite/globalTerms.php
    new file mode 100644
    index 0000000..24bdacd
    - +  
     1<?php
     2if ( is_multisite() ) :
     3
     4/**
     5 * @group taxonomy
     6 * @group multisite
     7 */
     8class Tests_Multisite_GlobalTerms extends WP_UnitTestCase {
     9        protected $suppress = false;
     10        static $global_terms_table_exists = false;
     11
     12        public static function setUpBeforeClass() {
     13                global $wpdb;
     14
     15                add_filter( 'global_terms_enabled', '__return_true' );
     16
     17                self::$global_terms_table_exists = (bool) $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->sitecategories}'" );
     18                if ( ! self::$global_terms_table_exists ) {
     19                        if ( ! function_exists( 'install_global_terms' ) ) {
     20                                require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
     21                        }
     22
     23                        install_global_terms();
     24                }
     25
     26                self::commit_transaction();
     27        }
     28
     29        public static function tearDownAfterClass() {
     30                global $wpdb;
     31
     32                if ( ! self::$global_terms_table_exists ) {
     33                        $wpdb->query( "DROP TABLE {$wpdb->sitecategories}" );
     34                }
     35
     36                remove_filter( 'global_terms_enabled', '__return_true' );
     37
     38                self::commit_transaction();
     39        }
     40
     41        /**
     42         * @ticket 31149
     43         */
     44        public function test_conflict_between_global_and_local_term_id_should_be_resolved_via_recursion() {
     45                global $wpdb;
     46                $global_terms_exists = (bool)
     47
     48                $sites = $this->factory->blog->create_many( 2 );
     49
     50                /*
     51                 * Fake a race condition by forcing a conflict between a global and a local term_id.
     52                 *
     53                 * On site 0:
     54                 * * Create a 'foo' term.
     55                 * * Fetch the ID of the corresponding term in the global terms table.
     56                 *
     57                 * On site 1:
     58                 * * Create a dummy term.
     59                 * * Update that term so that its local term_id matches the global_term_id identified above. This
     60                 *   represents the race condition.
     61                 * * Create a term 'foo' that will be matched in global terms but whose global_term_id will be a
     62                 *   mismatch with the local term_id.
     63                 */
     64
     65                switch_to_blog( $sites[0] );
     66
     67                $this->factory->category->create( array(
     68                        'name' => 'Foo',
     69                        'slug' => 'foo',
     70                ) );
     71
     72                // Get the randomly-generated global term ID.
     73                $global_term_id = $wpdb->get_var( "SELECT cat_ID from $wpdb->sitecategories WHERE category_nicename = 'foo'" );
     74
     75                restore_current_blog();
     76
     77                // On site 1:
     78                switch_to_blog( $sites[1] );
     79
     80                // Create a term, and then manually manipulate it to have a term_id conflict with the global term ID.
     81                $s2_c1 = $this->factory->category->create();
     82                $wpdb->update( $wpdb->terms, array( 'term_id' => $global_term_id ), array( 'term_id' => $s2_c1 ) );
     83
     84                // Now create a term that will trigger the conflict.
     85                $s2_c2 = wp_insert_term( 'Foo', 'category', array(
     86                        'slug' => 'foo',
     87                ) );
     88
     89                restore_current_blog();
     90
     91                $this->assertEquals( $global_term_id, $s2_c2['term_id'] );
     92        }
     93}
     94
     95endif;