namespace Tootoot;
defined('ABSPATH') || exit;
use \Elementor\Core\Base\Document;
use TootootResolver;
use Tootoot\PageHandler;
use Utilities;
class SitemapGenerator
{
public static function GetEventSiteMap()
{
$events = self::GetEventsForSitemap();
if (!isset($events) || empty($events)) {
return;
}
$result = "";
$result .= '' . PHP_EOL;
$result .= '' . PHP_EOL;
foreach ($events as $event) {
$urls = PageHandler::getEventDetailUrls($event->_id);
$result .= '' . PHP_EOL;
$lastUpdated = new \DateTime($event->LastUpdated);
$lastUpdated = new \DateTime('0001-01-01T00:00:00+00:00') == $lastUpdated ? Utilities::GetDateTimeFromObjectId($event->_id) : $lastUpdated;
$result .= '' . $lastUpdated->format(\DateTime::ATOM) . '' . PHP_EOL;
$result .= '' . 0.8 . '' . PHP_EOL;
$result .= 'daily' . PHP_EOL;
foreach ($urls as $key => $url) {
if ($key == 'MainUrl') {
$result .= '' . $url . '' . PHP_EOL;
} else {
$result .= '' . PHP_EOL;
}
}
$result .= '' . PHP_EOL;
}
$result .= '' . PHP_EOL;
return $result;
}
public static function GetArticleSiteMap()
{
$articles = self::GetArticleForSitemap();
if (!isset($articles) || empty($articles)) {
return;
}
$result = "";
$result .= '' . PHP_EOL;
$result .= '' . PHP_EOL;
foreach ($articles as $article) {
$urls = PageHandler::getArticleDetailUrls($article->_id);
$result .= '' . PHP_EOL;
$lastUpdated = new \DateTime($article->LastUpdated);
$lastUpdated = new \DateTime('0001-01-01T00:00:00+00:00') == $lastUpdated ? new \DateTime($article->PublishedDate) : $lastUpdated;
$result .= '' . $lastUpdated->format(\DateTime::ATOM) . '' . PHP_EOL;
$result .= '' . 0.5 . '' . PHP_EOL;
$result .= 'monthly' . PHP_EOL;
foreach ($urls as $key => $url) {
if ($key == 'MainUrl') {
$result .= '' . $url . '' . PHP_EOL;
} else {
$result .= '' . PHP_EOL;
}
}
$result .= '' . PHP_EOL;
}
$result .= '' . PHP_EOL;
return $result;
}
private static function GetEventsForSitemap()
{
$events = [];
$allEventListWidgets = self::GetAllWidgetByType(\Constants::EVENT_LIST_NAME);
foreach ($allEventListWidgets as $eventListWidget) {
if (!isset($eventListWidget['settings'])) {
continue;
}
$args = Utilities::ParseEventGetParameters(null, $eventListWidget['settings']);
if (!isset($args)) {
continue;
}
$args['page'] = 0;
$args['perPage'] = 999;
$content = TootootResolver::GetEvents_($args);
if (!isset($content) || !isset($content->Events) || !isset($content->Events->Items)) {
return null;
}
foreach ($content->Events->Items as $event) {
if (!isset($events[$event->_id])) {
$events[$event->_id] = $event;
}
}
}
return $events;
}
private static function GetArticleForSitemap()
{
$articles = [];
$allArticleListWidgets = self::GetAllWidgetByType(\Constants::ARTICLE_LIST_NAME);
foreach ($allArticleListWidgets as $articleListWidget) {
if (!isset($articleListWidget['settings'])) {
continue;
}
$args = Utilities::ParseArticleGetParameters(null, $articleListWidget['settings']);
if (!isset($args)) {
continue;
}
$args['page'] = 0;
$args['perPage'] = 999;
$content = TootootResolver::GetArticlesByProfileIds_($args);
if (!isset($content) || !isset($content->Articles) || !isset($content->Articles->Items)) {
return null;
}
foreach ($content->Articles->Items as $article) {
if (!isset($articles[$article->_id])) {
$articles[$article->_id] = $article;
}
}
}
return $articles;
}
private static function GetAllWidgetByType($widgetType)
{
$allWidgetsType = [];
$allPages = get_pages();
foreach ($allPages as $page) {
if ($page->post_status != 'publish') {
continue;
}
$document = \Elementor\Plugin::$instance->documents->get($page->ID)->get_elements_data();
if (isset($document) && !empty($document)) {
$widgetsOnPage = self::GetWidgetFromDocument($document, $widgetType);
if (!empty($widgetsOnPage)) {
$allWidgetsType = array_merge($allWidgetsType, $widgetsOnPage);
}
}
}
return $allWidgetsType;
}
/**
* @param Document[] $pages
* @param string $widgetType
*/
private static function GetWidgetFromDocument(array $documents, $widgetType)
{
$result = [];
foreach ($documents as $document) {
if (isset($document['widgetType']) && $document['widgetType'] == $widgetType) {
$result = array_merge($result, array($document['id'] => $document));
}
if (isset($document['elements']) && is_array($document['elements'])) {
$recursiveResult = self::GetWidgetFromDocument($document['elements'], $widgetType);
if (isset($recursiveResult) && !empty($recursiveResult)) {
$result = array_merge($result, $recursiveResult);
}
}
}
return $result;
}
}
Orlovňa Skalica
Mindblown: a blog about philosophy.
Got any book recommendations?