Ticket #11531: 21760.diff
File 21760.diff, 10.8 KB (added by , 12 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 /** -
wp-includes/taxonomy.php
861 861 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. 862 862 * 863 863 * @param int|object $term If integer, will get from database. If object will apply filters and return $term. 864 * @param string $taxonomy Taxonomy name that $term is part of.864 * @param string $taxonomy Optional if object is passed for $term. Taxonomy name that $term is part of. 865 865 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 866 866 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 867 867 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not 868 868 * exist then WP_Error will be returned. 869 869 */ 870 function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw') {870 function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 871 871 global $wpdb; 872 872 $null = null; 873 873 874 if ( empty( $term) ) {875 $error = new WP_Error( 'invalid_term', __('Empty Term'));874 if ( empty( $term ) ) { 875 $error = new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 876 876 return $error; 877 877 } 878 878 879 if ( ! taxonomy_exists($taxonomy) ) { 880 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); 881 return $error; 882 } 879 if ( is_object( $term ) ) 880 $taxonomy = $term->taxonomy; 883 881 884 if ( is_object($term) && empty($term->filter) ) { 885 wp_cache_add($term->term_id, $term, $taxonomy); 882 if ( ! taxonomy_exists( $taxonomy ) ) 883 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 884 885 if ( is_object( $term ) && empty( $term->filter ) ) { 886 update_term_cache( $term, $taxonomy ); 886 887 $_term = $term; 887 888 } else { 888 889 if ( is_object($term) ) 889 890 $term = $term->term_id; 890 891 if ( !$term = (int) $term ) 891 892 return $null; 892 if ( ! $_term = wp_cache_get( $term, $taxonomy) ) {893 if ( ! $_term = wp_cache_get( $term, term_cache_group( $taxonomy ) ) ) { 893 894 $_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) ); 894 895 if ( ! $_term ) 895 896 return $null; 896 wp_cache_add($term, $_term, $taxonomy); 897 898 update_term_cache( $_term, $taxonomy ); 897 899 } 898 900 } 899 901 … … 948 950 return false; 949 951 950 952 if ( 'slug' == $field ) { 953 $value = sanitize_title( $value ); 954 if ( empty( $value ) ) 955 return false; 956 957 $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) ); 951 958 $field = 't.slug'; 952 $value = sanitize_title($value);953 if ( empty($value) )954 return false;955 959 } else if ( 'name' == $field ) { 956 960 // Assume already escaped 957 961 $value = stripslashes($value); 962 $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) ); 958 963 $field = 't.name'; 959 964 } else { 960 $term = get_term( (int) $value, $taxonomy, $output, $filter); 965 $term_id = $value; 966 967 return get_term( $term_id, $taxonomy ); 968 } 969 970 if ( ! empty( $term_id ) ) { 971 $term = get_term( (int) $term_id, $taxonomy, $output, $filter ); 961 972 if ( is_wp_error( $term ) ) 962 973 $term = false; 974 963 975 return $term; 964 976 } 965 977 966 $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 $field = %s LIMIT 1", $taxonomy, $value) ); 967 if ( !$term ) 978 $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 $field = %s LIMIT 1", $taxonomy, $value ) ); 979 980 if ( ! $term ) 968 981 return false; 969 982 970 wp_cache_add($term->term_id, $term, $taxonomy);971 972 983 $term = apply_filters('get_term', $term, $taxonomy); 973 984 $term = apply_filters("get_$taxonomy", $term, $taxonomy); 974 985 $term = sanitize_term($term, $taxonomy, $filter); … … 1238 1249 } 1239 1250 1240 1251 // $args can be whatever, only use the args defined in defaults to compute the key 1241 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; 1242 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key ); 1252 // deprecate 'list_terms_exclusions' - use 'get_terms' filter instead 1253 if ( has_filter( 'list_terms_exclusions' ) ) 1254 _doing_it_wrong( __FUNCTION__, __( '"list_terms_exclusions" is no longer supported. Use the "get_terms" filter instead.' ), '3.6' ); 1255 1256 $key = md5( serialize( compact( array_keys( $defaults ) ) ) . serialize( $taxonomies ) ); 1243 1257 $last_changed = wp_cache_get('last_changed', 'terms'); 1244 1258 if ( !$last_changed ) { 1245 1259 $last_changed = time(); … … 2570 2584 2571 2585 foreach ( $object_ids as $id ) 2572 2586 foreach ( $taxonomies as $taxonomy ) 2573 wp_cache_delete( $id, "{$taxonomy}_relationships");2587 wp_cache_delete( $id, term_cache_group( $taxonomy, 'relationships' ) ); 2574 2588 2575 2589 do_action('clean_object_term_cache', $object_ids, $object_type); 2576 2590 } … … 2591 2605 global $wpdb; 2592 2606 static $cleaned = array(); 2593 2607 2594 if ( ! is_array($ids) )2595 $ids = array( $ids);2608 if ( ! is_array( $ids ) ) 2609 $ids = array( $ids ); 2596 2610 2597 2611 $taxonomies = array(); 2612 2613 $select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 2614 2615 $ids = array_map( 'intval', $ids ); 2616 2598 2617 // If no taxonomy, assume tt_ids. 2599 if ( empty($taxonomy) ) { 2600 $tt_ids = array_map('intval', $ids); 2601 $tt_ids = implode(', ', $tt_ids); 2602 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); 2618 if ( empty( $taxonomy ) ) { 2619 $terms = $wpdb->get_results( "$select WHERE tt.term_taxonomy_id IN (" . join( ',', $ids ) . ")" ); 2603 2620 $ids = array(); 2604 foreach ( (array)$terms as $term ) {2621 foreach ( $terms as $term ) { 2605 2622 $taxonomies[] = $term->taxonomy; 2606 2623 $ids[] = $term->term_id; 2607 wp_cache_delete($term->term_id, $term->taxonomy);2608 2624 } 2609 2625 $taxonomies = array_unique($taxonomies); 2610 2626 } else { 2611 $taxonomies = array($taxonomy); 2612 foreach ( $taxonomies as $taxonomy ) { 2613 foreach ( $ids as $id ) { 2614 wp_cache_delete($id, $taxonomy); 2615 } 2616 } 2627 $terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND tt.term_id IN (" . join( ',', $ids ) . ")", $taxonomy ) ); 2628 $taxonomies = array( $taxonomy ); 2617 2629 } 2618 2630 2631 foreach ( $terms as $term ) { 2632 wp_cache_delete( $term->term_id, term_cache_group( $term->taxonomy ) ); 2633 wp_cache_delete( $term->name, term_cache_group( $term->taxonomy, 'names' ) ); 2634 wp_cache_delete( $term->slug, term_cache_group( $term->taxonomy, 'slugs' ) ); 2635 } 2636 2619 2637 foreach ( $taxonomies as $taxonomy ) { 2620 2638 if ( isset($cleaned[$taxonomy]) ) 2621 2639 continue; 2622 2640 $cleaned[$taxonomy] = true; 2623 2641 2624 2642 if ( $clean_taxonomy ) { 2625 wp_cache_delete('all_ids', $taxonomy);2626 wp_cache_delete('get', $taxonomy);2627 2643 delete_option("{$taxonomy}_children"); 2628 2644 // Regenerate {$taxonomy}_children 2629 2645 _get_term_hierarchy($taxonomy); … … 2649 2665 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. 2650 2666 */ 2651 2667 function get_object_term_cache($id, $taxonomy) { 2652 $cache = wp_cache_get( $id, "{$taxonomy}_relationships");2668 $cache = wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ); 2653 2669 return $cache; 2654 2670 } 2655 2671 … … 2689 2705 $ids = array(); 2690 2706 foreach ( (array) $object_ids as $id ) { 2691 2707 foreach ( $taxonomies as $taxonomy ) { 2692 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships") ) {2708 if ( false === wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ) ) { 2693 2709 $ids[] = $id; 2694 2710 break; 2695 2711 } … … 2717 2733 2718 2734 foreach ( $object_terms as $id => $value ) { 2719 2735 foreach ( $value as $taxonomy => $terms ) { 2720 wp_cache_add( $id, $terms, "{$taxonomy}_relationships");2736 wp_cache_add( $id, $terms, term_cache_group( $taxonomy, 'relationships' ) ); 2721 2737 } 2722 2738 } 2723 2739 } … … 2732 2748 * @param array $terms List of Term objects to change 2733 2749 * @param string $taxonomy Optional. Update Term to this taxonomy in cache 2734 2750 */ 2735 function update_term_cache($terms, $taxonomy = '') { 2736 foreach ( (array) $terms as $term ) { 2737 $term_taxonomy = $taxonomy; 2738 if ( empty($term_taxonomy) ) 2739 $term_taxonomy = $term->taxonomy; 2751 function update_term_cache( $terms, $taxonomy = '' ) { 2752 if ( ! is_array( $terms ) ) 2753 $terms = array( $terms ); 2740 2754 2741 wp_cache_add($term->term_id, $term, $term_taxonomy); 2755 foreach ( $terms as $term ) { 2756 $tax = $taxonomy; 2757 if ( empty( $tax ) ) 2758 $tax = $term->taxonomy; 2759 2760 wp_cache_add( $term->term_id, $term, term_cache_group( $tax ) ); 2761 wp_cache_add( $term->name, $term->term_id, term_cache_group( $tax, 'names' ) ); 2762 wp_cache_add( $term->slug, $term->term_id, term_cache_group( $tax, 'slugs' ) ); 2742 2763 } 2743 2764 } 2744 2765 2766 /** 2767 * 2768 * @package WordPress 2769 * @subpackage Taxonomy 2770 * @since 3.6.0 2771 * 2772 * @param string $taxonomy Taxonomy 2773 * @param string $group Optional. ids, names, slugs, or relationships. 2774 */ 2775 function term_cache_group( $taxonomy, $group = 'ids' ) { 2776 $tax = get_taxonomy( $taxonomy ); 2777 2778 if ( ! $tax ) 2779 return false; 2780 2781 if ( $tax->_builtin ) { 2782 switch ( $group ) { 2783 case 'names': 2784 case 'slugs': 2785 $key = "$taxonomy:$group"; 2786 break; 2787 case 'relationships': 2788 $key = "{$taxonomy}_relationships"; 2789 break; 2790 default: 2791 $key = $taxonomy; 2792 break; 2793 } 2794 } else { 2795 switch ( $group ) { 2796 case 'ids': 2797 case 'names': 2798 case 'slugs': 2799 case 'relationships': 2800 $key = "taxonomy:$taxonomy:$group"; 2801 break; 2802 default: 2803 $key = "taxonomy:$taxonomy:ids"; 2804 break; 2805 } 2806 } 2807 2808 return $key; 2809 } 2810 2745 2811 // 2746 2812 // Private 2747 2813 // -
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 );