Skip to content

Commit

Permalink
Merge pull request #66 from nevigen/patch-1
Browse files Browse the repository at this point in the history
Add images
  • Loading branch information
zikkuratvk authored Nov 24, 2020
2 parents 2d02e2a + 28e5b05 commit 153e082
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
27 changes: 26 additions & 1 deletion com_jlsitemap/site/models/sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public function getXML($rows = array())
. ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.w3.org/1999/xhtml http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"'
. ' xmlns:xhtml="http://www.w3.org/1999/xhtml"'
. ' xhtml="http://www.w3.org/1999/xhtml"'
. ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"'
. '/>');

// Add urls
Expand Down Expand Up @@ -352,6 +353,16 @@ public function getXML($rows = array())
$alternate->addAttribute('href', $href);
}
}

// Images
if ($images = $row->get('images', false))
{
foreach ($images as $href)
{
$image = $url->addChild('image', '', 'http://www.google.com/schemas/sitemap-image/1.1');
$image->addChild('loc', $href, 'http://www.google.com/schemas/sitemap-image/1.1');
}
}
}
}
$xml = $sitemap->asXML();
Expand Down Expand Up @@ -721,6 +732,19 @@ protected function getUrls()
}
}

// Prepare images
$images = array();
if ($item->get('images', false))
{
foreach ($item->get('images') as $href)
{
if (!empty($href))
{
$images[] = $href;
}
}
}

// Exist url override
if (isset($all[$key]))
{
Expand Down Expand Up @@ -779,6 +803,7 @@ protected function getUrls()
$url->set('exclude', (!empty($exclude)) ? $exclude : false);
$url->set('lastmod', $lastmod);
$url->set('alternates', (!empty($alternates)) ? $alternates : false);
$url->set('images', (!empty($images)) ? $images : false);

// Add url to arrays
$all[$key] = $url;
Expand Down Expand Up @@ -1171,4 +1196,4 @@ public function setConfigurationParameter($path, $value, $separator = null)

$this->_configuration->set($path, $value, $separator);
}
}
}
29 changes: 26 additions & 3 deletions plg_jlsitemap_content/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Registry\Registry;
use Joomla\CMS\Uri\Uri;

class plgJLSitemapContent extends CMSPlugin
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function onGetUrls(&$urls, $config)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select(array('c.id', 'c.title', 'c.published', 'c.access', 'c.metadata', 'c.language', 'MAX(a.modified) as modified'))
->select(array('c.id', 'c.title', 'c.published', 'c.access', 'c.metadata', 'c.language', 'MAX(a.modified) as modified', 'c.params'))
->from($db->quoteName('#__categories', 'c'))
->join('LEFT', '#__content AS a ON a.catid = c.id')
->where($db->quoteName('c.extension') . ' = ' . $db->quote('com_content'))
Expand All @@ -77,6 +78,8 @@ public function onGetUrls(&$urls, $config)
$changefreq = $this->params->get('categories_changefreq', $config->get('changefreq', 'weekly'));
$priority = $this->params->get('categories_priority', $config->get('priority', '0.5'));

$categories_images_enable = $this->params->get('categories_images_enable', 1);

// Add categories to arrays
$categories = array();
$alternates = array();
Expand Down Expand Up @@ -124,6 +127,15 @@ public function onGetUrls(&$urls, $config)
$category->exclude = (!empty($exclude)) ? $exclude : false;
$category->alternates = ($multilanguage && !empty($row->association)) ? $row->association : false;

if ($categories_images_enable)
{
$category_params = json_decode($row->params);
if (is_object($category_params) && !empty($category_params->image))
{
$category->images = array(Uri::root() . $category_params->image);
}
}

// Add category to array
$categories[] = $category;

Expand Down Expand Up @@ -159,7 +171,7 @@ public function onGetUrls(&$urls, $config)
$query = $db->getQuery(true)
->select(array('a.id', 'a.title', 'a.alias', 'a.state', 'a.modified', 'a.publish_up', 'a.publish_down', 'a.access',
'a.metadata', 'a.language', 'c.id as category_id', 'c.published as category_published',
'c.access as category_access'))
'c.access as category_access', 'a.images'))
->from($db->quoteName('#__content', 'a'))
->join('LEFT', '#__categories AS c ON c.id = a.catid')
->group('a.id')
Expand All @@ -181,6 +193,8 @@ public function onGetUrls(&$urls, $config)
$changefreq = $this->params->get('articles_changefreq', $config->get('changefreq', 'weekly'));
$priority = $this->params->get('articles_priority', $config->get('priority', '0.5'));

$articles_images_enable = $this->params->get('articles_images_enable', 1);

// Add articles to urls arrays
$articles = array();
$alternates = array();
Expand Down Expand Up @@ -253,6 +267,15 @@ public function onGetUrls(&$urls, $config)
$article->exclude = (!empty($exclude)) ? $exclude : false;
$article->alternates = ($multilanguage && !empty($row->association)) ? $row->association : false;

if ($articles_images_enable)
{
$article_images = json_decode($row->images);
if (is_object($article_images) && !empty($article_images->image_fulltext))
{
$article->images = array(Uri::root() . $article_images->image_fulltext);
}
}

// Add article to array
$articles[] = $article;

Expand Down Expand Up @@ -283,4 +306,4 @@ public function onGetUrls(&$urls, $config)

return $urls;
}
}
}
16 changes: 15 additions & 1 deletion plg_jlsitemap_content/content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
<field name="articles_images_enable" type="radio"
label="JGLOBAL_FIELDSET_IMAGE_OPTIONS"
default="1"
class="btn-group btn-group-yesno">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
<field name="articles_changefreq" type="list" default="weekly"
label="PLG_JLSITEMAP_CONTENT_PARAMS_CHANGEFREQ"
showon="articles_enable:1">
Expand Down Expand Up @@ -63,6 +70,13 @@
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
<field name="categories_images_enable" type="radio"
label="JGLOBAL_FIELDSET_IMAGE_OPTIONS"
default="1"
class="btn-group btn-group-yesno">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
<field name="categories_changefreq" type="list" default="weekly"
label="PLG_JLSITEMAP_CONTENT_PARAMS_CHANGEFREQ"
showon="categories_enable:1">
Expand Down Expand Up @@ -92,4 +106,4 @@
</fieldset>
</fields>
</config>
</extension>
</extension>

0 comments on commit 153e082

Please sign in to comment.