Skip to content

Commit

Permalink
Added ability to insert image definitions into sitemap.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncave committed May 13, 2016
1 parent 36cc358 commit d6cb097
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions Sitemap.php
Expand Up @@ -6,8 +6,8 @@
* This class used for generating Google Sitemap files
*
* @package Sitemap
* @author Osman Üngür <osmanungur@gmail.com>
* @copyright 2009-2015 Osman Üngür
* @author Osman Ungur <osmanungur@gmail.com>
* @copyright 2009-2016 Osman Ungur
* @license http://opensource.org/licenses/MIT MIT License
* @link http://github.com/o/sitemap-php
*/
Expand Down Expand Up @@ -70,15 +70,15 @@ private function getWriter() {
/**
* 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() {
Expand All @@ -87,7 +87,7 @@ private function getPath() {

/**
* Sets paths of sitemaps
*
*
* @param string $path
* @return Sitemap
*/
Expand All @@ -98,7 +98,7 @@ public function setPath($path) {

/**
* Returns filename of sitemap file
*
*
* @return string
*/
private function getFilename() {
Expand All @@ -107,7 +107,7 @@ private function getFilename() {

/**
* Sets filename of sitemap file
*
*
* @param string $filename
* @return Sitemap
*/
Expand All @@ -127,7 +127,7 @@ private function getCurrentItem() {

/**
* Increases item counter
*
*
*/
private function incCurrentItem() {
$this->current_item = $this->current_item + 1;
Expand All @@ -144,15 +144,15 @@ private function getCurrentSitemap() {

/**
* 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());
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down

1 comment on commit d6cb097

@mablae
Copy link

@mablae mablae commented on d6cb097 May 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @johncave,

I need exactly the same functionality :)
could you please send a pull request upstream?

We could better work together instead of implementing it twice, what do you think?

Please sign in to comment.