abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
5 years ago
emails
5 years ago
export
5 years ago
gateways
5 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
5 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
5 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
5 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
wc-term-functions.php
639 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 | /** |
| 14 | * Change get terms defaults for attributes to order by the sorting setting, or default to menu_order for sortable taxonomies. |
| 15 | * |
| 16 | * @since 3.6.0 Sorting options are now set as the default automatically, so you no longer have to request to orderby menu_order. |
| 17 | * |
| 18 | * @param array $defaults An array of default get_terms() arguments. |
| 19 | * @param array $taxonomies An array of taxonomies. |
| 20 | * @return array |
| 21 | */ |
| 22 | function wc_change_get_terms_defaults( $defaults, $taxonomies ) { |
| 23 | if ( is_array( $taxonomies ) && 1 < count( $taxonomies ) ) { |
| 24 | return $defaults; |
| 25 | } |
| 26 | $taxonomy = is_array( $taxonomies ) ? (string) current( $taxonomies ) : $taxonomies; |
| 27 | $orderby = 'name'; |
| 28 | |
| 29 | if ( taxonomy_is_product_attribute( $taxonomy ) ) { |
| 30 | $orderby = wc_attribute_orderby( $taxonomy ); |
| 31 | } elseif ( in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ), true ) ) { |
| 32 | $orderby = 'menu_order'; |
| 33 | } |
| 34 | |
| 35 | // Change defaults. Invalid values will be changed later @see wc_change_pre_get_terms. |
| 36 | // These are in place so we know if a specific order was requested. |
| 37 | switch ( $orderby ) { |
| 38 | case 'menu_order': |
| 39 | case 'name_num': |
| 40 | case 'parent': |
| 41 | $defaults['orderby'] = $orderby; |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | return $defaults; |
| 46 | } |
| 47 | add_filter( 'get_terms_defaults', 'wc_change_get_terms_defaults', 10, 2 ); |
| 48 | |
| 49 | /** |
| 50 | * Adds support to get_terms for menu_order argument. |
| 51 | * |
| 52 | * @since 3.6.0 |
| 53 | * @param WP_Term_Query $terms_query Instance of WP_Term_Query. |
| 54 | */ |
| 55 | function wc_change_pre_get_terms( $terms_query ) { |
| 56 | $args = &$terms_query->query_vars; |
| 57 | |
| 58 | // Put back valid orderby values. |
| 59 | if ( 'menu_order' === $args['orderby'] ) { |
| 60 | $args['orderby'] = 'name'; |
| 61 | $args['force_menu_order_sort'] = true; |
| 62 | } |
| 63 | |
| 64 | if ( 'name_num' === $args['orderby'] ) { |
| 65 | $args['orderby'] = 'name'; |
| 66 | $args['force_numeric_name'] = true; |
| 67 | } |
| 68 | |
| 69 | // When COUNTING, disable custom sorting. |
| 70 | if ( 'count' === $args['fields'] ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Support menu_order arg used in previous versions. |
| 75 | if ( ! empty( $args['menu_order'] ) ) { |
| 76 | $args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC'; |
| 77 | $args['force_menu_order_sort'] = true; |
| 78 | } |
| 79 | |
| 80 | if ( ! empty( $args['force_menu_order_sort'] ) ) { |
| 81 | $args['orderby'] = 'meta_value_num'; |
| 82 | $args['meta_key'] = 'order'; // phpcs:ignore |
| 83 | $terms_query->meta_query->parse_query_vars( $args ); |
| 84 | } |
| 85 | } |
| 86 | add_action( 'pre_get_terms', 'wc_change_pre_get_terms', 10, 1 ); |
| 87 | |
| 88 | /** |
| 89 | * Adjust term query to handle custom sorting parameters. |
| 90 | * |
| 91 | * @param array $clauses Clauses. |
| 92 | * @param array $taxonomies Taxonomies. |
| 93 | * @param array $args Arguments. |
| 94 | * @return array |
| 95 | */ |
| 96 | function wc_terms_clauses( $clauses, $taxonomies, $args ) { |
| 97 | global $wpdb; |
| 98 | |
| 99 | // No need to filter when counting. |
| 100 | if ( strpos( $clauses['fields'], 'COUNT(*)' ) !== false ) { |
| 101 | return $clauses; |
| 102 | } |
| 103 | |
| 104 | // Force numeric sort if using name_num custom sorting param. |
| 105 | if ( ! empty( $args['force_numeric_name'] ) ) { |
| 106 | $clauses['orderby'] = str_replace( 'ORDER BY t.name', 'ORDER BY t.name+0', $clauses['orderby'] ); |
| 107 | } |
| 108 | |
| 109 | // For sorting, force left join in case order meta is missing. |
| 110 | if ( ! empty( $args['force_menu_order_sort'] ) ) { |
| 111 | $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'] ); |
| 112 | $clauses['where'] = str_replace( "{$wpdb->termmeta}.meta_key = 'order'", "( {$wpdb->termmeta}.meta_key = 'order' OR {$wpdb->termmeta}.meta_key IS NULL )", $clauses['where'] ); |
| 113 | $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'] ); |
| 114 | } |
| 115 | |
| 116 | return $clauses; |
| 117 | } |
| 118 | add_filter( 'terms_clauses', 'wc_terms_clauses', 99, 3 ); |
| 119 | |
| 120 | /** |
| 121 | * Helper to get cached object terms and filter by field using wp_list_pluck(). |
| 122 | * Works as a cached alternative for wp_get_post_terms() and wp_get_object_terms(). |
| 123 | * |
| 124 | * @since 3.0.0 |
| 125 | * @param int $object_id Object ID. |
| 126 | * @param string $taxonomy Taxonomy slug. |
| 127 | * @param string $field Field name. |
| 128 | * @param string $index_key Index key name. |
| 129 | * @return array |
| 130 | */ |
| 131 | function wc_get_object_terms( $object_id, $taxonomy, $field = null, $index_key = null ) { |
| 132 | // Test if terms exists. get_the_terms() return false when it finds no terms. |
| 133 | $terms = get_the_terms( $object_id, $taxonomy ); |
| 134 | |
| 135 | if ( ! $terms || is_wp_error( $terms ) ) { |
| 136 | return array(); |
| 137 | } |
| 138 | |
| 139 | return is_null( $field ) ? $terms : wp_list_pluck( $terms, $field, $index_key ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Cached version of wp_get_post_terms(). |
| 144 | * This is a private function (internal use ONLY). |
| 145 | * |
| 146 | * @since 3.0.0 |
| 147 | * @param int $product_id Product ID. |
| 148 | * @param string $taxonomy Taxonomy slug. |
| 149 | * @param array $args Query arguments. |
| 150 | * @return array |
| 151 | */ |
| 152 | function _wc_get_cached_product_terms( $product_id, $taxonomy, $args = array() ) { |
| 153 | $cache_key = 'wc_' . $taxonomy . md5( wp_json_encode( $args ) ); |
| 154 | $cache_group = WC_Cache_Helper::get_cache_prefix( 'product_' . $product_id ) . $product_id; |
| 155 | $terms = wp_cache_get( $cache_key, $cache_group ); |
| 156 | |
| 157 | if ( false !== $terms ) { |
| 158 | return $terms; |
| 159 | } |
| 160 | |
| 161 | $terms = wp_get_post_terms( $product_id, $taxonomy, $args ); |
| 162 | |
| 163 | wp_cache_add( $cache_key, $terms, $cache_group ); |
| 164 | |
| 165 | return $terms; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Wrapper used to get terms for a product. |
| 170 | * |
| 171 | * @param int $product_id Product ID. |
| 172 | * @param string $taxonomy Taxonomy slug. |
| 173 | * @param array $args Query arguments. |
| 174 | * @return array |
| 175 | */ |
| 176 | function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) { |
| 177 | if ( ! taxonomy_exists( $taxonomy ) ) { |
| 178 | return array(); |
| 179 | } |
| 180 | |
| 181 | return apply_filters( 'woocommerce_get_product_terms', _wc_get_cached_product_terms( $product_id, $taxonomy, $args ), $product_id, $taxonomy, $args ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Sort by name (numeric). |
| 186 | * |
| 187 | * @param WP_Post $a First item to compare. |
| 188 | * @param WP_Post $b Second item to compare. |
| 189 | * @return int |
| 190 | */ |
| 191 | function _wc_get_product_terms_name_num_usort_callback( $a, $b ) { |
| 192 | $a_name = (float) $a->name; |
| 193 | $b_name = (float) $b->name; |
| 194 | |
| 195 | if ( abs( $a_name - $b_name ) < 0.001 ) { |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | return ( $a_name < $b_name ) ? -1 : 1; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Sort by parent. |
| 204 | * |
| 205 | * @param WP_Post $a First item to compare. |
| 206 | * @param WP_Post $b Second item to compare. |
| 207 | * @return int |
| 208 | */ |
| 209 | function _wc_get_product_terms_parent_usort_callback( $a, $b ) { |
| 210 | if ( $a->parent === $b->parent ) { |
| 211 | return 0; |
| 212 | } |
| 213 | return ( $a->parent < $b->parent ) ? 1 : -1; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * WooCommerce Dropdown categories. |
| 218 | * |
| 219 | * @param array $args Args to control display of dropdown. |
| 220 | */ |
| 221 | function wc_product_dropdown_categories( $args = array() ) { |
| 222 | global $wp_query; |
| 223 | |
| 224 | $args = wp_parse_args( |
| 225 | $args, |
| 226 | array( |
| 227 | 'pad_counts' => 1, |
| 228 | 'show_count' => 1, |
| 229 | 'hierarchical' => 1, |
| 230 | 'hide_empty' => 1, |
| 231 | 'show_uncategorized' => 1, |
| 232 | 'orderby' => 'name', |
| 233 | 'selected' => isset( $wp_query->query_vars['product_cat'] ) ? $wp_query->query_vars['product_cat'] : '', |
| 234 | 'show_option_none' => __( 'Select a category', 'woocommerce' ), |
| 235 | 'option_none_value' => '', |
| 236 | 'value_field' => 'slug', |
| 237 | 'taxonomy' => 'product_cat', |
| 238 | 'name' => 'product_cat', |
| 239 | 'class' => 'dropdown_product_cat', |
| 240 | ) |
| 241 | ); |
| 242 | |
| 243 | if ( 'order' === $args['orderby'] ) { |
| 244 | $args['orderby'] = 'meta_value_num'; |
| 245 | $args['meta_key'] = 'order'; // phpcs:ignore |
| 246 | } |
| 247 | |
| 248 | wp_dropdown_categories( $args ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Custom walker for Product Categories. |
| 253 | * |
| 254 | * Previously used by wc_product_dropdown_categories, but wp_dropdown_categories has been fixed in core. |
| 255 | * |
| 256 | * @param mixed ...$args Variable number of parameters to be passed to the walker. |
| 257 | * @return mixed |
| 258 | */ |
| 259 | function wc_walk_category_dropdown_tree( ...$args ) { |
| 260 | if ( ! class_exists( 'WC_Product_Cat_Dropdown_Walker', false ) ) { |
| 261 | include_once WC()->plugin_path() . '/includes/walkers/class-wc-product-cat-dropdown-walker.php'; |
| 262 | } |
| 263 | |
| 264 | // The user's options are the third parameter. |
| 265 | if ( empty( $args[2]['walker'] ) || ! is_a( $args[2]['walker'], 'Walker' ) ) { |
| 266 | $walker = new WC_Product_Cat_Dropdown_Walker(); |
| 267 | } else { |
| 268 | $walker = $args[2]['walker']; |
| 269 | } |
| 270 | |
| 271 | return $walker->walk( ...$args ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Migrate data from WC term meta to WP term meta. |
| 276 | * |
| 277 | * When the database is updated to support term meta, migrate WC term meta data across. |
| 278 | * We do this when the new version is >= 34370, and the old version is < 34370 (34370 is when term meta table was added). |
| 279 | * |
| 280 | * @param string $wp_db_version The new $wp_db_version. |
| 281 | * @param string $wp_current_db_version The old (current) $wp_db_version. |
| 282 | */ |
| 283 | function wc_taxonomy_metadata_migrate_data( $wp_db_version, $wp_current_db_version ) { |
| 284 | if ( $wp_db_version >= 34370 && $wp_current_db_version < 34370 ) { |
| 285 | global $wpdb; |
| 286 | 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;" ) ) { |
| 287 | $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" ); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | add_action( 'wp_upgrade', 'wc_taxonomy_metadata_migrate_data', 10, 2 ); |
| 292 | |
| 293 | /** |
| 294 | * Move a term before the a given element of its hierarchy level. |
| 295 | * |
| 296 | * @param int $the_term Term ID. |
| 297 | * @param int $next_id The id of the next sibling element in save hierarchy level. |
| 298 | * @param string $taxonomy Taxnomy. |
| 299 | * @param int $index Term index (default: 0). |
| 300 | * @param mixed $terms List of terms. (default: null). |
| 301 | * @return int |
| 302 | */ |
| 303 | function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) { |
| 304 | if ( ! $terms ) { |
| 305 | $terms = get_terms( $taxonomy, 'hide_empty=0&parent=0&menu_order=ASC' ); |
| 306 | } |
| 307 | if ( empty( $terms ) ) { |
| 308 | return $index; |
| 309 | } |
| 310 | |
| 311 | $id = intval( $the_term->term_id ); |
| 312 | |
| 313 | $term_in_level = false; // Flag: is our term to order in this level of terms. |
| 314 | |
| 315 | foreach ( $terms as $term ) { |
| 316 | $term_id = intval( $term->term_id ); |
| 317 | |
| 318 | if ( $term_id === $id ) { // Our term to order, we skip. |
| 319 | $term_in_level = true; |
| 320 | continue; // Our term to order, we skip. |
| 321 | } |
| 322 | // the nextid of our term to order, lets move our term here. |
| 323 | if ( null !== $next_id && $term_id === $next_id ) { |
| 324 | $index++; |
| 325 | $index = wc_set_term_order( $id, $index, $taxonomy, true ); |
| 326 | } |
| 327 | |
| 328 | // Set order. |
| 329 | $index++; |
| 330 | $index = wc_set_term_order( $term_id, $index, $taxonomy ); |
| 331 | |
| 332 | /** |
| 333 | * After a term has had it's order set. |
| 334 | */ |
| 335 | do_action( 'woocommerce_after_set_term_order', $term, $index, $taxonomy ); |
| 336 | |
| 337 | // If that term has children we walk through them. |
| 338 | $children = get_terms( $taxonomy, "parent={$term_id}&hide_empty=0&menu_order=ASC" ); |
| 339 | if ( ! empty( $children ) ) { |
| 340 | $index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children ); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // No nextid meaning our term is in last position. |
| 345 | if ( $term_in_level && null === $next_id ) { |
| 346 | $index = wc_set_term_order( $id, $index + 1, $taxonomy, true ); |
| 347 | } |
| 348 | |
| 349 | return $index; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Set the sort order of a term. |
| 354 | * |
| 355 | * @param int $term_id Term ID. |
| 356 | * @param int $index Index. |
| 357 | * @param string $taxonomy Taxonomy. |
| 358 | * @param bool $recursive Recursive (default: false). |
| 359 | * @return int |
| 360 | */ |
| 361 | function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) { |
| 362 | |
| 363 | $term_id = (int) $term_id; |
| 364 | $index = (int) $index; |
| 365 | |
| 366 | update_term_meta( $term_id, 'order', $index ); |
| 367 | |
| 368 | if ( ! $recursive ) { |
| 369 | return $index; |
| 370 | } |
| 371 | |
| 372 | $children = get_terms( $taxonomy, "parent=$term_id&hide_empty=0&menu_order=ASC" ); |
| 373 | |
| 374 | foreach ( $children as $term ) { |
| 375 | $index++; |
| 376 | $index = wc_set_term_order( $term->term_id, $index, $taxonomy, true ); |
| 377 | } |
| 378 | |
| 379 | clean_term_cache( $term_id, $taxonomy ); |
| 380 | |
| 381 | return $index; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Function for recounting product terms, ignoring hidden products. |
| 386 | * |
| 387 | * @param array $terms List of terms. |
| 388 | * @param object $taxonomy Taxonomy. |
| 389 | * @param bool $callback Callback. |
| 390 | * @param bool $terms_are_term_taxonomy_ids If terms are from term_taxonomy_id column. |
| 391 | */ |
| 392 | function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) { |
| 393 | global $wpdb; |
| 394 | |
| 395 | // Standard callback. |
| 396 | if ( $callback ) { |
| 397 | _update_post_term_count( $terms, $taxonomy ); |
| 398 | } |
| 399 | |
| 400 | $exclude_term_ids = array(); |
| 401 | $product_visibility_term_ids = wc_get_product_visibility_term_ids(); |
| 402 | |
| 403 | if ( $product_visibility_term_ids['exclude-from-catalog'] ) { |
| 404 | $exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog']; |
| 405 | } |
| 406 | |
| 407 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) { |
| 408 | $exclude_term_ids[] = $product_visibility_term_ids['outofstock']; |
| 409 | } |
| 410 | |
| 411 | $query = array( |
| 412 | 'fields' => " |
| 413 | SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} p |
| 414 | ", |
| 415 | 'join' => '', |
| 416 | 'where' => " |
| 417 | WHERE 1=1 |
| 418 | AND p.post_status = 'publish' |
| 419 | AND p.post_type = 'product' |
| 420 | |
| 421 | ", |
| 422 | ); |
| 423 | |
| 424 | if ( count( $exclude_term_ids ) ) { |
| 425 | $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'; |
| 426 | $query['where'] .= ' AND exclude_join.object_id IS NULL'; |
| 427 | } |
| 428 | |
| 429 | // Pre-process term taxonomy ids. |
| 430 | if ( ! $terms_are_term_taxonomy_ids ) { |
| 431 | // We passed in an array of TERMS in format id=>parent. |
| 432 | $terms = array_filter( (array) array_keys( $terms ) ); |
| 433 | } else { |
| 434 | // If we have term taxonomy IDs we need to get the term ID. |
| 435 | $term_taxonomy_ids = $terms; |
| 436 | $terms = array(); |
| 437 | foreach ( $term_taxonomy_ids as $term_taxonomy_id ) { |
| 438 | $term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name ); |
| 439 | $terms[] = $term->term_id; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Exit if we have no terms to count. |
| 444 | if ( empty( $terms ) ) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | // Ancestors need counting. |
| 449 | if ( is_taxonomy_hierarchical( $taxonomy->name ) ) { |
| 450 | foreach ( $terms as $term_id ) { |
| 451 | $terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) ); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // Unique terms only. |
| 456 | $terms = array_unique( $terms ); |
| 457 | |
| 458 | // Count the terms. |
| 459 | foreach ( $terms as $term_id ) { |
| 460 | $terms_to_count = array( absint( $term_id ) ); |
| 461 | |
| 462 | if ( is_taxonomy_hierarchical( $taxonomy->name ) ) { |
| 463 | // We need to get the $term's hierarchy so we can count its children too. |
| 464 | $children = get_term_children( $term_id, $taxonomy->name ); |
| 465 | |
| 466 | if ( $children && ! is_wp_error( $children ) ) { |
| 467 | $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) ); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Generate term query. |
| 472 | $term_query = $query; |
| 473 | $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'; |
| 474 | |
| 475 | // Get the count. |
| 476 | $count = $wpdb->get_var( implode( ' ', $term_query ) ); // WPCS: unprepared SQL ok. |
| 477 | |
| 478 | // Update the count. |
| 479 | update_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) ); |
| 480 | } |
| 481 | |
| 482 | delete_transient( 'wc_term_counts' ); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Recount terms after the stock amount changes. |
| 487 | * |
| 488 | * @param int $product_id Product ID. |
| 489 | */ |
| 490 | function wc_recount_after_stock_change( $product_id ) { |
| 491 | if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | $product_terms = get_the_terms( $product_id, 'product_cat' ); |
| 496 | |
| 497 | if ( $product_terms ) { |
| 498 | $product_cats = array(); |
| 499 | |
| 500 | foreach ( $product_terms as $term ) { |
| 501 | $product_cats[ $term->term_id ] = $term->parent; |
| 502 | } |
| 503 | |
| 504 | _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false ); |
| 505 | } |
| 506 | |
| 507 | $product_terms = get_the_terms( $product_id, 'product_tag' ); |
| 508 | |
| 509 | if ( $product_terms ) { |
| 510 | $product_tags = array(); |
| 511 | |
| 512 | foreach ( $product_terms as $term ) { |
| 513 | $product_tags[ $term->term_id ] = $term->parent; |
| 514 | } |
| 515 | |
| 516 | _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false ); |
| 517 | } |
| 518 | } |
| 519 | add_action( 'woocommerce_product_set_stock_status', 'wc_recount_after_stock_change' ); |
| 520 | |
| 521 | |
| 522 | /** |
| 523 | * Overrides the original term count for product categories and tags with the product count. |
| 524 | * that takes catalog visibility into account. |
| 525 | * |
| 526 | * @param array $terms List of terms. |
| 527 | * @param string|array $taxonomies Single taxonomy or list of taxonomies. |
| 528 | * @return array |
| 529 | */ |
| 530 | function wc_change_term_counts( $terms, $taxonomies ) { |
| 531 | if ( is_admin() || is_ajax() ) { |
| 532 | return $terms; |
| 533 | } |
| 534 | |
| 535 | if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ), true ) ) { |
| 536 | return $terms; |
| 537 | } |
| 538 | |
| 539 | $o_term_counts = get_transient( 'wc_term_counts' ); |
| 540 | $term_counts = $o_term_counts; |
| 541 | |
| 542 | foreach ( $terms as &$term ) { |
| 543 | if ( is_object( $term ) ) { |
| 544 | $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_term_meta( $term->term_id, 'product_count_' . $taxonomies[0], true ); |
| 545 | |
| 546 | if ( '' !== $term_counts[ $term->term_id ] ) { |
| 547 | $term->count = absint( $term_counts[ $term->term_id ] ); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // Update transient. |
| 553 | if ( $term_counts !== $o_term_counts ) { |
| 554 | set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 ); |
| 555 | } |
| 556 | |
| 557 | return $terms; |
| 558 | } |
| 559 | add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 ); |
| 560 | |
| 561 | /** |
| 562 | * Return products in a given term, and cache value. |
| 563 | * |
| 564 | * To keep in sync, product_count will be cleared on "set_object_terms". |
| 565 | * |
| 566 | * @param int $term_id Term ID. |
| 567 | * @param string $taxonomy Taxonomy. |
| 568 | * @return array |
| 569 | */ |
| 570 | function wc_get_term_product_ids( $term_id, $taxonomy ) { |
| 571 | $product_ids = get_term_meta( $term_id, 'product_ids', true ); |
| 572 | |
| 573 | if ( false === $product_ids || ! is_array( $product_ids ) ) { |
| 574 | $product_ids = get_objects_in_term( $term_id, $taxonomy ); |
| 575 | update_term_meta( $term_id, 'product_ids', $product_ids ); |
| 576 | } |
| 577 | |
| 578 | return $product_ids; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * When a post is updated and terms recounted (called by _update_post_term_count), clear the ids. |
| 583 | * |
| 584 | * @param int $object_id Object ID. |
| 585 | * @param array $terms An array of object terms. |
| 586 | * @param array $tt_ids An array of term taxonomy IDs. |
| 587 | * @param string $taxonomy Taxonomy slug. |
| 588 | * @param bool $append Whether to append new terms to the old terms. |
| 589 | * @param array $old_tt_ids Old array of term taxonomy IDs. |
| 590 | */ |
| 591 | function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
| 592 | foreach ( $old_tt_ids as $term_id ) { |
| 593 | delete_term_meta( $term_id, 'product_ids' ); |
| 594 | } |
| 595 | foreach ( $tt_ids as $term_id ) { |
| 596 | delete_term_meta( $term_id, 'product_ids' ); |
| 597 | } |
| 598 | } |
| 599 | add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 ); |
| 600 | |
| 601 | /** |
| 602 | * Get full list of product visibilty term ids. |
| 603 | * |
| 604 | * @since 3.0.0 |
| 605 | * @return int[] |
| 606 | */ |
| 607 | function wc_get_product_visibility_term_ids() { |
| 608 | if ( ! taxonomy_exists( 'product_visibility' ) ) { |
| 609 | 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' ); |
| 610 | return array(); |
| 611 | } |
| 612 | return array_map( |
| 613 | 'absint', |
| 614 | wp_parse_args( |
| 615 | wp_list_pluck( |
| 616 | get_terms( |
| 617 | array( |
| 618 | 'taxonomy' => 'product_visibility', |
| 619 | 'hide_empty' => false, |
| 620 | ) |
| 621 | ), |
| 622 | 'term_taxonomy_id', |
| 623 | 'name' |
| 624 | ), |
| 625 | array( |
| 626 | 'exclude-from-catalog' => 0, |
| 627 | 'exclude-from-search' => 0, |
| 628 | 'featured' => 0, |
| 629 | 'outofstock' => 0, |
| 630 | 'rated-1' => 0, |
| 631 | 'rated-2' => 0, |
| 632 | 'rated-3' => 0, |
| 633 | 'rated-4' => 0, |
| 634 | 'rated-5' => 0, |
| 635 | ) |
| 636 | ) |
| 637 | ); |
| 638 | } |
| 639 |