| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Sitemap; |
| 6 |
|
| 7 |
use Yatra\Services\SettingsService; |
| 8 |
use Yatra\Sitemap\Integrations\AioseoIntegration; |
| 9 |
use Yatra\Sitemap\Integrations\CoreProvider; |
| 10 |
use Yatra\Sitemap\Integrations\RankMathIntegration; |
| 11 |
use Yatra\Sitemap\Integrations\YoastIntegration; |
| 12 |
|
| 13 |
/** |
| 14 |
* Boots Yatra's sitemap and wires it into whatever generates sitemaps on the |
| 15 |
* site. |
| 16 |
* |
| 17 |
* Yatra trips/classifications live in custom tables, so no sitemap generator |
| 18 |
* can discover them on its own. Strategy: |
| 19 |
* |
| 20 |
* 1. Serve ONE consolidated sitemap at /yatra-sitemap.xml and advertise it in |
| 21 |
* robots.txt — the universal baseline that works with any/no SEO plugin. |
| 22 |
* 2. Additionally register that one sitemap with whichever system owns the |
| 23 |
* site's sitemap so it also appears in their index: Yoast, Rank Math or |
| 24 |
* AIOSEO if present, otherwise WordPress core sitemaps. |
| 25 |
* |
| 26 |
* Exactly one third-party integration is wired (they are mutually exclusive in |
| 27 |
* practice — each disables the others' / core sitemaps), so the sitemap is |
| 28 |
* never listed twice within a single index. |
| 29 |
*/ |
| 30 |
class SitemapManager |
| 31 |
{ |
| 32 |
private static bool $booted = false; |
| 33 |
|
| 34 |
public static function init(): void |
| 35 |
{ |
| 36 |
if (self::$booted) { |
| 37 |
return; |
| 38 |
} |
| 39 |
self::$booted = true; |
| 40 |
|
| 41 |
// On by default; user-controllable via the SEO settings toggle and the |
| 42 |
// `yatra_sitemap_enabled` filter. `enable_sitemap` is registered in |
| 43 |
// SettingsService defaults (true), so isEnabled() reflects a saved |
| 44 |
// false correctly. |
| 45 |
$enabled = !class_exists(SettingsService::class) || SettingsService::isEnabled('enable_sitemap'); |
| 46 |
if (!apply_filters('yatra_sitemap_enabled', $enabled)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$service = new SitemapService(); |
| 51 |
$renderer = new SitemapRenderer(); |
| 52 |
$router = new SitemapRouter($service, $renderer); |
| 53 |
|
| 54 |
// Canonical, always-on Yatra sitemap + robots.txt advertisement. |
| 55 |
$router->register(); |
| 56 |
|
| 57 |
// Native integration with the active sitemap owner. |
| 58 |
self::registerActiveIntegration($service, $router, $renderer); |
| 59 |
|
| 60 |
// Keep the cached URL list fresh on content changes. |
| 61 |
foreach (['yatra_trip_created', 'yatra_trip_updated', 'yatra_trip_deleted'] as $hook) { |
| 62 |
add_action($hook, [SitemapService::class, 'flushCache']); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
private static function registerActiveIntegration( |
| 67 |
SitemapService $service, |
| 68 |
SitemapRouter $router, |
| 69 |
SitemapRenderer $renderer |
| 70 |
): void { |
| 71 |
if (defined('WPSEO_VERSION') || class_exists('WPSEO_Sitemaps')) { |
| 72 |
(new YoastIntegration($service, $router, $renderer))->register(); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if (defined('RANK_MATH_VERSION') || class_exists('RankMath\\Helper')) { |
| 77 |
(new RankMathIntegration($service, $router, $renderer))->register(); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if (defined('AIOSEO_VERSION') || function_exists('aioseo')) { |
| 82 |
(new AioseoIntegration($service, $router))->register(); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// No third-party SEO plugin: integrate with WordPress core sitemaps. |
| 87 |
$registerCore = static function () use ($service): void { |
| 88 |
if (!function_exists('wp_register_sitemap_provider')) { |
| 89 |
return; // Core sitemaps disabled or WP < 5.5. |
| 90 |
} |
| 91 |
if (function_exists('wp_sitemaps_get_server')) { |
| 92 |
wp_sitemaps_get_server(); // ensure base provider class is loaded |
| 93 |
} |
| 94 |
if (class_exists('WP_Sitemaps_Provider')) { |
| 95 |
wp_register_sitemap_provider('yatra', new CoreProvider($service)); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
// Providers can boot before OR after `init` depending on the load path, |
| 100 |
// so register immediately when `init` already fired, otherwise hook it. |
| 101 |
if (did_action('init')) { |
| 102 |
$registerCore(); |
| 103 |
} else { |
| 104 |
add_action('init', $registerCore, 20); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|