Ticket #21760: 21760.diff

File 21760.diff, 10.8 KB (added by wonderboymusic, 4 months ago)
Line 
1Index: wp-includes/category.php
2===================================================================
3--- wp-includes/category.php    (revision 23294)
4+++ wp-includes/category.php    (working copy)
5@@ -14,12 +14,8 @@
6  * @return object List of all of the category IDs.
7  */
8 function get_all_category_ids() {
9-       if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) {
10-               $cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') );
11-               wp_cache_add( 'all_category_ids', $cat_ids, 'category' );
12-       }
13-
14-       return $cat_ids;
15+       // this call returns from cache if present
16+       return get_terms( 'category', array( 'fields' => 'ids', 'get' => 'all' ) );
17 }
18 
19 /**
20Index: wp-includes/taxonomy.php
21===================================================================
22--- wp-includes/taxonomy.php    (revision 23294)
23+++ wp-includes/taxonomy.php    (working copy)
24@@ -861,39 +861,41 @@
25  * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
26  *
27  * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
28- * @param string $taxonomy Taxonomy name that $term is part of.
29+ * @param string $taxonomy Optional if object is passed for $term. Taxonomy name that $term is part of.
30  * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
31  * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
32  * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
33  * exist then WP_Error will be returned.
34  */
35-function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
36+function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
37        global $wpdb;
38        $null = null;
39 
40-       if ( empty($term) ) {
41-               $error = new WP_Error('invalid_term', __('Empty Term'));
42+       if ( empty( $term ) ) {
43+               $error = new WP_Error( 'invalid_term', __( 'Empty Term' ) );
44                return $error;
45        }
46 
47-       if ( ! taxonomy_exists($taxonomy) ) {
48-               $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
49-               return $error;
50-       }
51+       if ( is_object( $term ) )
52+               $taxonomy = $term->taxonomy;
53 
54-       if ( is_object($term) && empty($term->filter) ) {
55-               wp_cache_add($term->term_id, $term, $taxonomy);
56+       if ( ! taxonomy_exists( $taxonomy ) )
57+               return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
58+
59+       if ( is_object( $term ) && empty( $term->filter ) ) {
60+               update_term_cache( $term, $taxonomy );
61                $_term = $term;
62        } else {
63                if ( is_object($term) )
64                        $term = $term->term_id;
65                if ( !$term = (int) $term )
66                        return $null;
67-               if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
68+               if ( ! $_term = wp_cache_get( $term, term_cache_group( $taxonomy ) ) ) {
69                        $_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) );
70                        if ( ! $_term )
71                                return $null;
72-                       wp_cache_add($term, $_term, $taxonomy);
73+
74+                       update_term_cache( $_term, $taxonomy );
75                }
76        }
77 
78@@ -948,27 +950,36 @@
79                return false;
80 
81        if ( 'slug' == $field ) {
82+               $value = sanitize_title( $value );
83+               if ( empty( $value ) )
84+                       return false;
85+
86+               $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
87                $field = 't.slug';
88-               $value = sanitize_title($value);
89-               if ( empty($value) )
90-                       return false;
91        } else if ( 'name' == $field ) {
92                // Assume already escaped
93                $value = stripslashes($value);
94+               $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
95                $field = 't.name';
96        } else {
97-               $term = get_term( (int) $value, $taxonomy, $output, $filter);
98+               $term_id = $value;
99+
100+               return get_term( $term_id, $taxonomy );
101+       }
102+
103+       if ( ! empty( $term_id ) ) {
104+               $term = get_term( (int) $term_id, $taxonomy, $output, $filter );
105                if ( is_wp_error( $term ) )
106                        $term = false;
107+
108                return $term;
109        }
110 
111-       $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) );
112-       if ( !$term )
113+       $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 ) );
114+
115+       if ( ! $term )
116                return false;
117 
118-       wp_cache_add($term->term_id, $term, $taxonomy);
119-
120        $term = apply_filters('get_term', $term, $taxonomy);
121        $term = apply_filters("get_$taxonomy", $term, $taxonomy);
122        $term = sanitize_term($term, $taxonomy, $filter);
123@@ -1238,8 +1249,11 @@
124        }
125 
126        // $args can be whatever, only use the args defined in defaults to compute the key
127-       $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
128-       $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
129+       // deprecate 'list_terms_exclusions' - use 'get_terms' filter instead
130+       if ( has_filter( 'list_terms_exclusions' ) )
131+               _doing_it_wrong( __FUNCTION__, __( '"list_terms_exclusions" is no longer supported. Use the "get_terms" filter instead.' ), '3.6' );
132+
133+       $key = md5( serialize( compact( array_keys( $defaults ) ) ) . serialize( $taxonomies ) );
134        $last_changed = wp_cache_get('last_changed', 'terms');
135        if ( !$last_changed ) {
136                $last_changed = time();
137@@ -2570,7 +2584,7 @@
138 
139        foreach ( $object_ids as $id )
140                foreach ( $taxonomies as $taxonomy )
141-                       wp_cache_delete($id, "{$taxonomy}_relationships");
142+                       wp_cache_delete( $id, term_cache_group( $taxonomy, 'relationships' ) );
143 
144        do_action('clean_object_term_cache', $object_ids, $object_type);
145 }
146@@ -2591,39 +2605,41 @@
147        global $wpdb;
148        static $cleaned = array();
149 
150-       if ( !is_array($ids) )
151-               $ids = array($ids);
152+       if ( ! is_array( $ids ) )
153+               $ids = array( $ids );
154 
155        $taxonomies = array();
156+
157+       $select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
158+
159+       $ids = array_map( 'intval', $ids );
160+
161        // If no taxonomy, assume tt_ids.
162-       if ( empty($taxonomy) ) {
163-               $tt_ids = array_map('intval', $ids);
164-               $tt_ids = implode(', ', $tt_ids);
165-               $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
166+       if ( empty( $taxonomy ) ) {
167+               $terms = $wpdb->get_results( "$select WHERE tt.term_taxonomy_id IN (" . join( ',', $ids ) . ")" );
168                $ids = array();
169-               foreach ( (array) $terms as $term ) {
170+               foreach ( $terms as $term ) {
171                        $taxonomies[] = $term->taxonomy;
172                        $ids[] = $term->term_id;
173-                       wp_cache_delete($term->term_id, $term->taxonomy);
174                }
175                $taxonomies = array_unique($taxonomies);
176        } else {
177-               $taxonomies = array($taxonomy);
178-               foreach ( $taxonomies as $taxonomy ) {
179-                       foreach ( $ids as $id ) {
180-                               wp_cache_delete($id, $taxonomy);
181-                       }
182-               }
183+               $terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND tt.term_id IN (" . join( ',', $ids ) . ")", $taxonomy ) );
184+               $taxonomies = array( $taxonomy );
185        }
186 
187+       foreach ( $terms as $term ) {
188+               wp_cache_delete( $term->term_id, term_cache_group( $term->taxonomy ) );
189+               wp_cache_delete( $term->name, term_cache_group( $term->taxonomy, 'names' ) );
190+               wp_cache_delete( $term->slug, term_cache_group( $term->taxonomy, 'slugs' ) );
191+       }
192+
193        foreach ( $taxonomies as $taxonomy ) {
194                if ( isset($cleaned[$taxonomy]) )
195                        continue;
196                $cleaned[$taxonomy] = true;
197 
198                if ( $clean_taxonomy ) {
199-                       wp_cache_delete('all_ids', $taxonomy);
200-                       wp_cache_delete('get', $taxonomy);
201                        delete_option("{$taxonomy}_children");
202                        // Regenerate {$taxonomy}_children
203                        _get_term_hierarchy($taxonomy);
204@@ -2649,7 +2665,7 @@
205  * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
206  */
207 function get_object_term_cache($id, $taxonomy) {
208-       $cache = wp_cache_get($id, "{$taxonomy}_relationships");
209+       $cache = wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) );
210        return $cache;
211 }
212 
213@@ -2689,7 +2705,7 @@
214        $ids = array();
215        foreach ( (array) $object_ids as $id ) {
216                foreach ( $taxonomies as $taxonomy ) {
217-                       if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
218+                       if ( false === wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ) ) {
219                                $ids[] = $id;
220                                break;
221                        }
222@@ -2717,7 +2733,7 @@
223 
224        foreach ( $object_terms as $id => $value ) {
225                foreach ( $value as $taxonomy => $terms ) {
226-                       wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
227+                       wp_cache_add( $id, $terms, term_cache_group( $taxonomy, 'relationships' ) );
228                }
229        }
230 }
231@@ -2732,16 +2748,66 @@
232  * @param array $terms List of Term objects to change
233  * @param string $taxonomy Optional. Update Term to this taxonomy in cache
234  */
235-function update_term_cache($terms, $taxonomy = '') {
236-       foreach ( (array) $terms as $term ) {
237-               $term_taxonomy = $taxonomy;
238-               if ( empty($term_taxonomy) )
239-                       $term_taxonomy = $term->taxonomy;
240+function update_term_cache( $terms, $taxonomy = '' ) {
241+       if ( ! is_array( $terms ) )
242+               $terms = array( $terms );
243 
244-               wp_cache_add($term->term_id, $term, $term_taxonomy);
245+       foreach ( $terms as $term ) {
246+               $tax = $taxonomy;
247+               if ( empty( $tax ) )
248+                       $tax = $term->taxonomy;
249+
250+               wp_cache_add( $term->term_id, $term, term_cache_group( $tax ) );
251+               wp_cache_add( $term->name, $term->term_id, term_cache_group( $tax, 'names' ) );
252+               wp_cache_add( $term->slug, $term->term_id, term_cache_group( $tax, 'slugs' ) );
253        }
254 }
255 
256+/**
257+ *
258+ * @package WordPress
259+ * @subpackage Taxonomy
260+ * @since 3.6.0
261+ *
262+ * @param string $taxonomy Taxonomy
263+ * @param string $group Optional. ids, names, slugs, or relationships.
264+ */
265+function term_cache_group( $taxonomy, $group = 'ids' ) {
266+       $tax = get_taxonomy( $taxonomy );
267+
268+       if ( ! $tax )
269+               return false;
270+
271+       if ( $tax->_builtin ) {
272+               switch ( $group ) {
273+                       case 'names':
274+                       case 'slugs':
275+                               $key = "$taxonomy:$group";
276+                               break;
277+                       case 'relationships':
278+                               $key = "{$taxonomy}_relationships";
279+                               break;
280+                       default:
281+                               $key = $taxonomy;
282+                               break;
283+               }
284+       } else {
285+               switch ( $group ) {
286+                       case 'ids':
287+                       case 'names':
288+                       case 'slugs':
289+                       case 'relationships':
290+                               $key = "taxonomy:$taxonomy:$group";
291+                               break;
292+                       default:
293+                               $key = "taxonomy:$taxonomy:ids";
294+                               break;
295+               }
296+       }
297+
298+       return $key;
299+}
300+
301 //
302 // Private
303 //
304Index: wp-includes/category-template.php
305===================================================================
306--- wp-includes/category-template.php   (revision 23294)
307+++ wp-includes/category-template.php   (working copy)
308@@ -1065,7 +1065,7 @@
309        $terms = get_object_term_cache( $post->ID, $taxonomy );
310        if ( false === $terms ) {
311                $terms = wp_get_object_terms( $post->ID, $taxonomy );
312-               wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
313+               wp_cache_add( $post->ID, $terms, term_cache_group( $taxonomy, 'relationships' ) );
314        }
315 
316        $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );