Changeset 38667 for trunk/src/wp-includes/taxonomy.php
- Timestamp:
- 09/28/2016 03:54:36 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r38621 r38667 1942 1942 * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or 1943 1943 * 'all_with_object_id', an array of `WP_Term` objects will be returned. 1944 * @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments. 1944 1945 * 1945 1946 * @global wpdb $wpdb WordPress database abstraction object. … … 1947 1948 * @param int|array $object_ids The ID(s) of the object(s) to retrieve. 1948 1949 * @param string|array $taxonomies The taxonomies to retrieve terms from. 1949 * @param array|string $args { 1950 * Array of arguments. 1951 * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 1952 * 'term_group', 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. 1953 * Default 'name'. 1954 * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'. 1955 * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and 1956 * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result 1957 * in an array of term objects being returned, 'ids' will return an array of 1958 * integers, and 'names' an array of strings. 1959 * @type int $parent Optional. Limit results to the direct children of a given term ID. 1960 * @type bool $update_term_meta_cache Whether to prime termmeta cache for matched terms. Only applies when 1961 * `$fields` is 'all', 'all_with_object_id', or 'term_id'. Default true. 1962 * @type array $meta_query Meta query clauses to limit retrieved terms by. See `WP_Meta_Query`. 1963 * Default empty. 1964 * } 1950 * @param array|string $args See WP_Term_Query::__construct() for supported arguments. 1965 1951 * @return array|WP_Error The requested term data or empty array if no terms found. 1966 1952 * WP_Error if any of the $taxonomies don't exist. … … 1984 1970 $object_ids = array_map('intval', $object_ids); 1985 1971 1986 $defaults = array( 1987 'orderby' => 'name', 1988 'order' => 'ASC', 1989 'fields' => 'all', 1990 'parent' => '', 1991 'update_term_meta_cache' => true, 1992 'meta_query' => '', 1993 ); 1994 $args = wp_parse_args( $args, $defaults ); 1995 1996 $terms = array(); 1997 if ( count($taxonomies) > 1 ) { 1998 foreach ( $taxonomies as $index => $taxonomy ) { 1999 $t = get_taxonomy($taxonomy); 2000 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { 2001 unset($taxonomies[$index]); 2002 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); 2003 } 2004 } 2005 } else { 2006 $t = get_taxonomy($taxonomies[0]); 2007 if ( isset($t->args) && is_array($t->args) ) 2008 $args = array_merge($args, $t->args); 2009 } 2010 2011 $orderby = $args['orderby']; 2012 $order = $args['order']; 2013 $fields = $args['fields']; 2014 2015 if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) { 2016 $orderby = "t.$orderby"; 2017 } elseif ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) { 2018 $orderby = "tt.$orderby"; 2019 } elseif ( 'term_order' === $orderby ) { 2020 $orderby = 'tr.term_order'; 2021 } elseif ( 'none' === $orderby ) { 2022 $orderby = ''; 2023 $order = ''; 2024 } else { 2025 $orderby = 't.term_id'; 2026 } 2027 2028 // tt_ids queries can only be none or tr.term_taxonomy_id 2029 if ( ('tt_ids' == $fields) && !empty($orderby) ) 2030 $orderby = 'tr.term_taxonomy_id'; 2031 2032 if ( !empty($orderby) ) 2033 $orderby = "ORDER BY $orderby"; 2034 2035 $order = strtoupper( $order ); 2036 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) 2037 $order = 'ASC'; 2038 2039 $taxonomy_array = $taxonomies; 2040 $object_id_array = $object_ids; 2041 $taxonomies = "'" . implode("', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 2042 $object_ids = implode(', ', $object_ids); 2043 2044 $select_this = ''; 2045 if ( 'all' == $fields ) { 2046 $select_this = 't.*, tt.*'; 2047 } elseif ( 'ids' == $fields ) { 2048 $select_this = 't.term_id'; 2049 } elseif ( 'names' == $fields ) { 2050 $select_this = 't.name'; 2051 } elseif ( 'slugs' == $fields ) { 2052 $select_this = 't.slug'; 2053 } elseif ( 'all_with_object_id' == $fields ) { 2054 $select_this = 't.*, tt.*, tr.object_id'; 2055 } 2056 2057 $where = array( 2058 "tt.taxonomy IN ($taxonomies)", 2059 "tr.object_id IN ($object_ids)", 2060 ); 2061 2062 if ( '' !== $args['parent'] ) { 2063 $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] ); 2064 } 2065 2066 // Meta query support. 2067 $meta_query_join = ''; 2068 if ( ! empty( $args['meta_query'] ) ) { 2069 $mquery = new WP_Meta_Query( $args['meta_query'] ); 2070 $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' ); 2071 2072 $meta_query_join .= $mq_sql['join']; 2073 2074 // Strip leading AND. 2075 $where[] = preg_replace( '/^\s*AND/', '', $mq_sql['where'] ); 2076 } 2077 2078 $where = implode( ' AND ', $where ); 2079 2080 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id $meta_query_join WHERE $where $orderby $order"; 2081 2082 $objects = false; 2083 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 2084 $_terms = $wpdb->get_results( $query ); 2085 $object_id_index = array(); 2086 foreach ( $_terms as $key => $term ) { 2087 $term = sanitize_term( $term, $taxonomy, 'raw' ); 2088 $_terms[ $key ] = $term; 2089 2090 if ( isset( $term->object_id ) ) { 2091 $object_id_index[ $key ] = $term->object_id; 2092 } 2093 } 2094 2095 update_term_cache( $_terms ); 2096 $_terms = array_map( 'get_term', $_terms ); 2097 2098 // Re-add the object_id data, which is lost when fetching terms from cache. 2099 if ( 'all_with_object_id' === $fields ) { 2100 foreach ( $_terms as $key => $_term ) { 2101 if ( isset( $object_id_index[ $key ] ) ) { 2102 $_term->object_id = $object_id_index[ $key ]; 2103 } 2104 } 2105 } 2106 2107 $terms = array_merge( $terms, $_terms ); 2108 $objects = true; 2109 2110 } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { 2111 $_terms = $wpdb->get_col( $query ); 2112 $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; 2113 foreach ( $_terms as $key => $term ) { 2114 $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); 2115 } 2116 $terms = array_merge( $terms, $_terms ); 2117 } elseif ( 'tt_ids' == $fields ) { 2118 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order"); 2119 foreach ( $terms as $key => $tt_id ) { 2120 $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. 2121 } 2122 } 2123 2124 // Update termmeta cache, if necessary. 2125 if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) { 2126 if ( 'ids' === $fields ) { 2127 $term_ids = $terms; 2128 } else { 2129 $term_ids = wp_list_pluck( $terms, 'term_id' ); 2130 } 2131 2132 update_termmeta_cache( $term_ids ); 2133 } 2134 2135 if ( ! $terms ) { 2136 $terms = array(); 2137 } elseif ( $objects && 'all_with_object_id' !== $fields ) { 2138 $_tt_ids = array(); 2139 $_terms = array(); 2140 foreach ( $terms as $term ) { 2141 if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) { 2142 continue; 2143 } 2144 2145 $_tt_ids[] = $term->term_taxonomy_id; 2146 $_terms[] = $term; 2147 } 2148 $terms = $_terms; 2149 } elseif ( ! $objects ) { 2150 $terms = array_values( array_unique( $terms ) ); 2151 } 1972 $args['taxonomy'] = $taxonomies; 1973 $args['object_ids'] = $object_ids; 1974 1975 $terms = get_terms( $args ); 2152 1976 2153 1977 /** … … 2156 1980 * @since 4.2.0 2157 1981 * 2158 * @param array $terms 2159 * @param array $object_id _arrayArray of object IDs for which `$terms` were retrieved.2160 * @param array $taxonom y_arrayArray of taxonomies from which `$terms` were retrieved.2161 * @param array $args 2162 * 1982 * @param array $terms An array of terms for the given object or objects. 1983 * @param array $object_ids Array of object IDs for which `$terms` were retrieved. 1984 * @param array $taxonomies Array of taxonomies from which `$terms` were retrieved. 1985 * @param array $args An array of arguments for retrieving terms for the given 1986 * object(s). See wp_get_object_terms() for details. 2163 1987 */ 2164 $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args ); 1988 $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args ); 1989 1990 $object_ids = implode( ',', $object_ids ); 1991 $taxonomies = implode( ',', $taxonomies ); 2165 1992 2166 1993 /**
Note: See TracChangeset
for help on using the changeset viewer.