base_prefix}sitecategories ( cat_ID bigint(20) NOT NULL auto_increment, cat_name varchar(55) NOT NULL default '', category_nicename varchar(200) NOT NULL default '', last_updated timestamp NOT NULL, PRIMARY KEY (cat_ID), KEY category_nicename (category_nicename), KEY last_updated (last_updated) ) $charset_collate;"; dbDelta( $create_table ); } } class GlobalTerms { /** * Table name were global terms are held * * @var string */ var $table; /** * Singleton instance * * @var GlobalTerms */ private $instance; /** * Singleton accessor * * @return GlobalTerms $instance */ public static function get_instance() { if ( ! isset( self::$instance ) ) self::$instance = new GlobalTerms; return self::$instance; } private function __construct() { global $wpdb; $this->table = $wpdb->base_prefix . 'sitecategories'; add_filter( 'term_id_filter', array( $this, 'global_terms' ), 10, 2 ); add_filter( 'get_term', array( $this, 'sync_term_slugs' ), 10, 2 ); } /** * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table. * * @since (was) WP 3.0.0 * * @see term_id_filter * * @param int $term_id An ID for a term on the current blog. * @return int An ID from the global terms table mapped from $term_id. */ function global_terms( $term_id ) { global $wpdb; static $global_terms_recurse = null; if ( ! get_site_option( 'global_terms_enabled' ) ) return $term_id; // prevent a race condition $recurse_start = false; if ( $global_terms_recurse === null ) { $recurse_start = true; $global_terms_recurse = 1; } elseif ( 10 < $global_terms_recurse++ ) { return $term_id; } $term_id = intval( $term_id ); $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) ); $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $this->table WHERE category_nicename = %s", $c->slug ) ); if ( $global_id == null ) { $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $this->table WHERE cat_ID = %d", $c->term_id ) ); if ( null == $used_global_id ) { $wpdb->insert( $this->table, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); $global_id = $wpdb->insert_id; if ( empty( $global_id ) ) return $term_id; } else { $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $this->table" ); $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" ); $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 ); $wpdb->insert( $this->table, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) ); $global_id = $wpdb->insert_id; } } elseif ( $global_id != $term_id ) { $local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) ); if ( null != $local_id ) $local_id = global_terms( $local_id ); if ( 10 < $global_terms_recurse ) $global_id = $term_id; } if ( $global_id != $term_id ) { if ( $term_id == get_option( 'default_category' ) ) update_option( 'default_category', $global_id ); $wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) ); $wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) ); $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) ); clean_term_cache( $term_id ); } if ( $recurse_start ) $global_terms_recurse = null; return $global_id; } /** * * @param object $term * @param string $taxonomy * @return object Term */ function sync_term_slugs( $term, $taxonomy ) { if ( ! get_site_option( 'global_terms_enabled' ) || ! in_array( $taxonomy, array( 'category', 'post_tag' ) ) ) return $term; if ( is_object( $term ) ) { $term->slug = sanitize_title( $term->name ); } else { $term['slug'] = sanitize_title( $term['name'] ); } return $term; } } GlobalTerms::get_instance(); if ( ! function_exists( 'global_terms_enabled' ) ) { /** * Whether global terms are enabled. * * @since WP 3.0.0 * * @return bool True if multisite and global terms enabled */ function global_terms_enabled() { if ( ! is_multisite() ) return false; static $global_terms = null; if ( is_null( $global_terms ) ) { $filter = apply_filters( 'global_terms_enabled', null ); if ( ! is_null( $filter ) ) $global_terms = (bool) $filter; else $global_terms = (bool) get_site_option( 'global_terms_enabled', false ); } return $global_terms; } }