44
55use Illuminate \Support \Collection as IlluminateCollection ;
66use Illuminate \Support \LazyCollection ;
7- use Statamic \Contracts \Entries \QueryBuilder ;
7+ use Statamic \Contracts \Entries \Entry ;
8+ use Statamic \Contracts \Query \Builder ;
9+ use Statamic \Contracts \Taxonomies \Term ;
810use Statamic \Facades \Blink ;
911use Statamic \Facades \Collection ;
1012use Statamic \Facades \Entry as EntryFacade ;
13+ use Statamic \Facades \Site as SiteFacade ;
1114use Statamic \Facades \Taxonomy ;
1215use Statamic \SeoPro \Cascade ;
1316use Statamic \SeoPro \GetsSectionDefaults ;
1417use Statamic \SeoPro \SiteDefaults ;
18+ use Statamic \Sites \Site ;
1519use Statamic \Support \Traits \Hookable ;
1620
1721class Sitemap
@@ -128,6 +132,8 @@ protected function getPages($items)
128132 ->withCurrent ($ content )
129133 ->get ();
130134
135+ $ data ['hreflangs ' ] = $ this ->hrefLangs ($ content );
136+
131137 return (new Page )->with ($ data );
132138 })
133139 ->filter ();
@@ -148,7 +154,7 @@ protected function publishedEntriesQuery()
148154 return EntryFacade::query ()
149155 ->when (
150156 $ this ->sites ->isNotEmpty (),
151- fn (QueryBuilder $ query ) => $ query ->whereIn ('site ' , $ this ->sites ->map ->handle ()->all ())
157+ fn (Builder $ query ) => $ query ->whereIn ('site ' , $ this ->sites ->map ->handle ()->all ())
152158 )
153159 ->whereIn ('collection ' , $ collections )
154160 ->whereNotNull ('uri ' )
@@ -182,7 +188,10 @@ protected function publishedTerms()
182188 return Taxonomy::all ()
183189 ->flatMap (function ($ taxonomy ) {
184190 return $ taxonomy ->cascade ('seo ' ) !== false
185- ? $ taxonomy ->queryTerms ()->get ()
191+ ? $ taxonomy
192+ ->queryTerms ()
193+ ->when ($ this ->sites ->isNotEmpty (), fn (Builder $ query ) => $ query ->whereIn ('site ' , $ this ->sites ->map ->handle ()->all ()))
194+ ->get ()
186195 : collect ();
187196 })
188197 ->filter
@@ -202,7 +211,10 @@ protected function publishedCollectionTerms()
202211 })
203212 ->flatMap (function ($ taxonomy ) {
204213 return $ taxonomy ->cascade ('seo ' ) !== false
205- ? $ taxonomy ->queryTerms ()->get ()->map ->collection ($ taxonomy ->collection ())
214+ ? $ taxonomy
215+ ->queryTerms ()
216+ ->when ($ this ->sites ->isNotEmpty (), fn (Builder $ query ) => $ query ->whereIn ('site ' , $ this ->sites ->map ->handle ()->all ()))
217+ ->get ()->map ->collection ($ taxonomy ->collection ())
206218 : collect ();
207219 })
208220 ->filter
@@ -227,4 +239,54 @@ protected function getSiteDefaults()
227239 return SiteDefaults::load ()->all ();
228240 });
229241 }
242+
243+ protected function hrefLangs ($ content ): array
244+ {
245+ if (
246+ config ('statamic.seo-pro.alternate_locales ' ) === false
247+ || config ('statamic.seo-pro.alternate_locales.enabled ' ) === false
248+ ) {
249+ return [];
250+ }
251+
252+ return match (true ) {
253+ $ content instanceof Entry => $ this ->hrefLangsForEntry ($ content ),
254+ $ content instanceof Term => $ this ->hrefLangsForTerm ($ content ),
255+ default => [],
256+ };
257+ }
258+
259+ private function hrefLangsForEntry (Entry $ entry ): array
260+ {
261+ return SiteFacade::all ()
262+ ->values ()
263+ ->filter (fn (Site $ site ) => $ entry ->in ($ site ->handle ()))
264+ ->filter (fn (Site $ site ) => $ entry ->in ($ site ->handle ())->published ())
265+ ->reject (fn (Site $ site ) => collect (config ('statamic.seo-pro.alternate_locales.excluded_sites ' ))->contains ($ site ->handle ()))
266+ ->map (fn (Site $ site ) => [
267+ 'href ' => $ entry ->in ($ site ->handle ())->absoluteUrl (),
268+ 'hreflang ' => strtolower (str_replace ('_ ' , '- ' , $ site ->locale ())),
269+ ])
270+ ->push ([
271+ 'href ' => $ entry ->root ()->absoluteUrl (),
272+ 'hreflang ' => 'x-default ' ,
273+ ])
274+ ->all ();
275+ }
276+
277+ private function hrefLangsForTerm (Term $ term ): array
278+ {
279+ return SiteFacade::all ()
280+ ->values ()
281+ ->reject (fn (Site $ site ) => collect (config ('statamic.seo-pro.alternate_locales.excluded_sites ' ))->contains ($ site ->handle ()))
282+ ->map (fn (Site $ site ) => [
283+ 'href ' => $ term ->in ($ site ->handle ())->absoluteUrl (),
284+ 'hreflang ' => strtolower (str_replace ('_ ' , '- ' , $ site ->locale ())),
285+ ])
286+ ->push ([
287+ 'href ' => $ term ->inDefaultLocale ()->absoluteUrl (),
288+ 'hreflang ' => 'x-default ' ,
289+ ])
290+ ->all ();
291+ }
230292}
0 commit comments