Skip to content

Commit

Permalink
[5.1] Add aggregateRating
Browse files Browse the repository at this point in the history
  • Loading branch information
conseilgouz committed Mar 3, 2024
1 parent f2f2565 commit 2110d3b
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 7 deletions.
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."
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 libraries/src/Schemaorg/SchemaorgPrepareProductAggregateRating.php
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 libraries/src/Schemaorg/SchemaorgPrepareRecipeAggregateRating.php
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;
}
}
7 changes: 0 additions & 7 deletions plugins/content/vote/tmpl/rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@
?>
<div class="content_rating" role="img" aria-label="<?php echo Text::sprintf('PLG_VOTE_STAR_RATING', $rating); ?>">
<?php if ($rcount) : ?>
<div class="visually-hidden" itemscope="itemscope" itemtype="https://schema.org/Product">
<span itemprop="name"><?php echo $row->title;?></span>
<p class="visually-hidden" itemprop="aggregateRating" itemscope itemtype="https://schema.org/AggregateRating">
<?php echo Text::sprintf('PLG_VOTE_USER_RATING', '<span itemprop="ratingValue">' . $rating . '</span>', '<span itemprop="bestRating">5</span>'); ?>
<meta itemprop="ratingCount" content="<?php echo $rcount; ?>">
<meta itemprop="worstRating" content="1">
</p>
<?php if ($this->params->get('show_total_votes', 0)) : ?>
<?php echo Text::sprintf('PLG_VOTE_TOTAL_VOTES', $rcount); ?>
<?php endif; ?>
Expand Down
21 changes: 21 additions & 0 deletions plugins/schemaorg/aggregaterating/aggregaterating.xml
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>
47 changes: 47 additions & 0 deletions plugins/schemaorg/aggregaterating/services/provider.php
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 plugins/schemaorg/aggregaterating/src/Extension/AggregateRating.php
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);
}
}
}

0 comments on commit 2110d3b

Please sign in to comment.