Make WordPress Core


Ignore:
Timestamp:
05/31/2007 09:38:33 PM (18 years ago)
Author:
ryan
Message:

Abstract object term cache a bit more. see #4189

File:
1 edited

Legend:

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

    r5613 r5616  
    400400}
    401401
     402function get_object_taxonomies($object_type) {
     403    global $wp_taxonomies;
     404
     405    $taxonomies = array();
     406    foreach ( $wp_taxonomies as $taxonomy ) {
     407        if ( $object_type == $taxonomy->object_type )
     408            $taxonomies[] = $taxonomy->name;
     409    }
     410
     411    return $taxonomies;
     412}
     413
    402414/**
    403415 * Returns the terms associated with the given object(s), in the supplied taxonomies.
     
    408420function get_object_terms($object_ids, $taxonomies, $args = array()) {
    409421    global $wpdb;
    410     error_log("Objects: " . var_export($object_ids, true), 0);
     422
    411423    if ( !is_array($taxonomies) )
    412424        $taxonomies = array($taxonomies);
     
    716728}
    717729
     730function clean_object_term_cache($object_ids, $object_type) {
     731    global $object_term_cache, $blog_id;
     732
     733    if ( !is_array($ids) )
     734        $ids = array($ids);
     735
     736    $taxonomies = get_object_taxonomies($object_type);
     737
     738    foreach ( $ids as $id ) {
     739        foreach ( $taxonomies as $taxonomy ) {
     740            if ( isset($object_term_cache[$blog_id][$id][$taxonomy]) )
     741                unset($object_term_cache[$blog_id][$id][$taxonomy]);
     742        }
     743    }
     744}
     745
     746function &get_object_term_cache($id, $taxonomy) {
     747    global $object_term_cache, $blog_id;
     748
     749    if ( isset($object_term_cache[$blog_id][$id][$taxonomy]) )
     750        return $object_term_cache[$blog_id][$id][$taxonomy];
     751
     752    if ( isset($object_term_cache[$blog_id][$id]) )
     753        return array();
     754
     755    return false;
     756}
     757
     758function update_object_term_cache($object_ids, $object_type) {
     759    global $wpdb, $object_term_cache, $blog_id;
     760
     761    if ( empty($object_ids) )
     762        return;
     763
     764    if ( !is_array($object_ids) )
     765        $object_ids = explode(',', $object_ids);
     766
     767    $count = count( $object_ids);
     768    for ( $i = 0; $i < $count; $i++ ) {
     769        $object_id = (int) $object_ids[ $i ];
     770        if ( isset( $object_term_cache[$blog_id][$object_id] ) ) {
     771            unset( $object_ids[ $i ] );
     772            continue;
     773        }
     774    }
     775
     776    if ( count( $object_ids ) == 0 )
     777        return;
     778
     779    $terms = get_object_terms($object_ids, get_object_taxonomies($object_type), 'fields=all_with_object_id');
     780
     781    if ( empty($terms) )
     782        return;
     783
     784    foreach ( $terms as $term )
     785        $object_term_cache[$blog_id][$term->object_id][$term->taxonomy][$term->term_id] = $term;
     786
     787    foreach ( $object_ids as $id ) {
     788        if ( ! isset($object_term_cache[$blog_id][$id]) )
     789                $object_term_cache[$blog_id][$id] = array();
     790    }
     791}
     792
    718793function _get_term_hierarchy($taxonomy) {
    719794    // TODO Make sure taxonomy is hierarchical
Note: See TracChangeset for help on using the changeset viewer.