SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/SitemapGen.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Sprout\Helpers\Enc;
  17. use Sprout\Helpers\Sprout;
  18.  
  19.  
  20. abstract class SitemapGen
  21. {
  22.  
  23. /**
  24.   * Echo XML for a single URL in the sitemap
  25.   *
  26.   * @param string $loc The location.
  27.   * Can be an absolute or relative url
  28.   * @param date $mod The last modified date.
  29.   * Should be anything parseable by strtotime()
  30.   * @param string $freq The frequency of updates.
  31.   * Options include 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'
  32.   * @param float $prio Priority relative to other pages on the site.
  33.   * Range 0.0 (unimportant) to 1.0 (very important)
  34.   * @return void Outputs XML directly
  35.   */
  36. protected function url($loc, $mod = null, $freq = null, $prio = 0.5)
  37. {
  38. if (! preg_match('!https?://!', $loc)) {
  39. $loc = Sprout::absRoot() . ltrim($loc, '/');
  40. }
  41.  
  42. echo '<url>';
  43. echo '<loc>', Enc::xml($loc), '</loc>';
  44. if ($mod) {
  45. $mod = date('c', strtotime($mod));
  46. echo '<lastmod>', Enc::xml($mod), '</lastmod>';
  47. }
  48. if ($freq) {
  49. echo '<changefreq>', Enc::xml($freq), '</changefreq>';
  50. }
  51. if ($prio and $prio > 0.0) {
  52. echo '<priority>', number_format($prio, 2), '</priority>';
  53. }
  54. echo '</url>';
  55. echo PHP_EOL;
  56. }
  57.  
  58.  
  59. /**
  60.   * Generate sitemap entries by calling the {@see SitemapGen::url} method
  61.   *
  62.   * @return void Outputs XML directly
  63.   */
  64. public abstract function generate();
  65.  
  66. }