// --- GENERATORE TAG AUTOMATICI --- add_action('wp_ajax_mpm_gen_auto_tags', 'mpm_gen_auto_tags_handler'); function mpm_gen_auto_tags_handler() { if (!current_user_can('edit_products')) wp_send_json_error('Permessi negati'); $pid = intval($_POST['pid']); $product = wc_get_product($pid); if (!$product) wp_send_json_error('Prodotto non trovato'); // 1. Dati Base $name = $product->get_name(); $race = get_post_meta($pid, '_mpm_race', true); $cats = get_the_terms($pid, 'product_cat'); $cat_name = ($cats && !is_wp_error($cats)) ? $cats[0]->name : ''; // Fallback se manca la razza nel meta if(!$race && $cat_name) $race = $cat_name; // 2. Iniziamo la lista dei Tag $tags = []; $tags[] = "Games Workshop"; // Tag base sempre presente // Aggiungiamo la Razza/Fazione se esiste if($race && $race !== 'Games Workshop') { $tags[] = $race; } // 3. Rilevamento Sistema di Gioco (Logica a parole chiave) $context = $name . ' ' . $race . ' ' . $cat_name; // Warhammer 40.000 if (preg_match('/(40k|40\.000|40,000|Space Marine|Necron|Tyranid|Aeldari|Drukhari|Tau Empire|Ork|Custodes|Mechanicus|Militarum|Sororitas|Chaos Space|Votann)/i', $context)) { $tags[] = "Warhammer 40.000"; } // Age of Sigmar elseif (preg_match('/(Sigmar|Stormcast|Skaven|Sylvaneth|Lumineth|Kharadron|Fyreslayers|Idoneth|Daughter of Khaine|Nighthaunt|Ossiarch|Gloomspite|Ogor|Gargant)/i', $context)) { $tags[] = "Age of Sigmar"; } // Horus Heresy elseif (preg_match('/(Horus Heresy|30k|Legiones Astartes)/i', $context)) { $tags[] = "The Horus Heresy"; } // Blood Bowl elseif (stripos($context, 'Blood Bowl') !== false) { $tags[] = "Blood Bowl"; } // Kill Team elseif (stripos($context, 'Kill Team') !== false) { $tags[] = "Kill Team"; $tags[] = "Warhammer 40.000"; // Di solito è associato } // Warcry elseif (stripos($context, 'Warcry') !== false) { $tags[] = "Warcry"; $tags[] = "Age of Sigmar"; } // Underworlds elseif (stripos($context, 'Underworlds') !== false) { $tags[] = "Warhammer Underworlds"; } // Old World elseif (stripos($context, 'Old World') !== false || stripos($context, 'Bretonnia') !== false || stripos($context, 'Tomb Kings') !== false) { $tags[] = "The Old World"; } // 4. Rilevamento Tipo Prodotto (Simile alla logica descrizioni) if (stripos($context, 'Brush') !== false || stripos($context, 'Pennell') !== false) { $tags[] = "Modellismo"; $tags[] = "Pennelli"; } elseif (stripos($context, 'Dice') !== false || stripos($context, 'Dadi') !== false) { $tags[] = "Accessori"; $tags[] = "Dadi"; } elseif (stripos($context, 'Datacards') !== false || stripos($context, 'Carte') !== false) { $tags[] = "Accessori"; $tags[] = "Carte"; } elseif (stripos($context, 'Paint') !== false || stripos($context, 'Color') !== false || stripos($context, 'Contrast') !== false || stripos($context, 'Layer') !== false || stripos($context, 'Base') !== false || stripos($context, 'Spray') !== false) { if (stripos($context, 'Book') === false) { $tags[] = "Modellismo"; $tags[] = "Colori Citadel"; } } elseif (stripos($context, 'Codex') !== false || stripos($context, 'Battletome') !== false || stripos($context, 'Rulebook') !== false || stripos($context, 'Manuale') !== false || stripos($context, 'Novel') !== false || stripos($context, 'Black Library') !== false) { $tags[] = "Libri"; $tags[] = "Manuali"; } else { // Se non è niente di sopra, è probabilmente una miniatura $tags[] = "Miniature"; } // 5. Salvataggio // array_unique per evitare duplicati, wp_set_object_terms con 'append' = false per sovrascrivere e pulire $tags = array_unique($tags); wp_set_object_terms($pid, $tags, 'product_tag', false); // Flag completato update_post_meta($pid, '_mpm_tags_optimized', 'yes'); wp_send_json_success(['tags' => implode(', ', $tags)]); }