Skip to content

Commit

Permalink
News Sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Venkatesh-KCET committed Jun 17, 2023
1 parent 20aaf5f commit 73b0567
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 53 deletions.
6 changes: 0 additions & 6 deletions sitemap-index.xml

This file was deleted.

41 changes: 0 additions & 41 deletions sitemap0.xml

This file was deleted.

46 changes: 40 additions & 6 deletions src/sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ public function __construct($loc, $lastmod, $changefreq, $priority, $images, $vi

}

if(!empty($news)) {

if(!isset($news["publication"]["name"])) {
throw new Exception("Name Of The Publisher Is Required");
}
if(!isset($news["publication"]["language"])) {
throw new Exception("Language Of The Publisher Is Required");
}

if(!isset($news["publication_date"])) {
throw new Exception("publication_date Is Required");
}
if(!isset($news["title"])) {
throw new Exception("title Is Required");
}
$this->news = $news;
}
}
}

Expand Down Expand Up @@ -229,6 +246,9 @@ function write_sitemap($folderPath = "/", $limit = 50000) {
if($this->videoSitemap) {
$urlset->setAttribute('xmlns:video', 'http://www.google.com/schemas/sitemap-video/1.1');
}
if($this->newsSitemap) {
$urlset->setAttribute('xmlns:news', 'http://www.google.com/schemas/sitemap-news/0.9');
}
$xml->appendChild($urlset);

for ($j = $i; $j < $i + 50000; $j++) {
Expand All @@ -239,12 +259,26 @@ function write_sitemap($folderPath = "/", $limit = 50000) {
$url = $xml->createElement('url');
foreach ($this->urls[$j] as $tagName => $tagValue) {
if(!empty($tagValue)) {
if($tagName == "images") {
foreach($tagValue as $imageUrl) {
// Create <loc> element for image URL
$Imageloc = $xml->createElement('loc', $imageUrl);
$url->appendChild($Imageloc);

if($tagName == "news") {
$newsData = $xml->createElement('news:news');

foreach ($tagValue as $key => $value) {
if ($key === 'publication' && is_array($value)) {
$publicationElement = $xml->createElement('news:publication');
foreach ($value as $publication_key => $publication_value) {
$subelement = $xml->createElement("news:{$publication_key}", $publication_value);
$publicationElement->appendChild($subelement);
}
$newsData->appendChild($publicationElement);
} else {
$element = $xml->createElement("news:{$key}", $value);
$newsData->appendChild($element);
}
}

$url->appendChild($newsData);
} else if($tagName == "images") {
foreach($tagValue as $imageUrl) {
// Create <image:image> element with <image:loc> for the image URL
$image = $xml->createElement('image:image');
$imageLoc = $xml->createElement('image:loc', $imageUrl);
Expand Down
8 changes: 8 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
"content_loc" => "http://streamserver.example.com/video123.mp4",
"player_loc" => "https://www.example.com/videoplayer.php?video=123"
]
],
'news' => [
'publication' => [
'name' => 'The Example Times',
'language' => 'en'
],
'publication_date' => '2008-12-23',
'title' => 'Companies A, B in Merger Talks'
]
];
$sitemap->add_url($array);
Expand Down

0 comments on commit 73b0567

Please sign in to comment.