0) { $ids[] = $front; } /** * Filter the list of post IDs treated as inherently key pages. * * @param array $ids Post IDs. */ return array_values(array_unique(array_map('intval', apply_filters('a1ai_key_page_ids', $ids)))); } /** * Importance rank for a single item. * * 2 = key page (pricing/services/contact/etc. or the front page) * 1 = any other page * 0 = blog post or any non-page type (never outranks a page) * * @param string $post_type The post type ('page', 'post', ...). * @param string $slug The post slug (post_name). * @param int $post_id Optional post ID (for front-page detection). * @param string $title Optional title (secondary key-term match). * @return int 0, 1 or 2. */ public static function importance_rank($post_type, $slug, $post_id = 0, $title = '') { $is_page = (strtolower((string) $post_type) === 'page'); $is_front = ($post_id && in_array((int) $post_id, self::front_page_ids(), true)); // Posts and other non-page types never outrank pages. if (!$is_page && !$is_front) { return 0; } if ($is_front) { return 2; } if (self::matches_key_terms($slug) || self::matches_key_terms($title)) { return 2; } return 1; } /** * Whether a slug/title contains any key-page token. * * @param string $text Slug or title. * @return bool */ private static function matches_key_terms($text) { $text = strtolower((string) $text); if ($text === '') { return false; } // Normalise separators so "our-pricing" / "Plans & Pricing" both match. $normalized = preg_replace('/[^a-z0-9]+/', ' ', $text); foreach (self::key_page_slugs() as $token) { if ($token !== '' && strpos($normalized, $token) !== false) { return true; } } return false; } /** * Extract the trailing slug from a permalink URL. * * @param string $url The permalink. * @return string The last path segment, or ''. */ public static function slug_from_url($url) { $path = wp_parse_url((string) $url, PHP_URL_PATH); if (empty($path)) { return ''; } $parts = array_values(array_filter(explode('/', trim($path, '/')))); return empty($parts) ? '' : strtolower(end($parts)); } /** * Stable-sort an array of result objects so important pages lead while the * existing (relevance) order is preserved within each importance tier. * * Handles both shapes the chatbot uses: content-search objects * (post_type/post_name/ID) and workspace-knowledge rows (page_type/page_url). * * @param array $results Passed by reference; reordered in place. * @return void */ public static function sort_objects(&$results) { if (!is_array($results) || count($results) < 2) { return; } $indexed = array(); foreach ($results as $i => $item) { $indexed[] = array( 'idx' => $i, 'rank' => self::rank_of($item), 'item' => $item, ); } // Higher rank first; original index breaks ties (keeps relevance order, // and guards against non-stable usort on PHP < 8.0). usort($indexed, function ($a, $b) { return ($b['rank'] <=> $a['rank']) ?: ($a['idx'] <=> $b['idx']); }); $results = array_map(function ($row) { return $row['item']; }, $indexed); } /** * Compute the importance rank of a single result object/array. * * @param object|array $item A result row. * @return int */ private static function rank_of($item) { $get = function ($keys) use ($item) { foreach ((array) $keys as $key) { if (is_object($item) && isset($item->$key)) { return $item->$key; } if (is_array($item) && isset($item[$key])) { return $item[$key]; } } return ''; }; $post_type = $get(array('post_type', 'page_type')); $post_id = (int) $get(array('ID', 'post_id')); $title = $get(array('post_title', 'title', 'page_title')); $slug = $get(array('post_name', 'slug')); if ($slug === '') { $url = $get(array('permalink', 'page_url')); if ($url !== '') { $slug = self::slug_from_url($url); } elseif ($post_id > 0) { $post = get_post($post_id); if ($post) { $slug = $post->post_name; $post_type = $post_type !== '' ? $post_type : $post->post_type; } } } return self::importance_rank($post_type, $slug, $post_id, $title); } }