feat(adapters): third-party extension support (#5) #21

Merged
jmiller merged 1 commits from feature/5-third-party-adapters into dev 2026-05-23 23:40:52 +00:00
5 changed files with 292 additions and 0 deletions
+1
View File
@@ -42,6 +42,7 @@
<filename>provider.php</filename>
</files>
<files folder="src">
<folder>ContentType</folder>
<folder>Controller</folder>
<folder>Extension</folder>
<folder>Model</folder>
@@ -0,0 +1,60 @@
<?php
/**
* @package MokoOpenGraph
* @subpackage com_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\Component\MokoOG\Administrator\ContentType;
defined('_JEXEC') or die;
interface ContentTypeInterface
{
/**
* Check if this adapter can handle the given component/view.
*
* @param string $option Component option (e.g. com_virtuemart)
* @param string $view View name (e.g. productdetails)
*
* @return bool
*/
public function canHandle(string $option, string $view): bool;
/**
* Get the content type identifier for database storage.
*
* @return string
*/
public function getContentType(): string;
/**
* Get the title for the content item.
*
* @param int $id Content item ID
*
* @return string
*/
public function getTitle(int $id): string;
/**
* Get a description for the content item.
*
* @param int $id Content item ID
*
* @return string
*/
public function getDescription(int $id): string;
/**
* Get the primary image for the content item.
*
* @param int $id Content item ID
*
* @return string Image path relative to JPATH_ROOT, or empty string
*/
public function getImage(int $id): string;
}
@@ -0,0 +1,76 @@
<?php
/**
* @package MokoOpenGraph
* @subpackage com_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\Component\MokoOG\Administrator\ContentType;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
class HikaShopAdapter implements ContentTypeInterface
{
public function canHandle(string $option, string $view): bool
{
return $option === 'com_hikashop' && $view === 'product';
}
public function getContentType(): string
{
return 'com_hikashop';
}
public function getTitle(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('product_name'))
->from($db->quoteName('#__hikashop_product'))
->where($db->quoteName('product_id') . ' = ' . $id);
$db->setQuery($query);
return $db->loadResult() ?: '';
}
public function getDescription(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('product_description'))
->from($db->quoteName('#__hikashop_product'))
->where($db->quoteName('product_id') . ' = ' . $id);
$db->setQuery($query);
$text = $db->loadResult() ?: '';
$text = strip_tags($text);
$text = trim(preg_replace('/\s+/', ' ', $text));
if (\strlen($text) > 160) {
$text = mb_substr($text, 0, 157) . '...';
}
return $text;
}
public function getImage(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('f.file_path'))
->from($db->quoteName('#__hikashop_file', 'f'))
->where($db->quoteName('f.file_ref_id') . ' = ' . $id)
->where($db->quoteName('f.file_type') . ' = ' . $db->quote('product'))
->order($db->quoteName('f.file_ordering') . ' ASC');
$db->setQuery($query, 0, 1);
return $db->loadResult() ?: '';
}
}
@@ -0,0 +1,73 @@
<?php
/**
* @package MokoOpenGraph
* @subpackage com_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\Component\MokoOG\Administrator\ContentType;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
class K2Adapter implements ContentTypeInterface
{
public function canHandle(string $option, string $view): bool
{
return $option === 'com_k2' && $view === 'item';
}
public function getContentType(): string
{
return 'com_k2';
}
public function getTitle(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__k2_items'))
->where($db->quoteName('id') . ' = ' . $id);
$db->setQuery($query);
return $db->loadResult() ?: '';
}
public function getDescription(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('introtext'))
->from($db->quoteName('#__k2_items'))
->where($db->quoteName('id') . ' = ' . $id);
$db->setQuery($query);
$text = $db->loadResult() ?: '';
$text = strip_tags($text);
$text = trim(preg_replace('/\s+/', ' ', $text));
if (\strlen($text) > 160) {
$text = mb_substr($text, 0, 157) . '...';
}
return $text;
}
public function getImage(int $id): string
{
// K2 stores images as media/k2/items/cache/{md5}_L.jpg
$imagePath = 'media/k2/items/cache/' . md5('Image' . $id) . '_L.jpg';
if (is_file(JPATH_ROOT . '/' . $imagePath)) {
return $imagePath;
}
return '';
}
}
@@ -0,0 +1,82 @@
<?php
/**
* @package MokoOpenGraph
* @subpackage com_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\Component\MokoOG\Administrator\ContentType;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
class VirtueMartAdapter implements ContentTypeInterface
{
public function canHandle(string $option, string $view): bool
{
return $option === 'com_virtuemart' && $view === 'productdetails';
}
public function getContentType(): string
{
return 'com_virtuemart';
}
public function getTitle(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('product_name'))
->from($db->quoteName('#__virtuemart_products_' . $this->getLangTag()))
->where($db->quoteName('virtuemart_product_id') . ' = ' . $id);
$db->setQuery($query);
return $db->loadResult() ?: '';
}
public function getDescription(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('product_s_desc'))
->from($db->quoteName('#__virtuemart_products_' . $this->getLangTag()))
->where($db->quoteName('virtuemart_product_id') . ' = ' . $id);
$db->setQuery($query);
$desc = $db->loadResult() ?: '';
return strip_tags($desc);
}
public function getImage(int $id): string
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('m.file_url'))
->from($db->quoteName('#__virtuemart_product_medias', 'pm'))
->join('INNER', $db->quoteName('#__virtuemart_medias', 'm') . ' ON ' . $db->quoteName('m.virtuemart_media_id') . ' = ' . $db->quoteName('pm.virtuemart_media_id'))
->where($db->quoteName('pm.virtuemart_product_id') . ' = ' . $id)
->order($db->quoteName('pm.ordering') . ' ASC');
$db->setQuery($query, 0, 1);
return $db->loadResult() ?: '';
}
/**
* Get the VirtueMart language table suffix.
*
* @return string
*/
private function getLangTag(): string
{
$lang = Factory::getLanguage()->getTag();
return strtolower(str_replace('-', '_', $lang));
}
}