Nessuna descrizione

class-wp-sitemaps-renderer.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Renderer class
  4. *
  5. * Responsible for rendering Sitemaps data to XML in accordance with sitemap protocol.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Class WP_Sitemaps_Renderer
  13. *
  14. * @since 5.5.0
  15. */
  16. class WP_Sitemaps_Renderer {
  17. /**
  18. * XSL stylesheet for styling a sitemap for web browsers.
  19. *
  20. * @since 5.5.0
  21. *
  22. * @var string
  23. */
  24. protected $stylesheet = '';
  25. /**
  26. * XSL stylesheet for styling a sitemap for web browsers.
  27. *
  28. * @since 5.5.0
  29. *
  30. * @var string
  31. */
  32. protected $stylesheet_index = '';
  33. /**
  34. * WP_Sitemaps_Renderer constructor.
  35. *
  36. * @since 5.5.0
  37. */
  38. public function __construct() {
  39. $stylesheet_url = $this->get_sitemap_stylesheet_url();
  40. if ( $stylesheet_url ) {
  41. $this->stylesheet = '<?xml-stylesheet type="text/xsl" href="' . esc_url( $stylesheet_url ) . '" ?>';
  42. }
  43. $stylesheet_index_url = $this->get_sitemap_index_stylesheet_url();
  44. if ( $stylesheet_index_url ) {
  45. $this->stylesheet_index = '<?xml-stylesheet type="text/xsl" href="' . esc_url( $stylesheet_index_url ) . '" ?>';
  46. }
  47. }
  48. /**
  49. * Gets the URL for the sitemap stylesheet.
  50. *
  51. * @since 5.5.0
  52. *
  53. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  54. *
  55. * @return string The sitemap stylesheet URL.
  56. */
  57. public function get_sitemap_stylesheet_url() {
  58. global $wp_rewrite;
  59. $sitemap_url = home_url( '/wp-sitemap.xsl' );
  60. if ( ! $wp_rewrite->using_permalinks() ) {
  61. $sitemap_url = home_url( '/?sitemap-stylesheet=sitemap' );
  62. }
  63. /**
  64. * Filters the URL for the sitemap stylesheet.
  65. *
  66. * If a falsey value is returned, no stylesheet will be used and
  67. * the "raw" XML of the sitemap will be displayed.
  68. *
  69. * @since 5.5.0
  70. *
  71. * @param string $sitemap_url Full URL for the sitemaps XSL file.
  72. */
  73. return apply_filters( 'wp_sitemaps_stylesheet_url', $sitemap_url );
  74. }
  75. /**
  76. * Gets the URL for the sitemap index stylesheet.
  77. *
  78. * @since 5.5.0
  79. *
  80. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  81. *
  82. * @return string The sitemap index stylesheet URL.
  83. */
  84. public function get_sitemap_index_stylesheet_url() {
  85. global $wp_rewrite;
  86. $sitemap_url = home_url( '/wp-sitemap-index.xsl' );
  87. if ( ! $wp_rewrite->using_permalinks() ) {
  88. $sitemap_url = home_url( '/?sitemap-stylesheet=index' );
  89. }
  90. /**
  91. * Filters the URL for the sitemap index stylesheet.
  92. *
  93. * If a falsey value is returned, no stylesheet will be used and
  94. * the "raw" XML of the sitemap index will be displayed.
  95. *
  96. * @since 5.5.0
  97. *
  98. * @param string $sitemap_url Full URL for the sitemaps index XSL file.
  99. */
  100. return apply_filters( 'wp_sitemaps_stylesheet_index_url', $sitemap_url );
  101. }
  102. /**
  103. * Renders a sitemap index.
  104. *
  105. * @since 5.5.0
  106. *
  107. * @param array $sitemaps Array of sitemap URLs.
  108. */
  109. public function render_index( $sitemaps ) {
  110. header( 'Content-type: application/xml; charset=UTF-8' );
  111. $this->check_for_simple_xml_availability();
  112. $index_xml = $this->get_sitemap_index_xml( $sitemaps );
  113. if ( ! empty( $index_xml ) ) {
  114. // All output is escaped within get_sitemap_index_xml().
  115. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  116. echo $index_xml;
  117. }
  118. }
  119. /**
  120. * Gets XML for a sitemap index.
  121. *
  122. * @since 5.5.0
  123. *
  124. * @param array $sitemaps Array of sitemap URLs.
  125. * @return string|false A well-formed XML string for a sitemap index. False on error.
  126. */
  127. public function get_sitemap_index_xml( $sitemaps ) {
  128. $sitemap_index = new SimpleXMLElement(
  129. sprintf(
  130. '%1$s%2$s%3$s',
  131. '<?xml version="1.0" encoding="UTF-8" ?>',
  132. $this->stylesheet_index,
  133. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'
  134. )
  135. );
  136. foreach ( $sitemaps as $entry ) {
  137. $sitemap = $sitemap_index->addChild( 'sitemap' );
  138. // Add each element as a child node to the <sitemap> entry.
  139. foreach ( $entry as $name => $value ) {
  140. if ( 'loc' === $name ) {
  141. $sitemap->addChild( $name, esc_url( $value ) );
  142. } elseif ( 'lastmod' === $name ) {
  143. $sitemap->addChild( $name, esc_xml( $value ) );
  144. } else {
  145. _doing_it_wrong(
  146. __METHOD__,
  147. sprintf(
  148. /* translators: %s: List of element names. */
  149. __( 'Fields other than %s are not currently supported for the sitemap index.' ),
  150. implode( ',', array( 'loc', 'lastmod' ) )
  151. ),
  152. '5.5.0'
  153. );
  154. }
  155. }
  156. }
  157. return $sitemap_index->asXML();
  158. }
  159. /**
  160. * Renders a sitemap.
  161. *
  162. * @since 5.5.0
  163. *
  164. * @param array $url_list Array of URLs for a sitemap.
  165. */
  166. public function render_sitemap( $url_list ) {
  167. header( 'Content-type: application/xml; charset=UTF-8' );
  168. $this->check_for_simple_xml_availability();
  169. $sitemap_xml = $this->get_sitemap_xml( $url_list );
  170. if ( ! empty( $sitemap_xml ) ) {
  171. // All output is escaped within get_sitemap_xml().
  172. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  173. echo $sitemap_xml;
  174. }
  175. }
  176. /**
  177. * Gets XML for a sitemap.
  178. *
  179. * @since 5.5.0
  180. *
  181. * @param array $url_list Array of URLs for a sitemap.
  182. * @return string|false A well-formed XML string for a sitemap index. False on error.
  183. */
  184. public function get_sitemap_xml( $url_list ) {
  185. $urlset = new SimpleXMLElement(
  186. sprintf(
  187. '%1$s%2$s%3$s',
  188. '<?xml version="1.0" encoding="UTF-8" ?>',
  189. $this->stylesheet,
  190. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'
  191. )
  192. );
  193. foreach ( $url_list as $url_item ) {
  194. $url = $urlset->addChild( 'url' );
  195. // Add each element as a child node to the <url> entry.
  196. foreach ( $url_item as $name => $value ) {
  197. if ( 'loc' === $name ) {
  198. $url->addChild( $name, esc_url( $value ) );
  199. } elseif ( in_array( $name, array( 'lastmod', 'changefreq', 'priority' ), true ) ) {
  200. $url->addChild( $name, esc_xml( $value ) );
  201. } else {
  202. _doing_it_wrong(
  203. __METHOD__,
  204. sprintf(
  205. /* translators: %s: List of element names. */
  206. __( 'Fields other than %s are not currently supported for sitemaps.' ),
  207. implode( ',', array( 'loc', 'lastmod', 'changefreq', 'priority' ) )
  208. ),
  209. '5.5.0'
  210. );
  211. }
  212. }
  213. }
  214. return $urlset->asXML();
  215. }
  216. /**
  217. * Checks for the availability of the SimpleXML extension and errors if missing.
  218. *
  219. * @since 5.5.0
  220. */
  221. private function check_for_simple_xml_availability() {
  222. if ( ! class_exists( 'SimpleXMLElement' ) ) {
  223. add_filter(
  224. 'wp_die_handler',
  225. static function () {
  226. return '_xml_wp_die_handler';
  227. }
  228. );
  229. wp_die(
  230. sprintf(
  231. /* translators: %s: SimpleXML */
  232. esc_xml( __( 'Could not generate XML sitemap due to missing %s extension' ) ),
  233. 'SimpleXML'
  234. ),
  235. esc_xml( __( 'WordPress &rsaquo; Error' ) ),
  236. array(
  237. 'response' => 501, // "Not implemented".
  238. )
  239. );
  240. }
  241. }
  242. }