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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-term-functions.php
woocommerce / includes Last commit date
abstracts 9 years ago admin 9 years ago api 9 years ago cli 9 years ago data-stores 9 years ago emails 9 years ago gateways 9 years ago interfaces 9 years ago legacy 9 years ago libraries 9 years ago log-handlers 9 years ago payment-tokens 9 years ago shipping 9 years ago shortcodes 9 years ago theme-support 9 years ago vendor 9 years ago walkers 9 years ago widgets 9 years ago class-wc-ajax.php 9 years ago class-wc-api.php 9 years ago class-wc-auth.php 9 years ago class-wc-autoloader.php 9 years ago class-wc-background-emailer.php 9 years ago class-wc-background-updater.php 9 years ago class-wc-breadcrumb.php 9 years ago class-wc-cache-helper.php 9 years ago class-wc-cart.php 9 years ago class-wc-checkout.php 9 years ago class-wc-cli.php 9 years ago class-wc-comments.php 9 years ago class-wc-countries.php 9 years ago class-wc-coupon.php 9 years ago class-wc-customer-download.php 9 years ago class-wc-customer.php 9 years ago class-wc-data-exception.php 9 years ago class-wc-data-store.php 9 years ago class-wc-datetime.php 9 years ago class-wc-deprecated-action-hooks.php 9 years ago class-wc-deprecated-filter-hooks.php 9 years ago class-wc-download-handler.php 9 years ago class-wc-emails.php 9 years ago class-wc-embed.php 9 years ago class-wc-form-handler.php 9 years ago class-wc-frontend-scripts.php 9 years ago class-wc-geo-ip.php 9 years ago class-wc-geolocation.php 9 years ago class-wc-https.php 9 years ago class-wc-install.php 9 years ago class-wc-integrations.php 9 years ago class-wc-legacy-api.php 9 years ago class-wc-log-levels.php 9 years ago class-wc-logger.php 9 years ago class-wc-order-factory.php 9 years ago class-wc-order-item-coupon.php 9 years ago class-wc-order-item-fee.php 9 years ago class-wc-order-item-meta.php 9 years ago class-wc-order-item-product.php 9 years ago class-wc-order-item-shipping.php 9 years ago class-wc-order-item-tax.php 9 years ago class-wc-order-item.php 9 years ago class-wc-order-refund.php 9 years ago class-wc-order.php 9 years ago class-wc-payment-gateways.php 9 years ago class-wc-payment-tokens.php 9 years ago class-wc-post-data.php 9 years ago class-wc-post-types.php 9 years ago class-wc-product-attribute.php 9 years ago class-wc-product-download.php 9 years ago class-wc-product-external.php 9 years ago class-wc-product-factory.php 9 years ago class-wc-product-grouped.php 9 years ago class-wc-product-simple.php 9 years ago class-wc-product-variable.php 9 years ago class-wc-product-variation.php 9 years ago class-wc-query.php 9 years ago class-wc-register-wp-admin-settings.php 9 years ago class-wc-session-handler.php 9 years ago class-wc-shipping-rate.php 9 years ago class-wc-shipping-zone.php 9 years ago class-wc-shipping-zones.php 9 years ago class-wc-shipping.php 9 years ago class-wc-shortcodes.php 9 years ago class-wc-structured-data.php 9 years ago class-wc-tax.php 9 years ago class-wc-template-loader.php 9 years ago class-wc-tracker.php 9 years ago class-wc-validation.php 9 years ago class-wc-webhook.php 9 years ago wc-account-functions.php 9 years ago wc-attribute-functions.php 9 years ago wc-cart-functions.php 9 years ago wc-conditional-functions.php 9 years ago wc-core-functions.php 9 years ago wc-coupon-functions.php 9 years ago wc-deprecated-functions.php 9 years ago wc-formatting-functions.php 9 years ago wc-notice-functions.php 9 years ago wc-order-functions.php 9 years ago wc-order-item-functions.php 9 years ago wc-page-functions.php 9 years ago wc-product-functions.php 9 years ago wc-rest-functions.php 9 years ago wc-stock-functions.php 9 years ago wc-template-functions.php 9 years ago wc-template-hooks.php 9 years ago wc-term-functions.php 9 years ago wc-update-functions.php 9 years ago wc-user-functions.php 9 years ago wc-webhook-functions.php 9 years ago wc-widget-functions.php 9 years ago
wc-term-functions.php
765 lines
1 <?php
2 /**
3 * WooCommerce Terms
4 *
5 * Functions for handling terms/term meta.
6 *
7 * @author WooThemes
8 * @category Core
9 * @package WooCommerce/Functions
10 * @version 2.1.0
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Helper to get cached object terms and filter by field using wp_list_pluck().
19 * Works as a cached alternative for wp_get_post_terms() and wp_get_object_terms().
20 *
21 * @since 3.0.0
22 * @param int $object_id Object ID.
23 * @param string $taxonomy Taxonomy slug.
24 * @param string $field Field name.
25 * @param string $index_key Index key name.
26 * @return array
27 */
28 function wc_get_object_terms( $object_id, $taxonomy, $field = null, $index_key = null ) {
29 // Test if terms exists. get_the_terms() return false when it finds no terms.
30 $terms = get_the_terms( $object_id, $taxonomy );
31
32 if ( $terms && ! is_wp_error( $terms ) ) {
33 if ( ! is_null( $field ) ) {
34 $terms = wp_list_pluck( $terms, $field, $index_key );
35 }
36 } else {
37 $terms = array();
38 }
39
40 return $terms;
41 }
42
43 /**
44 * Cached version of wp_get_post_terms().
45 * This is a private function (internal use ONLY).
46 *
47 * @since 3.0.0
48 * @param int $product_id Product ID.
49 * @param string $taxonomy Taxonomy slug.
50 * @param array $args Query arguments.
51 * @return array
52 */
53 function _wc_get_cached_product_terms( $product_id, $taxonomy, $args = array() ) {
54 $cache_key = 'wc_' . $taxonomy . md5( json_encode( $args ) );
55 $terms = wp_cache_get( $product_id, $cache_key );
56
57 if ( false !== $terms ) {
58 return $terms;
59 }
60
61 // @codingStandardsIgnoreStart
62 $terms = wp_get_post_terms( $product_id, $taxonomy, $args );
63 // @codingStandardsIgnoreEnd
64
65 wp_cache_add( $product_id, $terms, $cache_key );
66
67 return $terms;
68 }
69
70 /**
71 * Wrapper for wp_get_post_terms which supports ordering by parent.
72 *
73 * NOTE: At this point in time, ordering by menu_order for example isn't possible with this function. wp_get_post_terms has no.
74 * filters which we can utilise to modify it's query. https://core.trac.wordpress.org/ticket/19094.
75 *
76 * @param int $product_id Product ID.
77 * @param string $taxonomy Taxonomy slug.
78 * @param array $args Query arguments.
79 * @return array
80 */
81 function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) {
82 if ( ! taxonomy_exists( $taxonomy ) ) {
83 return array();
84 }
85
86 if ( empty( $args['orderby'] ) && taxonomy_is_product_attribute( $taxonomy ) ) {
87 $args['orderby'] = wc_attribute_orderby( $taxonomy );
88 }
89
90 // Support ordering by parent.
91 if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
92 $fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
93 $orderby = $args['orderby'];
94
95 // Unset for wp_get_post_terms.
96 unset( $args['orderby'] );
97 unset( $args['fields'] );
98
99 $terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args );
100
101 switch ( $orderby ) {
102 case 'name_num' :
103 usort( $terms, '_wc_get_product_terms_name_num_usort_callback' );
104 break;
105 case 'parent' :
106 usort( $terms, '_wc_get_product_terms_parent_usort_callback' );
107 break;
108 }
109
110 switch ( $fields ) {
111 case 'names' :
112 $terms = wp_list_pluck( $terms, 'name' );
113 break;
114 case 'ids' :
115 $terms = wp_list_pluck( $terms, 'term_id' );
116 break;
117 case 'slugs' :
118 $terms = wp_list_pluck( $terms, 'slug' );
119 break;
120 }
121 } elseif ( ! empty( $args['orderby'] ) && 'menu_order' === $args['orderby'] ) {
122 // wp_get_post_terms doesn't let us use custom sort order.
123 $args['include'] = wc_get_object_terms( $product_id, $taxonomy, 'term_id' );
124
125 if ( empty( $args['include'] ) ) {
126 $terms = array();
127 } else {
128 // This isn't needed for get_terms.
129 unset( $args['orderby'] );
130
131 // Set args for get_terms.
132 $args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
133 $args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0;
134 $args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names';
135
136 // Ensure slugs is valid for get_terms - slugs isn't supported.
137 $args['fields'] = ( 'slugs' === $args['fields'] ) ? 'id=>slug' : $args['fields'];
138 $terms = get_terms( $taxonomy, $args );
139 }
140 } else {
141 $terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args );
142 }
143
144 return apply_filters( 'woocommerce_get_product_terms' , $terms, $product_id, $taxonomy, $args );
145 }
146
147 /**
148 * Sort by name (numeric).
149 * @param WP_POST object $a
150 * @param WP_POST object $b
151 * @return int
152 */
153 function _wc_get_product_terms_name_num_usort_callback( $a, $b ) {
154 if ( $a->name + 0 === $b->name + 0 ) {
155 return 0;
156 }
157 return ( $a->name + 0 < $b->name + 0 ) ? -1 : 1;
158 }
159
160 /**
161 * Sort by parent.
162 * @param WP_POST object $a
163 * @param WP_POST object $b
164 * @return int
165 */
166 function _wc_get_product_terms_parent_usort_callback( $a, $b ) {
167 if ( $a->parent === $b->parent ) {
168 return 0;
169 }
170 return ( $a->parent < $b->parent ) ? 1 : -1;
171 }
172
173 /**
174 * WooCommerce Dropdown categories.
175 *
176 * Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258.
177 * We use a custom walker, just like WordPress does.
178 *
179 * @param int $deprecated_show_uncategorized (default: 1)
180 * @return string
181 */
182 function wc_product_dropdown_categories( $args = array(), $deprecated_hierarchical = 1, $deprecated_show_uncategorized = 1, $deprecated_orderby = '' ) {
183 global $wp_query;
184
185 if ( ! is_array( $args ) ) {
186 wc_deprecated_argument( 'wc_product_dropdown_categories()', '2.1', 'show_counts, hierarchical, show_uncategorized and orderby arguments are invalid - pass a single array of values instead.' );
187
188 $args['show_count'] = $args;
189 $args['hierarchical'] = $deprecated_hierarchical;
190 $args['show_uncategorized'] = $deprecated_show_uncategorized;
191 $args['orderby'] = $deprecated_orderby;
192 }
193
194 $current_product_cat = isset( $wp_query->query_vars['product_cat'] ) ? $wp_query->query_vars['product_cat'] : '';
195 $defaults = array(
196 'pad_counts' => 1,
197 'show_count' => 1,
198 'hierarchical' => 1,
199 'hide_empty' => 1,
200 'show_uncategorized' => 1,
201 'orderby' => 'name',
202 'selected' => $current_product_cat,
203 'menu_order' => false,
204 );
205
206 $args = wp_parse_args( $args, $defaults );
207
208 if ( 'order' === $args['orderby'] ) {
209 $args['menu_order'] = 'asc';
210 $args['orderby'] = 'name';
211 }
212
213 $terms = get_terms( 'product_cat', apply_filters( 'wc_product_dropdown_categories_get_terms_args', $args ) );
214
215 if ( empty( $terms ) ) {
216 return;
217 }
218
219 $output = "<select name='product_cat' class='dropdown_product_cat'>";
220 $output .= '<option value="" ' . selected( $current_product_cat, '', false ) . '>' . esc_html__( 'Select a category', 'woocommerce' ) . '</option>';
221 $output .= wc_walk_category_dropdown_tree( $terms, 0, $args );
222 if ( $args['show_uncategorized'] ) {
223 $output .= '<option value="0" ' . selected( $current_product_cat, '0', false ) . '>' . esc_html__( 'Uncategorized', 'woocommerce' ) . '</option>';
224 }
225 $output .= "</select>";
226
227 echo $output;
228 }
229
230 /**
231 * Walk the Product Categories.
232 *
233 * @return mixed
234 */
235 function wc_walk_category_dropdown_tree() {
236 $args = func_get_args();
237
238 if ( ! class_exists( 'WC_Product_Cat_Dropdown_Walker', false ) ) {
239 include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php' );
240 }
241
242 // the user's options are the third parameter
243 if ( empty( $args[2]['walker'] ) || ! is_a( $args[2]['walker'], 'Walker' ) ) {
244 $walker = new WC_Product_Cat_Dropdown_Walker;
245 } else {
246 $walker = $args[2]['walker'];
247 }
248
249 return call_user_func_array( array( &$walker, 'walk' ), $args );
250 }
251
252 /**
253 * When a term is split, ensure meta data maintained.
254 * @param int $old_term_id
255 * @param int $new_term_id
256 * @param string $term_taxonomy_id
257 * @param string $taxonomy
258 */
259 function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
260 global $wpdb;
261
262 if ( get_option( 'db_version' ) < 34370 ) {
263 if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
264 $old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
265
266 // Copy across to split term
267 if ( $old_meta_data ) {
268 foreach ( $old_meta_data as $meta_data ) {
269 $wpdb->insert(
270 "{$wpdb->prefix}woocommerce_termmeta",
271 array(
272 'woocommerce_term_id' => $new_term_id,
273 'meta_key' => $meta_data->meta_key,
274 'meta_value' => $meta_data->meta_value,
275 )
276 );
277 }
278 }
279 }
280 }
281 }
282 add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_terms', 10, 4 );
283
284 /**
285 * Migrate data from WC term meta to WP term meta
286 *
287 * When the database is updated to support term meta, migrate WC term meta data across.
288 * We do this when the new version is >= 34370, and the old version is < 34370 (34370 is when term meta table was added).
289 *
290 * @param string $wp_db_version The new $wp_db_version.
291 * @param string $wp_current_db_version The old (current) $wp_db_version.
292 */
293 function wc_taxonomy_metadata_migrate_data( $wp_db_version, $wp_current_db_version ) {
294 if ( $wp_db_version >= 34370 && $wp_current_db_version < 34370 ) {
295 global $wpdb;
296 if ( $wpdb->query( "INSERT INTO {$wpdb->termmeta} ( term_id, meta_key, meta_value ) SELECT woocommerce_term_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta;" ) ) {
297 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
298 }
299 }
300 }
301 add_action( 'wp_upgrade', 'wc_taxonomy_metadata_migrate_data', 10, 2 );
302
303 /**
304 * WooCommerce Term Meta API
305 *
306 * WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
307 * This function serves as a wrapper, using the new table if present, or falling back to the WC table.
308 *
309 * @param mixed $term_id
310 * @param string $meta_key
311 * @param mixed $meta_value
312 * @param string $prev_value (default: '')
313 * @return bool
314 */
315 function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
316 return function_exists( 'update_term_meta' ) ? update_term_meta( $term_id, $meta_key, $meta_value, $prev_value ) : update_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $prev_value );
317 }
318
319 /**
320 * WooCommerce Term Meta API
321 *
322 * WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
323 * This function serves as a wrapper, using the new table if present, or falling back to the WC table.
324 *
325 * @param mixed $term_id
326 * @param mixed $meta_key
327 * @param mixed $meta_value
328 * @param bool $unique (default: false)
329 * @return bool
330 */
331 function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
332 return function_exists( 'add_term_meta' ) ? add_term_meta( $term_id, $meta_key, $meta_value, $unique ) : add_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $unique );
333 }
334
335 /**
336 * WooCommerce Term Meta API
337 *
338 * WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
339 * This function serves as a wrapper, using the new table if present, or falling back to the WC table.
340 *
341 * @param mixed $term_id
342 * @param string $meta_key
343 * @param string $meta_value (default: '')
344 * @param bool $deprecated (default: false)
345 * @return bool
346 */
347 function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $deprecated = false ) {
348 return function_exists( 'delete_term_meta' ) ? delete_term_meta( $term_id, $meta_key, $meta_value ) : delete_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value );
349 }
350
351 /**
352 * WooCommerce Term Meta API
353 *
354 * WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
355 * This function serves as a wrapper, using the new table if present, or falling back to the WC table.
356 *
357 * @param mixed $term_id
358 * @param string $key
359 * @param bool $single (default: true)
360 * @return mixed
361 */
362 function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
363 return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single );
364 }
365
366 /**
367 * Move a term before the a given element of its hierarchy level.
368 *
369 * @param int $the_term
370 * @param int $next_id the id of the next sibling element in save hierarchy level
371 * @param string $taxonomy
372 * @param int $index (default: 0)
373 * @param mixed $terms (default: null)
374 * @return int
375 */
376 function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) {
377 if ( ! $terms ) $terms = get_terms( $taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' );
378 if ( empty( $terms ) ) return $index;
379
380 $id = $the_term->term_id;
381
382 $term_in_level = false; // flag: is our term to order in this level of terms
383
384 foreach ( $terms as $term ) {
385
386 if ( $term->term_id == $id ) { // our term to order, we skip
387 $term_in_level = true;
388 continue; // our term to order, we skip
389 }
390 // the nextid of our term to order, lets move our term here
391 if ( null !== $next_id && $term->term_id == $next_id ) {
392 $index++;
393 $index = wc_set_term_order( $id, $index, $taxonomy, true );
394 }
395
396 // set order
397 $index++;
398 $index = wc_set_term_order( $term->term_id, $index, $taxonomy );
399
400 // if that term has children we walk through them
401 $children = get_terms( $taxonomy, "parent={$term->term_id}&menu_order=ASC&hide_empty=0" );
402 if ( ! empty( $children ) ) {
403 $index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children );
404 }
405 }
406
407 // no nextid meaning our term is in last position
408 if ( $term_in_level && null === $next_id ) {
409 $index = wc_set_term_order( $id, $index + 1, $taxonomy, true );
410 }
411
412 return $index;
413 }
414
415 /**
416 * Set the sort order of a term.
417 *
418 * @param int $term_id
419 * @param int $index
420 * @param string $taxonomy
421 * @param bool $recursive (default: false)
422 * @return int
423 */
424 function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) {
425
426 $term_id = (int) $term_id;
427 $index = (int) $index;
428
429 // Meta name
430 if ( taxonomy_is_product_attribute( $taxonomy ) )
431 $meta_name = 'order_' . esc_attr( $taxonomy );
432 else
433 $meta_name = 'order';
434
435 update_woocommerce_term_meta( $term_id, $meta_name, $index );
436
437 if ( ! $recursive ) return $index;
438
439 $children = get_terms( $taxonomy, "parent=$term_id&menu_order=ASC&hide_empty=0" );
440
441 foreach ( $children as $term ) {
442 $index++;
443 $index = wc_set_term_order( $term->term_id, $index, $taxonomy, true );
444 }
445
446 clean_term_cache( $term_id, $taxonomy );
447
448 return $index;
449 }
450
451 /**
452 * Add term ordering to get_terms.
453 *
454 * It enables the support a 'menu_order' parameter to get_terms for the product_cat taxonomy.
455 * By default it is 'ASC'. It accepts 'DESC' too.
456 *
457 * To disable it, set it ot false (or 0).
458 *
459 * @param array $clauses
460 * @param array $taxonomies
461 * @param array $args
462 * @return array
463 */
464 function wc_terms_clauses( $clauses, $taxonomies, $args ) {
465 global $wpdb;
466
467 // No sorting when menu_order is false.
468 if ( isset( $args['menu_order'] ) && ( false === $args['menu_order'] || 'false' === $args['menu_order'] ) ) {
469 return $clauses;
470 }
471
472 // No sorting when orderby is non default.
473 if ( isset( $args['orderby'] ) && 'name' !== $args['orderby'] ) {
474 return $clauses;
475 }
476
477 // No sorting in admin when sorting by a column.
478 if ( is_admin() && isset( $_GET['orderby'] ) ) {
479 return $clauses;
480 }
481
482 // No need to filter counts
483 if ( strpos( 'COUNT(*)', $clauses['fields'] ) !== false ) {
484 return $clauses;
485 }
486
487 // WordPress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes.
488 $found = false;
489 foreach ( (array) $taxonomies as $taxonomy ) {
490 if ( taxonomy_is_product_attribute( $taxonomy ) || in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ) ) ) {
491 $found = true;
492 break;
493 }
494 }
495 if ( ! $found ) {
496 return $clauses;
497 }
498
499 // Meta name.
500 if ( ! empty( $taxonomies[0] ) && taxonomy_is_product_attribute( $taxonomies[0] ) ) {
501 $meta_name = 'order_' . esc_attr( $taxonomies[0] );
502 } else {
503 $meta_name = 'order';
504 }
505
506 // Query fields.
507 $clauses['fields'] = 'DISTINCT ' . $clauses['fields'] . ', tm.meta_value';
508
509 // Query join.
510 if ( get_option( 'db_version' ) < 34370 ) {
511 $clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') ";
512 } else {
513 $clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON (t.term_id = tm.term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') ";
514 }
515
516 // Default to ASC.
517 if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper( $args['menu_order'] ), array( 'ASC', 'DESC' ) ) ) {
518 $args['menu_order'] = 'ASC';
519 }
520
521 $order = "ORDER BY tm.meta_value+0 " . $args['menu_order'];
522
523 if ( $clauses['orderby'] ) {
524 $clauses['orderby'] = str_replace( 'ORDER BY', $order . ',', $clauses['orderby'] );
525 } else {
526 $clauses['orderby'] = $order;
527 }
528
529 return $clauses;
530 }
531
532 add_filter( 'terms_clauses', 'wc_terms_clauses', 10, 3 );
533
534 /**
535 * Function for recounting product terms, ignoring hidden products.
536 *
537 * @param array $terms
538 * @param string $taxonomy
539 * @param bool $callback
540 * @param bool $terms_are_term_taxonomy_ids
541 */
542 function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) {
543 global $wpdb;
544
545 // Standard callback.
546 if ( $callback ) {
547 _update_post_term_count( $terms, $taxonomy );
548 }
549
550 // Main query.
551 $count_query = "
552 SELECT COUNT( DISTINCT posts.ID ) FROM {$wpdb->posts} as posts
553 LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
554 LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
555 WHERE post_status = 'publish'
556 AND post_type = 'product'
557 ";
558
559 $product_visibility_term_ids = wc_get_product_visibility_term_ids();
560
561 if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
562 $count_query .= " AND term_taxonomy_id !=" . $product_visibility_term_ids['exclude-from-catalog'];
563 }
564
565 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
566 $count_query .= " AND term_taxonomy_id !=" . $product_visibility_term_ids['outofstock'];
567 }
568
569 // Pre-process term taxonomy ids.
570 if ( ! $terms_are_term_taxonomy_ids ) {
571 // We passed in an array of TERMS in format id=>parent.
572 $terms = array_filter( (array) array_keys( $terms ) );
573 } else {
574 // If we have term taxonomy IDs we need to get the term ID.
575 $term_taxonomy_ids = $terms;
576 $terms = array();
577 foreach ( $term_taxonomy_ids as $term_taxonomy_id ) {
578 $term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name );
579 $terms[] = $term->term_id;
580 }
581 }
582
583 // Exit if we have no terms to count.
584 if ( empty( $terms ) ) {
585 return;
586 }
587
588 // Ancestors need counting.
589 if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
590 foreach ( $terms as $term_id ) {
591 $terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) );
592 }
593 }
594
595 // Unique terms only.
596 $terms = array_unique( $terms );
597
598 // Count the terms.
599 foreach ( $terms as $term_id ) {
600 $terms_to_count = array( absint( $term_id ) );
601
602 if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
603 // We need to get the $term's hierarchy so we can count its children too
604 if ( ( $children = get_term_children( $term_id, $taxonomy->name ) ) && ! is_wp_error( $children ) ) {
605 $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
606 }
607 }
608
609 // Generate term query
610 $term_query = ' AND term_id IN ( ' . implode( ',', $terms_to_count ) . ' )';
611
612 // Get the count
613 $count = $wpdb->get_var( $count_query . $term_query );
614
615 // Update the count
616 update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
617 }
618
619 delete_transient( 'wc_term_counts' );
620 }
621
622 /**
623 * Recount terms after the stock amount changes.
624 *
625 * @param int $product_id
626 */
627 function wc_recount_after_stock_change( $product_id ) {
628 if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
629 return;
630 }
631
632 $product_terms = get_the_terms( $product_id, 'product_cat' );
633
634 if ( $product_terms ) {
635 $product_cats = array();
636
637 foreach ( $product_terms as $term ) {
638 $product_cats[ $term->term_id ] = $term->parent;
639 }
640
641 _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false );
642 }
643
644 $product_terms = get_the_terms( $product_id, 'product_tag' );
645
646 if ( $product_terms ) {
647 $product_tags = array();
648
649 foreach ( $product_terms as $term ) {
650 $product_tags[ $term->term_id ] = $term->parent;
651 }
652
653 _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false );
654 }
655 }
656 add_action( 'woocommerce_product_set_stock_status', 'wc_recount_after_stock_change' );
657
658
659 /**
660 * Overrides the original term count for product categories and tags with the product count.
661 * that takes catalog visibility into account.
662 *
663 * @param array $terms
664 * @param string|array $taxonomies
665 * @return array
666 */
667 function wc_change_term_counts( $terms, $taxonomies ) {
668 if ( is_admin() || is_ajax() ) {
669 return $terms;
670 }
671
672 if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ) ) ) {
673 return $terms;
674 }
675
676 $term_counts = $o_term_counts = get_transient( 'wc_term_counts' );
677
678 foreach ( $terms as &$term ) {
679 if ( is_object( $term ) ) {
680 $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_woocommerce_term_meta( $term->term_id, 'product_count_' . $taxonomies[0] , true );
681
682 if ( '' !== $term_counts[ $term->term_id ] ) {
683 $term->count = absint( $term_counts[ $term->term_id ] );
684 }
685 }
686 }
687
688 // Update transient
689 if ( $term_counts != $o_term_counts ) {
690 set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 );
691 }
692
693 return $terms;
694 }
695 add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );
696
697 /**
698 * Return products in a given term, and cache value.
699 *
700 * To keep in sync, product_count will be cleared on "set_object_terms".
701 *
702 * @param int $term_id
703 * @param string $taxonomy
704 * @return array
705 */
706 function wc_get_term_product_ids( $term_id, $taxonomy ) {
707 $product_ids = get_woocommerce_term_meta( $term_id, 'product_ids', true );
708
709 if ( false === $product_ids || ! is_array( $product_ids ) ) {
710 $product_ids = get_objects_in_term( $term_id, $taxonomy );
711 update_woocommerce_term_meta( $term_id, 'product_ids', $product_ids );
712 }
713
714 return $product_ids;
715 }
716
717 /**
718 * When a post is updated and terms recounted (called by _update_post_term_count), clear the ids.
719 * @param int $object_id Object ID.
720 * @param array $terms An array of object terms.
721 * @param array $tt_ids An array of term taxonomy IDs.
722 * @param string $taxonomy Taxonomy slug.
723 * @param bool $append Whether to append new terms to the old terms.
724 * @param array $old_tt_ids Old array of term taxonomy IDs.
725 */
726 function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
727 foreach ( $old_tt_ids as $term_id ) {
728 delete_woocommerce_term_meta( $term_id, 'product_ids' );
729 }
730 foreach ( $tt_ids as $term_id ) {
731 delete_woocommerce_term_meta( $term_id, 'product_ids' );
732 }
733 }
734 add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 );
735
736 /**
737 * Get full list of product visibilty term ids.
738 *
739 * @since 3.0.0
740 * @return int[]
741 */
742 function wc_get_product_visibility_term_ids() {
743 return array_map( 'absint', wp_parse_args(
744 wp_list_pluck(
745 get_terms( array(
746 'taxonomy' => 'product_visibility',
747 'hide_empty' => false,
748 ) ),
749 'term_taxonomy_id',
750 'name'
751 ),
752 array(
753 'exclude-from-catalog' => 0,
754 'exclude-from-search' => 0,
755 'featured' => 0,
756 'outofstock' => 0,
757 'rated-1' => 0,
758 'rated-2' => 0,
759 'rated-3' => 0,
760 'rated-4' => 0,
761 'rated-5' => 0,
762 )
763 ) );
764 }
765