36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Smilia\Core;
|
|
|
|
use SimpleXMLElement;
|
|
use WP_Sitemaps_Renderer;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Sitemap_Renderer extends WP_Sitemaps_Renderer {
|
|
|
|
private const XHTML = 'http://www.w3.org/1999/xhtml';
|
|
|
|
public function get_sitemap_xml( $url_list ) {
|
|
$urlset = new SimpleXMLElement(
|
|
'<?xml version="1.0" encoding="UTF-8" ?>' . $this->stylesheet . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="' . self::XHTML . '" />'
|
|
);
|
|
foreach ( $url_list as $item ) {
|
|
$url = $urlset->addChild( 'url' );
|
|
$url->addChild( 'loc', esc_url( $item['loc'] ) );
|
|
foreach ( array( 'lastmod', 'changefreq', 'priority' ) as $name ) {
|
|
if ( isset( $item[ $name ] ) ) {
|
|
$url->addChild( $name, esc_xml( (string) $item[ $name ] ) );
|
|
}
|
|
}
|
|
foreach ( (array) ( $item['alternates'] ?? array() ) as $language => $href ) {
|
|
$link = $url->addChild( 'xhtml:link', null, self::XHTML );
|
|
$link->addAttribute( 'rel', 'alternate' );
|
|
$link->addAttribute( 'hreflang', (string) $language );
|
|
$link->addAttribute( 'href', esc_url( $href ) );
|
|
}
|
|
}
|
|
return $urlset->asXML();
|
|
}
|
|
}
|