*/ class A1AI_Content_Search { /** * Search content using WordPress native search * * @since 1.5.6 * @param string $query The search query * @param int $limit Maximum number of results (default: 5) * @return array Array of post objects with search results */ public function search_content($query, $limit = 5) { if (empty($query)) { return array(); } // Extract keywords from query $keywords = $this->extract_keywords($query); // If no keywords extracted, use original query if (empty($keywords)) { $search_terms = $query; } else { // Use first few keywords for search $search_terms = implode(' ', array_slice($keywords, 0, 3)); } // Use WP_Query for search $args = array( 's' => $search_terms, 'post_type' => array('post', 'page'), 'post_status' => 'publish', 'posts_per_page' => $limit, 'orderby' => 'relevance', 'order' => 'DESC' ); $query_obj = new WP_Query($args); $results = array(); if ($query_obj->have_posts()) { while ($query_obj->have_posts()) { $query_obj->the_post(); $post = get_post(); $results[] = (object) array( 'ID' => $post->ID, 'post_id' => $post->ID, 'post_title' => $post->post_title, 'title' => $post->post_title, 'post_excerpt' => !empty($post->post_excerpt) ? $post->post_excerpt : wp_trim_words(wp_strip_all_tags($post->post_content), 30), 'post_date' => $post->post_date, 'post_type' => $post->post_type, 'post_name' => $post->post_name ); } wp_reset_postdata(); } // Add relevance scoring and filter low-relevance results if (!empty($results) && !empty($keywords)) { foreach ($results as $result_item) { $title_lower = strtolower($result_item->post_title); $excerpt_lower = strtolower($result_item->post_excerpt); $keyword_matches = 0; foreach ($keywords as $kw) { if (strpos($title_lower, $kw) !== false) { $keyword_matches += 2; // Title match worth more } if (strpos($excerpt_lower, $kw) !== false) { $keyword_matches += 1; } } $result_item->relevance_score = count($keywords) > 0 ? $keyword_matches / (count($keywords) * 3) : 0; } $results = array_filter($results, function($r) { return $r->relevance_score >= 0.15; }); $results = array_values($results); } return $results; } /** * Search for pricing-related content * * @since 1.5.6 * @param string $query The search query * @param int $limit Maximum number of results (default: 5) * @return array Array of post objects with pricing-related results */ public function search_pricing_content($query, $limit = 5) { // Search for pages with pricing-related terms in title or slug $pricing_terms = array('pricing', 'price', 'prices', 'cost', 'costs', 'fee', 'fees', 'rates', 'plans', 'packages'); // Combine user query with pricing terms $search_terms = $query . ' ' . implode(' ', $pricing_terms); $args = array( 's' => $search_terms, 'post_type' => array('post', 'page'), 'post_status' => 'publish', 'posts_per_page' => $limit, 'orderby' => 'relevance', 'order' => 'DESC', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Limited to small result set (max 5), needed to find pricing page templates 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_wp_page_template', 'value' => 'pricing', 'compare' => 'LIKE' ) ) ); $query_obj = new WP_Query($args); $results = array(); if ($query_obj->have_posts()) { while ($query_obj->have_posts()) { $query_obj->the_post(); $post = get_post(); // Check if URL or title contains pricing keywords $permalink = get_permalink($post->ID); $title_lower = strtolower($post->post_title); $url_lower = strtolower($permalink); $has_pricing_term = false; foreach ($pricing_terms as $term) { if (strpos($title_lower, $term) !== false || strpos($url_lower, $term) !== false) { $has_pricing_term = true; break; } } if ($has_pricing_term || strpos(strtolower($post->post_content), '$') !== false) { $results[] = (object) array( 'ID' => $post->ID, 'post_id' => $post->ID, 'post_title' => $post->post_title, 'title' => $post->post_title, 'post_excerpt' => !empty($post->post_excerpt) ? $post->post_excerpt : wp_trim_words(wp_strip_all_tags($post->post_content), 30), 'post_date' => $post->post_date, 'post_type' => $post->post_type, 'post_name' => $post->post_name, 'has_pricing' => true ); } } wp_reset_postdata(); } // If no results, try broader search if (empty($results)) { return $this->search_content($query, $limit); } return $results; } /** * Format search results for AI prompt * * @since 1.5.6 * @param array $results Search results * @param bool $is_pricing Whether this is a pricing query * @return string Formatted context string */ public function format_results_for_prompt($results, $is_pricing = false) { if (empty($results)) { return ''; } $context = $is_pricing ? "RELEVANT PRICING CONTENT FROM WEBSITE:\n\n" : "RELEVANT CONTENT FROM WEBSITE:\n\n"; foreach ($results as $result) { $post_id = isset($result->ID) ? $result->ID : (isset($result->post_id) ? $result->post_id : 0); $title = isset($result->post_title) ? $result->post_title : (isset($result->title) ? $result->title : ''); $permalink = $post_id > 0 ? get_permalink($post_id) : ''; $excerpt = isset($result->post_excerpt) ? $result->post_excerpt : ''; if (empty($title) || empty($permalink)) { continue; } $context .= "[" . esc_html($title) . "](" . esc_url($permalink) . ")\n"; if (!empty($excerpt)) { $context .= wp_trim_words($excerpt, 25) . "\n"; } // For pricing queries, note that pricing may be available if ($is_pricing && (!empty($result->has_pricing) || strpos(strtolower($title), 'pric') !== false)) { $context .= "Note: This page may contain pricing information. Visit the page for details.\n"; } $context .= "\n"; } // Add upgrade note for free version if ($is_pricing) { $context .= "NOTE: This is basic content search. Upgrade to Premium for:\n"; $context .= "- Detailed pricing extraction with specific amounts and ranges\n"; $context .= "- Advanced semantic search with relevance scoring\n"; $context .= "- Unlimited content analysis with Tier 2 features\n"; $context .= "- Content depth, media, links, and CTA analysis\n"; } else { $context .= "NOTE: Upgrade to Premium for advanced content analysis, detailed pricing extraction, and enhanced search capabilities.\n"; } return $context; } /** * Extract keywords from user message * * @since 1.5.6 * @param string $message The user message * @return array Array of keywords */ private function extract_keywords($message) { $message = strtolower($message); $message = preg_replace('/[^a-z0-9\s]/', ' ', $message); $stop_words = array( 'how', 'what', 'when', 'where', 'why', 'who', 'can', 'could', 'would', 'should', 'will', 'the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'i', 'you', 'me', 'my', 'your', 'is', 'are', 'was', 'were', 'do', 'does', 'did', 'have', 'has', 'had', 'a', 'an', 'about', 'this', 'that', 'these', 'those', 'there', 'their' ); $words = str_word_count($message, 1); $words = array_diff($words, $stop_words); // Filter words with length > 2 $keywords = array_filter($words, function($word) { return strlen($word) > 2; }); return array_values($keywords); } /** * Check if user message is a pricing-related query * * @since 1.5.6 * @param string $message The user message * @return bool True if pricing-related, false otherwise */ public function is_pricing_query($message) { $pricing_keywords = array( 'price', 'pricing', 'prices', 'cost', 'costs', 'costing', 'fee', 'fees', 'rate', 'rates', 'how much', 'how much does', 'how much do', 'what does it cost', 'what do they cost', 'afford', 'affordable', 'affordability', 'charge', 'charges', 'charging', 'pay', 'payment', 'payments', 'subscription', 'subscriptions', 'plan', 'plans', 'package', 'packages', 'tier', 'tiers', 'pricing tier' ); $message_lower = strtolower($message); foreach ($pricing_keywords as $keyword) { if (strpos($message_lower, $keyword) !== false) { return true; } } return false; } /** * Get content suggestions for chatbot responses * * @since 1.5.6 * @param string $user_message The user message * @param int $limit Maximum number of results * @return array Array of search results */ public function get_content_suggestions($user_message, $limit = 3) { $is_pricing = $this->is_pricing_query($user_message); $results = $is_pricing ? $this->search_pricing_content($user_message, $limit) : $this->search_content($user_message, $limit); // Reference the site's important pages first; blog posts fall through for // topics the main pages don't cover. Stable, so relevance order is kept // within each importance tier. if (class_exists('A1AI_Content_Priority')) { A1AI_Content_Priority::sort_objects($results); } return $results; } }