Skip to:
Content

BuddyPress.org

Ticket #7263: 7263.diff

File 7263.diff, 3.4 KB (added by boonebgorges, 8 years ago)
  • src/bp-core/bp-core-taxonomy.php

    diff --git src/bp-core/bp-core-taxonomy.php src/bp-core/bp-core-taxonomy.php
    index d3f8124..4147f9a 100644
    function bp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
    193193
    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, $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
     243/**
     244 * Get term data for terms in BuddyPress taxonomies.
     245 *
     246 * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some
     247 * multisite configurations may not be the same as the current site.
     248 *
     249 * @since 2.7.0
     250 *
     251 * @see get_term_by() for a full description of function and parameters.
     252 *
     253 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
     254 * @param string|int $value    Search for this term value
     255 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
     256 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
     257 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
     258 *
     259 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
     260 *                      or `$term` was not found.
     261 */
     262function bp_get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
     263        $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     264
     265        $switched = false;
     266        if ( $site_id !== get_current_blog_id() ) {
     267                switch_to_blog( $site_id );
     268                bp_register_taxonomies();
     269                $switched = true;
     270        }
     271
     272        $term = get_term_by( $field, $value, $taxonomy, $output, $filter );
     273
     274        if ( $switched ) {
     275                restore_current_blog();
     276        }
     277
     278        return $term;
     279}