From c9df2af5233cfe8b888adc3b279c759148c59a39 Mon Sep 17 00:00:00 2001 From: spyp Date: Sat, 23 Jan 2016 17:51:22 +0330 Subject: [PATCH] add image related to content in Sitemap --- Sitemap.php | 62 +++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/Sitemap.php b/Sitemap.php index a15e78b..072e86a 100644 --- a/Sitemap.php +++ b/Sitemap.php @@ -1,5 +1,4 @@ use_domain_url = $use_domain_url; $this->setDomain($domain); } - /** * Sets root path of the website, starting with http:// or https:// * @@ -48,7 +46,6 @@ public function setDomain($domain) { $this->domain = $domain; return $this; } - /** * Returns root path of the website * @@ -57,7 +54,6 @@ public function setDomain($domain) { private function getDomain() { return $this->domain; } - /** * Returns XMLWriter object instance * @@ -66,28 +62,25 @@ private function getDomain() { private function getWriter() { return $this->writer; } - /** * Assigns XMLWriter object instance * - * @param XMLWriter $writer + * @param XMLWriter $writer */ private function setWriter(XMLWriter $writer) { $this->writer = $writer; } - /** * Returns path of sitemaps - * + * * @return string */ private function getPath() { return $this->path; } - /** * Sets paths of sitemaps - * + * * @param string $path * @return Sitemap */ @@ -95,19 +88,17 @@ public function setPath($path) { $this->path = $path; return $this; } - /** * Returns filename of sitemap file - * + * * @return string */ private function getFilename() { return $this->filename; } - /** * Sets filename of sitemap file - * + * * @param string $filename * @return Sitemap */ @@ -115,7 +106,6 @@ public function setFilename($filename) { $this->filename = $filename; return $this; } - /** * Returns current item count * @@ -124,15 +114,13 @@ public function setFilename($filename) { private function getCurrentItem() { return $this->current_item; } - /** * Increases item counter - * + * */ private function incCurrentItem() { $this->current_item = $this->current_item + 1; } - /** * Returns current sitemap file count * @@ -141,18 +129,16 @@ private function incCurrentItem() { private function getCurrentSitemap() { return $this->current_sitemap; } - /** * Increases sitemap file count - * + * */ private function incCurrentSitemap() { $this->current_sitemap = $this->current_sitemap + 1; } - /** * Prepares sitemap XML document - * + * */ private function startSitemap() { $this->setWriter(new XMLWriter()); @@ -165,18 +151,18 @@ private function startSitemap() { $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); + $this->getWriter()->writeAttribute('xmlns:image', self::IMAGE); } - /** * 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,$image=false) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { if ($this->getWriter() instanceof XMLWriter) { $this->endSitemap(); @@ -186,8 +172,18 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = } $this->incCurrentItem(); $this->getWriter()->startElement('url'); - $this->getWriter()->writeElement('loc', $this->getDomain() . $loc); + $this->getWriter()->writeElement('loc', ($this->use_domain_url?$this->getDomain() . $loc:$loc)); $this->getWriter()->writeElement('priority', $priority); + if($image) + { + $this->getWriter()->startElement('image:image'); + $this->getWriter()->writeElement('image:loc',$image['loc']); + $this->getWriter()->writeElement('image:title',$image['title']); + $this->getWriter()->writeElement('image:caption',''); + $this->getWriter()->endElement(); + } + + if ($changefreq) $this->getWriter()->writeElement('changefreq', $changefreq); if ($lastmod) @@ -195,7 +191,6 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = $this->getWriter()->endElement(); return $this; } - /** * Prepares given date for sitemap * @@ -210,7 +205,6 @@ private function getLastModifiedDate($date) { return date('Y-m-d', $date); } } - /** * Finalizes tags of sitemap XML document. * @@ -222,7 +216,6 @@ private function endSitemap() { $this->getWriter()->endElement(); $this->getWriter()->endDocument(); } - /** * Writes Google sitemap index for generated sitemap files * @@ -246,5 +239,4 @@ public function createSitemapIndex($loc, $lastmod = 'Today') { $indexwriter->endElement(); $indexwriter->endDocument(); } - -} +} \ No newline at end of file