Skip to:
Content

BuddyPress.org

Changeset 10818


Ignore:
Timestamp:
05/28/2016 01:43:05 PM (8 years ago)
Author:
boonebgorges
Message:

Taxonomy: Allow the site used for taxonomy term storage to be filtered.

On some setups, it may be desirable to store the terms from a given BP taxonomy
on a site other than the default. For example, on a multi-network setup, where
users are shared between all networks, it may suit the purposes of the
installation better to have bp_member_type data stored on the primary network
and shared between networks.

The new function bp_get_taxonomy_term_site_id() function allows the value to
be filtered, on a taxonomy-specific basis. BP's taxonomy wrapper functions have
been updated to use this new function.

Props rekmla.
Fixes #7077.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-taxonomy.php

    r10765 r10818  
    4646
    4747/**
     48 * Gets the ID of the site that BP should use for taxonomy term storage.
     49 *
     50 * Defaults to the root blog ID.
     51 *
     52 * @since 2.6.0
     53 *
     54 * @return int
     55 */
     56function bp_get_taxonomy_term_site_id( $taxonomy = '' ) {
     57    $site_id = bp_get_root_blog_id();
     58
     59    /**
     60     * Filters the ID of the site where BP should store taxonomy terms.
     61     *
     62     * @since 2.6.0
     63     *
     64     * @param int    $site_id
     65     * @param string $taxonomy
     66     */
     67    return (int) apply_filters( 'bp_get_taxonomy_term_site_id', $site_id, $taxonomy );
     68}
     69
     70/**
    4871 * Set taxonomy terms on a BuddyPress object.
    4972 *
     
    5982 */
    6083function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
    61     $is_root_blog = bp_is_root_blog();
     84    $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
    6285
    63     if ( ! $is_root_blog ) {
    64         switch_to_blog( bp_get_root_blog_id() );
     86    $switched = false;
     87    if ( $site_id !== get_current_blog_id() ) {
     88        switch_to_blog( $site_id );
    6589        bp_register_taxonomies();
     90        $switched = true;
    6691    }
    6792
    6893    $retval = wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
    6994
    70     if ( ! $is_root_blog ) {
     95    if ( ! $switched ) {
    7196        restore_current_blog();
    7297    }
     
    88113 */
    89114function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
    90     $is_root_blog = bp_is_root_blog();
    91 
    92     if ( ! $is_root_blog ) {
    93         switch_to_blog( bp_get_root_blog_id() );
    94         bp_register_taxonomies();
     115    // Different taxonomies must be stored on different sites.
     116    $taxonomy_site_map = array();
     117    foreach ( (array) $taxonomies as $taxonomy ) {
     118        $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     119        $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
    95120    }
    96121
    97     $retval = wp_get_object_terms( $object_ids, $taxonomies, $args );
     122    $retval = array();
     123    foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
     124        $switched = false;
     125        if ( $taxonomy_site_id !== get_current_blog_id() ) {
     126            switch_to_blog( $site_id );
     127            bp_register_taxonomies();
     128            $switched = true;
     129        }
    98130
    99     if ( ! $is_root_blog ) {
    100         restore_current_blog();
     131        $site_terms = wp_get_object_terms( $object_ids, $site_taxonomies, $args );
     132        $retval     = array_merge( $retval, $site_terms );
     133
     134        if ( ! $switched ) {
     135            restore_current_blog();
     136        }
    101137    }
    102138
     
    117153 */
    118154function bp_remove_object_terms( $object_id, $terms, $taxonomy ) {
    119     $is_root_blog = bp_is_root_blog();
     155    $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
    120156
    121     if ( ! $is_root_blog ) {
    122         switch_to_blog( bp_get_root_blog_id() );
     157    $switched = false;
     158    if ( $site_id !== get_current_blog_id() ) {
     159        switch_to_blog( $site_id );
    123160        bp_register_taxonomies();
     161        $switched = true;
    124162    }
    125163
    126164    $retval = wp_remove_object_terms( $object_id, $terms, $taxonomy );
    127165
    128     if ( ! $is_root_blog ) {
     166    if ( ! $switched ) {
    129167        restore_current_blog();
    130168    }
Note: See TracChangeset for help on using the changeset viewer.