Skip to:
Content

BuddyPress.org

Changeset 11118


Ignore:
Timestamp:
09/19/2016 11:44:10 PM (8 years ago)
Author:
dcavins
Message:

Add multisite-aware wrapper for get_objects_in_term().

Add wrapper functions for working with BuddyPress taxonomies via
get_objects_in_term(). When working with BuddyPress objects like
users, we need to be sure that we’re referring to the taxonomies and
terms for the site on the network where the relevant taxonomy is
registered.

Props dcavins, boonebgorges, offereins.

See #7263.

File:
1 edited

Legend:

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

    r11110 r11118  
    194194    return $retval;
    195195}
     196
     197/**
     198 * Retrieve IDs of objects in valid taxonomies and terms for BuddyPress-related taxonomies.
     199 *
     200 * Note that object IDs are from the `bp_get_taxonomy_term_site_id()`, which on some
     201 * multisite configurations may not be the same as the current site.
     202 *
     203 * @since 2.7.0
     204 *
     205 * @see get_objects_in_term() for a full description of function and parameters.
     206 *
     207 * @param int|array    $term_ids   Term id or array of term ids of terms that will be used.
     208 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
     209 * @param array|string $args       Change the order of the object_ids, either ASC or DESC.
     210 *
     211 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success,
     212 *                        the array can be empty, meaning that there are no $object_ids found. When
     213 *                        object IDs are found, an array of those IDs will be returned.
     214 */
     215function bp_get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
     216    // Different taxonomies may be stored on different sites.
     217    $taxonomy_site_map = array();
     218    foreach ( (array) $taxonomies as $taxonomy ) {
     219        $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     220        $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
     221    }
     222
     223    $retval = array();
     224    foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
     225        $switched = false;
     226        if ( $taxonomy_site_id !== get_current_blog_id() ) {
     227            switch_to_blog( $taxonomy_site_id );
     228            bp_register_taxonomies();
     229            $switched = true;
     230        }
     231
     232        $site_objects = get_objects_in_term( $term_ids, $site_taxonomies, $args );
     233        $retval       = array_merge( $retval, $site_objects );
     234
     235        if ( $switched ) {
     236            restore_current_blog();
     237        }
     238    }
     239
     240    return $retval;
     241}
     242
Note: See TracChangeset for help on using the changeset viewer.