'error', 'text' => __('Select an agent and provide at least one sitemap URL (check boxes or enter manually).', 'agenized-for-woocommerce')]; } else { $ok = 0; $fail = 0; foreach ($urls as $u) { $res = AgenizedApi::create_agent_web_source($agent_id, [ 'url' => $u, 'type' => 'sitemap', ]); if ($res['ok']) { $ok++; } else { $fail++; } } if ($fail === 0) { /* translators: %1$d is the number of sitemap sources added. */ $notice = ['type' => 'success', 'text' => sprintf(__('Added %1$d sitemap source(s) to the agent.', 'agenized-for-woocommerce'), $ok)]; } else { /* translators: %1$d is number of sources added, %2$d is number of failed sources. */ $notice = ['type' => 'warning', 'text' => sprintf(__('Added %1$d source(s), %2$d failed. Check that the sitemap URLs are reachable by the Merchant API worker.', 'agenized-for-woocommerce'), $ok, $fail)]; } $selected_agent_id = $agent_id; } } if ($action === 'delete_web_source') { $agent_id = isset($_POST['agent_id']) ? sanitize_text_field(wp_unslash($_POST['agent_id'])) : ''; $web_source_id = isset($_POST['web_source_id']) ? sanitize_text_field(wp_unslash($_POST['web_source_id'])) : ''; $res = AgenizedApi::delete_agent_web_source($agent_id, $web_source_id); $notice = $res['ok'] ? ['type' => 'success', 'text' => __('Web source deleted.', 'agenized-for-woocommerce')] : ['type' => 'error', 'text' => __('Delete failed:', 'agenized-for-woocommerce') . ' ' . ($res['message'] ?? '')]; $selected_agent_id = $agent_id; } if ($action === 'refresh_web_source') { $agent_id = isset($_POST['agent_id']) ? sanitize_text_field(wp_unslash($_POST['agent_id'])) : ''; $web_source_id = isset($_POST['web_source_id']) ? sanitize_text_field(wp_unslash($_POST['web_source_id'])) : ''; $res = AgenizedApi::refresh_agent_web_source($agent_id, $web_source_id); $notice = $res['ok'] ? ['type' => 'success', 'text' => __('Refresh queued.', 'agenized-for-woocommerce')] : ['type' => 'error', 'text' => __('Refresh failed:', 'agenized-for-woocommerce') . ' ' . ($res['message'] ?? '')]; $selected_agent_id = $agent_id; } } $sitemap_data = self::discover_sitemaps(); $main_sitemap = $sitemap_data['main'] ?? ''; $subsitemaps = $sitemap_data['subsitemaps'] ?? []; $sources_res = ($selected_agent_id !== '') ? AgenizedApi::list_agent_web_sources($selected_agent_id) : ['ok' => true, 'sources' => []]; $sources = $sources_res['ok'] ? ($sources_res['sources'] ?? []) : []; ?>


0): ?>


' . esc_html($s_url) . '' : ''; ?>
} */ private static function discover_sitemaps(): array { $candidates = [ home_url('/wp-sitemap.xml'), home_url('/sitemap.xml'), ]; $main_sitemap = ''; $subsitemaps = []; // Try each candidate sitemap URL foreach ($candidates as $candidate) { $locs = self::fetch_sitemap_locs($candidate); if (count($locs) > 0) { // Found a valid sitemap index with subsitemaps if ($main_sitemap === '') { $main_sitemap = $candidate; } foreach ($locs as $loc) { $loc = trim((string) $loc); if ($loc && !in_array($loc, $subsitemaps, true)) { $subsitemaps[] = $loc; } } // If we found subsitemaps, we're done if (count($subsitemaps) > 0) { break; } } elseif ($main_sitemap === '') { // Even if no subsitemaps found, set as main if it's a valid sitemap // (might be a single sitemap, not an index) $main_sitemap = $candidate; } } return [ 'main' => $main_sitemap, 'subsitemaps' => array_values($subsitemaps), ]; } /** * Fetches sitemap XML and returns URLs (works for sitemapindex and urlset). * * @return array */ private static function fetch_sitemap_locs(string $sitemap_url): array { $xml = self::fetch_sitemap_xml($sitemap_url); // Fallback: if localhost failed, try host.docker.internal if ($xml === '' && strpos($sitemap_url, 'localhost') !== false) { $fallback_url = str_replace('localhost', 'host.docker.internal', $sitemap_url); $xml = self::fetch_sitemap_xml($fallback_url); } if ($xml === '') { return []; } $locs = []; // Try to use SimpleXML if available (handles namespaces better) if (function_exists('simplexml_load_string')) { libxml_use_internal_errors(true); $sxe = @simplexml_load_string($xml); if ($sxe !== false) { // Register namespace $sxe->registerXPathNamespace('s', 'http://www.sitemaps.org/schemas/sitemap/0.9'); // Try sitemapindex first (main sitemap with subsitemaps) $sitemaps = $sxe->xpath('//s:sitemap/s:loc'); if ($sitemaps && count($sitemaps) > 0) { foreach ($sitemaps as $loc) { $url = trim((string) $loc); if ($url) { $locs[] = $url; } } return $locs; } // Fallback: try urlset (individual sitemap with URLs) $urls = $sxe->xpath('//s:url/s:loc'); if ($urls && count($urls) > 0) { foreach ($urls as $loc) { $url = trim((string) $loc); if ($url) { $locs[] = $url; } } return $locs; } } } // Fallback: simple regex-based parsing // Match ... tags (works even with namespaces in the root element) if (preg_match_all('/([^<]+)<\/loc>/i', $xml, $matches)) { foreach ($matches[1] as $loc) { $loc = trim((string) $loc); if ($loc) { $locs[] = $loc; } } } return $locs; } /** * Fetches sitemap XML via the WordPress HTTP API (wp_remote_get). * * @param string $sitemap_url * @return string Empty string on failure. */ private static function fetch_sitemap_xml(string $sitemap_url): string { $host = (string) wp_parse_url($sitemap_url, PHP_URL_HOST); $is_local_host = in_array($host, ['localhost', '127.0.0.1', 'host.docker.internal'], true); $is_dev_env = function_exists('wp_get_environment_type') && wp_get_environment_type() !== 'production'; $ssl_verify = !($is_local_host && $is_dev_env); $res = wp_remote_get( $sitemap_url, [ 'timeout' => 10, 'sslverify' => $ssl_verify, 'redirection' => 5, ] ); if (is_wp_error($res)) { return ''; } $status = (int) wp_remote_retrieve_response_code($res); if ($status < 200 || $status >= 300) { return ''; } return (string) wp_remote_retrieve_body($res); } /** * Get a friendly label for a sitemap URL. * * @param string $url * @return string */ private static function get_sitemap_label(string $url): string { // Extract the sitemap type from the URL // Examples: // wp-sitemap-posts-post-1.xml -> "Blog Posts" // wp-sitemap-posts-page-1.xml -> "Pages" // wp-sitemap-posts-product-1.xml -> "Products" // wp-sitemap-taxonomies-category-1.xml -> "Categories" // wp-sitemap-taxonomies-product_cat-1.xml -> "Product Categories" // wp-sitemap-users-1.xml -> "Users" if (preg_match('/wp-sitemap-posts-([^-]+)/', $url, $m)) { $type = $m[1]; $labels = [ 'post' => 'Blog Posts', 'page' => 'Pages', 'product' => 'Products', ]; return $labels[$type] ?? ucfirst($type); } if (preg_match('/wp-sitemap-taxonomies-([^-]+)/', $url, $m)) { $type = $m[1]; $labels = [ 'category' => 'Categories', 'product_cat' => 'Product Categories', 'tag' => 'Tags', 'product_tag' => 'Product Tags', ]; return $labels[$type] ?? ucfirst(str_replace('_', ' ', $type)); } if (preg_match('/wp-sitemap-users/', $url)) { return 'Users'; } // Fallback: use the filename $path = wp_parse_url($url, PHP_URL_PATH); return $path !== null ? basename($path) : $url; } }