Website : phonehp.ir
backdoor
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Home
Console
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
nehpir
/
public_html
/
wp-content
/
plugins
/
woocommersa
/
includes
/
Services
/
Filename :
BrandService.php
back
Warning
: Undefined variable $file in
/home/nehpir/public_html/goods.php
on line
545
Copy
<?php namespace WooCommersa\Services; if (!defined('ABSPATH')) exit; /** * Dynamic Brand Management Service * Handles detection, retrieval and management of product brands across any structure. */ class BrandService { private static $instance = null; private $active_source = null; public static function get_instance() { if (null === self::$instance) { self::$instance = new self(); } return self::$instance; } private function __construct() { // Initialization happens after init to ensure all taxonomies/attributes are registered add_action('init', [$this, 'initialize'], 20); } public function initialize() { $this->active_source = $this->get_configured_source(); } /** * Get the active brand source (Manual Setting -> Auto Detect) */ public function get_configured_source() { $settings = get_option('woocommersa_settings', []); // 1. Manual User Setting (Explicit) if (!empty($settings['brand_source_type']) && !empty($settings['brand_source_slug'])) { return [ 'type' => $settings['brand_source_type'], 'slug' => $settings['brand_source_slug'] ]; } // 2. Legacy Backward Compatibility (For existing 1000+ users) $legacy_slug = !empty($settings['brand_taxonomy_slug']) ? $settings['brand_taxonomy_slug'] : ''; if ($legacy_slug && (taxonomy_exists($legacy_slug) || strpos($legacy_slug, 'pa_') === 0)) { return [ 'type' => (strpos($legacy_slug, 'pa_') === 0) ? 'attribute' : 'taxonomy', 'slug' => $legacy_slug ]; } // 3. Smart Auto Detection (Scoring System) return $this->detect_brand_source(); } /** * Smart Auto Detection Engine * Scores all possible sources and picks the best one. */ public function detect_brand_source() { $sources = $this->get_all_potential_sources(); $best_source = null; $max_score = 0; $keywords = [ 'product_brand' => 100, 'pwb-brand' => 90, 'brand' => 80, 'brands' => 70, 'manufacturer' => 60, 'make' => 50, 'label' => 40, 'pa_brand' => 85, // Common WC Attribute ]; foreach ($sources as $source) { $score = 0; $slug = $source['slug']; foreach ($keywords as $key => $weight) { if ($slug === $key) { $score += $weight; } elseif (strpos($slug, $key) !== false) { $score += ($weight / 2); } } if ($score > $max_score) { $max_score = $score; $best_source = $source; } } return $best_source ?: ['type' => 'taxonomy', 'slug' => 'product_brand']; // Default fallback } /** * Get all potential sources (Taxonomies, Attributes, Common Metas) * Cached using transients for performance. */ public function get_all_potential_sources($force_refresh = false) { $cache_key = 'wm_brand_potential_sources'; $cached = get_transient($cache_key); if ($cached !== false && !$force_refresh) { return $cached; } $sources = []; // 1. All Product Taxonomies $taxonomies = get_object_taxonomies('product', 'objects'); foreach ($taxonomies as $tax) { if (in_array($tax->name, ['product_cat', 'product_tag', 'product_type', 'product_visibility'])) continue; $sources[] = [ 'type' => 'taxonomy', 'slug' => $tax->name, 'label' => $tax->label . ' (Taxonomy)' ]; } // 2. All WooCommerce Attributes if (function_exists('wc_get_attribute_taxonomies')) { $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $attr) { $slug = 'pa_' . $attr->attribute_name; $sources[] = [ 'type' => 'attribute', 'slug' => $slug, 'label' => $attr->attribute_label . ' (Attribute)' ]; } } // 3. Common Meta Keys (Presets) $meta_presets = [ '_brand' => 'Brand (Meta)', '_manufacturer' => 'Manufacturer (Meta)', '_product_brand' => 'Product Brand (Meta)' ]; foreach ($meta_presets as $slug => $label) { $sources[] = [ 'type' => 'meta', 'slug' => $slug, 'label' => $label ]; } set_transient($cache_key, $sources, DAY_IN_SECONDS); return $sources; } /** * Get brand name(s) for a product * THE SINGLE SOURCE OF TRUTH */ public function get_product_brand($product_id) { if (!$this->active_source) { $this->active_source = $this->get_configured_source(); } $source = $this->active_source; if (!$source) return []; switch ($source['type']) { case 'taxonomy': case 'attribute': $terms = get_the_terms($product_id, $source['slug']); if ($terms && !is_wp_error($terms)) { return wp_list_pluck($terms, 'name'); } break; case 'meta': $val = get_post_meta($product_id, $source['slug'], true); return !empty($val) ? [$val] : []; } return []; } /** * Invalidate Cache */ public function clear_cache() { delete_transient('wm_brand_potential_sources'); } }