/** * NEW: Calculate bonus points for exact word matches (Stage 1 - no exclude list yet) */ function calculate_bonus_points($terms, $post_id, $post_title, $titleofsong, $main_artist, $authorsofsong, $category_string) { $bonus_points = 0; // Get lyrics content safely $chordstrans = get_field('chordstrans', $post_id); if (!$chordstrans) { $chordstrans = ''; // Default to empty string if no lyrics } // For each search term, check for exact word matches foreach ($terms as $term) { $term_clean = trim($term); if (empty($term_clean)) { continue; } // Count matches in high-value fields (2 points each) $high_value_fields = array($post_title, $titleofsong, $main_artist, $authorsofsong, $category_string); foreach ($high_value_fields as $field) { if ($field && $this->has_exact_word_match($term_clean, $field)) { $bonus_points += 2; break; // Only count once per term across high-value fields } } // Count matches in lyrics (1 point each) if ($chordstrans && $this->has_exact_word_match($term_clean, $chordstrans)) { $bonus_points += 1; } } return $bonus_points; } /** * Check if a word exists as an exact standalone word (case insensitive) */ function has_exact_word_match($word, $text) { // Use word boundaries to ensure exact word match return preg_match('/\b' . preg_quote($word, '/') . '\b/i', $text); } /** * Check for exact category match */ function has_exact_category_match($search_term_lower, $categories) { foreach ($categories as $category) { if (strtolower($category) === $search_term_lower) { return true; } } return false; }