| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Sitemap\Integrations; |
| 6 |
|
| 7 |
use Yatra\Sitemap\SitemapService; |
| 8 |
|
| 9 |
/** |
| 10 |
* Feeds Yatra URLs into WordPress core sitemaps (wp-sitemap.xml), used when no |
| 11 |
* third-party SEO plugin has taken over sitemap generation. Flat provider (no |
| 12 |
* subtypes): all Yatra URLs appear under a single wp-sitemap-yatra-N.xml, |
| 13 |
* mirroring the consolidated /yatra-sitemap.xml. |
| 14 |
* |
| 15 |
* Only referenced after WP_Sitemaps_Provider is confirmed loaded (see |
| 16 |
* SitemapManager), so extending the core class is safe. |
| 17 |
*/ |
| 18 |
class CoreProvider extends \WP_Sitemaps_Provider |
| 19 |
{ |
| 20 |
private SitemapService $service; |
| 21 |
|
| 22 |
public function __construct(SitemapService $service) |
| 23 |
{ |
| 24 |
$this->service = $service; |
| 25 |
$this->name = 'yatra'; |
| 26 |
$this->object_type = 'yatra'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param int $page_number |
| 31 |
* @param string $object_subtype |
| 32 |
* @return array<int, array{loc: string, lastmod: string}> |
| 33 |
*/ |
| 34 |
public function get_url_list($page_number, $object_subtype = ''): array |
| 35 |
{ |
| 36 |
$entries = $this->service->getAllEntries(); |
| 37 |
|
| 38 |
$perPage = $this->maxUrls(); |
| 39 |
$offset = ((int) $page_number - 1) * $perPage; |
| 40 |
|
| 41 |
$list = []; |
| 42 |
foreach (array_slice($entries, $offset, $perPage) as $entry) { |
| 43 |
// Core's renderer reads 'loc'; lastmod is surfaced only if a site |
| 44 |
// opts in via the wp_sitemaps_index_entry filter. |
| 45 |
$list[] = ['loc' => $entry['loc'], 'lastmod' => $entry['lastmod']]; |
| 46 |
} |
| 47 |
|
| 48 |
return $list; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param string $object_subtype |
| 53 |
*/ |
| 54 |
public function get_max_num_pages($object_subtype = ''): int |
| 55 |
{ |
| 56 |
$count = $this->service->getCount(); |
| 57 |
if ($count === 0) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
return (int) ceil($count / $this->maxUrls()); |
| 62 |
} |
| 63 |
|
| 64 |
private function maxUrls(): int |
| 65 |
{ |
| 66 |
$max = function_exists('wp_sitemaps_get_max_urls') |
| 67 |
? (int) wp_sitemaps_get_max_urls($this->object_type) |
| 68 |
: 2000; |
| 69 |
|
| 70 |
return $max > 0 ? $max : 2000; |
| 71 |
} |
| 72 |
} |
| 73 |
|