- $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
-
- $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
-
- $pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );
-
- /**
- * Filter the terms query SQL clauses.
- *
- * @since 3.1.0
- *
- * @param array $pieces Terms query SQL clauses.
- * @param array $taxonomies An array of taxonomies.
- * @param array $args An array of terms query arguments.
- */
- $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
-
- $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
- $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
- $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
- $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
- $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
- $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
- $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
-
- $query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
-
- // $args can be anything. Only use the args defined in defaults to compute the key.
- $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $query );
- $last_changed = wp_cache_get( 'last_changed', 'terms' );
- if ( ! $last_changed ) {
- $last_changed = microtime();
- wp_cache_set( 'last_changed', $last_changed, 'terms' );
- }
- $cache_key = "get_terms:$key:$last_changed";
- $cache = wp_cache_get( $cache_key, 'terms' );
- if ( false !== $cache ) {
- if ( 'all' === $_fields ) {
- $cache = array_map( 'get_term', $cache );
- }
-
- /**
- * Filter the given taxonomy's terms cache.
- *
- * @since 2.3.0
- *
- * @param array $cache Cached array of terms for the given taxonomy.
- * @param array $taxonomies An array of taxonomies.
- * @param array $args An array of get_terms() arguments.
- */
- return apply_filters( 'get_terms', $cache, $taxonomies, $args );
- }
-
- if ( 'count' == $_fields ) {
- return $wpdb->get_var( $query );
- }
-
- $terms = $wpdb->get_results($query);
- if ( 'all' == $_fields ) {
- update_term_cache( $terms );
- }
-
- // Prime termmeta cache.
- if ( $args['update_term_meta_cache'] ) {
- $term_ids = wp_list_pluck( $terms, 'term_id' );
- update_termmeta_cache( $term_ids );
- }
-
- if ( empty($terms) ) {
- wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
-
- /** This filter is documented in wp-includes/taxonomy.php */
- return apply_filters( 'get_terms', array(), $taxonomies, $args );
- }
-
- if ( $child_of ) {
- foreach ( $taxonomies as $_tax ) {
- $children = _get_term_hierarchy( $_tax );
- if ( ! empty( $children ) ) {
- $terms = _get_term_children( $child_of, $terms, $_tax );
- }
- }
- }
-
- // Update term counts to include children.
- if ( $args['pad_counts'] && 'all' == $_fields ) {
- foreach ( $taxonomies as $_tax ) {
- _pad_term_counts( $terms, $_tax );
- }
- }
-
- // Make sure we show empty categories that have children.
- if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
- foreach ( $terms as $k => $term ) {
- if ( ! $term->count ) {
- $children = get_term_children( $term->term_id, $term->taxonomy );
- if ( is_array( $children ) ) {
- foreach ( $children as $child_id ) {
- $child = get_term( $child_id, $term->taxonomy );
- if ( $child->count ) {
- continue 2;
- }
- }
- }
-
- // It really is empty.
- unset($terms[$k]);
- }
- }
- }
-
- $_terms = array();
- if ( 'id=>parent' == $_fields ) {
- foreach ( $terms as $term ) {
- $_terms[ $term->term_id ] = $term->parent;
- }
- } elseif ( 'ids' == $_fields ) {
- foreach ( $terms as $term ) {
- $_terms[] = $term->term_id;
- }
- } elseif ( 'names' == $_fields ) {
- foreach ( $terms as $term ) {
- $_terms[] = $term->name;
- }
- } elseif ( 'id=>name' == $_fields ) {
- foreach ( $terms as $term ) {
- $_terms[ $term->term_id ] = $term->name;
- }
- } elseif ( 'id=>slug' == $_fields ) {
- foreach ( $terms as $term ) {
- $_terms[ $term->term_id ] = $term->slug;
- }
- }
-
- if ( ! empty( $_terms ) ) {
- $terms = $_terms;
- }
-
- if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
- $terms = array_slice( $terms, $offset, $number );
- }
-
- wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
-
- if ( 'all' === $_fields ) {
- $terms = array_map( 'get_term', $terms );
- }
-
- /** This filter is documented in wp-includes/taxonomy.php */
- return apply_filters( 'get_terms', $terms, $taxonomies, $args );