Skip to:
Content

BuddyPress.org

Ticket #7263: 7263.01.patch

File 7263.01.patch, 3.1 KB (added by dcavins, 8 years ago)

Add BP wrappers for get_objects_in_term() and get_term_by().

  • 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..2d6c6d1 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 * @since 2.7.0
     201 *
     202 * @see get_objects_in_term() for a full description of function and parameters.
     203 *
     204 * @param int|array    $term_ids   Term id or array of term ids of terms that will be used.
     205 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
     206 * @param array|string $args       Change the order of the object_ids, either ASC or DESC.
     207 *
     208 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success,
     209 *                            the array can be empty meaning that there are no $object_ids found or it
     210 *                        will return the $object_ids found.
     211 */
     212function bp_get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
     213        // Different taxonomies may be stored on different sites.
     214        $taxonomy_site_map = array();
     215        foreach ( (array) $taxonomies as $taxonomy ) {
     216                $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     217                $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
     218        }
     219
     220        $retval = array();
     221        foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
     222                $switched = false;
     223                if ( $taxonomy_site_id !== get_current_blog_id() ) {
     224                        switch_to_blog( $taxonomy_site_id );
     225                        bp_register_taxonomies();
     226                        $switched = true;
     227                }
     228
     229                $site_objects = get_objects_in_term( $term_ids, $taxonomies, $args );
     230                $retval       = array_merge( $retval, $site_objects );
     231
     232                if ( $switched ) {
     233                        restore_current_blog();
     234                }
     235        }
     236
     237        return $retval;
     238}
     239
     240/**
     241 * Get term data for terms in BuddyPress taxonomies.
     242 *
     243 * @since 2.7.0
     244 *
     245 * @see get_term_by() for a full description of function and parameters.
     246 *
     247 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
     248 * @param string|int $value    Search for this term value
     249 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
     250 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
     251 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
     252 *
     253 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
     254 *                      or `$term` was not found.
     255 */
     256function bp_get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
     257        $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
     258
     259        $switched = false;
     260        if ( $site_id !== get_current_blog_id() ) {
     261                switch_to_blog( $site_id );
     262                bp_register_taxonomies();
     263                $switched = true;
     264        }
     265
     266        $term = get_term_by( $field, $value, $taxonomy, $output, $filter );
     267
     268        if ( $switched ) {
     269                restore_current_blog();
     270        }
     271
     272        return $term;
     273}
     274 No newline at end of file