- md5
- f501ce9a349d40f7bf10a37944a8d23d
- sha1
- 2c2292e117e638586b85bab843bba8dfa20f1aca
- sha256
- 106f15d9bfcbdc4ac516a94c0dc0d1ef7e1e17732d34f742fc46d7580e686a14
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Schemaorg;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\Event\EventInterface;
/**
* Trait for component schemaorg plugins.
*
* @since 5.0.0
*/
trait SchemaorgPluginTrait
{
/**
* The application object
*
* @var CMSApplication
*/
protected $app;
/**
* Define all fields which are media type to clean them
*
* @var array
*/
protected $imageFields = [
'image',
];
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since 5.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onSchemaPrepareForm' => 'onSchemaPrepareForm',
];
}
/**
* Add a new option to schemaType list field in schema form
*
* @param EventInterface $event
*
* @return boolean
*
* @since 5.0.0
*/
protected function addSchemaType(EventInterface $event)
{
$form = $event->getArgument('subject');
$name = $this->pluginName;
if (!$form || !$name) {
return false;
}
$schemaType = $form->getField('schemaType', 'schema');
if ($schemaType instanceof ListField) {
$schemaType->addOption($name, ['value' => $name]);
}
return true;
}
/**
* Add a new option to the schema type in the item editing page
*
* @param EventInterface $event The form to be altered.
*
* @return boolean
*/
public function onSchemaPrepareForm(EventInterface $event)
{
$form = $event->getArgument('subject');
$context = $form->getName();
if (!$this->isSupported($context)) {
return false;
}
$this->addSchemaType($event);
//Load the form fields
if (is_file(JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/forms/schemaorg.xml')) {
$form->loadFile(JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/forms/schemaorg.xml');
}
return true;
}
/**
* Removes empty fields and changes time duration to ISO format in schema form
*
* @param array $data JSON object of the data stored in schema form
*
* @return array
*
* @since 5.0.0
*/
protected function cleanupSchema(array $data)
{
// Let plugins implement their own cleanup
$data = $this->customCleanup($data);
$schema = [];
foreach ($data as $key => $value) {
// Subtypes need special handling
if (is_array($value) && !empty($value['@type'])) {
if ($value['@type'] === 'ImageObject') {
if (!empty($value['url'])) {
$value['url'] = $this->cleanupImage($value['url']);
}
if (empty($value['url'])) {
$value = [];
}
} elseif ($value['@type'] === 'Date') {
if (!empty($value['value'])) {
$value['value'] = Factory::getDate($value['value'])->format('Y-m-d');
}
if (empty($value['value'])) {
$value = [];
}
}
$value = $this->cleanupSchema($value);
// We don't save when the array contains only the @type
if (count($value) <= 1) {
$value = null;
}
} elseif (is_array($value) && $key == 'genericField') {
foreach ($value as $field) {
$schema[$field['genericTitle']] = $field['genericValue'];
}
continue;
}
// No data, no pary
if (empty($value)) {
continue;
}
if (in_array($key, $this->imageFields)) {
$value = $this->cleanupImage($value);
}
$schema[$key] = $value;
}
return $schema;
}
/**
* Cleanup media image files
*
* @param string|array $image
*
* @return string|null
*/
protected function cleanupImage($image)
{
if (is_array($image)) {
$newImages = [];
foreach ($image as $img) {
$newImages[] = $this->cleanupImage($img);
}
return $newImages;
}
$img = HTMLHelper::_('cleanImageUrl', $image);
return $img->url ?? null;
}
/**
* To normalize duration to ISO format
*
* @param array $schema Schema form
* @param array $durationKeys Keys with duration fields
*
* @return array
*/
protected function normalizeDurationsToISO(array $schema, array $durationKeys)
{
foreach ($durationKeys as $durationKey) {
$duration = $schema[$durationKey] ?? [];
if (empty($duration)) {
continue;
}
$min = $duration['min'] ?? 0;
$hour = $duration['hour'] ?? 0;
$newDuration = false;
if ($hour && $min && $min < 60) {
$newDuration = "PT" . $hour . "H" . $min . "M";
} elseif ($hour) {
$newDuration = "PT" . $hour . "H";
} elseif ($min && $min < 60) {
$newDuration = "PT" . $min . "M";
}
if ($newDuration === false) {
unset($schema[$durationKey]);
continue;
}
$schema[$durationKey] = $newDuration;
}
return $schema;
}
/**
* To create an array from repeatable text field data
*
* @param array $schema Schema form
* @param array $repeatableFields Names of all the Repeatable fields
*
* @return array
*/
protected function convertToArray(array $schema, array $repeatableFields)
{
foreach ($repeatableFields as $repeatableField) {
$field = $schema[$repeatableField] ?? [];
if (empty($field)) {
continue;
}
$result = [];
foreach ($field as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$result[] = $v;
}
continue;
}
$result[] = $value;
}
if (empty($result)) {
unset($schema[$repeatableField]);
continue;
}
$schema[$repeatableField] = $result;
}
return $schema;
}
/**
* To clean up the date fields in
*
* @param array $schema Schema form
* @param array $dateKeys Keys with date fields
*
* @return boolean
*/
protected function cleanupDate(array $schema, array $dateKeys)
{
foreach ($dateKeys as $dateKey) {
$date = $schema[$dateKey] ?? [];
if (empty($date)) {
continue;
}
$schema[$dateKey] = Factory::getDate($date)->format('Y-m-d');
}
return $schema;
}
/**
* To add plugin specific functions
*
* @param array $schema Schema form
*
* @return array
*/
protected function customCleanup(array $schema)
{
// Write your code for extra filtering
return $schema;
}
/**
* Check if the current plugin should execute schemaorg related activities
*
* @param string $context
*
* @return boolean
*
* @since 5.0.0
*/
protected function isSupported($context)
{
if (!$this->checkAllowedAndForbiddenlist($context)) {
return false;
}
$parts = explode('.', $context);
// We need at least the extension + view for loading the table fields
if (count($parts) < 2) {
return false;
}
$component = $this->app->bootComponent($parts[0]);
return $component instanceof SchemaorgServiceInterface;
}
/**
* Check if the context is listed in the allowed or forbidden lists and return the result.
*
* @param string $context Context to check
*
* @return boolean
*/
protected function checkAllowedAndForbiddenlist($context)
{
$allowedlist = \array_filter((array) $this->params->get('allowedlist', []));
$forbiddenlist = \array_filter((array) $this->params->get('forbiddenlist', []));
if (!empty($allowedlist)) {
foreach ($allowedlist as $allowed) {
if ($context === $allowed) {
return true;
}
}
return false;
}
foreach ($forbiddenlist as $forbidden) {
if ($context === $forbidden) {
return false;
}
}
return true;
}
}
No other release has this exact content. See every version of this file.