Esempio n. 1
0
/**
 * Pre-fetch member type data when initializing a Members loop.
 *
 * @since BuddyPress (2.2.0)
 *
 * @param BP_User_Query $bp_user_query BP_User_Query object.
 */
function bp_members_prefetch_member_type( BP_User_Query $bp_user_query ) {
	$uncached_member_ids = bp_get_non_cached_ids( $bp_user_query->user_ids, 'bp_member_type' );

	$member_types = bp_get_object_terms( $uncached_member_ids, 'bp_member_type', array(
		'fields' => 'all_with_object_id',
	) );

	// Rekey by user ID.
	$keyed_member_types = array();
	foreach ( $member_types as $member_type ) {
		if ( ! isset( $keyed_member_types[ $member_type->object_id ] ) ) {
			$keyed_member_types[ $member_type->object_id ] = array();
		}

		$keyed_member_types[ $member_type->object_id ][] = $member_type->name;
	}

	$cached_member_ids = array();
	foreach ( $keyed_member_types as $user_id => $user_member_types ) {
		wp_cache_set( $user_id, $user_member_types, 'bp_member_type' );
		$cached_member_ids[] = $user_id;
	}

	// Cache an empty value for users with no type.
	foreach ( array_diff( $uncached_member_ids, $cached_member_ids ) as $no_type_id ) {
		wp_cache_set( $no_type_id, '', 'bp_member_type' );
	}
}
/**
 * Get type for a member.
 *
 * @since 2.2.0
 *
 * @param int  $user_id ID of the user.
 * @param bool $single  Optional. Whether to return a single type string. If multiple types are found
 *                      for the user, the oldest one will be returned. Default: true.
 * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member
 *                           types (if $single is false). Returns false on failure.
 */
function bp_get_member_type($user_id, $single = true)
{
    $types = wp_cache_get($user_id, 'bp_member_member_type');
    if (false === $types) {
        $raw_types = bp_get_object_terms($user_id, bp_get_member_type_tax_name());
        if (!is_wp_error($raw_types)) {
            $types = array();
            // Only include currently registered group types.
            foreach ($raw_types as $mtype) {
                if (bp_get_member_type_object($mtype->name)) {
                    $types[] = $mtype->name;
                }
            }
            wp_cache_set($user_id, $types, 'bp_member_member_type');
        }
    }
    $type = false;
    if (!empty($types)) {
        if ($single) {
            $type = array_pop($types);
        } else {
            $type = $types;
        }
    }
    /**
     * Filters a user's member type(s).
     *
     * @since 2.2.0
     *
     * @param string $type    Member type.
     * @param int    $user_id ID of the user.
     * @param bool   $single  Whether to return a single type string, or an array.
     */
    return apply_filters('bp_get_member_type', $type, $user_id, $single);
}
/**
 * Get type for a group.
 *
 * @since 2.6.0
 *
 * @param int  $group_id ID of the group.
 * @param bool $single   Optional. Whether to return a single type string. If multiple types are found
 *                       for the group, the oldest one will be returned. Default: true.
 * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group
 *                           types (if `$single` is false). Returns false on failure.
 */
function bp_groups_get_group_type($group_id, $single = true)
{
    $types = wp_cache_get($group_id, 'bp_groups_group_type');
    if (false === $types) {
        $raw_types = bp_get_object_terms($group_id, 'bp_group_type');
        if (!is_wp_error($raw_types)) {
            $types = array();
            // Only include currently registered group types.
            foreach ($raw_types as $gtype) {
                if (bp_groups_get_group_type_object($gtype->name)) {
                    $types[] = $gtype->name;
                }
            }
            wp_cache_set($group_id, $types, 'bp_groups_group_type');
        }
    }
    $type = false;
    if (!empty($types)) {
        if ($single) {
            $type = end($types);
        } else {
            $type = $types;
        }
    }
    /**
     * Filters a groups's group type(s).
     *
     * @since 2.6.0
     *
     * @param string|array $type     Group type.
     * @param int          $group_id ID of the group.
     * @param bool         $single   Whether to return a single type string, or an array.
     */
    return apply_filters('bp_groups_get_group_type', $type, $group_id, $single);
}
/**
 * Get type for a member.
 *
 * @since 2.2.0
 *
 * @param int  $user_id ID of the user.
 * @param bool $single  Optional. Whether to return a single type string. If multiple types are found
 *                      for the user, the oldest one will be returned. Default: true.
 * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member
 *                           types (if $single is false). Returns false on failure.
 */
function bp_get_member_type($user_id, $single = true)
{
    $types = wp_cache_get($user_id, 'bp_member_member_type');
    if (false === $types) {
        $types = bp_get_object_terms($user_id, 'bp_member_type');
        if (!is_wp_error($types)) {
            $types = wp_list_pluck($types, 'name');
            wp_cache_set($user_id, $types, 'bp_member_member_type');
        }
    }
    $type = false;
    if (!empty($types)) {
        if ($single) {
            $type = array_pop($types);
        } else {
            $type = $types;
        }
    }
    /**
     * Filters a user's member type(s).
     *
     * @since 2.2.0
     *
     * @param string $type    Member type.
     * @param int    $user_id ID of the user.
     * @param bool   $single  Whether to return a single type string, or an array.
     */
    return apply_filters('bp_get_member_type', $type, $user_id, $single);
}
/**
 * Get type for a rendez-vous.
 *
 * @package Rendez Vous
 * @subpackage Functions
 *
 * @since Rendez Vous (1.2.0)
 *
 * @param  int $rendez_vous_id     ID of the rendez-vous.
 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.
 */
function rendez_vous_get_type($rendez_vous_id)
{
    $types = wp_cache_get($rendez_vous_id, 'rendez_vous_type');
    if (false === $types) {
        $types = bp_get_object_terms($rendez_vous_id, 'rendez_vous_type');
        if (!is_wp_error($types)) {
            wp_cache_set($rendez_vous_id, $types, 'rendez_vous_type');
        }
    }
    return apply_filters('rendez_vous_get_type', $types, $rendez_vous_id);
}