Ticket #11531: term-cache.patch
| File term-cache.patch, 13.0 KB (added by , 14 years ago) |
|---|
-
wp-includes/category.php
14 14 * @return object List of all of the category IDs. 15 15 */ 16 16 function get_all_category_ids() { 17 if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) { 18 $cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') ); 19 wp_cache_add( 'all_category_ids', $cat_ids, 'category' ); 20 } 21 22 return $cat_ids; 17 // this call returns from cache if present 18 return get_terms( 'category', array( 'fields' => 'ids', 'get' => 'all' ) ); 23 19 } 24 20 25 21 /** … … 36 32 * @param string|array $args Optional. Change the defaults retrieving categories. 37 33 * @return array List of categories. 38 34 */ 39 function &get_categories( $args = '' ) {35 function get_categories( $args = '' ) { 40 36 $defaults = array( 'taxonomy' => 'category' ); 41 37 $args = wp_parse_args( $args, $defaults ); 42 38 … … 78 74 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. 79 75 * @return mixed Category data in type defined by $output parameter. 80 76 */ 81 function &get_category( $category, $output = OBJECT, $filter = 'raw' ) {77 function get_category( $category, $output = OBJECT, $filter = 'raw' ) { 82 78 $category = get_term( $category, 'category', $output, $filter ); 83 79 if ( is_wp_error( $category ) ) 84 80 return $category; … … 249 245 * @param string|array $args Tag arguments to use when retrieving tags. 250 246 * @return array List of tags. 251 247 */ 252 function &get_tags( $args = '' ) {248 function get_tags( $args = '' ) { 253 249 $tags = get_terms( 'post_tag', $args ); 254 250 255 251 if ( empty( $tags ) ) { … … 280 276 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. 281 277 * @return object|array Return type based on $output value. 282 278 */ 283 function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {279 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { 284 280 return get_term( $tag, 'post_tag', $output, $filter ); 285 281 } 286 282 -
wp-includes/taxonomy.php
853 853 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. 854 854 * 855 855 * @param int|object $term If integer, will get from database. If object will apply filters and return $term. 856 * @param string $taxonomy Taxonomy name that $term is part of.856 * @param string $taxonomy Optional if object is passed for $term. Taxonomy name that $term is part of. 857 857 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 858 858 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 859 859 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not 860 860 * exist then WP_Error will be returned. 861 861 */ 862 function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {862 function get_term($term, $taxonomy = '', $output = OBJECT, $filter = 'raw') { 863 863 global $wpdb; 864 864 $null = null; 865 865 … … 868 868 return $error; 869 869 } 870 870 871 if ( ! taxonomy_exists($taxonomy) ) { 872 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); 873 return $error; 874 } 871 if ( is_object( $term ) ) 872 $taxonomy = $term->taxonomy; 875 873 874 if ( ! taxonomy_exists( $taxonomy ) ) 875 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 876 876 877 if ( is_object($term) && empty($term->filter) ) { 877 wp_cache_add($term->term_id, $term, $taxonomy);878 update_term_cache( $term, $taxonomy ); 878 879 $_term = $term; 879 880 } else { 880 881 if ( is_object($term) ) 881 882 $term = $term->term_id; 883 882 884 if ( !$term = (int) $term ) 883 885 return $null; 884 if ( ! $_term = wp_cache_get($term, $taxonomy) ) { 886 887 if ( ! $_term = wp_cache_get( $term, term_cache_group( $taxonomy ) ) ) { 885 888 $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) ); 886 889 if ( ! $_term ) 887 890 return $null; 888 wp_cache_add($term, $_term, $taxonomy); 891 892 update_term_cache( $_term, $taxonomy ); 889 893 } 890 894 } 891 895 … … 940 944 return false; 941 945 942 946 if ( 'slug' == $field ) { 943 $field = 't.slug'; 944 $value = sanitize_title($value); 945 if ( empty($value) ) 947 $value = sanitize_title( $value ); 948 if ( empty( $value ) ) 946 949 return false; 950 951 $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) ); 952 $field = 't.slug'; 947 953 } else if ( 'name' == $field ) { 948 954 // Assume already escaped 949 $value = stripslashes($value); 950 $field = 't.name'; 955 $value = stripslashes( $value ); 956 $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) ); 957 $field = 't.name'; 951 958 } else { 952 $term = get_term( (int) $value, $taxonomy, $output, $filter); 959 $term_id = $value; 960 } 961 962 if ( ! empty( $term_id ) ) { 963 $term = get_term( (int) $term_id, $taxonomy, $output, $filter ); 953 964 if ( is_wp_error( $term ) ) 954 965 $term = false; 966 955 967 return $term; 956 968 } 957 969 … … 959 971 if ( !$term ) 960 972 return false; 961 973 962 wp_cache_add($term->term_id, $term, $taxonomy);974 update_term_cache( $term, $taxonomy ); 963 975 964 976 $term = apply_filters('get_term', $term, $taxonomy); 965 977 $term = apply_filters("get_$taxonomy", $term, $taxonomy); … … 1176 1188 * @param string|array $args The values of what to search for when returning terms 1177 1189 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. 1178 1190 */ 1179 function &get_terms($taxonomies, $args = '') {1191 function get_terms($taxonomies, $args = '') { 1180 1192 global $wpdb; 1181 1193 $empty_array = array(); 1182 1194 … … 1232 1244 } 1233 1245 1234 1246 // $args can be whatever, only use the args defined in defaults to compute the key 1235 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; 1236 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key ); 1247 // deprecate 'list_terms_exclusions' - use 'get_terms' filter instead 1248 if ( has_filter( 'list_terms_exclusions' ) ) 1249 _doing_it_wrong( __FUNCTION__, __( '"list_terms_exclusions" is no longer supported. Use the "get_terms" filter instead.' ), '3.5' ); 1250 1251 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) ); 1237 1252 $last_changed = wp_cache_get('last_changed', 'terms'); 1238 1253 if ( !$last_changed ) { 1239 1254 $last_changed = time(); … … 2556 2571 2557 2572 foreach ( $object_ids as $id ) 2558 2573 foreach ( get_object_taxonomies($object_type) as $taxonomy ) 2559 wp_cache_delete( $id, "{$taxonomy}_relationships");2574 wp_cache_delete( $id, term_cache_group( $taxonomy, 'relationships' ) ); 2560 2575 2561 2576 do_action('clean_object_term_cache', $object_ids, $object_type); 2562 2577 } … … 2579 2594 2580 2595 if ( !is_array($ids) ) 2581 2596 $ids = array($ids); 2582 2597 2583 2598 $taxonomies = array(); 2599 2600 $select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 2601 2602 $ids = array_map( 'intval', $ids ); 2584 2603 // If no taxonomy, assume tt_ids. 2585 if ( empty($taxonomy) ) { 2586 $tt_ids = array_map('intval', $ids); 2587 $tt_ids = implode(', ', $tt_ids); 2588 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); 2604 if ( empty( $taxonomy ) ) { 2605 $terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.term_taxonomy_id IN (" . join( ',', $ids ) . ")" ) ); 2589 2606 $ids = array(); 2590 foreach ( (array)$terms as $term ) {2607 foreach ( $terms as $term ) { 2591 2608 $taxonomies[] = $term->taxonomy; 2592 2609 $ids[] = $term->term_id; 2593 wp_cache_delete($term->term_id, $term->taxonomy);2594 2610 } 2595 2611 $taxonomies = array_unique($taxonomies); 2596 2612 } else { 2597 $taxonomies = array($taxonomy); 2598 foreach ( $taxonomies as $taxonomy ) { 2599 foreach ( $ids as $id ) { 2600 wp_cache_delete($id, $taxonomy); 2601 } 2602 } 2613 $terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND tt.term_id IN (" . join( ',', $ids ) . ")", $taxonomy ) ); 2614 $taxonomies = array( $taxonomy ); 2603 2615 } 2616 2617 foreach ( $terms as $term ) { 2618 wp_cache_delete( $term->term_id, term_cache_group( $term->taxonomy ) ); 2619 wp_cache_delete( $term->name, term_cache_group( $term->taxonomy, 'names' ) ); 2620 wp_cache_delete( $term->slug, term_cache_group( $term->taxonomy, 'slugs' ) ); 2621 } 2604 2622 2605 2623 foreach ( $taxonomies as $taxonomy ) { 2606 2624 if ( isset($cleaned[$taxonomy]) ) … … 2608 2626 $cleaned[$taxonomy] = true; 2609 2627 2610 2628 if ( $clean_taxonomy ) { 2611 wp_cache_delete('all_ids', $taxonomy);2612 wp_cache_delete('get', $taxonomy);2613 2629 delete_option("{$taxonomy}_children"); 2614 2630 // Regenerate {$taxonomy}_children 2615 2631 _get_term_hierarchy($taxonomy); … … 2634 2650 * @param string $taxonomy Taxonomy Name 2635 2651 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. 2636 2652 */ 2637 function &get_object_term_cache($id, $taxonomy) {2638 $cache = wp_cache_get( $id, "{$taxonomy}_relationships");2653 function get_object_term_cache($id, $taxonomy) { 2654 $cache = wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ); 2639 2655 return $cache; 2640 2656 } 2641 2657 … … 2675 2691 $ids = array(); 2676 2692 foreach ( (array) $object_ids as $id ) { 2677 2693 foreach ( $taxonomies as $taxonomy ) { 2678 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships") ) {2694 if ( false === wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ) ) { 2679 2695 $ids[] = $id; 2680 2696 break; 2681 2697 } … … 2703 2719 2704 2720 foreach ( $object_terms as $id => $value ) { 2705 2721 foreach ( $value as $taxonomy => $terms ) { 2706 wp_cache_add( $id, $terms, "{$taxonomy}_relationships");2722 wp_cache_add( $id, $terms, term_cache_group( $taxonomy, 'relationships' ) ); 2707 2723 } 2708 2724 } 2709 2725 } … … 2718 2734 * @param array $terms List of Term objects to change 2719 2735 * @param string $taxonomy Optional. Update Term to this taxonomy in cache 2720 2736 */ 2721 function update_term_cache($terms, $taxonomy = '') { 2722 foreach ( (array) $terms as $term ) { 2723 $term_taxonomy = $taxonomy; 2724 if ( empty($term_taxonomy) ) 2725 $term_taxonomy = $term->taxonomy; 2737 function update_term_cache( $terms, $taxonomy = '' ) { 2738 if ( ! is_array( $terms ) ) 2739 $terms = array( $terms ); 2740 2741 foreach ( $terms as $term ) { 2742 $tax = $taxonomy; 2743 if ( empty( $tax ) ) 2744 $tax = $term->taxonomy; 2726 2745 2727 wp_cache_add($term->term_id, $term, $term_taxonomy); 2746 wp_cache_add( $term->term_id, $term, term_cache_group( $tax ) ); 2747 wp_cache_add( $term->name, $term->term_id, term_cache_group( $tax, 'names' ) ); 2748 wp_cache_add( $term->slug, $term->term_id, term_cache_group( $tax, 'slugs' ) ); 2728 2749 } 2729 2750 } 2730 2751 2752 /** 2753 * 2754 * @package WordPress 2755 * @subpackage Taxonomy 2756 * @since 3.5.0 2757 * 2758 * @param string $taxonomy Taxonomy 2759 * @param string $group Optional. ids, names, slugs, or relationships. 2760 */ 2761 function term_cache_group( $taxonomy, $group = 'ids' ) { 2762 $tax = get_taxonomy( $taxonomy ); 2763 2764 if ( ! $tax ) 2765 return false; 2766 2767 if ( $tax->_builtin ) { 2768 $key = $taxonomy; 2769 switch ( $group ) { 2770 case 'name': 2771 case 'names': 2772 $key = "$taxonomy:names"; 2773 break; 2774 case 'slug': 2775 case 'slugs': 2776 $key = "$taxonomy:slugs"; 2777 break; 2778 case 'relationship': 2779 case 'relationships': 2780 $key = "{$taxonomy}_relationships"; 2781 break; 2782 } 2783 } else { 2784 $key = "taxonomy:$taxonomy:ids"; 2785 switch ( $group ) { 2786 case 'name': 2787 case 'names': 2788 $key = "taxonomy:$taxonomy:names"; 2789 break; 2790 case 'slug': 2791 case 'slugs': 2792 $key = "taxonomy:$taxonomy:slugs"; 2793 break; 2794 case 'relationship': 2795 case 'relationships': 2796 $key = "taxonomy:$taxonomy:relationships"; 2797 break; 2798 } 2799 } 2800 2801 return $key; 2802 } 2803 2731 2804 // 2732 2805 // Private 2733 2806 // … … 2780 2853 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. 2781 2854 * @return array The subset of $terms that are descendants of $term_id. 2782 2855 */ 2783 function &_get_term_children($term_id, $terms, $taxonomy) {2856 function _get_term_children($term_id, $terms, $taxonomy) { 2784 2857 $empty_array = array(); 2785 2858 if ( empty($terms) ) 2786 2859 return $empty_array; -
wp-includes/media.php
1156 1156 * @uses WP_Embed::maybe_make_link() 1157 1157 * @uses get_option() 1158 1158 * @uses current_user_can() 1159 * @uses wp_cache_get()1160 * @uses wp_cache_set()1161 1159 * @uses get_post_meta() 1162 1160 * @uses update_post_meta() 1163 1161 * -
wp-includes/category-template.php
1065 1065 $terms = get_object_term_cache( $post->ID, $taxonomy ); 1066 1066 if ( false === $terms ) { 1067 1067 $terms = wp_get_object_terms( $post->ID, $taxonomy ); 1068 wp_cache_add( $post->ID, $terms, $taxonomy . '_relationships');1068 wp_cache_add( $post->ID, $terms, term_cache_group( $taxonomy, 'relationships' ) ); 1069 1069 } 1070 1070 1071 1071 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );