From d6cb0978e34ddc44ec2b8fda91f60cc05272331c Mon Sep 17 00:00:00 2001 From: John Cave Date: Fri, 13 May 2016 20:58:20 +1200 Subject: [PATCH] Added ability to insert image definitions into sitemap. --- Sitemap.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Sitemap.php b/Sitemap.php index a15e78b..dd44baf 100644 --- a/Sitemap.php +++ b/Sitemap.php @@ -6,8 +6,8 @@ * This class used for generating Google Sitemap files * * @package Sitemap - * @author Osman Üngür - * @copyright 2009-2015 Osman Üngür + * @author Osman Ungur + * @copyright 2009-2016 Osman Ungur * @license http://opensource.org/licenses/MIT MIT License * @link http://github.com/o/sitemap-php */ @@ -70,7 +70,7 @@ private function getWriter() { /** * Assigns XMLWriter object instance * - * @param XMLWriter $writer + * @param XMLWriter $writer */ private function setWriter(XMLWriter $writer) { $this->writer = $writer; @@ -78,7 +78,7 @@ private function setWriter(XMLWriter $writer) { /** * Returns path of sitemaps - * + * * @return string */ private function getPath() { @@ -87,7 +87,7 @@ private function getPath() { /** * Sets paths of sitemaps - * + * * @param string $path * @return Sitemap */ @@ -98,7 +98,7 @@ public function setPath($path) { /** * Returns filename of sitemap file - * + * * @return string */ private function getFilename() { @@ -107,7 +107,7 @@ private function getFilename() { /** * Sets filename of sitemap file - * + * * @param string $filename * @return Sitemap */ @@ -127,7 +127,7 @@ private function getCurrentItem() { /** * Increases item counter - * + * */ private function incCurrentItem() { $this->current_item = $this->current_item + 1; @@ -144,7 +144,7 @@ private function getCurrentSitemap() { /** * Increases sitemap file count - * + * */ private function incCurrentSitemap() { $this->current_sitemap = $this->current_sitemap + 1; @@ -152,7 +152,7 @@ private function incCurrentSitemap() { /** * Prepares sitemap XML document - * + * */ private function startSitemap() { $this->setWriter(new XMLWriter()); @@ -165,18 +165,19 @@ private function startSitemap() { $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); + $this->getWriter()->writeAttribute('xmlns:image', 'http://www.google.com/schemas/sitemap-image/1.1'); } /** * 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 $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. * @param string $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never. * @param string|int $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description. * @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, $images = NULL) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { if ($this->getWriter() instanceof XMLWriter) { $this->endSitemap(); @@ -192,6 +193,21 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = $this->getWriter()->writeElement('changefreq', $changefreq); if ($lastmod) $this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); + if($images){ + foreach($images as $image){ + $this -> getWriter()->startElement('image:image'); + $this -> getWriter()->writeElement('image:loc', $image['loc']); + if(isset($image['caption'])) + $this -> getWriter()->writeElement('image:caption', $image['caption']); + if(isset($image['geo_location'])) + $this -> getWriter()->writeElement('image:geo_location', $image['geo_location']); + if(isset($image['title'])) + $this -> getWriter()->writeElement('image:title', $image['title']); + if(isset($image['license'])) + $this -> getWriter()->writeElement('image:license', $image['license']); + $this -> getWriter()->endElement(); + } + } $this->getWriter()->endElement(); return $this; }