Files
MokoSuiteOpenGraph/source/packages/plg_system_mokoog/src/Helper/SitemapBuilder.php
T
Jonathan Miller 49d644566a
Universal: Auto Version Bump / Version Bump (push) Successful in 14s
Universal: Pre-Release / Build Pre-Release (${{ inputs.stability || github.ref_name }}) (push) Successful in 11s
feat: add custom schema, AI generation, XML sitemap, platform images
- Custom JSON-LD schema builder: per-article textarea for arbitrary
  structured data with JSON validation. Closes #70
- AI-powered meta generation: Generate with AI buttons for OG title
  and description, supports Claude and OpenAI APIs. Closes #71
- XML sitemap: auto-generates sitemap.xml on article save, respects
  noindex directives. Closes #72
- Per-platform image resizing: Twitter 1200x600, Pinterest 1000x1500,
  WhatsApp 400x400 alongside default Facebook 1200x630. Closes #74
- DB migration 01.05.00: adds custom_schema column
2026-06-23 12:54:01 -05:00

108 lines
3.4 KiB
PHP

<?php
/**
* @package MokoSuiteOpenGraph
* @subpackage plg_system_mokoog
* @author Moko Consulting <hello@mokoconsulting.tech>
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
namespace Joomla\Plugin\System\MokoOG\Helper;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
/**
* XML Sitemap builder.
*
* Generates a sitemap.xml containing all published articles, excluding
* those marked with noindex robots directives in the mokoog_tags table.
*/
class SitemapBuilder
{
/**
* Generate sitemap XML content.
*
* @param string $changefreq Default change frequency for entries
*
* @return string Complete sitemap XML
*/
public static function generate(string $changefreq = 'weekly'): string
{
$db = Factory::getDbo();
// Get all published articles
$query = $db->getQuery(true)
->select($db->quoteName(['a.id', 'a.alias', 'a.catid', 'a.modified', 'a.language']))
->from($db->quoteName('#__content', 'a'))
->where($db->quoteName('a.state') . ' = 1');
$db->setQuery($query);
$articles = $db->loadObjectList();
// Get noindex articles from mokoog_tags
$noindexQuery = $db->getQuery(true)
->select($db->quoteName('content_id'))
->from($db->quoteName('#__mokoog_tags'))
->where($db->quoteName('content_type') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('robots') . ' LIKE ' . $db->quote('%noindex%'));
$db->setQuery($noindexQuery);
$noindexIds = $db->loadColumn();
$root = rtrim(Uri::root(), '/');
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
// Homepage
$xml .= ' <url>' . "\n";
$xml .= ' <loc>' . $root . '/</loc>' . "\n";
$xml .= ' <changefreq>daily</changefreq>' . "\n";
$xml .= ' <priority>1.0</priority>' . "\n";
$xml .= ' </url>' . "\n";
foreach ($articles as $article) {
// Skip noindexed
if (in_array((int) $article->id, $noindexIds)) {
continue;
}
$url = $root . '/index.php?option=com_content&view=article&id=' . $article->id;
$lastmod = $article->modified && $article->modified !== '0000-00-00 00:00:00'
? date('Y-m-d', strtotime($article->modified)) : '';
$xml .= ' <url>' . "\n";
$xml .= ' <loc>' . htmlspecialchars($url, ENT_XML1) . '</loc>' . "\n";
if ($lastmod) {
$xml .= ' <lastmod>' . $lastmod . '</lastmod>' . "\n";
}
$xml .= ' <changefreq>' . $changefreq . '</changefreq>' . "\n";
$xml .= ' <priority>0.8</priority>' . "\n";
$xml .= ' </url>' . "\n";
}
$xml .= '</urlset>';
return $xml;
}
/**
* Write sitemap XML to the site root.
*
* @param string $xml The sitemap XML content
*
* @return bool True on success
*/
public static function writeToFile(string $xml): bool
{
$path = JPATH_ROOT . '/sitemap.xml';
return (bool) file_put_contents($path, $xml);
}
}