wp/wp-includes/class-wp-term-query.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-includes/class-wp-term-query.php	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/class-wp-term-query.php	Mon Oct 14 18:28:13 2019 +0200
@@ -123,8 +123,8 @@
 	 *     @type int          $offset                 The number by which to offset the terms query. Default empty.
 	 *     @type string       $fields                 Term fields to query for. Accepts 'all' (returns an array of
 	 *                                                complete term objects), 'all_with_object_id' (returns an
-	 *                                                array of term objects with the 'object_id' param; only works
-	 *                                                when the `$fields` parameter is 'object_ids' ), 'ids'
+	 *                                                array of term objects with the 'object_id' param; works only
+	 *                                                when the `$object_ids` parameter is populated), 'ids'
 	 *                                                (returns an array of ids), 'tt_ids' (returns an array of
 	 *                                                term taxonomy ids), 'id=>parent' (returns an associative
 	 *                                                array with ids as keys, parent term IDs as values), 'names'
@@ -172,8 +172,8 @@
 	 *                                                Can be used in conjunction with `$meta_value`. Default empty.
 	 *     @type string       $meta_value             Limit terms to those matching a specific metadata value.
 	 *                                                Usually used in conjunction with `$meta_key`. Default empty.
-	 *     @type string       $meta_type              Type of object metadata is for (e.g., comment, post, or user).
-	 *                                                Default empty.
+	 *     @type string       $meta_type              MySQL data type that the `$meta_value` will be CAST to for
+	 *                                                comparisons. Default empty.
 	 *     @type string       $meta_compare           Comparison operator to test the 'meta_value'. Default empty.
 	 * }
 	 */
@@ -238,8 +238,8 @@
 		 *
 		 * @since 4.4.0
 		 *
-		 * @param array $defaults   An array of default get_terms() arguments.
-		 * @param array $taxonomies An array of taxonomies.
+		 * @param array    $defaults   An array of default get_terms() arguments.
+		 * @param string[] $taxonomies An array of taxonomy names.
 		 */
 		$this->query_var_defaults = apply_filters( 'get_terms_defaults', $this->query_var_defaults, $taxonomies );
 
@@ -254,11 +254,11 @@
 		}
 
 		if ( 'all' == $query['get'] ) {
-			$query['childless'] = false;
-			$query['child_of'] = 0;
-			$query['hide_empty'] = 0;
+			$query['childless']    = false;
+			$query['child_of']     = 0;
+			$query['hide_empty']   = 0;
 			$query['hierarchical'] = false;
-			$query['pad_counts'] = false;
+			$query['pad_counts']   = false;
 		}
 
 		$query['taxonomy'] = $taxonomies;
@@ -330,7 +330,7 @@
 
 		if ( ! $has_hierarchical_tax ) {
 			$args['hierarchical'] = false;
-			$args['pad_counts'] = false;
+			$args['pad_counts']   = false;
 		}
 
 		// 'parent' overrides 'child_of'.
@@ -339,11 +339,11 @@
 		}
 
 		if ( 'all' == $args['get'] ) {
-			$args['childless'] = false;
-			$args['child_of'] = 0;
-			$args['hide_empty'] = 0;
+			$args['childless']    = false;
+			$args['child_of']     = 0;
+			$args['hide_empty']   = 0;
 			$args['hierarchical'] = false;
-			$args['pad_counts'] = false;
+			$args['pad_counts']   = false;
 		}
 
 		/**
@@ -351,8 +351,8 @@
 		 *
 		 * @since 3.1.0
 		 *
-		 * @param array $args       An array of get_terms() arguments.
-		 * @param array $taxonomies An array of taxonomies.
+		 * @param array    $args       An array of get_terms() arguments.
+		 * @param string[] $taxonomies An array of taxonomy names.
 		 */
 		$args = apply_filters( 'get_terms_args', $args, $taxonomies );
 
@@ -379,7 +379,12 @@
 			}
 
 			if ( ! $in_hierarchy ) {
-				return array();
+				if ( 'count' == $args['fields'] ) {
+					return 0;
+				} else {
+					$this->terms = array();
+					return $this->terms;
+				}
 			}
 		}
 
@@ -406,9 +411,9 @@
 
 		$inclusions = '';
 		if ( ! empty( $include ) ) {
-			$exclude = '';
+			$exclude      = '';
 			$exclude_tree = '';
-			$inclusions = implode( ',', wp_parse_id_list( $include ) );
+			$inclusions   = implode( ',', wp_parse_id_list( $include ) );
 		}
 
 		if ( ! empty( $inclusions ) ) {
@@ -417,16 +422,19 @@
 
 		$exclusions = array();
 		if ( ! empty( $exclude_tree ) ) {
-			$exclude_tree = wp_parse_id_list( $exclude_tree );
+			$exclude_tree      = wp_parse_id_list( $exclude_tree );
 			$excluded_children = $exclude_tree;
 			foreach ( $exclude_tree as $extrunk ) {
 				$excluded_children = array_merge(
 					$excluded_children,
-					(array) get_terms( reset( $taxonomies ), array(
-						'child_of' => intval( $extrunk ),
-						'fields' => 'ids',
-						'hide_empty' => 0
-					) )
+					(array) get_terms(
+						reset( $taxonomies ),
+						array(
+							'child_of'   => intval( $extrunk ),
+							'fields'     => 'ids',
+							'hide_empty' => 0,
+						)
+					)
 				);
 			}
 			$exclusions = array_merge( $excluded_children, $exclusions );
@@ -441,7 +449,7 @@
 		if ( $childless ) {
 			foreach ( $taxonomies as $_tax ) {
 				$term_hierarchy = _get_term_hierarchy( $_tax );
-				$exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
+				$exclusions     = array_merge( array_keys( $term_hierarchy ), $exclusions );
 			}
 		}
 
@@ -456,9 +464,9 @@
 		 *
 		 * @since 2.3.0
 		 *
-		 * @param string $exclusions `NOT IN` clause of the terms query.
-		 * @param array  $args       An array of terms query arguments.
-		 * @param array  $taxonomies An array of taxonomies.
+		 * @param string   $exclusions `NOT IN` clause of the terms query.
+		 * @param array    $args       An array of terms query arguments.
+		 * @param string[] $taxonomies An array of taxonomy names.
 		 */
 		$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
 
@@ -485,10 +493,10 @@
 			( is_string( $args['slug'] ) && 0 !== strlen( $args['slug'] ) )
 		) {
 			if ( is_array( $args['slug'] ) ) {
-				$slug = array_map( 'sanitize_title', $args['slug'] );
+				$slug                               = array_map( 'sanitize_title', $args['slug'] );
 				$this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')";
 			} else {
-				$slug = sanitize_title( $args['slug'] );
+				$slug                               = sanitize_title( $args['slug'] );
 				$this->sql_clauses['where']['slug'] = "t.slug = '$slug'";
 			}
 		}
@@ -498,16 +506,16 @@
 				$tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
 				$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
 			} else {
-				$this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
+				$this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( 'tt.term_taxonomy_id = %d', $args['term_taxonomy_id'] );
 			}
 		}
 
 		if ( ! empty( $args['name__like'] ) ) {
-			$this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
+			$this->sql_clauses['where']['name__like'] = $wpdb->prepare( 't.name LIKE %s', '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
 		}
 
 		if ( ! empty( $args['description__like'] ) ) {
-			$this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
+			$this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
 		}
 
 		if ( ! empty( $args['object_ids'] ) ) {
@@ -516,7 +524,7 @@
 				$object_ids = array( $object_ids );
 			}
 
-			$object_ids = implode( ', ', array_map( 'intval', $object_ids ) );
+			$object_ids                               = implode( ', ', array_map( 'intval', $object_ids ) );
 			$this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)";
 		}
 
@@ -529,7 +537,7 @@
 		}
 
 		if ( '' !== $parent ) {
-			$parent = (int) $parent;
+			$parent                               = (int) $parent;
 			$this->sql_clauses['where']['parent'] = "tt.parent = '$parent'";
 		}
 
@@ -537,7 +545,7 @@
 		if ( 'count' == $args['fields'] ) {
 			$hierarchical = false;
 		}
-		if ( $args['hide_empty'] && !$hierarchical ) {
+		if ( $args['hide_empty'] && ! $hierarchical ) {
 			$this->sql_clauses['where']['count'] = 'tt.count > 0';
 		}
 
@@ -555,33 +563,32 @@
 			$limits = '';
 		}
 
-
 		if ( ! empty( $args['search'] ) ) {
 			$this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] );
 		}
 
 		// Meta query support.
-		$join = '';
+		$join     = '';
 		$distinct = '';
 
 		// Reparse meta_query query_vars, in case they were modified in a 'pre_get_terms' callback.
 		$this->meta_query->parse_query_vars( $this->query_vars );
-		$mq_sql = $this->meta_query->get_sql( 'term', 't', 'term_id' );
+		$mq_sql       = $this->meta_query->get_sql( 'term', 't', 'term_id' );
 		$meta_clauses = $this->meta_query->get_clauses();
 
 		if ( ! empty( $meta_clauses ) ) {
-			$join .= $mq_sql['join'];
+			$join                                    .= $mq_sql['join'];
 			$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] );
-			$distinct .= "DISTINCT";
+			$distinct                                .= 'DISTINCT';
 
 		}
 
 		$selects = array();
 		switch ( $args['fields'] ) {
 			case 'all':
-			case 'all_with_object_id' :
-			case 'tt_ids' :
-			case 'slugs' :
+			case 'all_with_object_id':
+			case 'tt_ids':
+			case 'slugs':
 				$selects = array( 't.*', 'tt.*' );
 				if ( 'all_with_object_id' === $args['fields'] && ! empty( $args['object_ids'] ) ) {
 					$selects[] = 'tr.object_id';
@@ -596,7 +603,7 @@
 				break;
 			case 'count':
 				$orderby = '';
-				$order = '';
+				$order   = '';
 				$selects = array( 'COUNT(*)' );
 				break;
 			case 'id=>name':
@@ -621,9 +628,9 @@
 		 *
 		 * @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.
+		 * @param string[] $selects    An array of fields to select for the terms query.
+		 * @param array    $args       An array of term query arguments.
+		 * @param string[] $taxonomies An array of taxonomy names.
 		 */
 		$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
 
@@ -640,19 +647,19 @@
 		 *
 		 * @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.
+		 * @param string[] $pieces     Array of query SQL clauses.
+		 * @param string[] $taxonomies An array of taxonomy names.
+		 * @param array    $args       An array of term query arguments.
 		 */
 		$clauses = apply_filters( 'terms_clauses', compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ), $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' ] : '';
+		$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'] : '';
 
 		if ( $where ) {
 			$where = "WHERE $where";
@@ -666,10 +673,10 @@
 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['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( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
+		$key          = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
 		$last_changed = wp_cache_get_last_changed( 'terms' );
-		$cache_key = "get_terms:$key:$last_changed";
-		$cache = wp_cache_get( $cache_key, 'terms' );
+		$cache_key    = "get_terms:$key:$last_changed";
+		$cache        = wp_cache_get( $cache_key, 'terms' );
 		if ( false !== $cache ) {
 			if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
 				$cache = $this->populate_terms( $cache );
@@ -751,7 +758,7 @@
 				}
 
 				$_tt_ids[ $term->term_id ] = 1;
-				$_terms[] = $term;
+				$_terms[]                  = $term;
 			}
 
 			$terms = $_terms;
@@ -822,7 +829,7 @@
 	 * @return string|false Value to used in the ORDER clause. False otherwise.
 	 */
 	protected function parse_orderby( $orderby_raw ) {
-		$_orderby = strtolower( $orderby_raw );
+		$_orderby           = strtolower( $orderby_raw );
 		$maybe_orderby_meta = false;
 
 		if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) {
@@ -835,7 +842,7 @@
 			$include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
 			$orderby = "FIELD( t.term_id, $include )";
 		} elseif ( 'slug__in' == $_orderby && ! empty( $this->query_vars['slug'] ) && is_array( $this->query_vars['slug'] ) ) {
-			$slugs = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug'] ) );
+			$slugs   = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug'] ) );
 			$orderby = "FIELD( t.slug, '" . $slugs . "')";
 		} elseif ( 'none' == $_orderby ) {
 			$orderby = '';
@@ -853,9 +860,9 @@
 		 *
 		 * @since 2.8.0
 		 *
-		 * @param string $orderby    `ORDERBY` clause of the terms query.
-		 * @param array  $args       An array of terms query arguments.
-		 * @param array  $taxonomies An array of taxonomies.
+		 * @param string   $orderby    `ORDERBY` clause of the terms query.
+		 * @param array    $args       An array of term query arguments.
+		 * @param string[] $taxonomies An array of taxonomy names.
 		 */
 		$orderby = apply_filters( 'get_terms_orderby', $orderby, $this->query_vars, $this->query_vars['taxonomy'] );
 
@@ -888,12 +895,12 @@
 			return $orderby;
 		}
 
-		$allowed_keys = array();
-		$primary_meta_key = null;
+		$allowed_keys       = array();
+		$primary_meta_key   = null;
 		$primary_meta_query = reset( $meta_clauses );
 		if ( ! empty( $primary_meta_query['key'] ) ) {
 			$primary_meta_key = $primary_meta_query['key'];
-			$allowed_keys[] = $primary_meta_key;
+			$allowed_keys[]   = $primary_meta_key;
 		}
 		$allowed_keys[] = 'meta_value';
 		$allowed_keys[] = 'meta_value_num';
@@ -903,7 +910,7 @@
 			return $orderby;
 		}
 
-		switch( $orderby_raw ) {
+		switch ( $orderby_raw ) {
 			case $primary_meta_key:
 			case 'meta_value':
 				if ( ! empty( $primary_meta_query['type'] ) ) {
@@ -921,7 +928,7 @@
 				if ( array_key_exists( $orderby_raw, $meta_clauses ) ) {
 					// $orderby corresponds to a meta_query clause.
 					$meta_clause = $meta_clauses[ $orderby_raw ];
-					$orderby = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
+					$orderby     = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
 				}
 				break;
 		}