-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2f2565
commit 2110d3b
Showing
8 changed files
with
276 additions
and
7 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
administrator/language/en-GB/plg_schemaorg_aggregaterating.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
; Joomla! Project | ||
; (C) 2023 Open Source Matters, Inc. <https://www.joomla.org> | ||
; License GNU General Public License version 2 or later; see LICENSE.txt | ||
; Note : All ini files need to be saved as UTF-8 | ||
|
||
PLG_SCHEMAORG_AGGREGATERATING="Schema.org - AggregateRating" | ||
PLG_SCHEMAORG_AGGREGATERATING_DESCRIPTION_LABEL="More information on the Schema.org AggregateRating Type can be found at <a href=\"https://schema.org/AggregateRating\" target=\"_blank\" rel=\"noopener noreferrer\">https://schema.org/AggregateRating</a>" | ||
PLG_SCHEMAORG_AGGREGATERATING_XML_DESCRIPTION="Adds AggregateRating as a new schema type in existing schemas." |
7 changes: 7 additions & 0 deletions
7
administrator/language/en-GB/plg_schemaorg_aggregaterating.sys.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
; Joomla! Project | ||
; (C) 2023 Open Source Matters, Inc. <https://www.joomla.org> | ||
; License GNU General Public License version 2 or later; see LICENSE.txt | ||
; Note : All ini files need to be saved as UTF-8 | ||
|
||
PLG_SCHEMAORG_BLOGPOSTING="Schema.org - Rating" | ||
PLG_SCHEMAORG_BLOGPOSTING_XML_DESCRIPTION="Adds Rating as a new schema type in existing schemas." |
46 changes: 46 additions & 0 deletions
46
libraries/src/Schemaorg/SchemaorgPrepareProductAggregateRating.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* Joomla! Content Management System | ||
* | ||
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org> | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | ||
|
||
namespace Joomla\CMS\Schemaorg; | ||
|
||
// phpcs:disable PSR1.Files.SideEffects | ||
\defined('_JEXEC') or die; | ||
// phpcs:enable PSR1.Files.SideEffects | ||
use Joomla\CMS\Uri\Uri; | ||
/** | ||
* Prepare Product AggregateRating to be valid for JSON-LD output | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
trait SchemaorgPrepareProductAggregateRating | ||
{ | ||
/** | ||
* Prepare duration | ||
* | ||
* @param array $duration | ||
* | ||
* @return ?string | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
protected function prepareProductAggregateRating($context) | ||
{ | ||
[$extension, $view, $id] = explode('.', $context); | ||
if ($view == 'article') { | ||
$baseId = Uri::root() . '#/schema/'; | ||
$schemaId = $baseId . str_replace('.', '/', $context); | ||
|
||
$component = $this->getApplication()->bootComponent('com_content')->getMVCFactory(); | ||
$model = $component->createModel('Article', 'Site'); | ||
$article = $model->getItem($id); | ||
return ['@isPartOf' => ['@id' => $schemaId,'@type' => 'Product','name' => $article->title,'aggregateRating' => ['@type'=> "AggregateRating","ratingCount" => (string)$article->rating_count,"ratingValue" => (string)$article->rating]]]; | ||
} | ||
return false; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
libraries/src/Schemaorg/SchemaorgPrepareRecipeAggregateRating.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* Joomla! Content Management System | ||
* | ||
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org> | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | ||
|
||
namespace Joomla\CMS\Schemaorg; | ||
|
||
// phpcs:disable PSR1.Files.SideEffects | ||
\defined('_JEXEC') or die; | ||
// phpcs:enable PSR1.Files.SideEffects | ||
use Joomla\CMS\Uri\Uri; | ||
/** | ||
* Prepare Recipe AggregateRating to be valid for JSON-LD output | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
trait SchemaorgPrepareRecipeAggregateRating | ||
{ | ||
/** | ||
* Prepare duration | ||
* | ||
* @param array $duration | ||
* | ||
* @return ?string | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
protected function prepareRecipeAggregateRating($context) | ||
{ | ||
[$extension, $view, $id] = explode('.', $context); | ||
if ($view == 'article') { | ||
$baseId = Uri::root() . '#/schema/'; | ||
$schemaId = $baseId . str_replace('.', '/', $context); | ||
|
||
$component = $this->getApplication()->bootComponent('com_content')->getMVCFactory(); | ||
$model = $component->createModel('Article', 'Site'); | ||
$article = $model->getItem($id); | ||
return ['@isPartOf' => ['@id' => $schemaId,'aggregateRating' => ['@type'=> "AggregateRating","ratingCount" => (string)$article->rating_count,"ratingValue" => (string)$article->rating]]]; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<extension type="plugin" group="schemaorg" method="upgrade"> | ||
<name>plg_schemaorg_aggregaterating</name> | ||
<author>Joomla! Project</author> | ||
<creationDate>2023-07</creationDate> | ||
<copyright>(C) 2023 Open Source Matters, Inc.</copyright> | ||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license> | ||
<authorEmail>admin@joomla.org</authorEmail> | ||
<authorUrl>www.joomla.org</authorUrl> | ||
<version>5.0.0</version> | ||
<description>PLG_SCHEMAORG_AGGREGATERATING_XML_DESCRIPTION</description> | ||
<namespace path="src">Joomla\Plugin\Schemaorg\AggregateRating</namespace> | ||
<files> | ||
<folder plugin="aggregaterating">services</folder> | ||
<folder>src</folder> | ||
</files> | ||
<languages> | ||
<language tag="en-GB">language/en-GB/plg_schemaorg_aggregaterating.ini</language> | ||
<language tag="en-GB">language/en-GB/plg_schemaorg_aggregaterating.sys.ini</language> | ||
</languages> | ||
</extension> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* @package Joomla.Plugin | ||
* @subpackage Schemaorg.AggregateRating | ||
* | ||
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org> | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | ||
|
||
\defined('_JEXEC') or die; | ||
|
||
use Joomla\CMS\Extension\PluginInterface; | ||
use Joomla\CMS\Factory; | ||
use Joomla\CMS\Plugin\PluginHelper; | ||
use Joomla\DI\Container; | ||
use Joomla\DI\ServiceProviderInterface; | ||
use Joomla\Event\DispatcherInterface; | ||
use Joomla\Plugin\Schemaorg\AggregateRating\Extension\AggregateRating; | ||
|
||
return new class () implements ServiceProviderInterface { | ||
/** | ||
* Registers the service provider with a DI container. | ||
* | ||
* @param Container $container The DI container. | ||
* | ||
* @return void | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
public function register(Container $container) | ||
{ | ||
$container->set( | ||
PluginInterface::class, | ||
function (Container $container) { | ||
$dispatcher = $container->get(DispatcherInterface::class); | ||
$plugin = new AggregateRating( | ||
$dispatcher, | ||
(array) PluginHelper::getPlugin('schemaorg', 'aggregaterating') | ||
); | ||
$plugin->setApplication(Factory::getApplication()); | ||
|
||
return $plugin; | ||
} | ||
); | ||
} | ||
}; |
101 changes: 101 additions & 0 deletions
101
plugins/schemaorg/aggregaterating/src/Extension/AggregateRating.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/** | ||
* @package Joomla.Plugin | ||
* @subpackage Schemaorg.AggregateRating | ||
* | ||
* @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\Plugin\Schemaorg\AggregateRating\Extension; | ||
|
||
use Joomla\CMS\Event\Plugin\System\Schemaorg\BeforeCompileHeadEvent; | ||
use Joomla\CMS\Plugin\CMSPlugin; | ||
use Joomla\CMS\Schemaorg\SchemaorgPrepareProductAggregateRating; | ||
use Joomla\CMS\Schemaorg\SchemaorgPrepareRecipeAggregateRating; | ||
use Joomla\Event\Priority; | ||
use Joomla\CMS\Plugin\PluginHelper; | ||
use Joomla\Event\SubscriberInterface; | ||
|
||
// phpcs:disable PSR1.Files.SideEffects | ||
\defined('_JEXEC') or die; | ||
// phpcs:enable PSR1.Files.SideEffects | ||
|
||
/** | ||
* Schemaorg Plugin | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
final class AggregateRating extends CMSPlugin implements SubscriberInterface | ||
{ | ||
use SchemaorgPrepareProductAggregateRating; | ||
use SchemaorgPrepareRecipeAggregateRating; | ||
|
||
/** | ||
* Load the language file on instantiation. | ||
* | ||
* @var boolean | ||
* @since 5.0.0 | ||
*/ | ||
protected $autoloadLanguage = true; | ||
|
||
/** | ||
* The name of the schema form | ||
* | ||
* @var string | ||
* @since 5.0.0 | ||
*/ | ||
protected $pluginName = 'AggregateRating'; | ||
|
||
/** | ||
* Returns an array of events this subscriber will listen to. | ||
* | ||
* @return array | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'onSchemaBeforeCompileHead' => ['onSchemaBeforeCompileHead', Priority::BELOW_NORMAL], | ||
]; | ||
} | ||
|
||
/** | ||
* Cleanup all BlogPosting types | ||
* | ||
* @param BeforeCompileHeadEvent $event The given event | ||
* | ||
* @return void | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
public function onSchemaBeforeCompileHead(BeforeCompileHeadEvent $event): void | ||
{ | ||
$schema = $event->getSchema(); | ||
|
||
$graph = $schema->get('@graph'); | ||
|
||
$need_vote = PluginHelper::isEnabled('content', 'vote'); | ||
|
||
if (!$need_vote) { | ||
return; | ||
} | ||
foreach ($graph as $key => &$entry) { | ||
if (!isset($entry['@type'])) { | ||
continue; | ||
} | ||
if ($entry['@type'] == 'Recipe') { | ||
$rating = $this->prepareRecipeAggregateRating($event->getContext()); | ||
continue; | ||
} | ||
$rating = $this->prepareProductAggregateRating($event->getContext()); | ||
} | ||
|
||
if ($rating) { | ||
$graph[] = $rating; | ||
$schema->set('@graph', $graph); | ||
} | ||
} | ||
} |