| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace Yatra\Sitemap; |
| 6 | 6 | |
| 7 | +use Yatra\Services\SettingsService; | |
| 8 | + | |
| 7 | 9 | use Yatra\Database\Tables\ClassificationsTable; |
| 8 | 10 | use Yatra\Database\Tables\TripsTable; |
| 9 | 11 | |
| 10 | 12 | /** |
| @@ -37,9 +39,21 @@ | ||
| 37 | 39 | self::TYPE_ACTIVITY, |
| 38 | 40 | self::TYPE_CATEGORY, |
| 39 | 41 | ]; |
| 40 | 42 | |
| 43 | + /** | |
| 44 | + * Per-request memo. Held as a class property rather than a method static so | |
| 45 | + * flushCache() can actually clear it — a flush that only dropped the | |
| 46 | + * transient left this stale for the rest of the request. | |
| 47 | + * | |
| 48 | + * @var array<int, array{loc: string, lastmod: string}>|null | |
| 49 | + */ | |
| 50 | + private static $runtime = null; | |
| 51 | + | |
| 41 | 52 | private const CACHE_KEY = 'yatra_sitemap_entries_all'; |
| 53 | + | |
| 54 | + /** Setting holding the types the operator wants published. */ | |
| 55 | + private const TYPES_SETTING = 'sitemap_types'; | |
| 42 | 56 | private const CACHE_TTL = HOUR_IN_SECONDS; |
| 43 | 57 | |
| 44 | 58 | /** sitemaps.org caps a single sitemap file at 50,000 URLs. */ |
| 45 | 59 | private const MAX_URLS = 50000; |
| @@ -50,21 +64,20 @@ | ||
| 50 | 64 | * @return array<int, array{loc: string, lastmod: string}> |
| 51 | 65 | */ |
| 52 | 66 | public function getAllEntries(): array |
| 53 | 67 | { |
| 54 | - static $runtime = null; | |
| 55 | - if ($runtime !== null) { | |
| 56 | - return $runtime; | |
| 68 | + if (self::$runtime !== null) { | |
| 69 | + return self::$runtime; | |
| 57 | 70 | } |
| 58 | 71 | |
| 59 | 72 | $cached = get_transient(self::CACHE_KEY); |
| 60 | 73 | if (is_array($cached)) { |
| 61 | - $runtime = $cached; | |
| 74 | + self::$runtime = $cached; | |
| 62 | 75 | return $cached; |
| 63 | 76 | } |
| 64 | 77 | |
| 65 | 78 | $entries = []; |
| 66 | - foreach (self::TYPES as $type) { | |
| 79 | + foreach ($this->enabledTypes() as $type) { | |
| 67 | 80 | foreach ($this->entriesForType($type) as $entry) { |
| 68 | 81 | $entries[] = $entry; |
| 69 | 82 | } |
| 70 | 83 | } |
| @@ -86,9 +99,9 @@ | ||
| 86 | 99 | $entries = array_slice($entries, 0, $max); |
| 87 | 100 | } |
| 88 | 101 | |
| 89 | 102 | set_transient(self::CACHE_KEY, $entries, self::CACHE_TTL); |
| 90 | - $runtime = $entries; | |
| 103 | + self::$runtime = $entries; | |
| 91 | 104 | |
| 92 | 105 | return $entries; |
| 93 | 106 | } |
| 94 | 107 | |
| @@ -114,10 +127,58 @@ | ||
| 114 | 127 | |
| 115 | 128 | /** |
| 116 | 129 | * Drop the cached list. Call when a trip or classification changes. |
| 117 | 130 | */ |
| 131 | + /** | |
| 132 | + * Types the operator has chosen to publish, in the canonical order. | |
| 133 | + * | |
| 134 | + * An unset or empty setting means "everything", which is what every site | |
| 135 | + * had before this was configurable — so an upgrade changes nothing until | |
| 136 | + * the operator actually makes a choice. | |
| 137 | + * | |
| 138 | + * @return string[] | |
| 139 | + */ | |
| 140 | + public function enabledTypes(): array | |
| 141 | + { | |
| 142 | + $selected = null; | |
| 143 | + | |
| 144 | + if (class_exists(SettingsService::class)) { | |
| 145 | + $selected = SettingsService::get(self::TYPES_SETTING, null); | |
| 146 | + } | |
| 147 | + | |
| 148 | + if (!is_array($selected) || $selected === []) { | |
| 149 | + return self::TYPES; | |
| 150 | + } | |
| 151 | + | |
| 152 | + $selected = array_map('strval', $selected); | |
| 153 | + | |
| 154 | + // Intersect rather than trust the stored list, so an unknown or stale | |
| 155 | + // type can never introduce an entry type this class cannot build. | |
| 156 | + $types = array_values(array_intersect(self::TYPES, $selected)); | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Final say over which Yatra content types the sitemap publishes. | |
| 160 | + * | |
| 161 | + * @param string[] $types Selected types, canonical order. | |
| 162 | + * @param string[] $selected Raw stored selection. | |
| 163 | + */ | |
| 164 | + return (array) apply_filters('yatra_sitemap_types', $types, $selected); | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Is this content type published in the sitemap? | |
| 169 | + * | |
| 170 | + * Also answers "should this page be noindexed?" for the SEO layer, which | |
| 171 | + * is why it is public. | |
| 172 | + */ | |
| 173 | + public function isTypeEnabled(string $type): bool | |
| 174 | + { | |
| 175 | + return in_array($type, $this->enabledTypes(), true); | |
| 176 | + } | |
| 177 | + | |
| 118 | 178 | public static function flushCache(): void |
| 119 | 179 | { |
| 180 | + self::$runtime = null; | |
| 120 | 181 | delete_transient(self::CACHE_KEY); |
| 121 | 182 | } |
| 122 | 183 | |
| 123 | 184 | /** |