-
Notifications
You must be signed in to change notification settings - Fork 0
/
phocagallery.php
176 lines (146 loc) · 5.57 KB
/
phocagallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* @package JLSitemap - Phoca Gallery Plugin
* @version 1.0.0
* @author Sergey Tolkachyov - web-tolk.ru
* @copyright Copyright (c) 2022 Sergey Tolkachyov. All rights reserved.
* @license GNU General Public License v3.0
* @link https://web-tolk.ru/
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Registry\Registry;
use Joomla\CMS\Uri\Uri;
class plgJLSitemapPhocagallery extends CMSPlugin
{
/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
*
* @since 1.0.0
*/
protected $autoloadLanguage = true;
/**
* Method to get urls array
*
* @param array $urls Urls array
* @param Registry $config Component config
*
* @return array Urls array with attributes
*
* @since 1.0.0
*/
public function onGetUrls(&$urls, $config)
{
$categoryExcludeStates = array(
0 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY_UNPUBLISH'),
-2 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY_TRASH'),
2 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY_ARCHIVE'));
$articleExcludeStates = array(
0 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_ARTICLE_UNPUBLISH'),
-2 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_ARTICLE_TRASH'),
2 => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_ARTICLE_ARCHIVE'));
$multilanguage = $config->get('multilanguage');
$db = Factory::getDbo();
$query = 'SELECT *'
. ' FROM #__phocagallery_categories AS a'
. ' WHERE a.published = 1'
. ' ORDER BY a.ordering';
$db->setQuery($query);
$rows = $db->loadObjectList();
$nullDate = $db->getNullDate();
$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();
JLoader::register('PhocaGalleryLoader', JPATH_ADMINISTRATOR . '/components/com_phocagallery/libraries/loader.php');
JLoader::register('PhocaGalleryAccess', JPATH_ADMINISTRATOR . '/components/com_phocagallery/libraries/phocagallery/access/access.php');
JLoader::register('PhocaGalleryImageFront', JPATH_ADMINISTRATOR . '/components/com_phocagallery/libraries/phocagallery/image/imagefront.php');
foreach ($rows as $row)
{
// Prepare loc attribute
$loc = 'index.php?option=com_phocagallery&view=category&id=' . $row->id;
if (!empty($row->language) && $row->language !== '*' && $multilanguage)
{
$loc .= '&lang=' . $row->language;
}
// Prepare exclude attribute
$metadata = new Registry($row->metadata);
$exclude = array();
if (preg_match('/noindex/', $metadata->get('robots', $config->get('siteRobots'))))
{
$exclude[] = array('type' => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY'),
'msg' => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY_ROBOTS'));
}
if (isset($categoryExcludeStates[$row->published]))
{
$exclude[] = array('type' => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY'),
'msg' => $categoryExcludeStates[$row->published]);
}
if (!in_array($row->access, $config->get('guestAccess', array())))
{
$exclude[] = array('type' => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY'),
'msg' => Text::_('PLG_JLSITEMAP_CONTENT_EXCLUDE_CATEGORY_ACCESS'));
}
// Prepare lastmod attribute
$lastmod = (!empty($row->date) && $row->date != $nullDate) ? $row->date : false;
// Prepare category object
$category = new stdClass();
$category->type = Text::_('PLG_JLSITEMAP_CONTENT_TYPES_CATEGORY');
$category->title = $row->title;
$category->loc = $loc;
$category->changefreq = $changefreq;
$category->priority = $priority;
$category->lastmod = $lastmod;
$category->exclude = (!empty($exclude)) ? $exclude : false;
$category->alternates = ($multilanguage && !empty($row->association)) ? $row->association : false;
if ($categories_images_enable)
{
if (!empty($row->id))
{
$category_images = PhocaGalleryImageFront::getCategoryImages($row->id); //массив объектов
/**
* @link https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps
* no more than 1000 images
*/
$images_xml = [];
if (0 < count($category_images) && count($category_images) <= 1000)
{// no more than 1000 images
foreach ($category_images as $image)
{
$images_xml[] = JUri::root() . 'images/phocagallery/' . $image->filename;
}
}
$category->images = $images_xml;
}
}
// Add category to array
$categories[] = $category;
// Add category to alternates array
if ($multilanguage && !empty($row->association) && empty($exclude))
{
if (!isset($alternates[$row->association]))
{
$alternates[$row->association] = array();
}
$alternates[$row->association][$row->language] = $loc;
};
}
// Add alternates to categories
if (!empty($alternates))
{
foreach ($categories as &$category)
{
$category->alternates = ($category->alternates) ? $alternates[$category->alternates] : false;
}
}
// Add categories to urls
$urls = array_merge($urls, $categories);
return $urls;
}
}