Skip to content

Commit

Permalink
Closes #4760 - Added image support to generated Google sitemaps.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Apr 21, 2022
1 parent c2fc8fc commit fe8d27c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 11 additions & 1 deletion e107_plugins/news/e_gsitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,25 @@ public function allPosts()
{
$data = $this->getNewsPosts();

/** @var news_shortcodes $sc */
$sc = e107::getScBatch('news');

e107::getParser()->thumbWidth(1000);

$ret = [];

foreach($data as $row)
{
$sc->setScVar('news_item', $row);

$imgUrl = $sc->sc_news_image(['item'=>1, 'type'=>'src']);

$ret[] = [
'url' => $this->url('news', $row),
'lastmod' => !empty($row['news_modified']) ? $row['news_modified'] : (int) $row['news_datestamp'],
'freq' => 'hourly',
'priority' => 0.5
'priority' => 0.5,
'image' => (strpos($imgUrl, 'http') === 0) ? $imgUrl : SITEURLBASE.$sc->sc_news_image(['item'=>1, 'type'=>'src']),
];
}

Expand Down
27 changes: 23 additions & 4 deletions gsitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function renderXML($items=array())
{
header('Content-type: application/xml', TRUE);
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:image='https://www.google.com/schemas/sitemap-image/1.1'>";

if(empty($items))
{
Expand Down Expand Up @@ -105,10 +105,29 @@ function renderXMLItems($data, $prefix = '')
$loc = (strpos($url, 'http') === 0) ? $url : SITEURL.$tp->replaceConstants($url,true);
$xml .= "
<url>
<loc>".$loc."</loc>
<loc>".$loc."</loc>";

if(!empty($sm[$prefix.'image']))
{
$imgUrl = $sm[$prefix.'image'];

if($imgUrl[0] === '/')
{
$imgUrl = ltrim($imgUrl, '/');
}

$imgUrl = (strpos($imgUrl, 'http') === 0) ? $imgUrl : SITEURL.$tp->replaceConstants($imgUrl,true);

$xml .= "
<image:image>
<image:loc>".$imgUrl."</image:loc>
</image:image>";
}

$xml .= "
<lastmod>".date('c', (int) $sm[$prefix.'lastmod'])."</lastmod>
<changefreq>".$sm[$prefix.'freq']."</changefreq>
<priority>".$sm[$prefix.'priority']."</priority>
<changefreq>".$sm[$prefix.'freq']."</changefreq>
<priority>".$sm[$prefix.'priority']."</priority>
</url>";
}

Expand Down

0 comments on commit fe8d27c

Please sign in to comment.