From 627292d62748c51ef423930940bc9ce63536cebc Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Tue, 17 Apr 2018 12:14:18 -0300 Subject: [PATCH] Include categories and tags by default --- includes/wc-template-functions.php | 68 +++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index 19ae0701433..d9dee65fa69 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -512,6 +512,40 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) { return $classes; } +/** + * Get product taxonomy HTML classes. + * + * @since 3.4.0 + * @param array $term_ids Array of terms IDs or objects. + * @param string $taxonomy Taxonomy. + * @return array + */ +function wc_get_product_taxonomy_class( $term_ids, $taxonomy ) { + $classes = array(); + + foreach ( $term_ids as $term_id ) { + $term = get_term( $term_id, $taxonomy ); + + if ( empty( $term->slug ) ) { + continue; + } + + $term_class = sanitize_html_class( $term->slug, $term->term_id ); + if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { + $term_class = $term->term_id; + } + + // 'post_tag' uses the 'tag' prefix for backward compatibility. + if ( 'post_tag' === $taxonomy ) { + $classes[] = 'tag-' . $term_class; + } else { + $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id ); + } + } + + return $classes; +} + /** * Retrieves the classes for the post div as an array. * @@ -524,10 +558,13 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) { */ function wc_get_product_class( $class = '', $product_id = null ) { if ( is_a( $product_id, 'WC_Product' ) ) { + $product = $product_id; $product_id = $product_id->get_id(); + } else { + $post = get_post( $product_id ); + $product = wc_get_product( $post->ID ); } - $post = get_post( $product_id ); $classes = array(); if ( $class ) { @@ -540,7 +577,7 @@ function wc_get_product_class( $class = '', $product_id = null ) { $class = array(); } - if ( ! $post ) { + if ( ! $post || ! $product ) { return $classes; } @@ -587,31 +624,20 @@ function wc_get_product_class( $class = '', $product_id = null ) { // Hentry for hAtom compliance. $classes[] = 'hentry'; - // All public taxonomies. + // Include attributes and any extra taxonomy. if ( apply_filters( 'woocommerce_get_product_class_include_taxonomies', false ) ) { $taxonomies = get_taxonomies( array( 'public' => true ) ); foreach ( (array) $taxonomies as $taxonomy ) { - if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { - foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) { - if ( empty( $term->slug ) ) { - continue; - } - - $term_class = sanitize_html_class( $term->slug, $term->term_id ); - if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { - $term_class = $term->term_id; - } - - // 'post_tag' uses the 'tag' prefix for backward compatibility. - if ( 'post_tag' === $taxonomy ) { - $classes[] = 'tag-' . $term_class; - } else { - $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id ); - } - } + if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) && in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) { + $classes = array_merge( $classes, wc_get_product_taxonomy_class( (array) get_the_terms( $post->ID, $taxonomy ), $taxonomy ) ); } } } + // Categories. + $classes = array_merge( $classes, wc_get_product_taxonomy_class( $product->get_category_ids(), 'product_cat' ) ); + + // Tags. + $classes = array_merge( $classes, wc_get_product_taxonomy_class( $product->get_tag_ids(), 'product_tag' ) ); $classes = array_map( 'esc_attr', $classes ); $classes = apply_filters( 'post_class', $classes, $class, $post->ID );