← All changes
|
modules/sitemaps/sitemap-buffer-news-xmlwriter.php
+75
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,75 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * XMLWriter implementation of the news sitemap buffer. | |
| 4 | + * | |
| 5 | + * @since 14.6 | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit( 0 ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * A buffer for constructing sitemap news xml files using XMLWriter. | |
| 15 | + * | |
| 16 | + * @since 14.6 | |
| 17 | + */ | |
| 18 | +class Jetpack_Sitemap_Buffer_News_XMLWriter extends Jetpack_Sitemap_Buffer_XMLWriter { | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Initialize the buffer with required headers (no root element here). | |
| 22 | + */ | |
| 23 | + protected function initialize_buffer() { | |
| 24 | + // Add generator comment | |
| 25 | + $this->writer->writeComment( "generator='jetpack-" . JETPACK__VERSION . "'" ); | |
| 26 | + $this->writer->writeComment( 'Jetpack_Sitemap_Buffer_News_XMLWriter' ); | |
| 27 | + | |
| 28 | + // Add stylesheet | |
| 29 | + $this->writer->writePi( | |
| 30 | + 'xml-stylesheet', | |
| 31 | + 'type="text/xsl" href="' . $this->finder->construct_sitemap_url( 'news-sitemap.xsl' ) . '"' | |
| 32 | + ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Start the root element and write its namespaces. | |
| 37 | + */ | |
| 38 | + protected function start_root() { | |
| 39 | + $this->writer->startElement( 'urlset' ); | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Filter the attribute value pairs used for namespace and namespace URI mappings. | |
| 43 | + * | |
| 44 | + * @module sitemaps | |
| 45 | + * | |
| 46 | + * @since 4.8.0 | |
| 47 | + * | |
| 48 | + * @param array $namespaces Associative array with namespaces and namespace URIs. | |
| 49 | + */ | |
| 50 | + $namespaces = apply_filters( | |
| 51 | + 'jetpack_sitemap_news_ns', | |
| 52 | + array( | |
| 53 | + 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9', | |
| 54 | + 'xmlns:news' => 'http://www.google.com/schemas/sitemap-news/0.9', | |
| 55 | + 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', | |
| 56 | + 'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd', | |
| 57 | + ) | |
| 58 | + ); | |
| 59 | + | |
| 60 | + foreach ( $namespaces as $name => $value ) { | |
| 61 | + $this->writer->writeAttribute( $name, $value ); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Append a URL entry with news information to the sitemap. | |
| 67 | + * | |
| 68 | + * @param array $array The URL item to append. | |
| 69 | + */ | |
| 70 | + protected function append_item( $array ) { | |
| 71 | + if ( ! empty( $array['url'] ) ) { | |
| 72 | + $this->array_to_xml( $array ); | |
| 73 | + } | |
| 74 | + } | |
| 75 | +} | |