diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index be05562f7..8a56dbb8e 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -66,7 +66,7 @@ function get_the_category($id = false) { if ( !$id ) $id = (int) $post->ID; - $categories = get_post_term_cache($id, 'category'); + $categories = get_object_term_cache($id, 'category'); if ( false === $categories ) $categories = get_object_terms($id, 'category'); @@ -418,7 +418,7 @@ function get_the_tags( $id = 0 ) { if ( !$id ) $id = (int) $post->ID; - $tags = get_post_term_cache($id, 'post_tag'); + $tags = get_object_term_cache($id, 'post_tag'); if ( false === $tags ) $tags = get_object_terms($id, 'post_tag'); diff --git a/wp-includes/post.php b/wp-includes/post.php index 98283cf79..d123f8de8 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -1664,8 +1664,7 @@ function clean_post_cache($id) { if ( isset ($post_meta_cache[$blog_id][$id] ) ) unset( $post_meta_cache[$blog_id][$id] ); - if ( isset( $post_term_cache[$blog_id][$id]) ) - unset ( $post_term_cache[$blog_id][$id] ); + clean_object_term_cache($id, 'post'); } function update_page_cache(&$pages) { @@ -1691,53 +1690,6 @@ function clean_page_cache($id) { wp_cache_delete( 'get_pages', 'page' ); } -function &get_post_term_cache($id, $taxonomy) { - global $post_term_cache, $blog_id; - - if ( isset($post_term_cache[$blog_id][$id][$taxonomy]) ) - return $post_term_cache[$blog_id][$id][$taxonomy]; - - return false; -} - -// TODO abstract this to work for any object. -function update_post_term_cache($post_ids) { - global $wpdb, $post_term_cache, $blog_id; - - if ( empty($post_ids) ) - return; - - if ( is_array($post_ids) ) - $post_id_list = implode(',', $post_ids); - - $post_id_array = (array) explode(',', $post_ids); - $count = count( $post_id_array); - for ( $i = 0; $i < $count; $i++ ) { - $post_id = (int) $post_id_array[ $i ]; - if ( isset( $post_term_cache[$blog_id][$post_id] ) ) { - unset( $post_id_array[ $i ] ); - continue; - } - } - if ( count( $post_id_array ) == 0 ) - return; - - $terms = get_object_terms($post_id_array, array('category', 'post_tag'), 'fields=all_with_object_id'); - - if ( empty($terms) ) - return; - - foreach ( $terms as $term ) - $post_term_cache[$blog_id][$term->object_id][$term->taxonomy][$term->term_id] = $term; - - foreach ( $post_id_array as $id ) { - if ( ! isset($post_term_cache[$blog_id][$id]) ) { - $post_term_cache[$blog_id][$id]['category'] = array(); - $post_term_cache[$blog_id][$id]['post_tag'] = array(); - } - } -} - function update_post_caches(&$posts) { global $post_cache; global $wpdb, $blog_id; @@ -1754,7 +1706,7 @@ function update_post_caches(&$posts) { $post_id_list = implode(',', $post_id_array); - update_post_term_cache($post_id_list); + update_object_term_cache($post_id_list, 'post'); update_postmeta_cache($post_id_list); } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index ec69579af..03b334544 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -399,6 +399,18 @@ function get_objects_in_term( $terms, $taxonomies, $args = array() ) { return $object_ids; } +function get_object_taxonomies($object_type) { + global $wp_taxonomies; + + $taxonomies = array(); + foreach ( $wp_taxonomies as $taxonomy ) { + if ( $object_type == $taxonomy->object_type ) + $taxonomies[] = $taxonomy->name; + } + + return $taxonomies; +} + /** * Returns the terms associated with the given object(s), in the supplied taxonomies. * @param int|array $object_id The id of the object(s)) to retrieve for. @@ -407,7 +419,7 @@ function get_objects_in_term( $terms, $taxonomies, $args = array() ) { */ function get_object_terms($object_ids, $taxonomies, $args = array()) { global $wpdb; - error_log("Objects: " . var_export($object_ids, true), 0); + if ( !is_array($taxonomies) ) $taxonomies = array($taxonomies); @@ -715,6 +727,69 @@ function clean_term_cache($ids, $taxonomy) { wp_cache_delete('get_terms', 'terms'); } +function clean_object_term_cache($object_ids, $object_type) { + global $object_term_cache, $blog_id; + + if ( !is_array($ids) ) + $ids = array($ids); + + $taxonomies = get_object_taxonomies($object_type); + + foreach ( $ids as $id ) { + foreach ( $taxonomies as $taxonomy ) { + if ( isset($object_term_cache[$blog_id][$id][$taxonomy]) ) + unset($object_term_cache[$blog_id][$id][$taxonomy]); + } + } +} + +function &get_object_term_cache($id, $taxonomy) { + global $object_term_cache, $blog_id; + + if ( isset($object_term_cache[$blog_id][$id][$taxonomy]) ) + return $object_term_cache[$blog_id][$id][$taxonomy]; + + if ( isset($object_term_cache[$blog_id][$id]) ) + return array(); + + return false; +} + +function update_object_term_cache($object_ids, $object_type) { + global $wpdb, $object_term_cache, $blog_id; + + if ( empty($object_ids) ) + return; + + if ( !is_array($object_ids) ) + $object_ids = explode(',', $object_ids); + + $count = count( $object_ids); + for ( $i = 0; $i < $count; $i++ ) { + $object_id = (int) $object_ids[ $i ]; + if ( isset( $object_term_cache[$blog_id][$object_id] ) ) { + unset( $object_ids[ $i ] ); + continue; + } + } + + if ( count( $object_ids ) == 0 ) + return; + + $terms = get_object_terms($object_ids, get_object_taxonomies($object_type), 'fields=all_with_object_id'); + + if ( empty($terms) ) + return; + + foreach ( $terms as $term ) + $object_term_cache[$blog_id][$term->object_id][$term->taxonomy][$term->term_id] = $term; + + foreach ( $object_ids as $id ) { + if ( ! isset($object_term_cache[$blog_id][$id]) ) + $object_term_cache[$blog_id][$id] = array(); + } +} + function _get_term_hierarchy($taxonomy) { // TODO Make sure taxonomy is hierarchical $children = get_option("{$taxonomy}_children");