diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index 02b745a..e860b6c 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -26,6 +26,7 @@ class Sitemap { private $filename = 'sitemap'; private $current_item = 0; private $current_sitemap = 0; + private $urlset_attributes = array(); const EXT = '.xml'; const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; @@ -73,7 +74,7 @@ private function getWriter() { /** * Assigns XMLWriter object instance * - * @param \XMLWriter $writer + * @param \XMLWriter $writer */ private function setWriter(\XMLWriter $writer) { $this->writer = $writer; @@ -81,7 +82,7 @@ private function setWriter(\XMLWriter $writer) { /** * Returns path of sitemaps - * + * * @return string */ private function getPath() { @@ -90,7 +91,7 @@ private function getPath() { /** * Sets paths of sitemaps - * + * * @param string $path * @return Sitemap */ @@ -101,7 +102,7 @@ public function setPath($path) { /** * Returns filename of sitemap file - * + * * @return string */ private function getFilename() { @@ -110,7 +111,7 @@ private function getFilename() { /** * Sets filename of sitemap file - * + * * @param string $filename * @return Sitemap */ @@ -119,6 +120,18 @@ public function setFilename($filename) { return $this; } + /** + * Add sitemap urlset attribute + * + * @param string $name + * @param string $value + * @return Sitemap + */ + public function addUrlsetAttribute($name, $value) { + $this->urlset_attributes[] = compact('name', 'value'); + return $this; + } + /** * Returns current item count * @@ -130,7 +143,7 @@ private function getCurrentItem() { /** * Increases item counter - * + * */ private function incCurrentItem() { $this->current_item = $this->current_item + 1; @@ -147,7 +160,7 @@ private function getCurrentSitemap() { /** * Increases sitemap file count - * + * */ private function incCurrentSitemap() { $this->current_sitemap = $this->current_sitemap + 1; @@ -155,7 +168,7 @@ private function incCurrentSitemap() { /** * Prepares sitemap XML document - * + * */ private function startSitemap() { $this->setWriter(new \XMLWriter()); @@ -168,18 +181,22 @@ private function startSitemap() { $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); + foreach($this->urlset_attributes as $attr) { + $this->getWriter()->writeAttribute($attr['name'], $attr['value']); + } } /** * Adds an item to sitemap * - * @param string $loc URL of the page. This value must be less than 2,048 characters. + * @param string $loc URL of the page. This value must be less than 2,048 characters. * @param string|null $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. * @param string|null $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never. * @param string|int|null $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description. + * @param callable|null $ext Use if you want to add other attribute or child element. callable's 1st argument MUST BE XMLWriter ( * $this->getWriter() ). * @return Sitemap */ - public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) { + public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL, $ext = NULL) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { if ($this->getWriter() instanceof \XMLWriter) { $this->endSitemap(); @@ -196,6 +213,8 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = $this->getWriter()->writeElement('changefreq', $changefreq); if ($lastmod) $this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); + if ($ext && is_callable($ext)) + $ext($this->getWriter()); $this->getWriter()->endElement(); return $this; }