get_settings(); if ( empty( $s['enabled'] ) ) { return; } add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_action( 'woocommerce_single_product_summary', array( $this, 'output_classic_before_title' ), 4 ); add_action( 'woocommerce_single_product_summary', array( $this, 'output_classic_under_title' ), 6 ); add_action( 'woocommerce_product_meta_end', array( $this, 'output_classic_after_post_meta' ) ); } public function output_classic_before_title() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( $s['position'] === 'before_title' ) { echo wp_kses_post( $this->get_html( 'before_title' ) ); return; } if ( $s['position'] === 'mobile_before_desktop_inline' ) { echo wp_kses_post( $this->get_html( 'mobile_only_before' ) ); } } public function output_classic_under_title() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( in_array( $s['position'], array( 'under_title', 'in_title_row', 'mobile_before_desktop_inline' ), true ) ) { echo wp_kses_post( $this->get_html() ); } } public function output_classic_after_post_meta() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( $s['position'] === 'after_post_meta' ) { echo wp_kses_post( $this->get_html() ); } } public function get_settings($raw = false) { $stored = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $merged = array_merge( array( 'enabled' => false, 'mode' => 'text', 'position' => 'before_title', 'show_label' => false, 'label_text' => '', 'image_max_width' => 120, 'image_max_height' => 40, 'image_max_width_mobile' => 90, 'image_max_height_mobile' => 30, 'mobile_breakpoint' => 768, 'text_color' => '#212121', 'label_color' => '#6b7280', 'link_color' => '', 'title_bold' => true, 'font_size' => '0.92em', 'align' => 'flex-start', 'margin_top' => 10, 'margin_bottom' => 10, 'custom_class' => '', ), $stored ); if ( class_exists( 'AIEO_DMM_Product_Display' ) ) { $bz = AIEO_DMM_Product_Display::get_cluster_zones(); $bz = isset( $bz['brand_display'] ) ? (string) $bz['brand_display'] : 'info'; if ( $bz === 'beside_title' ) { $merged['position'] = 'in_title_row'; } elseif ( in_array( $merged['position'], array( 'in_title_row', 'mobile_before_desktop_inline' ), true ) ) { $merged['position'] = 'under_title'; } } return $raw ? ($merged) : self::fill_i18n_defaults($merged); } private static function i18n_labels() { return array( 'label_text' => __('Brand', 'ai-eshop-optimizer'), ); } private static function fill_i18n_defaults(array $s) { if (!did_action('init')) { return $s; } foreach (self::i18n_labels() as $k => $v) { if (!isset($s[$k]) || '' === trim((string) $s[$k])) { $s[$k] = $v; } } return $s; } public function ajax_save() { AIEO_DMM::verify_ajax_request(); update_option( self::OPTION_KEY, array( 'enabled' => ! empty( $_POST['enabled'] ), 'mode' => ( isset( $_POST['mode'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['mode'] ) ), array( 'text', 'image' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : 'text', 'position' => ( isset( $_POST['position'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['position'] ) ), array( 'before_title', 'in_title_row', 'under_title', 'after_post_meta', 'mobile_before_desktop_inline' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['position'] ) ) : 'under_title', 'show_label' => ! empty( $_POST['show_label'] ), 'label_text' => isset( $_POST['label_text'] ) ? sanitize_text_field( wp_unslash( $_POST['label_text'] ) ) : '', 'image_max_width' => max( 20, min( 400, isset( $_POST['image_max_width'] ) ? (int) $_POST['image_max_width'] : 120 ) ), 'image_max_height' => max( 20, min( 400, isset( $_POST['image_max_height'] ) ? (int) $_POST['image_max_height'] : 40 ) ), 'image_max_width_mobile' => max( 20, min( 400, isset( $_POST['image_max_width_mobile'] ) ? (int) $_POST['image_max_width_mobile'] : 90 ) ), 'image_max_height_mobile' => max( 20, min( 400, isset( $_POST['image_max_height_mobile'] ) ? (int) $_POST['image_max_height_mobile'] : 30 ) ), 'mobile_breakpoint' => max( 320, min( 1280, isset( $_POST['mobile_breakpoint'] ) ? (int) $_POST['mobile_breakpoint'] : 768 ) ), 'text_color' => isset( $_POST['text_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['text_color'] ) ) ?: '#212121' ) : '#212121', 'label_color' => isset( $_POST['label_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['label_color'] ) ) ?: '#6b7280' ) : '#6b7280', 'link_color' => isset( $_POST['link_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['link_color'] ) ) ?: '' ) : '', 'title_bold' => ! empty( $_POST['title_bold'] ), 'font_size' => ( isset( $_POST['font_size'] ) && preg_match( '/^[0-9]*\.?[0-9]+\s*(rem|em|px|%|vw|vh|pt)$/i', trim( (string) wp_unslash( $_POST['font_size'] ) ) ) ) ? trim( (string) wp_unslash( $_POST['font_size'] ) ) : '0.92em', 'align' => ( isset( $_POST['align'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['align'] ) ), array( 'flex-start', 'center', 'flex-end' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['align'] ) ) : 'flex-start', 'margin_top' => max( 0, min( 100, isset( $_POST['margin_top'] ) ? (int) $_POST['margin_top'] : 10 ) ), 'margin_bottom' => max( 0, min( 100, isset( $_POST['margin_bottom'] ) ? (int) $_POST['margin_bottom'] : 10 ) ), 'custom_class' => isset( $_POST['custom_class'] ) ? sanitize_html_class( wp_unslash( $_POST['custom_class'] ) ) : '', ), false ); wp_send_json_success( array( 'message' => __( 'Brand Display settings saved.', 'ai-eshop-optimizer' ) ) ); } public function enqueue_styles() { if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; } $s = $this->get_settings(); if ( 'after_post_meta' === $this->block_slot( $s['position'] ) ) { AIEO_DMM_Product_Meta::enqueue_table(); } $css = sprintf( '.aieo-brand-display{' . 'display:flex;' . 'align-items:center;' . 'justify-content:%1$s;' . 'gap:6px;' . 'margin:%2$dpx 0 %3$dpx;' . 'color:%4$s;' . 'font-size:%11$s;' . '}' . '.aieo-brand-display a{color:inherit;text-decoration:none;}' . '.aieo-brand-display a:hover{text-decoration:underline;}' . '.aieo-brand-display-label{color:%5$s;}' . '.aieo-brand-display--image img{' . 'display:block;' . 'width:%6$dpx;' . 'height:%7$dpx;' . 'object-fit:contain;' . 'object-position:%12$s center;' . '}' . '.aieo-brand-display--mobile-only-before{display:none;}' . '.aieo-brand-display--desktop-only-inline{display:flex;}' . '@media (max-width:%8$dpx){' . '.aieo-brand-display--image img{' . 'width:%9$dpx;' . 'height:%10$dpx;' . '}' . '.aieo-brand-display--before-title{' . 'justify-content:center;' . '}' . '.aieo-brand-display--before-title.aieo-brand-display--image img{' . 'object-position:center center;' . '}' . '.aieo-brand-display--mobile-only-before{display:flex;}' . '.aieo-brand-display--desktop-only-inline{display:none !important;}' . '}', esc_attr( $s['align'] ), (int) $s['margin_top'], (int) $s['margin_bottom'], esc_attr( $s['text_color'] ), esc_attr( $s['label_color'] ), (int) $s['image_max_width'], (int) $s['image_max_height'], (int) $s['mobile_breakpoint'], (int) $s['image_max_width_mobile'], (int) $s['image_max_height_mobile'], esc_attr( (string) $s['font_size'] ), 'flex-end' === $s['align'] ? 'right' : ( 'center' === $s['align'] ? 'center' : 'left' ) ); $css .= '.aieo-brand-display .aieo-brand-display-label{font-weight:' . ( ! empty( $s['title_bold'] ) ? '600' : '400' ) . ';}'; if ( ! empty( $s['link_color'] ) ) { $css .= '.aieo-brand-display a{color:' . esc_attr( (string) $s['link_color'] ) . ';}'; } $css .= 'html[data-rooster-design="theme"] .aieo-brand-display{color:var(--wp--preset--color--fg);}' . 'html[data-rooster-design="theme"] .aieo-brand-display a{color:inherit;}' . 'html[data-rooster-design="theme"] .aieo-brand-display .aieo-brand-display-label{color:var(--wp--preset--color--muted);}'; $css .= '.aieo-brand-display--text a{display:inline-block;padding-block:4px;margin-block:-4px;}'; $css .= 'html[data-rooster-design="theme"] .aieo-brand-display--text{margin:0;font-size:var(--wp--preset--font-size--xs);letter-spacing:var(--wp--custom--rooster--tracking--wide);text-transform:uppercase;color:var(--wp--preset--color--muted);}' . 'html[data-rooster-design="theme"] .aieo-brand-display--text.aieo-brand-display--before-title{justify-content:flex-start;align-items:flex-start;}'; $css .= 'html[data-rooster-design="theme"] .aieo-brand-display--text.aieo-brand-display--before-title > *{text-box:trim-both cap alphabetic;}' . 'html[data-rooster-design="theme"] .aieo-brand-display--text.aieo-brand-display--before-title > a{padding-block:11px;margin-block:-11px;}'; wp_register_style( 'aieo-brand-display', false, array(), defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1.0' ); wp_enqueue_style( 'aieo-brand-display' ); wp_add_inline_style( 'aieo-brand-display', $css ); } public function is_active() { $s = $this->get_settings(); return ! empty( $s['enabled'] ); } public function render_before_title() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( $this->block_slot( $s['position'] ) === 'before_title' ) { echo wp_kses_post( $this->get_html( 'before_title' ) ); return; } if ( $s['position'] === 'mobile_before_desktop_inline' ) { echo wp_kses_post( $this->get_html( 'mobile_only_before' ) ); return; } } public function render_under_title() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( $this->block_slot( $s['position'] ) !== 'under_title' ) { return; } echo wp_kses_post( $this->get_html() ); } public function render_after_post_meta() { if ( ! $this->is_active() ) { return; } $s = $this->get_settings(); if ( $this->block_slot( $s['position'] ) !== 'after_post_meta' ) { return; } echo wp_kses_post( $this->get_row_html() ); } private function get_row_html() { global $product; $product_id = ( $product && is_a( $product, 'WC_Product' ) ) ? (int) $product->get_id() : ( ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) ? (int) get_the_ID() : 0 ); $brand_term = $product_id ? $this->get_brand_term( $product_id ) : null; if ( ! $brand_term ) { return ''; } $url = get_term_link( $brand_term ); if ( is_wp_error( $url ) ) { return ''; } $s = $this->get_settings(); $value = '' . esc_html( $brand_term->name ) . ''; $image = false; if ( 'image' === $s['mode'] ) { $image_id = $this->get_brand_logo_id( (int) $brand_term->term_id ); $img = $image_id > 0 ? $this->build_logo_img_html( $image_id, $brand_term, (int) $s['image_max_width'], (int) $s['image_max_height'] ) : ''; if ( '' !== $img ) { $value = '' . $img . ''; $image = true; } } $cls = 'aieo-product-meta aieo-brand-display--meta' . ( $image ? ' aieo-brand-display--image' : '' ) . ( ! empty( $s['custom_class'] ) ? ' ' . (string) $s['custom_class'] : '' ); return '
' . esc_html__( 'Brand Display shortcode can only be used on single product pages.', 'ai-eshop-optimizer' ) . '
'; } return ''; } return $this->get_html(); } private function get_brand_term( $product_id ) { if ( ! $product_id ) { return null; } $detected = class_exists( 'AIEO_DMM_Brand_Discounts' ) ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : null; $taxes = $detected ? array( $detected ) : array( 'product_brand', 'pwb-brand', 'yith_product_brand', 'product_brands', 'berocket_brand' ); foreach ( $taxes as $tax ) { if ( ! taxonomy_exists( $tax ) ) { continue; } $terms = get_the_terms( $product_id, $tax ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { return $terms[0]; } } return null; } private function get_brand_logo_id( $term_id ) { if ( ! $term_id ) { return 0; } global $wpdb; $table = $wpdb->prefix . 'aieo_dmm_brand_extras'; if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT logo_image_id FROM {$table} WHERE term_id = %d LIMIT 1", $term_id ) ); if ( $id > 0 ) { return $id; } } $wc_id = (int) get_term_meta( $term_id, 'thumbnail_id', true ); return $wc_id > 0 ? $wc_id : 0; } private function build_logo_img_html( $image_id, $brand_term, $max_w, $max_h ) { $alt = (string) $brand_term->name; $html = wp_get_attachment_image( (int) $image_id, 'medium', false, array( 'class' => 'aieo-brand-display-img', 'alt' => $alt, 'title' => $alt, 'loading' => 'lazy', 'decoding' => 'async', 'sizes' => sprintf( '%dpx', (int) $max_w ), ) ); return is_string( $html ) ? $html : ''; } private function get_html( $render_position = '' ) { global $product; $product_id = 0; if ( $product && is_a( $product, 'WC_Product' ) ) { $product_id = (int) $product->get_id(); } elseif ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) { $product_id = (int) get_the_ID(); } if ( ! $product_id ) { return ''; } $brand_term = $this->get_brand_term( $product_id ); if ( ! $brand_term ) { return ''; } $url = get_term_link( $brand_term ); if ( is_wp_error( $url ) ) { return ''; } $s = $this->get_settings(); $custom_class = ! empty( $s['custom_class'] ) ? ' ' . esc_attr( $s['custom_class'] ) : ''; if ( $render_position === 'in_title_row' ) { $custom_class .= ' aieo-brand-display--in-title-row'; } elseif ( $render_position === 'before_title' ) { $custom_class .= ' aieo-brand-display--before-title'; } elseif ( $render_position === 'mobile_only_before' ) { $custom_class .= ' aieo-brand-display--before-title aieo-brand-display--mobile-only-before'; } elseif ( $render_position === 'desktop_only_inline' ) { $custom_class .= ' aieo-brand-display--in-title-row aieo-brand-display--desktop-only-inline'; } $label = ''; if ( ! empty( $s['show_label'] ) && ! empty( $s['label_text'] ) ) { $label = '' . esc_html( $s['label_text'] ) . ' '; } if ( $s['mode'] === 'image' ) { $image_id = $this->get_brand_logo_id( (int) $brand_term->term_id ); if ( $image_id > 0 ) { $img = $this->build_logo_img_html( $image_id, $brand_term, (int) $s['image_max_width'], (int) $s['image_max_height'] ); if ( $img !== '' ) { return sprintf( '