Make WordPress Core

Changeset 5232


Ignore:
Timestamp:
04/10/2007 06:55:51 PM (18 years ago)
Author:
ryan
Message:

Add get_tags(). See #3723

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category.php

    r5202 r5232  
    345345    return $children;
    346346}
     347
     348// Tags
     349
     350function &get_tags($args = '') {
     351    global $wpdb, $category_links;
     352
     353    if ( is_array($args) )
     354        $r = &$args;
     355    else
     356        parse_str($args, $r);
     357
     358    $defaults = array('orderby' => 'name', 'order' => 'ASC',
     359        'hide_empty' => true, 'exclude' => '', 'include' => '',
     360        'number' => '');
     361    $r = array_merge($defaults, $r);
     362    if ( 'count' == $r['orderby'] )
     363        $r['orderby'] = 'category_count';
     364    else
     365        $r['orderby'] = "cat_" . $r['orderby'];  // restricts order by to cat_ID and cat_name fields
     366    $r['number'] = (int) $r['number'];
     367    extract($r);
     368
     369    $key = md5( serialize( $r ) );
     370    if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
     371        if ( isset( $cache[ $key ] ) )
     372            return apply_filters('get_tags', $cache[$key], $r);
     373
     374    $where = 'cat_ID > 0';
     375    $inclusions = '';
     376    if ( !empty($include) ) {
     377        $child_of = 0; //ignore child_of and exclude params if using include
     378        $exclude = '';
     379        $incategories = preg_split('/[\s,]+/',$include);
     380        if ( count($incategories) ) {
     381            foreach ( $incategories as $incat ) {
     382                if (empty($inclusions))
     383                    $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
     384                else
     385                    $inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
     386            }
     387        }
     388    }
     389
     390    if (!empty($inclusions))
     391        $inclusions .= ')';
     392    $where .= $inclusions;
     393
     394    $exclusions = '';
     395    if ( !empty($exclude) ) {
     396        $excategories = preg_split('/[\s,]+/',$exclude);
     397        if ( count($excategories) ) {
     398            foreach ( $excategories as $excat ) {
     399                if (empty($exclusions))
     400                    $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
     401                else
     402                    $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
     403            }
     404        }
     405    }
     406
     407    if (!empty($exclusions))
     408        $exclusions .= ')';
     409    $exclusions = apply_filters('list_tags_exclusions', $exclusions, $r );
     410    $where .= $exclusions;
     411
     412    if ( $hide_empty )
     413        $where .= ' AND tag_count > 0';
     414
     415    $where .= ' AND ( type & ' . TAXONOMY_TAG . ' != 0 ) ';
     416
     417    if ( !empty($number) )
     418        $number = 'LIMIT ' . $number;
     419    else
     420        $number = '';
     421
     422    $tags = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
     423
     424    if ( empty($tags) )
     425        return array();
     426
     427    $cache[ $key ] = $tags;
     428    wp_cache_set( 'get_tags', $cache, 'category' );
     429
     430    $tags = apply_filters('get_tags', $tags, $r);
     431    return $tags;
     432}
     433
    347434?>
Note: See TracChangeset for help on using the changeset viewer.