PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / schema-aggregator / application / schema_map / schema-map-xml-renderer.php

schema-map-xml-renderer.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/schema-aggregator/application/schema_map/schema-map-xml-renderer.php

85 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3 namespace Yoast\WP\SEO\Schema_Aggregator\Application\Schema_Map;
4
5 use DOMDocument;
6 use RuntimeException;
7 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map\Schema_Map_Config;
8
9 /**
10 * Converts the schema map to an xml representation.
11 */
12 class Schema_Map_Xml_Renderer {
13
14 /**
15 * The schema map configuration.
16 *
17 * @var Schema_Map_Config
18 */
19 private $config;
20
21 /**
22 * Constructor.
23 *
24 * @param Schema_Map_Config $config The schema map configuration.
25 */
26 public function __construct( Schema_Map_Config $config ) {
27 $this->config = $config;
28 }
29
30 /**
31 * Converts the schema map to an XML string.
32 *
33 * @param array<array<string>> $schema_map The schema map data.
34 *
35 * @return string The XML representation of the schema map.
36 *
37 * @throws RuntimeException If the input structure is invalid or XML generation fails.
38 */
39 public function render( array $schema_map ): string {
40 $dom = new DOMDocument( '1.0', 'UTF-8' );
41
42 $url_set = $dom->createElement( 'urlset' );
43 $url_set->setAttribute( 'xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
44 $dom->appendChild( $url_set );
45
46 $change_freq = $this->config->get_changefreq();
47 $priority = $this->config->get_priority();
48
49 foreach ( $schema_map as $entry ) {
50 if ( ! isset( $entry['url'] ) || ! isset( $entry['lastmod'] ) ) {
51 continue;
52 }
53
54 $url = $dom->createElement( 'url' );
55
56 $url->setAttribute( 'contentType', 'structuredData/schema.org' );
57
58 $loc = $dom->createElement( 'loc' );
59 $loc->appendChild( $dom->createTextNode( $entry['url'] ) );
60 $url->appendChild( $loc );
61
62 $last_mod = $dom->createElement( 'lastmod' );
63 $last_mod->appendChild( $dom->createTextNode( $entry['lastmod'] ) );
64 $url->appendChild( $last_mod );
65
66 $cf = $dom->createElement( 'changefreq' );
67 $cf->appendChild( $dom->createTextNode( $change_freq ) );
68 $url->appendChild( $cf );
69
70 $prio = $dom->createElement( 'priority' );
71 $prio->appendChild( $dom->createTextNode( $priority ) );
72 $url->appendChild( $prio );
73
74 $url_set->appendChild( $url );
75 }
76
77 $xml = $dom->saveXML();
78 if ( $xml === false ) {
79 throw new RuntimeException( 'Failed to generate XML from DOMDocument' );
80 }
81
82 return $xml;
83 }
84 }
85