| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Sitemap\Integrations; |
| 6 |
|
| 7 |
use Yatra\Sitemap\SitemapService; |
| 8 |
use Yatra\Sitemap\SitemapRouter; |
| 9 |
|
| 10 |
/** |
| 11 |
* Links Yatra's consolidated sitemap into All in One SEO's sitemap index via |
| 12 |
* the `aioseo_sitemap_indexes` filter — a simple URL pointer (loc/lastmod/ |
| 13 |
* count), which is lower-risk than AIOSEO's page-object `additional_pages` |
| 14 |
* filter. AIOSEO then lists /yatra-sitemap.xml as a sub-sitemap in its index. |
| 15 |
* |
| 16 |
* Note: AIOSEO must have XML sitemaps enabled. Even if this index injection is |
| 17 |
* unavailable on a given AIOSEO version, the robots.txt advertisement still |
| 18 |
* exposes /yatra-sitemap.xml, so discovery is never lost. |
| 19 |
*/ |
| 20 |
class AioseoIntegration |
| 21 |
{ |
| 22 |
private SitemapService $service; |
| 23 |
private SitemapRouter $router; |
| 24 |
|
| 25 |
public function __construct(SitemapService $service, SitemapRouter $router) |
| 26 |
{ |
| 27 |
$this->service = $service; |
| 28 |
$this->router = $router; |
| 29 |
} |
| 30 |
|
| 31 |
public function register(): void |
| 32 |
{ |
| 33 |
add_filter('aioseo_sitemap_indexes', [$this, 'addIndex']); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param mixed $indexes |
| 38 |
* @return array<int, array<string, mixed>> |
| 39 |
*/ |
| 40 |
public function addIndex($indexes): array |
| 41 |
{ |
| 42 |
$indexes = is_array($indexes) ? $indexes : []; |
| 43 |
|
| 44 |
$indexes[] = [ |
| 45 |
'loc' => $this->router->sitemapUrl(), |
| 46 |
'lastmod' => $this->service->getLatestLastmod() ?: gmdate('Y-m-d\TH:i:s+00:00'), |
| 47 |
'count' => $this->service->getCount(), |
| 48 |
]; |
| 49 |
|
| 50 |
return $indexes; |
| 51 |
} |
| 52 |
} |
| 53 |
|