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3.2.1 6.3.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 4 weeks ago admin 4 weeks ago blocks 10 months ago cli 7 months ago customizer 3 months ago data-stores 3 weeks ago emails 3 weeks ago export 1 year ago gateways 2 months ago import 2 months ago integrations 4 weeks ago interfaces 3 months ago legacy 3 months ago libraries 1 year ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 3 months ago react-admin 3 months ago rest-api 4 weeks ago shipping 2 months ago shortcodes 2 months ago theme-support 2 years ago tracks 3 months ago traits 5 years ago walkers 5 years ago wccom-site 4 weeks ago widgets 4 weeks ago class-wc-ajax.php 4 weeks ago class-wc-auth.php 1 year ago class-wc-autoloader.php 7 months ago class-wc-background-emailer.php 4 weeks ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 4 months ago class-wc-breadcrumb.php 3 months ago class-wc-cache-helper.php 4 weeks ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 months ago class-wc-cart-totals.php 10 months ago class-wc-cart.php 2 months ago class-wc-checkout.php 4 weeks ago class-wc-cli.php 9 months ago class-wc-comments.php 3 months ago class-wc-countries.php 4 weeks ago class-wc-coupon.php 4 weeks ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 4 weeks ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 2 months ago class-wc-discounts.php 10 months ago class-wc-download-handler.php 1 year ago class-wc-emails.php 3 weeks ago class-wc-embed.php 1 year ago class-wc-form-handler.php 2 months ago class-wc-frontend-scripts.php 4 weeks ago class-wc-geo-ip.php 7 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 4 weeks ago class-wc-https.php 2 years ago class-wc-install.php 3 weeks ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 3 months ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 4 weeks ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 weeks ago class-wc-order-item-shipping.php 4 months ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 4 months ago class-wc-order-query.php 3 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 3 weeks ago class-wc-payment-gateways.php 4 weeks ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 4 weeks ago class-wc-post-types.php 4 weeks ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 9 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 11 months ago class-wc-product-attribute.php 3 months ago class-wc-product-download.php 3 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 2 months ago class-wc-product-grouped.php 2 months ago class-wc-product-query.php 3 months ago class-wc-product-simple.php 10 months ago class-wc-product-variable.php 2 months ago class-wc-product-variation.php 1 year ago class-wc-query.php 4 weeks ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 months ago class-wc-shipping-rate.php 11 months ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 6 months ago class-wc-shipping.php 4 weeks ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 4 weeks ago class-wc-tax.php 4 weeks ago class-wc-template-loader.php 6 months ago class-wc-tracker.php 7 months ago class-wc-validation.php 2 years ago class-wc-webhook.php 4 weeks ago class-woocommerce.php 3 weeks ago wc-account-functions.php 6 months ago wc-attribute-functions.php 4 weeks ago wc-brands-functions.php 1 year ago wc-cart-functions.php 4 months ago wc-conditional-functions.php 10 months ago wc-core-functions.php 4 weeks ago wc-coupon-functions.php 4 months ago wc-deprecated-functions.php 3 months ago wc-formatting-functions.php 6 months ago wc-interactivity-api-functions.php 4 weeks ago wc-notice-functions.php 4 months ago wc-order-functions.php 3 weeks ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 3 months ago wc-page-functions.php 3 weeks ago wc-product-functions.php 4 weeks ago wc-rest-functions.php 6 months ago wc-stock-functions.php 6 months ago wc-template-functions.php 4 weeks ago wc-template-hooks.php 9 months ago wc-term-functions.php 4 weeks ago wc-update-functions.php 3 weeks ago wc-user-functions.php 4 weeks ago wc-webhook-functions.php 4 weeks ago wc-widget-functions.php 5 years ago
wc-term-functions.php
774 lines
1 <?php
2 /**
3 * WooCommerce Terms
4 *
5 * Functions for handling terms/term meta.
6 *
7 * @package WooCommerce\Functions
8 * @version 2.1.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 use Automattic\WooCommerce\Enums\ProductStockStatus;
14
15 /**
16 * Change get terms defaults for attributes to order by the sorting setting, or default to menu_order for sortable taxonomies.
17 *
18 * @since 3.6.0 Sorting options are now set as the default automatically, so you no longer have to request to orderby menu_order.
19 *
20 * @param array $defaults An array of default get_terms() arguments.
21 * @param array $taxonomies An array of taxonomies.
22 * @return array
23 */
24 function wc_change_get_terms_defaults( $defaults, $taxonomies ) {
25 if ( is_array( $taxonomies ) && 1 < count( $taxonomies ) ) {
26 return $defaults;
27 }
28 $taxonomy = is_array( $taxonomies ) ? (string) current( $taxonomies ) : $taxonomies;
29 $orderby = 'name';
30
31 if ( taxonomy_is_product_attribute( $taxonomy ) ) {
32 $orderby = wc_attribute_orderby( $taxonomy );
33 } elseif ( in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ), true ) ) {
34 $orderby = 'menu_order';
35 }
36
37 // Change defaults. Invalid values will be changed later @see wc_change_pre_get_terms.
38 // These are in place so we know if a specific order was requested.
39 switch ( $orderby ) {
40 case 'menu_order':
41 case 'name_num':
42 case 'parent':
43 $defaults['orderby'] = $orderby;
44 break;
45 }
46
47 return $defaults;
48 }
49 add_filter( 'get_terms_defaults', 'wc_change_get_terms_defaults', 10, 2 );
50
51 /**
52 * Adds support to get_terms for menu_order argument.
53 *
54 * @since 3.6.0
55 * @param WP_Term_Query $terms_query Instance of WP_Term_Query.
56 */
57 function wc_change_pre_get_terms( $terms_query ) {
58 $args = &$terms_query->query_vars;
59
60 // Put back valid orderby values.
61 if ( 'menu_order' === $args['orderby'] ) {
62 $args['orderby'] = 'name';
63 $args['force_menu_order_sort'] = true;
64 }
65
66 if ( 'name_num' === $args['orderby'] ) {
67 $args['orderby'] = 'name';
68 $args['force_numeric_name'] = true;
69 }
70
71 // When COUNTING, disable custom sorting.
72 if ( 'count' === $args['fields'] ) {
73 return;
74 }
75
76 // Support menu_order arg used in previous versions.
77 if ( ! empty( $args['menu_order'] ) ) {
78 $args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC';
79 $args['force_menu_order_sort'] = true;
80 }
81
82 if ( ! empty( $args['force_menu_order_sort'] ) ) {
83 $args['orderby'] = 'meta_value_num';
84 $args['meta_key'] = 'order'; // phpcs:ignore
85 $terms_query->meta_query->parse_query_vars( $args );
86 }
87 }
88 add_action( 'pre_get_terms', 'wc_change_pre_get_terms', 10, 1 );
89
90 /**
91 * Adjust term query to handle custom sorting parameters.
92 *
93 * @param array $clauses Clauses.
94 * @param array $taxonomies Taxonomies.
95 * @param array $args Arguments.
96 * @return array
97 */
98 function wc_terms_clauses( $clauses, $taxonomies, $args ) {
99 global $wpdb;
100
101 // No need to filter when counting.
102 if ( strpos( $clauses['fields'], 'COUNT(*)' ) !== false ) {
103 return $clauses;
104 }
105
106 // Force numeric sort if using name_num custom sorting param.
107 if ( ! empty( $args['force_numeric_name'] ) ) {
108 $clauses['orderby'] = str_replace( 'ORDER BY t.name', 'ORDER BY t.name+0', $clauses['orderby'] );
109 }
110
111 // For sorting, force left join in case order meta is missing.
112 if ( ! empty( $args['force_menu_order_sort'] ) ) {
113 $clauses['join'] = str_replace( "INNER JOIN {$wpdb->termmeta} ON ( t.term_id = {$wpdb->termmeta}.term_id )", "LEFT JOIN {$wpdb->termmeta} ON ( t.term_id = {$wpdb->termmeta}.term_id AND {$wpdb->termmeta}.meta_key='order')", $clauses['join'] );
114 $clauses['where'] = str_replace( "{$wpdb->termmeta}.meta_key = 'order'", "( {$wpdb->termmeta}.meta_key = 'order' OR {$wpdb->termmeta}.meta_key IS NULL )", $clauses['where'] );
115 $clauses['orderby'] = 'DESC' === $args['order'] ? str_replace( 'meta_value+0', 'meta_value+0 DESC, t.name', $clauses['orderby'] ) : str_replace( 'meta_value+0', 'meta_value+0 ASC, t.name', $clauses['orderby'] );
116 }
117
118 return $clauses;
119 }
120 add_filter( 'terms_clauses', 'wc_terms_clauses', 99, 3 );
121
122 /**
123 * Helper to get cached object terms and filter by field using wp_list_pluck().
124 * Works as a cached alternative for wp_get_post_terms() and wp_get_object_terms().
125 *
126 * @since 3.0.0
127 * @param int $object_id Object ID.
128 * @param string $taxonomy Taxonomy slug.
129 * @param string $field Field name.
130 * @param string $index_key Index key name.
131 * @return array
132 */
133 function wc_get_object_terms( $object_id, $taxonomy, $field = null, $index_key = null ) {
134 // Test if terms exists. get_the_terms() return false when it finds no terms.
135 $terms = get_the_terms( $object_id, $taxonomy );
136
137 if ( ! $terms || is_wp_error( $terms ) ) {
138 return array();
139 }
140
141 return is_null( $field ) ? $terms : wp_list_pluck( $terms, $field, $index_key );
142 }
143
144 /**
145 * Cached version of wp_get_post_terms().
146 * This is a private function (internal use ONLY).
147 *
148 * @since 3.0.0
149 * @param int $product_id Product ID.
150 * @param string $taxonomy Taxonomy slug.
151 * @param array $args Query arguments.
152 * @return array
153 */
154 function _wc_get_cached_product_terms( $product_id, $taxonomy, $args = array() ) {
155 $cache_key = 'wc_' . $taxonomy . md5( wp_json_encode( $args ) );
156 $cache_group = WC_Cache_Helper::get_cache_prefix( 'product_' . $product_id ) . $product_id;
157 $terms = wp_cache_get( $cache_key, $cache_group );
158
159 if ( false !== $terms ) {
160 return $terms;
161 }
162
163 $terms = wp_get_post_terms( $product_id, $taxonomy, $args );
164
165 wp_cache_add( $cache_key, $terms, $cache_group );
166
167 return $terms;
168 }
169
170 /**
171 * Wrapper used to get terms for a product.
172 *
173 * @param int $product_id Product ID.
174 * @param string $taxonomy Taxonomy slug.
175 * @param array $args Query arguments.
176 * @return array
177 */
178 function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) {
179 if ( ! taxonomy_exists( $taxonomy ) ) {
180 return array();
181 }
182
183 return apply_filters( 'woocommerce_get_product_terms', _wc_get_cached_product_terms( $product_id, $taxonomy, $args ), $product_id, $taxonomy, $args );
184 }
185
186 /**
187 * Sort by name (numeric).
188 *
189 * @param WP_Post $a First item to compare.
190 * @param WP_Post $b Second item to compare.
191 * @return int
192 */
193 function _wc_get_product_terms_name_num_usort_callback( $a, $b ) {
194 $a_name = (float) $a->name;
195 $b_name = (float) $b->name;
196
197 if ( abs( $a_name - $b_name ) < 0.001 ) {
198 return 0;
199 }
200
201 return ( $a_name < $b_name ) ? -1 : 1;
202 }
203
204 /**
205 * Sort by parent.
206 *
207 * @param WP_Post $a First item to compare.
208 * @param WP_Post $b Second item to compare.
209 * @return int
210 */
211 function _wc_get_product_terms_parent_usort_callback( $a, $b ) {
212 if ( $a->parent === $b->parent ) {
213 return 0;
214 }
215 return ( $a->parent < $b->parent ) ? 1 : -1;
216 }
217
218 /**
219 * WooCommerce Dropdown categories.
220 *
221 * @param array $args Args to control display of dropdown.
222 */
223 function wc_product_dropdown_categories( $args = array() ) {
224 global $wp_query;
225
226 $args = wp_parse_args(
227 $args,
228 array(
229 'pad_counts' => 1,
230 'show_count' => 1,
231 'hierarchical' => 1,
232 'hide_empty' => 1,
233 'show_uncategorized' => 1,
234 'orderby' => 'name',
235 'selected' => isset( $wp_query->query_vars['product_cat'] ) ? $wp_query->query_vars['product_cat'] : '',
236 'show_option_none' => __( 'Select a category', 'woocommerce' ),
237 'option_none_value' => '',
238 'value_field' => 'slug',
239 'taxonomy' => 'product_cat',
240 'name' => 'product_cat',
241 'class' => 'dropdown_product_cat',
242 )
243 );
244
245 if ( 'order' === $args['orderby'] ) {
246 $args['orderby'] = 'meta_value_num';
247 $args['meta_key'] = 'order'; // phpcs:ignore
248 }
249
250 wp_dropdown_categories( $args );
251 }
252
253 /**
254 * Custom walker for Product Categories.
255 *
256 * Previously used by wc_product_dropdown_categories, but wp_dropdown_categories has been fixed in core.
257 *
258 * @param mixed ...$args Variable number of parameters to be passed to the walker.
259 * @return mixed
260 */
261 function wc_walk_category_dropdown_tree( ...$args ) {
262 if ( ! class_exists( 'WC_Product_Cat_Dropdown_Walker', false ) ) {
263 include_once WC()->plugin_path() . '/includes/walkers/class-wc-product-cat-dropdown-walker.php';
264 }
265
266 // The user's options are the third parameter.
267 if ( empty( $args[2]['walker'] ) || ! is_a( $args[2]['walker'], 'Walker' ) ) {
268 $walker = new WC_Product_Cat_Dropdown_Walker();
269 } else {
270 $walker = $args[2]['walker'];
271 }
272
273 return $walker->walk( ...$args );
274 }
275
276 /**
277 * Migrate data from WC term meta to WP term meta.
278 *
279 * When the database is updated to support term meta, migrate WC term meta data across.
280 * We do this when the new version is >= 34370, and the old version is < 34370 (34370 is when term meta table was added).
281 *
282 * @param string $wp_db_version The new $wp_db_version.
283 * @param string $wp_current_db_version The old (current) $wp_db_version.
284 */
285 function wc_taxonomy_metadata_migrate_data( $wp_db_version, $wp_current_db_version ) {
286 if ( $wp_db_version >= 34370 && $wp_current_db_version < 34370 ) {
287 global $wpdb;
288 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;" ) ) {
289 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
290 }
291 }
292 }
293 add_action( 'wp_upgrade', 'wc_taxonomy_metadata_migrate_data', 10, 2 );
294
295 /**
296 * Move a term before the a given element of its hierarchy level.
297 *
298 * @param int $the_term Term ID.
299 * @param int $next_id The id of the next sibling element in save hierarchy level.
300 * @param string $taxonomy Taxonomy.
301 * @param int $index Term index (default: 0).
302 * @param mixed $terms List of terms. (default: null).
303 * @return int
304 */
305 function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) {
306 if ( ! $terms ) {
307 $terms = get_terms( $taxonomy, 'hide_empty=0&parent=0&menu_order=ASC' );
308 }
309 if ( empty( $terms ) ) {
310 return $index;
311 }
312
313 $id = intval( $the_term->term_id );
314
315 $term_in_level = false; // Flag: is our term to order in this level of terms.
316
317 foreach ( $terms as $term ) {
318 $term_id = intval( $term->term_id );
319
320 if ( $term_id === $id ) { // Our term to order, we skip.
321 $term_in_level = true;
322 continue; // Our term to order, we skip.
323 }
324 // the nextid of our term to order, lets move our term here.
325 if ( null !== $next_id && $term_id === $next_id ) {
326 ++$index;
327 $index = wc_set_term_order( $id, $index, $taxonomy, true );
328 }
329
330 // Set order.
331 ++$index;
332 $index = wc_set_term_order( $term_id, $index, $taxonomy );
333
334 /**
335 * After a term has had it's order set.
336 */
337 do_action( 'woocommerce_after_set_term_order', $term, $index, $taxonomy );
338
339 // If that term has children we walk through them.
340 $children = get_terms( $taxonomy, "parent={$term_id}&hide_empty=0&menu_order=ASC" );
341 if ( ! empty( $children ) ) {
342 $index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children );
343 }
344 }
345
346 // No nextid meaning our term is in last position.
347 if ( $term_in_level && null === $next_id ) {
348 $index = wc_set_term_order( $id, $index + 1, $taxonomy, true );
349 }
350
351 return $index;
352 }
353
354 /**
355 * Set the sort order of a term.
356 *
357 * @param int $term_id Term ID.
358 * @param int $index Index.
359 * @param string $taxonomy Taxonomy.
360 * @param bool $recursive Recursive (default: false).
361 * @return int
362 */
363 function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) {
364
365 $term_id = (int) $term_id;
366 $index = (int) $index;
367
368 update_term_meta( $term_id, 'order', $index );
369
370 if ( ! $recursive ) {
371 return $index;
372 }
373
374 $children = get_terms( $taxonomy, "parent=$term_id&hide_empty=0&menu_order=ASC" );
375
376 foreach ( $children as $term ) {
377 ++$index;
378 $index = wc_set_term_order( $term->term_id, $index, $taxonomy, true );
379 }
380
381 clean_term_cache( $term_id, $taxonomy );
382
383 return $index;
384 }
385
386 /**
387 * Function for recounting product terms, ignoring hidden products.
388 *
389 * This is used as the update_count_callback for the Product Category, Product Tag, and Product Brand
390 * taxonomies. By default, it actually calculates two (possibly different) counts for each
391 * term, which it stores in two different places. The first count is the one done by WordPress
392 * itself, and is based on the status of the objects that are assigned the terms. In this case,
393 * only products with the publish status are counted. This count is stored in the
394 * `wp_term_taxonomy` table in the `count` field.
395 *
396 * The second count is based on WooCommerce-specific characteristics. In addition to the
397 * publish status requirement, products are only counted if they are considered visible in the
398 * catalog. This count is stored in the `wp_termmeta` table. The wc_change_term_counts function
399 * is used to override the first count with the second count in some circumstances.
400 *
401 * Since the first count only needs to be recalculated when a product status is changed in some
402 * way, it can sometimes be skipped (thus avoiding some potentially expensive queries). Setting
403 * the $callback parameter to false skips the first count.
404 *
405 * @param array $terms List of terms. For legacy reasons, this can
406 * either be a list of taxonomy term IDs or an
407 * associative array in the format of
408 * term ID > parent term ID.
409 * @param WP_Taxonomy $taxonomy The relevant taxonomy.
410 * @param bool $callback Whether to also recalculate the term counts
411 * using the WP Core callback. Default true.
412 * @param bool $terms_are_term_taxonomy_ids Flag to indicate which format the list of
413 * terms is in. Default true, which indicates
414 * that it is a list of taxonomy term IDs.
415 */
416 function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) {
417 global $wpdb;
418
419 /**
420 * Filter to allow/prevent recounting of terms as it could be expensive.
421 * A likely scenario for this is when bulk importing products. We could
422 * then prevent it from recounting per product but instead recount it once
423 * when import is done. Of course this means the import logic has to support this.
424 *
425 * @since 5.2
426 * @param bool
427 */
428 if ( ! apply_filters( 'woocommerce_product_recount_terms', true ) ) {
429 return;
430 }
431
432 if ( true === $terms_are_term_taxonomy_ids ) {
433 $taxonomy_term_ids = $terms;
434 $term_ids = array_map(
435 function ( $term_taxonomy_id ) use ( $taxonomy ) {
436 $term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name );
437 return $term instanceof WP_Term ? $term->term_id : null;
438 },
439 $terms
440 );
441 } else {
442 $taxonomy_term_ids = array(); // Defer querying these until the callback check.
443 $term_ids = array_keys( $terms );
444 }
445
446 $term_ids = array_unique( array_filter( $term_ids ) );
447 $taxonomy_term_ids = array_unique( array_filter( $taxonomy_term_ids ) );
448
449 // Exit if we have no terms to count.
450 if ( empty( $term_ids ) ) {
451 return;
452 }
453
454 // Standard WP callback for calculating post term counts.
455 if ( $callback ) {
456 if ( count( $taxonomy_term_ids ) < 1 ) {
457 $taxonomy_term_ids = array_map(
458 function ( $term_id ) use ( $taxonomy ) {
459 $term = get_term_by( 'term_id', $term_id, $taxonomy->name );
460 return $term instanceof WP_Term ? $term->term_taxonomy_id : null;
461 },
462 $term_ids
463 );
464 }
465
466 _update_post_term_count( $taxonomy_term_ids, $taxonomy );
467 }
468
469 $exclude_term_ids = array();
470 $product_visibility_term_ids = wc_get_product_visibility_term_ids();
471
472 if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
473 $exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog'];
474 }
475
476 if (
477 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' )
478 && $product_visibility_term_ids[ ProductStockStatus::OUT_OF_STOCK ]
479 ) {
480 $exclude_term_ids[] = $product_visibility_term_ids[ ProductStockStatus::OUT_OF_STOCK ];
481 }
482
483 $query = array(
484 'fields' => "
485 SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} p
486 ",
487 'join' => '',
488 'where' => "
489 WHERE 1=1
490 AND p.post_status = 'publish'
491 AND p.post_type = 'product'
492 ",
493 );
494
495 if ( count( $exclude_term_ids ) ) {
496 $query['join'] .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join ON exclude_join.object_id = p.ID';
497 $query['where'] .= ' AND exclude_join.object_id IS NULL';
498 }
499
500 // Ancestors need counting.
501 if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
502 foreach ( $term_ids as $term_id ) {
503 $term_ids = array_merge( $term_ids, get_ancestors( $term_id, $taxonomy->name ) );
504 }
505
506 $term_ids = array_unique( $term_ids );
507 }
508
509 // Count the terms.
510 foreach ( $term_ids as $term_id ) {
511 $terms_to_count = array( absint( $term_id ) );
512
513 if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
514 // We need to get the $term's hierarchy so we can count its children too.
515 $children = get_term_children( $term_id, $taxonomy->name );
516
517 if ( $children && ! is_wp_error( $children ) ) {
518 $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
519 }
520 }
521
522 // Generate term query.
523 $term_query = $query;
524 $term_query['join'] .= " INNER JOIN ( SELECT object_id FROM {$wpdb->term_relationships} INNER JOIN {$wpdb->term_taxonomy} using( term_taxonomy_id ) WHERE term_id IN ( " . implode( ',', array_map( 'absint', $terms_to_count ) ) . ' ) ) AS include_join ON include_join.object_id = p.ID';
525
526 // Get the count.
527 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
528 $count = $wpdb->get_var( implode( ' ', $term_query ) );
529
530 // Update the count.
531 update_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
532 }
533
534 delete_transient( 'wc_term_counts' );
535 }
536
537 /**
538 * Recount terms after the stock amount changes.
539 *
540 * @param int $product_id Product ID.
541 */
542 function wc_recount_after_stock_change( $product_id ) {
543 if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
544 return;
545 }
546 if ( wp_defer_term_counting() ) {
547 // When deferring term counts, we're using the built in handling of `wp_update_term_count()` to deal with the deferring
548 // and, though, this will cause both the standard and stock based counts to be rerun, it is still more efficient
549 // in cases where deferred term counting was warranted.
550 $product_terms = get_the_terms( $product_id, 'product_cat' );
551 if ( is_array( $product_terms ) ) {
552 wp_update_term_count( array_column( $product_terms, 'term_taxonomy_id' ), 'product_cat' );
553 }
554
555 $product_terms = get_the_terms( $product_id, 'product_tag' );
556 if ( is_array( $product_terms ) ) {
557 wp_update_term_count( array_column( $product_terms, 'term_taxonomy_id' ), 'product_tag' );
558 }
559 } else {
560 _wc_recount_terms_by_product( $product_id );
561 }
562 }
563 add_action( 'woocommerce_product_set_stock_status', 'wc_recount_after_stock_change' );
564
565
566 /**
567 * Overrides the original term count for product categories and tags with the product count.
568 * that takes catalog visibility into account.
569 *
570 * @param array $terms List of terms.
571 * @param string|array $taxonomies Single taxonomy or list of taxonomies.
572 * @return array
573 */
574 function wc_change_term_counts( $terms, $taxonomies ) {
575 if ( is_admin() || wp_doing_ajax() ) {
576 return $terms;
577 }
578
579 /**
580 * Filter which product taxonomies should have their term counts overridden to take catalog visibility into account.
581 *
582 * @since 2.1.0
583 *
584 * @param array $valid_taxonomies List of taxonomy slugs.
585 */
586 $valid_taxonomies = apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag', 'product_brand' ) );
587 $current_taxonomies = array_intersect( (array) $taxonomies, $valid_taxonomies );
588
589 if ( empty( $current_taxonomies ) ) {
590 return $terms;
591 }
592
593 $o_term_counts = get_transient( 'wc_term_counts' );
594 $term_counts = false === $o_term_counts ? array() : $o_term_counts;
595
596 foreach ( $terms as &$term ) {
597 if ( $term instanceof WP_Term && in_array( $term->taxonomy, $current_taxonomies, true ) ) {
598 $key = $term->term_id . '_' . $term->taxonomy;
599 if ( ! isset( $term_counts[ $key ] ) ) {
600 $count = get_term_meta( $term->term_id, 'product_count_' . $term->taxonomy, true );
601 $count = '' !== $count ? absint( $count ) : 0;
602 $term_counts[ $key ] = $count;
603 }
604
605 $term->count = $term_counts[ $key ];
606 }
607 }
608
609 // Update transient.
610 if ( $term_counts !== $o_term_counts ) {
611 set_transient( 'wc_term_counts', $term_counts, MONTH_IN_SECONDS );
612 }
613
614 return $terms;
615 }
616 add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );
617
618 /**
619 * Return products in a given term, and cache value.
620 *
621 * To keep in sync, product_count will be cleared on "set_object_terms".
622 *
623 * @param int $term_id Term ID.
624 * @param string $taxonomy Taxonomy.
625 * @return array
626 */
627 function wc_get_term_product_ids( $term_id, $taxonomy ) {
628 $product_ids = get_term_meta( $term_id, 'product_ids', true );
629
630 if ( false === $product_ids || ! is_array( $product_ids ) ) {
631 $product_ids = get_objects_in_term( $term_id, $taxonomy );
632 update_term_meta( $term_id, 'product_ids', $product_ids );
633 }
634
635 return $product_ids;
636 }
637
638 /**
639 * When a post is updated and terms recounted (called by _update_post_term_count), clear the ids.
640 *
641 * @param int $object_id Object ID.
642 * @param array $terms An array of object terms.
643 * @param array $tt_ids An array of term taxonomy IDs.
644 * @param string $taxonomy Taxonomy slug.
645 * @param bool $append Whether to append new terms to the old terms.
646 * @param array $old_tt_ids Old array of term taxonomy IDs.
647 */
648 function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
649 foreach ( $old_tt_ids as $term_id ) {
650 delete_term_meta( $term_id, 'product_ids' );
651 }
652 foreach ( $tt_ids as $term_id ) {
653 delete_term_meta( $term_id, 'product_ids' );
654 }
655 }
656 add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 );
657
658 /**
659 * Get full list of product visibility term ids.
660 *
661 * @since 3.0.0
662 * @return int[]
663 */
664 function wc_get_product_visibility_term_ids() {
665 if ( ! taxonomy_exists( 'product_visibility' ) ) {
666 wc_doing_it_wrong( __FUNCTION__, 'wc_get_product_visibility_term_ids should not be called before taxonomies are registered (woocommerce_after_register_post_type action).', '3.1' );
667 return array();
668 }
669
670 static $term_ids_by_blog = array();
671
672 $blog_id = get_current_blog_id();
673
674 if ( isset( $term_ids_by_blog[ $blog_id ] ) && ! class_exists( 'WC_Unit_Tests_Bootstrap' ) ) {
675 return $term_ids_by_blog[ $blog_id ];
676 }
677
678 $term_ids_by_blog[ $blog_id ] = array_map(
679 'absint',
680 wp_parse_args(
681 wp_list_pluck(
682 get_terms(
683 array(
684 'taxonomy' => 'product_visibility',
685 'hide_empty' => false,
686 )
687 ),
688 'term_taxonomy_id',
689 'name'
690 ),
691 array(
692 'exclude-from-catalog' => 0,
693 'exclude-from-search' => 0,
694 'featured' => 0,
695 'outofstock' => 0,
696 'rated-1' => 0,
697 'rated-2' => 0,
698 'rated-3' => 0,
699 'rated-4' => 0,
700 'rated-5' => 0,
701 )
702 )
703 );
704
705 return $term_ids_by_blog[ $blog_id ];
706 }
707
708 /**
709 * Recounts all terms for product categories and product tags.
710 *
711 * @since 5.2
712 *
713 * @param bool $include_callback True to update the standard term counts in addition to the product-specific counts,
714 * which will cause a lot more queries to run.
715 *
716 * @return void
717 */
718 function wc_recount_all_terms( bool $include_callback = true ) {
719 $product_cats = get_terms(
720 array(
721 'taxonomy' => 'product_cat',
722 'hide_empty' => false,
723 'fields' => 'id=>parent',
724 )
725 );
726 _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), $include_callback, false );
727
728 $product_tags = get_terms(
729 array(
730 'taxonomy' => 'product_tag',
731 'hide_empty' => false,
732 'fields' => 'id=>parent',
733 )
734 );
735 _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), $include_callback, false );
736 }
737
738 /**
739 * Recounts terms by product.
740 *
741 * @since 5.2
742 * @param int $product_id The ID of the product.
743 * @return void
744 */
745 function _wc_recount_terms_by_product( $product_id = '' ) {
746 if ( empty( $product_id ) ) {
747 return;
748 }
749
750 $product_terms = get_the_terms( $product_id, 'product_cat' );
751
752 if ( $product_terms ) {
753 $product_cats = array();
754
755 foreach ( $product_terms as $term ) {
756 $product_cats[ $term->term_id ] = $term->parent;
757 }
758
759 _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false );
760 }
761
762 $product_terms = get_the_terms( $product_id, 'product_tag' );
763
764 if ( $product_terms ) {
765 $product_tags = array();
766
767 foreach ( $product_terms as $term ) {
768 $product_tags[ $term->term_id ] = $term->parent;
769 }
770
771 _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false );
772 }
773 }
774