[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/ -> bp-core-taxonomy.php (source)

   1  <?php
   2  /**
   3   * BuddyPress taxonomy functions.
   4   *
   5   * Most BuddyPress taxonomy functions are wrappers for their WordPress counterparts.
   6   * Because BuddyPress can be activated in various ways in a network environment, we
   7   * must switch to the root blog before using the WP functions.
   8   *
   9   * @package BuddyPress
  10   * @subpackage Core
  11   * @since 2.2.0
  12   */
  13  
  14  // Exit if accessed directly.
  15  defined( 'ABSPATH' ) || exit;
  16  
  17  /**
  18   * Register our default taxonomies.
  19   *
  20   * @since 2.2.0
  21   */
  22  function bp_register_default_taxonomies() {
  23      // Member Type.
  24      register_taxonomy( bp_get_member_type_tax_name(), 'user', array(
  25          'public' => false,
  26      ) );
  27  
  28      // Email type.
  29      register_taxonomy(
  30          bp_get_email_tax_type(),
  31          bp_get_email_post_type(),
  32          apply_filters( 'bp_register_email_tax_type', array(
  33              'description'   => _x( 'BuddyPress email types', 'email type taxonomy description', 'buddypress' ),
  34              'labels'        => bp_get_email_tax_type_labels(),
  35              'meta_box_cb'   => 'bp_email_tax_type_metabox',
  36              'public'        => false,
  37              'query_var'     => false,
  38              'rewrite'       => false,
  39              'show_in_menu'  => false,
  40              'show_tagcloud' => false,
  41              'show_ui'       => bp_is_root_blog() && bp_current_user_can( 'bp_moderate' ),
  42          ) )
  43      );
  44  }
  45  add_action( 'bp_register_taxonomies', 'bp_register_default_taxonomies' );
  46  
  47  /**
  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   * @param string $taxonomy Taxonomy slug to check for.
  55   * @return int
  56   */
  57  function bp_get_taxonomy_term_site_id( $taxonomy = '' ) {
  58      $site_id = bp_get_root_blog_id();
  59  
  60      /**
  61       * Filters the ID of the site where BP should store taxonomy terms.
  62       *
  63       * @since 2.6.0
  64       *
  65       * @param int    $site_id  Site ID to cehck for.
  66       * @param string $taxonomy Taxonomy slug to check for.
  67       */
  68      return (int) apply_filters( 'bp_get_taxonomy_term_site_id', $site_id, $taxonomy );
  69  }
  70  
  71  /**
  72   * Set taxonomy terms on a BuddyPress object.
  73   *
  74   * @since 2.2.0
  75   *
  76   * @see wp_set_object_terms() for a full description of function and parameters.
  77   *
  78   * @param int          $object_id Object ID.
  79   * @param string|array $terms     Term or terms to set.
  80   * @param string       $taxonomy  Taxonomy name.
  81   * @param bool         $append    Optional. True to append terms to existing terms. Default: false.
  82   * @return array Array of term taxonomy IDs.
  83   */
  84  function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
  85      $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  86  
  87      $switched = false;
  88      if ( $site_id !== get_current_blog_id() ) {
  89          switch_to_blog( $site_id );
  90          bp_register_taxonomies();
  91          $switched = true;
  92      }
  93  
  94      $tt_ids = wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
  95  
  96      if ( $switched ) {
  97          restore_current_blog();
  98      }
  99  
 100      /**
 101       * Fires when taxonomy terms have been set on BuddyPress objects.
 102       *
 103       * @since 2.7.0
 104       *
 105       * @param int    $object_id Object ID.
 106       * @param array  $terms     Term or terms to remove.
 107       * @param array  $tt_ids    Array of term taxonomy IDs.
 108       * @param string $taxonomy  Taxonomy name.
 109       */
 110      do_action( 'bp_set_object_terms', $object_id, $terms, $tt_ids, $taxonomy );
 111  
 112      return $tt_ids;
 113  }
 114  
 115  /**
 116   * Get taxonomy terms for a BuddyPress object.
 117   *
 118   * @since 2.2.0
 119   *
 120   * @see wp_get_object_terms() for a full description of function and parameters.
 121   *
 122   * @param int|array    $object_ids ID or IDs of objects.
 123   * @param string|array $taxonomies Name or names of taxonomies to match.
 124   * @param array        $args       See {@see wp_get_object_terms()}.
 125   * @return array
 126   */
 127  function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
 128      // Different taxonomies must be stored on different sites.
 129      $taxonomy_site_map = array();
 130      foreach ( (array) $taxonomies as $taxonomy ) {
 131          $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
 132          $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
 133      }
 134  
 135      $retval = array();
 136      foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
 137          $switched = false;
 138          if ( $taxonomy_site_id !== get_current_blog_id() ) {
 139              switch_to_blog( $taxonomy_site_id );
 140              bp_register_taxonomies();
 141              $switched = true;
 142          }
 143  
 144          $site_terms = wp_get_object_terms( $object_ids, $site_taxonomies, $args );
 145          $retval     = array_merge( $retval, $site_terms );
 146  
 147          if ( $switched ) {
 148              restore_current_blog();
 149          }
 150      }
 151  
 152      return $retval;
 153  }
 154  
 155  /**
 156   * Remove taxonomy terms on a BuddyPress object.
 157   *
 158   * @since 2.3.0
 159   *
 160   * @see wp_remove_object_terms() for a full description of function and parameters.
 161   *
 162   * @param int          $object_id Object ID.
 163   * @param string|array $terms     Term or terms to remove.
 164   * @param string       $taxonomy  Taxonomy name.
 165   * @return bool|WP_Error True on success, false or WP_Error on failure.
 166   */
 167  function bp_remove_object_terms( $object_id, $terms, $taxonomy ) {
 168      $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
 169  
 170      $switched = false;
 171      if ( $site_id !== get_current_blog_id() ) {
 172          switch_to_blog( $site_id );
 173          bp_register_taxonomies();
 174          $switched = true;
 175      }
 176  
 177      $retval = wp_remove_object_terms( $object_id, $terms, $taxonomy );
 178  
 179      if ( $switched ) {
 180          restore_current_blog();
 181      }
 182  
 183      /**
 184       * Fires when taxonomy terms have been removed from BuddyPress objects.
 185       *
 186       * @since 2.7.0
 187       *
 188       * @param int    $object_id Object ID.
 189       * @param array  $terms     Term or terms to remove.
 190       * @param string $taxonomy  Taxonomy name.
 191       */
 192      do_action( 'bp_remove_object_terms', $object_id, $terms, $taxonomy );
 193  
 194      return $retval;
 195  }
 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   */
 215  function 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  
 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   */
 262  function 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  }


Generated: Wed Jul 17 01:01:40 2019 Cross-referenced by PHPXref 0.7.1