- $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
-
- if ( ! empty( $exclusions ) ) {
- $where .= $exclusions;
- }
-
- if ( ! empty( $args['name'] ) ) {
- $names = (array) $args['name'];
- foreach ( $names as &$_name ) {
- $_name = sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' );
- }
-
- $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
- }
-
- if ( ! empty( $args['slug'] ) ) {
- if ( is_array( $args['slug'] ) ) {
- $slug = array_map( 'sanitize_title', $args['slug'] );
- $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')";
- } else {
- $slug = sanitize_title( $args['slug'] );
- $where .= " AND t.slug = '$slug'";
- }
- }
-
- if ( ! empty( $args['name__like'] ) ) {
- $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
- }
-
- if ( ! empty( $args['description__like'] ) ) {
- $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
- }
-
- if ( '' !== $parent ) {
- $parent = (int) $parent;
- $where .= " AND tt.parent = '$parent'";
- }
-
- $hierarchical = $args['hierarchical'];
- if ( 'count' == $args['fields'] ) {
- $hierarchical = false;
- }
- if ( $args['hide_empty'] && !$hierarchical ) {
- $where .= ' AND tt.count > 0';
- }
-
- $number = $args['number'];
- $offset = $args['offset'];
-
- // Don't limit the query results when we have to descend the family tree.
- if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
- if ( $offset ) {
- $limits = 'LIMIT ' . $offset . ',' . $number;
- } else {
- $limits = 'LIMIT ' . $number;
- }
- } else {
- $limits = '';
- }
-
- if ( ! empty( $args['search'] ) ) {
- $like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
- $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
- }
-
- // Meta query support.
- $join = '';
- if ( ! empty( $args['meta_query'] ) ) {
- $mquery = new WP_Meta_Query( $args['meta_query'] );
- $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
-
- $join .= $mq_sql['join'];
- $where .= $mq_sql['where'];
- }
-
- $selects = array();
- switch ( $args['fields'] ) {
- case 'all':
- $selects = array( 't.*', 'tt.*' );
- break;
- case 'ids':
- case 'id=>parent':
- $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );
- break;
- case 'names':
- $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );
- break;
- case 'count':
- $orderby = '';
- $order = '';
- $selects = array( 'COUNT(*)' );
- break;
- case 'id=>name':
- $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );
- break;
- case 'id=>slug':
- $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );
- break;
- }
-
- $_fields = $args['fields'];
-
- /**
- * Filter the fields to select in the terms query.
- *
- * Field lists modified using this filter will only modify the term fields returned
- * by the function when the `$fields` parameter set to 'count' or 'all'. In all other
- * cases, the term fields in the results array will be determined by the `$fields`
- * parameter alone.
- *
- * Use of this filter can result in unpredictable behavior, and is not recommended.
- *
- * @since 2.8.0
- *
- * @param array $selects An array of fields to select for the terms query.
- * @param array $args An array of term query arguments.
- * @param array $taxonomies An array of taxonomies.
- */
- $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', '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' ] : '';
- $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
- $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
- $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
-
- $query = "SELECT $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 );