1 ? mb_substr( $words[0], 0, 1 ) . mb_substr( $words[1], 0, 1 ) : mb_substr( $words[0], 0, 2 ); return mb_strtoupper( $letters ); } public static function logo_id( $term_id, $taxonomy = 'product_brand' ) { $id = 0; if ( class_exists( 'AIEO_DMM_Brand_Extras_Data' ) ) { $extras = AIEO_DMM_Brand_Extras_Data::get( $taxonomy, (int) $term_id ); $id = (int) ( $extras['logo_image_id'] ?? 0 ); } if ( $id <= 0 ) { $id = (int) get_term_meta( (int) $term_id, 'thumbnail_id', true ); } if ( $id <= 0 || ! wp_attachment_is_image( $id ) ) { return 0; } $meta = wp_get_attachment_metadata( $id ); $w = (int) ( $meta['width'] ?? 0 ); $h = (int) ( $meta['height'] ?? 0 ); return ( $w > 0 && $h > 0 && $w / $h >= 0.8 && $w / $h <= 1.25 ) ? $id : 0; } public static function mark( WP_Term $term, $class, $size = 'medium' ) { $logo = self::logo_id( (int) $term->term_id, $term->taxonomy ); if ( $logo > 0 ) { $img = wp_get_attachment_image( $logo, $size, false, array( 'alt' => '', 'loading' => 'lazy', 'decoding' => 'async' ) ); if ( '' !== $img ) { return ''; } } return ''; } public static function area( $term_id ) { $area = sanitize_key( (string) get_term_meta( (int) $term_id, 'aieo_area', true ) ); return in_array( $area, self::AREAS, true ) ? $area : 'blush'; } public static function origin_line( $term_id ) { $parts = array(); $origin = trim( wp_strip_all_tags( (string) get_term_meta( (int) $term_id, 'aieo_brand_origin', true ) ) ); if ( '' !== $origin ) { $parts[] = $origin; } $since = (int) get_term_meta( (int) $term_id, 'aieo_brand_since', true ); if ( $since > 0 ) { $parts[] = sprintf( __( 'since %s', 'ai-eshop-optimizer' ), (string) $since ); } return implode( ' ยท ', $parts ); } public static function in_stock( WP_Term $term ) { $version = class_exists( 'AIEO_Core_Brand_Vitals' ) ? (string) AIEO_Core_Brand_Vitals::instance()->cache_version() : ''; $key = 'aieo_brand_stock_' . (int) $term->term_id; $kept = get_transient( $key ); if ( is_array( $kept ) && ( $kept[0] ?? null ) === $version ) { return (int) $kept[1]; } $query = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => (int) $term->term_id ), array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => array( 'outofstock' ), 'operator' => 'NOT IN' ), ), ) ); $count = (int) $query->found_posts; set_transient( $key, array( $version, $count ), HOUR_IN_SECONDS ); return $count; } public static function categories( WP_Term $term ) { global $wpdb; $hidden_cats = self::hidden_categories(); $version = ( class_exists( 'AIEO_Core_Brand_Vitals' ) ? (string) AIEO_Core_Brand_Vitals::instance()->cache_version() : '' ) . '|' . md5( implode( ',', array_keys( $hidden_cats ) ) ); $key = 'aieo_brand_cats_' . (int) $term->term_id; $kept = get_transient( $key ); if ( ! is_array( $kept ) || ( $kept[0] ?? null ) !== $version ) { $hidden = ''; if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $hidden = " AND p.ID NOT IN ( SELECT tr_o.object_id FROM {$wpdb->term_relationships} tr_o" . " INNER JOIN {$wpdb->term_taxonomy} tt_o ON tt_o.term_taxonomy_id = tr_o.term_taxonomy_id" . " INNER JOIN {$wpdb->terms} t_o ON t_o.term_id = tt_o.term_id" . " WHERE tt_o.taxonomy = 'product_visibility' AND t_o.slug = 'outofstock' )"; } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT tr_c.object_id AS product_id, tt_c.term_id AS cat_id FROM {$wpdb->term_relationships} tr_b INNER JOIN {$wpdb->term_taxonomy} tt_b ON tt_b.term_taxonomy_id = tr_b.term_taxonomy_id INNER JOIN {$wpdb->posts} p ON p.ID = tr_b.object_id INNER JOIN {$wpdb->term_relationships} tr_c ON tr_c.object_id = p.ID INNER JOIN {$wpdb->term_taxonomy} tt_c ON tt_c.term_taxonomy_id = tr_c.term_taxonomy_id WHERE tt_b.taxonomy = %s AND tt_b.term_id = %d AND p.post_type = 'product' AND p.post_status = 'publish' AND tt_c.taxonomy = 'product_cat'" . $hidden, $term->taxonomy, (int) $term->term_id ), ARRAY_A ); $default = (int) get_option( 'default_product_cat', 0 ); $paths = array(); $pairs = array(); foreach ( (array) $rows as $row ) { $cat = (int) $row['cat_id']; if ( $cat === $default ) { continue; } if ( ! isset( $paths[ $cat ] ) ) { $paths[ $cat ] = array_merge( array_reverse( array_map( 'intval', get_ancestors( $cat, 'product_cat', 'taxonomy' ) ) ), array( $cat ) ); } if ( array_intersect_key( array_flip( $paths[ $cat ] ), $hidden_cats ) ) { continue; } $pairs[] = array( (int) $row['product_id'], $paths[ $cat ] ); } $group = static function ( array $pairs, int $depth ): array { $by = array(); foreach ( $pairs as $pair ) { if ( isset( $pair[1][ $depth ] ) ) { $by[ $pair[1][ $depth ] ][ $pair[0] ] = true; } } return array_map( 'count', $by ); }; $counts = $group( $pairs, 0 ); if ( 1 === count( $counts ) ) { $deeper = $group( $pairs, 1 ); if ( count( $deeper ) > 1 ) { $counts = $deeper; } } arsort( $counts ); $kept = array( $version, $counts ); set_transient( $key, $kept, HOUR_IN_SECONDS ); } $out = array(); foreach ( (array) $kept[1] as $cat => $count ) { $cat_term = get_term( (int) $cat, 'product_cat' ); if ( $cat_term instanceof WP_Term ) { $out[] = array( 'term' => $cat_term, 'count' => (int) $count ); } } return $out; } private static function hidden_categories() { $raw = class_exists( 'AIEO_DMM_Settings' ) ? (string) AIEO_DMM_Settings::get( 'navigator_excluded_terms', '' ) : ''; $out = array(); foreach ( preg_split( '/[,\s]+/', $raw, -1, PREG_SPLIT_NO_EMPTY ) as $token ) { $parts = explode( ':', $token, 2 ); if ( 1 === count( $parts ) ) { $out[ (int) $parts[0] ] = true; } elseif ( 'product_cat' === sanitize_key( $parts[0] ) ) { $out[ (int) $parts[1] ] = true; } } unset( $out[0] ); ksort( $out ); return $out; } public static function neighbours( WP_Term $term, $limit = 4 ) { global $wpdb; $limit = max( 1, min( 12, (int) $limit ) ); $version = ( class_exists( 'AIEO_Core_Brand_Vitals' ) ? (string) AIEO_Core_Brand_Vitals::instance()->cache_version() : '' ) . '|' . md5( implode( ',', array_keys( self::hidden_categories() ) ) ); $key = 'aieo_brand_near_' . (int) $term->term_id; $kept = get_transient( $key ); if ( ! is_array( $kept ) || ( $kept[0] ?? null ) !== $version ) { $cats = array(); foreach ( self::categories( $term ) as $cat ) { $cats[] = (int) $cat['term']->term_id; $kids = get_term_children( (int) $cat['term']->term_id, 'product_cat' ); $cats = array_merge( $cats, is_array( $kids ) ? array_map( 'intval', $kids ) : array() ); } $ids = array(); if ( $cats ) { $in = implode( ',', array_map( 'intval', array_unique( $cats ) ) ); $ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt_b.term_id FROM {$wpdb->term_relationships} tr_c INNER JOIN {$wpdb->term_taxonomy} tt_c ON tt_c.term_taxonomy_id = tr_c.term_taxonomy_id INNER JOIN {$wpdb->posts} p ON p.ID = tr_c.object_id INNER JOIN {$wpdb->term_relationships} tr_b ON tr_b.object_id = p.ID INNER JOIN {$wpdb->term_taxonomy} tt_b ON tt_b.term_taxonomy_id = tr_b.term_taxonomy_id WHERE tt_c.taxonomy = 'product_cat' AND tt_c.term_id IN ({$in}) AND p.post_type = 'product' AND p.post_status = 'publish' AND tt_b.taxonomy = %s AND tt_b.term_id <> %d GROUP BY tt_b.term_id ORDER BY COUNT(DISTINCT p.ID) DESC, tt_b.term_id LIMIT 12", $term->taxonomy, (int) $term->term_id ) ); } if ( count( $ids ) < 12 ) { $top = get_terms( array( 'taxonomy' => $term->taxonomy, 'hide_empty' => true, 'orderby' => 'count', 'order' => 'DESC', 'number' => 13, 'fields' => 'ids' ) ); $ids = array_merge( array_map( 'intval', (array) $ids ), is_array( $top ) ? array_map( 'intval', $top ) : array() ); } $ids = array_slice( array_values( array_unique( array_diff( array_map( 'intval', $ids ), array( (int) $term->term_id ) ) ) ), 0, 12 ); $kept = array( $version, $ids ); set_transient( $key, $kept, HOUR_IN_SECONDS ); } $out = array(); foreach ( (array) $kept[1] as $id ) { $brand = get_term( (int) $id, $term->taxonomy ); if ( $brand instanceof WP_Term && $brand->count > 0 ) { $out[] = $brand; } if ( count( $out ) >= $limit ) { break; } } return $out; } public static function index_url() { $id = get_transient( 'aieo_brand_index' ); if ( false === $id ) { global $wpdb; $id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'page' AND post_status = 'publish' AND ( post_content LIKE '%