/**
  * Echos the current shopping cart link. If global cart is on reflects global location
  * @param bool $echo Optional, whether to echo. Defaults to true
  * @param bool $url Optional, whether to return a link or url. Defaults to show link.
  * @param string $link_text Optional, text to show in link.
  */
 function mp_cart_link($echo = true, $url = false, $link_text = '')
 {
     global $mp, $mp_wpmu;
     if ($mp->global_cart && is_object($mp_wpmu) && !$mp_wpmu->is_main_site() && function_exists('mp_main_site_id')) {
         switch_to_blog(mp_main_site_id());
         $link = home_url($mp->get_setting('slugs->store') . '/' . $mp->get_setting('slugs->cart') . '/');
         restore_current_blog();
     } else {
         $link = home_url($mp->get_setting('slugs->store') . '/' . $mp->get_setting('slugs->cart') . '/');
     }
     if (!$url) {
         $text = $link_text ? $link_text : __('Shopping Cart', 'mp');
         $link = '<a href="' . $link . '" class="mp_cart_link">' . $text . '</a>';
     }
     $link = apply_filters('mp_cart_link', $link, $echo, $url, $link_text);
     if ($echo) {
         echo $link;
     } else {
         return $link;
     }
 }
/**
 * Display Global Products tag cloud.
 *
 * @param bool $echo Optional. Whether or not to echo.
 * @param int $limit Optional. How many tags to display.
 * @param string $seperator Optional. String to seperate tags by.
 * @param string $include Optional. What to show, 'tags', 'categories', or 'both'.
 */
function mp_global_tag_cloud($echo = true, $limit = 45, $seperator = ' ', $include = 'both')
{
    global $wpdb;
    $settings = get_site_option('mp_network_settings');
    //include categories as well
    if ($include == 'tags') {
        $where = " WHERE t.type = 'product_tag'";
    } else {
        if ($include == 'categories') {
            $where = " WHERE t.type = 'product_category'";
        }
    }
    $limit = intval($limit);
    $tags = $wpdb->get_results("SELECT name, slug, type, count(post_id) as count FROM {$wpdb->base_prefix}mp_terms t LEFT JOIN {$wpdb->base_prefix}mp_term_relationships r ON t.term_id = r.term_id{$where} GROUP BY t.term_id ORDER BY count DESC LIMIT {$limit}", ARRAY_A);
    if (!$tags) {
        return;
    }
    $tags = apply_filters('mp_global_tag_cloud_tags', $tags);
    //sort by name
    foreach ($tags as $tag) {
        //skip empty tags
        if ($tag['count'] == 0) {
            continue;
        }
        if ($tag['type'] == 'product_category') {
            $tag['link'] = get_home_url(mp_main_site_id(), $settings['slugs']['marketplace'] . '/' . $settings['slugs']['categories'] . '/' . $tag['slug'] . '/');
        } else {
            if ($tag['type'] == 'product_tag') {
                $tag['link'] = get_home_url(mp_main_site_id(), $settings['slugs']['marketplace'] . '/' . $settings['slugs']['tags'] . '/' . $tag['slug'] . '/');
            }
        }
        $sorted_tags[$tag['name']] = $tag;
    }
    ksort($sorted_tags);
    //remove keys
    $tags = array();
    foreach ($sorted_tags as $tag) {
        $tags[] = $tag;
    }
    $counts = array();
    $real_counts = array();
    // For the alt tag
    foreach ((array) $tags as $key => $tag) {
        $real_counts[$key] = $tag['count'];
        $counts[$key] = $tag['count'];
    }
    $min_count = min($counts);
    $spread = max($counts) - $min_count;
    if ($spread <= 0) {
        $spread = 1;
    }
    $font_spread = 22 - 8;
    if ($font_spread < 0) {
        $font_spread = 1;
    }
    $font_step = $font_spread / $spread;
    $a = array();
    foreach ($tags as $key => $tag) {
        $count = $counts[$key];
        $real_count = $real_counts[$key];
        $tag_link = '#' != $tag['link'] ? esc_url($tag['link']) : '#';
        $tag_id = isset($tags[$key]['id']) ? $tags[$key]['id'] : $key;
        $tag_name = $tags[$key]['name'];
        $a[] = "<a href='{$tag_link}' class='tag-link-{$tag_id}' title='" . esc_attr($real_count) . ' ' . __('Products', 'mp') . "' style='font-size: " . (8 + ($count - $min_count) * $font_step) . "pt;'>{$tag_name}</a>";
    }
    $return = join($seperator, $a);
    if ($echo) {
        echo apply_filters('mp_global_tag_cloud', '<div id="mp_tag_cloud">' . $return . '</div>');
    }
    return apply_filters('mp_global_tag_cloud', '<div id="mp_tag_cloud">' . $return . '</div>');
}