Make WordPress Core

Ticket #29894: taxonomy.php.update4.patch

File taxonomy.php.update4.patch, 6.7 KB (added by webgeekconsulting, 10 years ago)

taxonomy.php patch 4

  • wp-includes/taxonomy.php

     
    13261326                }
    13271327        }
    13281328
    1329         // $args can be whatever, only use the args defined in defaults to compute the key
    1330         $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    1331         $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
    1332         $last_changed = wp_cache_get( 'last_changed', 'terms' );
    1333         if ( ! $last_changed ) {
    1334                 $last_changed = microtime();
    1335                 wp_cache_set( 'last_changed', $last_changed, 'terms' );
    1336         }
    1337         $cache_key = "get_terms:$key:$last_changed";
    1338         $cache = wp_cache_get( $cache_key, 'terms' );
    1339         if ( false !== $cache ) {
    1340 
    1341                 /**
    1342                  * Filter the given taxonomy's terms cache.
    1343                  *
    1344                  * @since 2.3.0
    1345                  *
    1346                  * @param array        $cache      Cached array of terms for the given taxonomy.
    1347                  * @param string|array $taxonomies A taxonomy or array of taxonomies.
    1348                  * @param array        $args       An array of arguments to get terms.
    1349                  */
    1350                 $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args );
    1351                 return $cache;
    1352         }
    1353 
    13541329        $_orderby = strtolower( $args['orderby'] );
    13551330        if ( 'count' == $_orderby ) {
    13561331                $orderby = 'tt.count';
     
    14941469                $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    14951470        }
    14961471
    1497         $selects = array();
    1498         switch ( $args['fields'] ) {
    1499                 case 'all':
    1500                         $selects = array( 't.*', 'tt.*' );
    1501                         break;
    1502                 case 'ids':
    1503                 case 'id=>parent':
    1504                         $selects = array( 't.term_id', 'tt.parent', 'tt.count' );
    1505                         break;
    1506                 case 'names':
    1507                         $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
    1508                         break;
    1509                 case 'count':
    1510                         $orderby = '';
    1511                         $order = '';
    1512                         $selects = array( 'COUNT(*)' );
    1513                         break;
    1514                 case 'id=>name':
    1515                         $selects = array( 't.term_id', 't.name' );
    1516                         break;
    1517                 case 'id=>slug':
    1518                         $selects = array( 't.term_id', 't.slug' );
    1519                         break;
    1520         }
    1521 
    1522         $_fields = $args['fields'];
    1523 
    1524         /**
    1525          * Filter the fields to select in the terms query.
    1526          *
    1527          * @since 2.8.0
    1528          *
    1529          * @param array        $selects    An array of fields to select for the terms query.
    1530          * @param array        $args       An array of term query arguments.
    1531          * @param string|array $taxonomies A taxonomy or array of taxonomies.
    1532          */
    1533         $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
    1534 
    15351472        $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
     1473        $pieces = array( 'join', 'where', 'orderby', 'order', 'limits' );
    15361474
    1537         $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
    1538 
    15391475        /**
    15401476         * Filter the terms query SQL clauses.
    15411477         *
     
    15461482         * @param array        $args       An array of terms query arguments.
    15471483         */
    15481484        $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
    1549         $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
    15501485        $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
    15511486        $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
    15521487        $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
    15531488        $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
    15541489        $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
    15551490
    1556         $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
     1491        $query = "SELECT * FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
    15571492
    1558         if ( 'count' == $_fields ) {
    1559                 $term_count = $wpdb->get_var($query);
    1560                 return $term_count;
     1493        // Attempt to use cached query
     1494        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
     1495        $key = md5( $query . $filter_key );
     1496        $last_changed = wp_cache_get( 'last_changed', 'terms' );
     1497        if ( ! $last_changed ) {
     1498                $last_changed = microtime();
     1499                wp_cache_set( 'last_changed', $last_changed, 'terms' );
    15611500        }
    1562 
    1563         $terms = $wpdb->get_results($query);
    1564         if ( 'all' == $_fields ) {
     1501        $cache_key = "get_terms:$key:$last_changed";
     1502        $cache = wp_cache_get( $cache_key, 'terms' );
     1503        if ( false !== $cache ) {
     1504                $terms = $cache;
     1505        } else {
     1506                $terms = $wpdb->get_results( $query );
     1507                wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS);
     1508        }
     1509       
     1510        if ( 'count' == $args['fields'] ) {
     1511                /**
     1512                 * Filter the given taxonomy's terms cache.
     1513                 *
     1514                 * @since 2.3.0
     1515                 *
     1516                 * @param array        $cache      Cached array of terms for the given taxonomy.
     1517                 * @param string|array $taxonomies A taxonomy or array of taxonomies.
     1518                 * @param array        $args       An array of arguments to get terms.
     1519                 */
     1520                $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
     1521                return count($terms);
     1522        }
     1523       
     1524        if ( 'all' == $args['fields'] ) {
    15651525                update_term_cache($terms);
    15661526        }
    15671527
    15681528        if ( empty($terms) ) {
    1569                 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
    1570 
    15711529                /** This filter is documented in wp-includes/taxonomy.php */
    1572                 $terms = apply_filters( 'get_terms', array(), $taxonomies, $args );
    1573                 return $terms;
     1530                return apply_filters( 'get_terms', array(), $taxonomies, $args );
    15741531        }
    15751532
    15761533        if ( $child_of ) {
     
    15811538        }
    15821539
    15831540        // Update term counts to include children.
    1584         if ( $args['pad_counts'] && 'all' == $_fields ) {
     1541        if ( $args['pad_counts'] && 'all' == $args['fields'] ) {
    15851542                _pad_term_counts( $terms, reset( $taxonomies ) );
    15861543        }
    15871544        // Make sure we show empty categories that have children.
     
    16061563        reset( $terms );
    16071564
    16081565        $_terms = array();
    1609         if ( 'id=>parent' == $_fields ) {
     1566        if ( 'id=>parent' == $args['fields'] ) {
    16101567                while ( $term = array_shift( $terms ) ) {
    16111568                        $_terms[$term->term_id] = $term->parent;
    16121569                }
    1613         } elseif ( 'ids' == $_fields ) {
     1570        } elseif ( 'ids' == $args['fields'] ) {
    16141571                while ( $term = array_shift( $terms ) ) {
    16151572                        $_terms[] = $term->term_id;
    16161573                }
    1617         } elseif ( 'names' == $_fields ) {
     1574        } elseif ( 'names' == $args['fields'] ) {
    16181575                while ( $term = array_shift( $terms ) ) {
    16191576                        $_terms[] = $term->name;
    16201577                }
    1621         } elseif ( 'id=>name' == $_fields ) {
     1578        } elseif ( 'id=>name' == $args['fields'] ) {
    16221579                while ( $term = array_shift( $terms ) ) {
    16231580                        $_terms[$term->term_id] = $term->name;
    16241581                }
    1625         } elseif ( 'id=>slug' == $_fields ) {
     1582        } elseif ( 'id=>slug' == $args['fields'] ) {
    16261583                while ( $term = array_shift( $terms ) ) {
    16271584                        $_terms[$term->term_id] = $term->slug;
    16281585                }
     
    16361593                $terms = array_slice( $terms, $offset, $number );
    16371594        }
    16381595
    1639         wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
    1640 
    16411596        /** This filter is documented in wp-includes/taxonomy */
    1642         $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
    1643         return $terms;
     1597        return apply_filters( 'get_terms', $terms, $taxonomies, $args );
    16441598}
    16451599
    16461600/**