Skip to:
Content

BuddyPress.org

Ticket #7077: 7077.patch

File 7077.patch, 5.7 KB (added by rekmla, 9 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 bd544b4..61c29c5 100644
    add_action( 'bp_register_taxonomies', 'bp_register_default_taxonomies' ); 
    5050 * @since 2.2.0
    5151 *
    5252 * @see wp_set_object_terms() for a full description of function and parameters.
     53 * @uses apply_filters() Calls 'bp_taxonomy_scope.
    5354 *
    5455 * @param int          $object_id Object ID.
    5556 * @param string|array $terms     Term or terms to set.
    5657 * @param string       $taxonomy  Taxonomy name.
    5758 * @param bool         $append    Optional. True to append terms to existing terms. Default: false.
     59 * @param string       $scope     Local, Network or Global
    5860 * @return array Array of term taxonomy IDs.
    5961 */
    60 function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
    61         $is_root_blog = bp_is_root_blog();
     62function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false, $scope = 'network' ) {
     63        $starting_blog_id =  get_current_blog_id();
     64
     65        $scope = apply_filters( "bp_taxonomy_${taxonomy}_scope", $scope );
     66
     67        switch ( $scope ) {
     68                case 'local':
     69                        $target_blog_id = $starting_blog_id;
     70                break;
     71                case 'network':
     72                        $target_blog_id = bp_get_root_blog_id();
     73                break;
     74                case 'global':
     75                        $main_network = wp_get_network( get_main_network_id() );
     76                        $target_blog_id = get_site_by_path( $main_network->domain, $main_network->path );
     77                break;
     78        }
    6279
    63         if ( ! $is_root_blog ) {
    64                 switch_to_blog( bp_get_root_blog_id() );
     80        if ( $starting_blog_id !== $target_blog_id ) {
     81                switch_to_blog( $target_blog_id );
    6582                bp_register_taxonomies();
    6683        }
    67 
     84 
    6885        $retval = wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
    69 
    70         if ( ! $is_root_blog ) {
     86 
     87        if ( $starting_blog_id !== $target_blog_id ) {
    7188                restore_current_blog();
    7289        }
    7390
    function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 
    8097 * @since 2.2.0
    8198 *
    8299 * @see wp_get_object_terms() for a full description of function and parameters.
     100 * @uses apply_filters() Calls 'bp_taxonomy_scope.
    83101 *
    84102 * @param int|array    $object_ids ID or IDs of objects.
    85103 * @param string|array $taxonomies Name or names of taxonomies to match.
    86104 * @param array        $args       See {@see wp_get_object_terms()}.
     105 * @param string       $scope     Local, Network or Global
    87106 * @return array
    88107 */
    89 function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
    90         $is_root_blog = bp_is_root_blog();
     108function bp_get_object_terms( $object_ids, $taxonomies, $args = array(), $scope = 'network' ) {
     109        $starting_blog_id =  get_current_blog_id();
    91110
    92         if ( ! $is_root_blog ) {
    93                 switch_to_blog( bp_get_root_blog_id() );
    94                 bp_register_taxonomies();
     111        if ( ! is_array( $taxonomies ) ) { // If we want to handle an array here then all results should match otherwise ignore the results.
     112                $scope = apply_filters( "bp_taxonomy_${taxonomies}_scope", $scope );
    95113        }
    96114
     115        switch ( $scope ) {
     116                case 'local':
     117                        $target_blog_id = $starting_blog_id;
     118                break;
     119                case 'network':
     120                        $target_blog_id = bp_get_root_blog_id();
     121                break;
     122                case 'global':
     123                        $main_network = wp_get_network( get_main_network_id() );
     124                        $target_blog_id = get_site_by_path( $main_network->domain, $main_network->path );
     125                break;
     126        }
     127
     128        if ( $starting_blog_id !== $target_blog_id ) {
     129                switch_to_blog( $target_blog_id );
     130                bp_register_taxonomies();
     131        }
     132
    97133        $retval = wp_get_object_terms( $object_ids, $taxonomies, $args );
    98134
    99         if ( ! $is_root_blog ) {
    100                 restore_current_blog();
    101         }
     135        if ( $starting_blog_id !== $target_blog_id ) {
     136                restore_current_blog();
     137        }
    102138
    103139        return $retval;
    104140}
    function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 
    109145 * @since 2.3.0
    110146 *
    111147 * @see wp_remove_object_terms() for a full description of function and parameters.
     148 * @uses apply_filters() Calls 'bp_taxonomy_scope.
    112149 *
    113150 * @param int          $object_id Object ID.
    114151 * @param string|array $terms     Term or terms to remove.
    115152 * @param string       $taxonomy  Taxonomy name.
     153 * @param string       $scope     Local, Network or Global
    116154 * @return bool|WP_Error True on success, false or WP_Error on failure.
    117155 */
    118 function bp_remove_object_terms( $object_id, $terms, $taxonomy ) {
    119         $is_root_blog = bp_is_root_blog();
    120 
    121         if ( ! $is_root_blog ) {
    122                 switch_to_blog( bp_get_root_blog_id() );
    123                 bp_register_taxonomies();
    124         }
     156function bp_remove_object_terms( $object_id, $terms, $taxonomy, $scope = 'network' ) {
     157        $starting_blog_id =  get_current_blog_id();
     158
     159        $scope = apply_filters( "bp_taxonomy_${taxonomy}_scope", $scope );
     160
     161        switch ( $scope ) {
     162                case 'local':
     163                        $target_blog_id = $starting_blog_id;
     164                break;
     165                case 'network':
     166                        $target_blog_id = bp_get_root_blog_id();
     167                break;
     168                case 'global':
     169                        $main_network = wp_get_network( get_main_network_id() );
     170                        $target_blog_id = get_site_by_path( $main_network->domain, $main_network->path );
     171                break;
     172        }
     173
     174        if ( $starting_blog_id !== $target_blog_id ) {
     175                switch_to_blog( $target_blog_id );
     176                bp_register_taxonomies();
     177        }
    125178
    126179        $retval = wp_remove_object_terms( $object_id, $terms, $taxonomy );
    127180
    128         if ( ! $is_root_blog ) {
    129                 restore_current_blog();
    130         }
     181        if ( $starting_blog_id !== $target_blog_id ) {
     182                restore_current_blog();
     183        }
    131184
    132185        return $retval;
    133186}