Include categories and tags by default

This commit is contained in:
Claudio Sanches 2018-04-17 12:14:18 -03:00
parent 60ad91266d
commit 627292d627
1 changed files with 47 additions and 21 deletions

View File

@ -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 );