diff --git a/plugins/content/confirmconsent/src/Extension/ConfirmConsent.php b/plugins/content/confirmconsent/src/Extension/ConfirmConsent.php index 99e9df14d0a93..4e14d46e1c43e 100644 --- a/plugins/content/confirmconsent/src/Extension/ConfirmConsent.php +++ b/plugins/content/confirmconsent/src/Extension/ConfirmConsent.php @@ -10,8 +10,9 @@ namespace Joomla\Plugin\Content\ConfirmConsent\Extension; -use Joomla\CMS\Form\Form; +use Joomla\CMS\Event\Model\PrepareFormEvent; use Joomla\CMS\Plugin\CMSPlugin; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -22,17 +23,8 @@ * * @since 3.9.0 */ -final class ConfirmConsent extends CMSPlugin +final class ConfirmConsent extends CMSPlugin implements SubscriberInterface { - /** - * Load the language file on instantiation. - * - * @var boolean - * - * @since 3.9.0 - */ - protected $autoloadLanguage = true; - /** * The supported form contexts * @@ -45,22 +37,39 @@ final class ConfirmConsent extends CMSPlugin 'com_privacy.request', ]; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentPrepareForm' => 'onContentPrepareForm', + ]; + } + /** * Add additional fields to the supported forms * - * @param Form $form The form to be altered. - * @param mixed $data The associated data for the form. + * @param PrepareFormEvent $event The event instance. * * @return boolean * * @since 3.9.0 */ - public function onContentPrepareForm(Form $form, $data) + public function onContentPrepareForm(PrepareFormEvent $event) { + $form = $event->getForm(); + if ($this->getApplication()->isClient('administrator') || !\in_array($form->getName(), $this->supportedContext)) { return true; } + $this->loadLanguage(); + // Get the consent box Text & the selected privacyarticle $consentboxText = (string) $this->params->get( 'consentbox_text', diff --git a/plugins/content/contact/src/Extension/Contact.php b/plugins/content/contact/src/Extension/Contact.php index 7ad9a153bee47..ee21563854498 100644 --- a/plugins/content/contact/src/Extension/Contact.php +++ b/plugins/content/contact/src/Extension/Contact.php @@ -10,13 +10,14 @@ namespace Joomla\Plugin\Content\Contact\Extension; +use Joomla\CMS\Event\Content\ContentPrepareEvent; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Router\Route; use Joomla\Component\Contact\Site\Helper\RouteHelper; use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; -use Joomla\Registry\Registry; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -27,22 +28,37 @@ * * @since 3.2 */ -final class Contact extends CMSPlugin +final class Contact extends CMSPlugin implements SubscriberInterface { use DatabaseAwareTrait; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentPrepare' => 'onContentPrepare', + ]; + } + /** * Plugin that retrieves contact information for contact * - * @param string $context The context of the content being passed to the plugin. - * @param mixed &$row An object with a "text" property - * @param mixed $params Additional parameters. See {@see PlgContentContent()}. - * @param integer $page Optional page number. Unused. Defaults to zero. + * @param ContentPrepareEvent $event The event instance. * * @return void */ - public function onContentPrepare($context, &$row, $params, $page = 0) + public function onContentPrepare(ContentPrepareEvent $event) { + $context = $event->getContext(); + $row = $event->getItem(); + $params = $event->getParams(); + $allowed_contexts = ['com_content.category', 'com_content.article', 'com_content.featured']; if (!\in_array($context, $allowed_contexts)) { @@ -50,7 +66,7 @@ public function onContentPrepare($context, &$row, $params, $page = 0) } // Return if we don't have valid params or don't link the author - if (!($params instanceof Registry) || !$params->get('link_author')) { + if (!$params->get('link_author')) { return; } diff --git a/plugins/content/fields/src/Extension/Fields.php b/plugins/content/fields/src/Extension/Fields.php index 0360f879f2d49..c2fff121240c1 100644 --- a/plugins/content/fields/src/Extension/Fields.php +++ b/plugins/content/fields/src/Extension/Fields.php @@ -10,8 +10,10 @@ namespace Joomla\Plugin\Content\Fields\Extension; +use Joomla\CMS\Event\Content\ContentPrepareEvent; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -23,22 +25,36 @@ * * @since 3.7.0 */ -final class Fields extends CMSPlugin +final class Fields extends CMSPlugin implements SubscriberInterface { + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentPrepare' => 'onContentPrepare', + ]; + } + /** * Plugin that shows a custom field * - * @param string $context The context of the content being passed to the plugin. - * @param object &$item The item object. Note $article->text is also available - * @param object &$params The article params - * @param int $page The 'page' number + * @param ContentPrepareEvent $event The event instance. * * @return void * * @since 3.7.0 */ - public function onContentPrepare($context, &$item, &$params, $page = 0) + public function onContentPrepare(ContentPrepareEvent $event) { + $context = $event->getContext(); + $item = $event->getItem(); + // If the item has a context, overwrite the existing one if ($context === 'com_finder.indexer' && !empty($item->context)) { $context = $item->context; diff --git a/plugins/content/finder/src/Extension/Finder.php b/plugins/content/finder/src/Extension/Finder.php index 83f44d9dbe2b3..a83509751124e 100644 --- a/plugins/content/finder/src/Extension/Finder.php +++ b/plugins/content/finder/src/Extension/Finder.php @@ -11,8 +11,10 @@ namespace Joomla\Plugin\Content\Finder\Extension; use Joomla\CMS\Event\Finder as FinderEvent; +use Joomla\CMS\Event\Model; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Plugin\PluginHelper; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -23,7 +25,7 @@ * * @since 2.5 */ -final class Finder extends CMSPlugin +final class Finder extends CMSPlugin implements SubscriberInterface { /** * Flag to check whether finder plugins already imported. @@ -34,28 +36,44 @@ final class Finder extends CMSPlugin */ protected $pluginsImported = false; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentBeforeSave' => 'onContentBeforeSave', + 'onContentAfterSave' => 'onContentAfterSave', + 'onContentAfterDelete' => 'onContentAfterDelete', + 'onContentChangeState' => 'onContentChangeState', + 'onCategoryChangeState' => 'onCategoryChangeState', + ]; + } + /** * Smart Search after save content method. * Content is passed by reference, but after the save, so no changes will be saved. * Method is called right after the content is saved. * - * @param string $context The context of the content passed to the plugin (added in 1.6) - * @param object $article A \Joomla\CMS\Table\Table\ object - * @param bool $isNew If the content has just been created + * @param Model\AfterSaveEvent $event The event instance. * * @return void * * @since 2.5 */ - public function onContentAfterSave($context, $article, $isNew): void + public function onContentAfterSave(Model\AfterSaveEvent $event): void { $this->importFinderPlugins(); // Trigger the onFinderAfterSave event. $this->getDispatcher()->dispatch('onFinderAfterSave', new FinderEvent\AfterSaveEvent('onFinderAfterSave', [ - 'context' => $context, - 'subject' => $article, - 'isNew' => $isNew, + 'context' => $event->getContext(), + 'subject' => $event->getItem(), + 'isNew' => $event->getIsNew(), ])); } @@ -63,23 +81,21 @@ public function onContentAfterSave($context, $article, $isNew): void * Smart Search before save content method. * Content is passed by reference. Method is called before the content is saved. * - * @param string $context The context of the content passed to the plugin (added in 1.6). - * @param object $article A \Joomla\CMS\Table\Table\ object. - * @param bool $isNew If the content is just about to be created. + * @param Model\BeforeSaveEvent $event The event instance. * * @return void * * @since 2.5 */ - public function onContentBeforeSave($context, $article, $isNew) + public function onContentBeforeSave(Model\BeforeSaveEvent $event) { $this->importFinderPlugins(); // Trigger the onFinderBeforeSave event. $this->getDispatcher()->dispatch('onFinderBeforeSave', new FinderEvent\BeforeSaveEvent('onFinderBeforeSave', [ - 'context' => $context, - 'subject' => $article, - 'isNew' => $isNew, + 'context' => $event->getContext(), + 'subject' => $event->getItem(), + 'isNew' => $event->getIsNew(), ])); } @@ -87,21 +103,20 @@ public function onContentBeforeSave($context, $article, $isNew) * Smart Search after delete content method. * Content is passed by reference, but after the deletion. * - * @param string $context The context of the content passed to the plugin (added in 1.6). - * @param object $article A \Joomla\CMS\Table\Table object. + * @param Model\AfterDeleteEvent $event The event instance. * * @return void * * @since 2.5 */ - public function onContentAfterDelete($context, $article): void + public function onContentAfterDelete(Model\AfterDeleteEvent $event): void { $this->importFinderPlugins(); // Trigger the onFinderAfterDelete event. $this->getDispatcher()->dispatch('onFinderAfterDelete', new FinderEvent\AfterDeleteEvent('onFinderAfterDelete', [ - 'context' => $context, - 'subject' => $article, + 'context' => $event->getContext(), + 'subject' => $event->getItem(), ])); } @@ -111,23 +126,21 @@ public function onContentAfterDelete($context, $article): void * from outside the edit screen. This is fired when the item is published, * unpublished, archived, or unarchived from the list view. * - * @param string $context The context for the content passed to the plugin. - * @param array $pks A list of primary key ids of the content that has changed state. - * @param integer $value The value of the state that the content has been changed to. + * @param Model\AfterChangeStateEvent $event The event instance. * * @return void * * @since 2.5 */ - public function onContentChangeState($context, $pks, $value) + public function onContentChangeState(Model\AfterChangeStateEvent $event) { $this->importFinderPlugins(); // Trigger the onFinderChangeState event. $this->getDispatcher()->dispatch('onFinderChangeState', new FinderEvent\AfterChangeStateEvent('onFinderChangeState', [ - 'context' => $context, - 'subject' => $pks, - 'value' => $value, + 'context' => $event->getContext(), + 'subject' => $event->getPks(), + 'value' => $event->getValue(), ])); } @@ -136,23 +149,21 @@ public function onContentChangeState($context, $pks, $value) * Method is called when the state of the category to which the * content item belongs is changed. * - * @param string $extension The extension whose category has been updated. - * @param array $pks A list of primary key ids of the content that has changed state. - * @param integer $value The value of the state that the content has been changed to. + * @param Model\AfterCategoryChangeStateEvent $event The event instance. * * @return void * * @since 2.5 */ - public function onCategoryChangeState($extension, $pks, $value) + public function onCategoryChangeState(Model\AfterCategoryChangeStateEvent $event) { $this->importFinderPlugins(); // Trigger the onFinderCategoryChangeState event. $this->getDispatcher()->dispatch('onFinderCategoryChangeState', new FinderEvent\AfterCategoryChangeStateEvent('onFinderCategoryChangeState', [ - 'context' => $extension, - 'subject' => $pks, - 'value' => $value, + 'context' => $event->getExtension(), + 'subject' => $event->getPks(), + 'value' => $event->getValue(), ])); } diff --git a/plugins/content/joomla/src/Extension/Joomla.php b/plugins/content/joomla/src/Extension/Joomla.php index 5e91d7b23e03b..a5615af60b2a1 100644 --- a/plugins/content/joomla/src/Extension/Joomla.php +++ b/plugins/content/joomla/src/Extension/Joomla.php @@ -12,6 +12,11 @@ use Joomla\CMS\Cache\CacheControllerFactory; use Joomla\CMS\Component\ComponentHelper; +use Joomla\CMS\Event\Model\AfterChangeStateEvent; +use Joomla\CMS\Event\Model\AfterSaveEvent; +use Joomla\CMS\Event\Model\BeforeChangeStateEvent; +use Joomla\CMS\Event\Model\BeforeDeleteEvent; +use Joomla\CMS\Event\Model\BeforeSaveEvent; use Joomla\CMS\Event\Plugin\System\Schemaorg\BeforeCompileHeadEvent; use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; @@ -26,6 +31,7 @@ use Joomla\Component\Workflow\Administrator\Table\WorkflowTable; use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; +use Joomla\Event\SubscriberInterface; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; @@ -38,32 +44,56 @@ * * @since 1.6 */ -final class Joomla extends CMSPlugin +final class Joomla extends CMSPlugin implements SubscriberInterface { use DatabaseAwareTrait; use UserFactoryAwareTrait; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentBeforeSave' => 'onContentBeforeSave', + 'onContentAfterSave' => 'onContentAfterSave', + 'onContentBeforeDelete' => 'onContentBeforeDelete', + 'onContentBeforeChangeState' => 'onContentBeforeChangeState', + 'onContentChangeState' => 'onContentChangeState', + 'onSchemaBeforeCompileHead' => 'onSchemaBeforeCompileHead', + ]; + } + /** * The save event. * - * @param string $context The context - * @param object $table The item - * @param boolean $isNew Is new item - * @param array $data The validated data + * @param BeforeSaveEvent $event The event instance. * - * @return boolean + * @return void * * @since 4.0.0 */ - public function onContentBeforeSave($context, $table, $isNew, $data) + public function onContentBeforeSave(BeforeSaveEvent $event) { + $context = $event->getContext(); + $table = $event->getItem(); + $isNew = $event->getIsNew(); + $data = $event->getData(); + $result = true; + if ($context === 'com_menus.item') { - return $this->checkMenuItemBeforeSave($context, $table, $isNew, $data); + $result = $this->checkMenuItemBeforeSave($context, $table, $isNew, $data); + $event->addResult($result); + return; } // Check we are handling the frontend edit form. if (!\in_array($context, ['com_workflow.stage', 'com_workflow.workflow']) || $isNew || !$table->hasField('published')) { - return true; + return; } $item = clone $table; @@ -75,14 +105,16 @@ public function onContentBeforeSave($context, $table, $isNew, $data) if ($item->$publishedField > 0 && isset($data[$publishedField]) && $data[$publishedField] < 1) { switch ($context) { case 'com_workflow.workflow': - return $this->workflowNotUsed($item->id); + $result = $this->workflowNotUsed($item->id); + break; case 'com_workflow.stage': - return $this->stageNotUsed($item->id); + $result = $this->stageNotUsed($item->id); + break; } } - return true; + $event->addResult($result); } /** @@ -90,16 +122,18 @@ public function onContentBeforeSave($context, $table, $isNew, $data) * Article is passed by reference, but after the save, so no changes will be saved. * Method is called right after the content is saved * - * @param string $context The context of the content passed to the plugin (added in 1.6) - * @param object $article A \Joomla\CMS\Table\Table object - * @param boolean $isNew If the content is just about to be created + * @param AfterSaveEvent $event The event instance. * * @return void * * @since 1.6 */ - public function onContentAfterSave($context, $article, $isNew): void + public function onContentAfterSave(AfterSaveEvent $event): void { + $context = $event->getContext(); + $article = $event->getItem(); + $isNew = $event->getIsNew(); + // Check we are handling the frontend edit form. if ($context !== 'com_content.form') { return; @@ -156,49 +190,58 @@ public function onContentAfterSave($context, $article, $isNew): void /** * Don't allow categories to be deleted if they contain items or subcategories with items * - * @param string $context The context for the content passed to the plugin. - * @param object $data The data relating to the content that was deleted. + * @param BeforeDeleteEvent $event The event instance. * - * @return boolean + * @return void * * @since 1.6 */ - public function onContentBeforeDelete($context, $data) + public function onContentBeforeDelete(BeforeDeleteEvent $event) { + $context = $event->getContext(); + $data = $event->getItem(); + // Skip plugin if we are deleting something other than categories if (!\in_array($context, ['com_categories.category', 'com_workflow.stage', 'com_workflow.workflow'])) { - return true; + return; } + $result = true; + switch ($context) { case 'com_categories.category': - return $this->canDeleteCategories($data); + $result = $this->canDeleteCategories($data); + break; case 'com_workflow.workflow': - return $this->workflowNotUsed($data->id); + $result = $this->workflowNotUsed($data->id); + break; case 'com_workflow.stage': - return $this->stageNotUsed($data->id); + $result = $this->stageNotUsed($data->id); + break; } - return true; + $event->addResult($result); } /** * Don't allow workflows/stages to be deleted if they contain items * - * @param string $context The context for the content passed to the plugin. - * @param object $pks The IDs of the records which will be changed. - * @param object $value The new state. + * @param BeforeChangeStateEvent $event The event instance. * - * @return boolean + * @return void * * @since 4.0.0 */ - public function onContentBeforeChangeState($context, $pks, $value) + public function onContentBeforeChangeState(BeforeChangeStateEvent $event) { + $context = $event->getContext(); + $pks = $event->getPks(); + $value = $event->getValue(); + if ($value > 0 || !\in_array($context, ['com_workflow.workflow', 'com_workflow.stage'])) { - return true; + return; } $result = true; @@ -215,7 +258,7 @@ public function onContentBeforeChangeState($context, $pks, $value) } } - return $result; + $event->addResult($result); } /** @@ -953,26 +996,29 @@ private function countItemsInChildren($table, $catid, $data) /** * Change the state in core_content if the stage in a table is changed * - * @param string $context The context for the content passed to the plugin. - * @param array $pks A list of primary key ids of the content that has changed stage. - * @param integer $value The value of the condition that the content has been changed to + * @param AfterChangeStateEvent $event The event instance. * - * @return boolean + * @return void * * @since 3.1 */ - public function onContentChangeState($context, $pks, $value) + public function onContentChangeState(AfterChangeStateEvent $event) { - $pks = ArrayHelper::toInteger($pks); + $context = $event->getContext(); + $pks = $event->getPks(); + $value = $event->getValue(); + $pks = ArrayHelper::toInteger($pks); if ($context === 'com_workflow.stage' && $value < 1) { foreach ($pks as $pk) { if (!$this->stageNotUsed($pk)) { - return false; + $event->addResult(false); + return; } } - return true; + $event->addResult(true); + return; } $db = $this->getDatabase(); @@ -988,7 +1034,7 @@ public function onContentChangeState($context, $pks, $value) $cctable = new CoreContent($db); $cctable->publish($ccIds, $value); - return true; + $event->addResult(true); } /** diff --git a/plugins/content/loadmodule/src/Extension/LoadModule.php b/plugins/content/loadmodule/src/Extension/LoadModule.php index 758eb4be8067a..7989425be8620 100644 --- a/plugins/content/loadmodule/src/Extension/LoadModule.php +++ b/plugins/content/loadmodule/src/Extension/LoadModule.php @@ -10,8 +10,10 @@ namespace Joomla\Plugin\Content\LoadModule\Extension; +use Joomla\CMS\Event\Content\ContentPrepareEvent; use Joomla\CMS\Helper\ModuleHelper; use Joomla\CMS\Plugin\CMSPlugin; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -23,26 +25,40 @@ * * @since 1.5 */ -final class LoadModule extends CMSPlugin +final class LoadModule extends CMSPlugin implements SubscriberInterface { protected static $modules = []; protected static $mods = []; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentPrepare' => 'onContentPrepare', + ]; + } + /** * Plugin that loads module positions within content * - * @param string $context The context of the content being passed to the plugin. - * @param object &$article The article object. Note $article->text is also available - * @param mixed &$params The article params - * @param integer $page The 'page' number + * @param ContentPrepareEvent $event The event instance. * * @return void * * @since 1.6 */ - public function onContentPrepare($context, &$article, &$params, $page = 0) + public function onContentPrepare(ContentPrepareEvent $event) { + $context = $event->getContext(); + $article = $event->getItem(); + // Only execute if $article is an object and has a text property if (!\is_object($article) || !property_exists($article, 'text') || \is_null($article->text)) { return; diff --git a/plugins/content/pagebreak/src/Extension/PageBreak.php b/plugins/content/pagebreak/src/Extension/PageBreak.php index 3a279b2f555ea..963302693267e 100644 --- a/plugins/content/pagebreak/src/Extension/PageBreak.php +++ b/plugins/content/pagebreak/src/Extension/PageBreak.php @@ -10,6 +10,7 @@ namespace Joomla\Plugin\Content\PageBreak\Extension; +use Joomla\CMS\Event\Content\ContentPrepareEvent; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Pagination\Pagination; @@ -17,6 +18,7 @@ use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Utility\Utility; use Joomla\Component\Content\Site\Helper\RouteHelper; +use Joomla\Event\SubscriberInterface; use Joomla\String\StringHelper; // phpcs:disable PSR1.Files.SideEffects @@ -38,7 +40,7 @@ * * @since 1.6 */ -final class PageBreak extends CMSPlugin +final class PageBreak extends CMSPlugin implements SubscriberInterface { /** * The navigation list with all page objects if parameter 'multipage_toc' is active. @@ -48,20 +50,36 @@ final class PageBreak extends CMSPlugin */ protected $list = []; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentPrepare' => 'onContentPrepare', + ]; + } + /** * Plugin that adds a pagebreak into the text and truncates text at that point * - * @param string $context The context of the content being passed to the plugin. - * @param object &$row The article object. Note $article->text is also available - * @param mixed &$params The article params - * @param integer $page The 'page' number + * @param ContentPrepareEvent $event The event instance. * * @return void * * @since 1.6 */ - public function onContentPrepare($context, &$row, &$params, $page = 0) + public function onContentPrepare(ContentPrepareEvent $event) { + $context = $event->getContext(); + $row = $event->getItem(); + $params = $event->getParams(); + $page = $event->getPage(); + $canProceed = $context === 'com_content.article'; if (!$canProceed) { diff --git a/plugins/content/pagenavigation/src/Extension/PageNavigation.php b/plugins/content/pagenavigation/src/Extension/PageNavigation.php index a36182441377a..a04b3c5f5b814 100644 --- a/plugins/content/pagenavigation/src/Extension/PageNavigation.php +++ b/plugins/content/pagenavigation/src/Extension/PageNavigation.php @@ -11,12 +11,14 @@ namespace Joomla\Plugin\Content\PageNavigation\Extension; use Joomla\CMS\Access\Access; +use Joomla\CMS\Event\Content\BeforeDisplayEvent; use Joomla\CMS\Factory; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Plugin\PluginHelper; use Joomla\Component\Content\Site\Helper\RouteHelper; use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -27,24 +29,39 @@ * * @since 1.5 */ -final class PageNavigation extends CMSPlugin +final class PageNavigation extends CMSPlugin implements SubscriberInterface { use DatabaseAwareTrait; + /** + * Returns an array of events this subscriber will listen to. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentBeforeDisplay' => 'onContentBeforeDisplay', + ]; + } + /** * If in the article view and the parameter is enabled shows the page navigation * - * @param string $context The context of the content being passed to the plugin - * @param object &$row The article object - * @param mixed &$params The article params - * @param integer $page The 'page' number + * @param BeforeDisplayEvent $event The event instance. * * @return void * * @since 1.6 */ - public function onContentBeforeDisplay($context, &$row, &$params, $page = 0) + public function onContentBeforeDisplay(BeforeDisplayEvent $event) { + $context = $event->getContext(); + $row = $event->getItem(); + $params = $event->getParams(); + $app = $this->getApplication(); $view = $app->getInput()->get('view'); $print = $app->getInput()->getBool('print'); diff --git a/plugins/content/vote/src/Extension/Vote.php b/plugins/content/vote/src/Extension/Vote.php index c01980c1af85f..74f7e52984266 100644 --- a/plugins/content/vote/src/Extension/Vote.php +++ b/plugins/content/vote/src/Extension/Vote.php @@ -10,9 +10,13 @@ namespace Joomla\Plugin\Content\Vote\Extension; +use Joomla\CMS\Event\Content\AfterDisplayEvent; +use Joomla\CMS\Event\Content\BeforeDisplayEvent; +use Joomla\CMS\Event\Plugin\System\Schemaorg; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Uri\Uri; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -23,7 +27,7 @@ * * @since 1.5 */ -final class Vote extends CMSPlugin +final class Vote extends CMSPlugin implements SubscriberInterface { /** * @var \Joomla\CMS\Application\CMSApplication @@ -36,60 +40,76 @@ final class Vote extends CMSPlugin protected $app; /** - * Displays the voting area when viewing an article and the voting section is displayed before the article + * Returns an array of events this subscriber will listen to. * - * @param string $context The context of the content being passed to the plugin - * @param object &$row The article object - * @param object &$params The article params - * @param integer $page The 'page' number + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public static function getSubscribedEvents(): array + { + return [ + 'onContentBeforeDisplay' => 'onContentBeforeDisplay', + 'onContentAfterDisplay' => 'onContentAfterDisplay', + 'onSchemaBeforeCompileHead' => 'onSchemaBeforeCompileHead', + ]; + } + + /** + * Displays the voting area when viewing an article and the voting section is displayed before the article. + * Add HTML string containing code for the votes if in com_content. * - * @return string|boolean HTML string containing code for the votes if in com_content else boolean false + * @param BeforeDisplayEvent $event The event instance. + * + * @return void * * @since 1.6 */ - public function onContentBeforeDisplay($context, &$row, &$params, $page = 0) + public function onContentBeforeDisplay(BeforeDisplayEvent $event) { if ($this->params->get('position', 'top') !== 'top') { - return ''; + return; } - return $this->displayVotingData($context, $row, $params, $page); + $event->addResult( + $this->displayVotingData($event->getContext(), $event->getItem(), $event->getParams(), $event->getPage()) + ); } /** - * Displays the voting area when viewing an article and the voting section is displayed after the article + * Displays the voting area when viewing an article and the voting section is displayed after the article. + * Add HTML string containing code for the votes if in com_content. * - * @param string $context The context of the content being passed to the plugin - * @param object &$row The article object - * @param object &$params The article params - * @param integer $page The 'page' number + * @param AfterDisplayEvent $event The event instance. * - * @return string|boolean HTML string containing code for the votes if in com_content else boolean false + * @return void * * @since 3.7.0 */ - public function onContentAfterDisplay($context, &$row, &$params, $page = 0) + public function onContentAfterDisplay(AfterDisplayEvent $event) { if ($this->params->get('position', 'top') !== 'bottom') { - return ''; + return; } - return $this->displayVotingData($context, $row, $params, $page); + $event->addResult( + $this->displayVotingData($event->getContext(), $event->getItem(), $event->getParams(), $event->getPage()) + ); } /** * Displays the voting area * * @param string $context The context of the content being passed to the plugin - * @param object &$row The article object - * @param object &$params The article params + * @param object $row The article object + * @param object $params The article params * @param integer $page The 'page' number * * @return string HTML string containing code for the votes if in com_content else empty string * * @since 3.7.0 */ - private function displayVotingData($context, &$row, &$params, $page) + private function displayVotingData($context, $row, $params, $page) { $parts = explode('.', $context); @@ -128,15 +148,16 @@ private function displayVotingData($context, &$row, &$params, $page) /** * Create SchemaOrg AggregateRating * - * @param object $schema The schema of the content being passed to the plugin - * @param string $context The context of the content being passed to the plugin + * @param Schemaorg\BeforeCompileHeadEvent $event The event instance. * * @return void * * @since 5.2.0 */ - public function onSchemaBeforeCompileHead($schema, $context): void + public function onSchemaBeforeCompileHead(Schemaorg\BeforeCompileHeadEvent $event): void { + $context = $event->getContext(); + $schema = $event->getSchema(); $graph = $schema->get('@graph'); $baseId = Uri::root() . '#/schema/'; $schemaId = $baseId . str_replace('.', '/', $context); diff --git a/tests/Unit/Plugin/Content/ConfirmConsent/Extension/ConfirmConsentTest.php b/tests/Unit/Plugin/Content/ConfirmConsent/Extension/ConfirmConsentTest.php index 6cc414799d9d0..b0c757cf92cb6 100644 --- a/tests/Unit/Plugin/Content/ConfirmConsent/Extension/ConfirmConsentTest.php +++ b/tests/Unit/Plugin/Content/ConfirmConsent/Extension/ConfirmConsentTest.php @@ -11,6 +11,7 @@ namespace Joomla\Tests\Unit\Plugin\Content\ConfirmConsent\Extension; use Joomla\CMS\Application\CMSApplicationInterface; +use Joomla\CMS\Event\Model\PrepareFormEvent; use Joomla\CMS\Form\Form; use Joomla\CMS\Language\Language; use Joomla\CMS\User\User; @@ -48,7 +49,10 @@ public function testLoadConsentFieldInForm() $dispatcher = new Dispatcher(); $plugin = new ConfirmConsent($dispatcher, ['params' => []]); $plugin->setApplication($app); - $plugin->onContentPrepareForm($form, []); + $plugin->onContentPrepareForm(new PrepareFormEvent('onContentPrepareForm', [ + 'subject' => $form, + 'data' => [], + ])); $this->assertNotFalse($form->getField('consentbox')); } @@ -68,7 +72,10 @@ public function testLoadConsentFieldInFormWrongContext() $dispatcher = new Dispatcher(); $plugin = new ConfirmConsent($dispatcher, ['params' => []]); $plugin->setApplication($this->createStub(CMSApplicationInterface::class)); - $plugin->onContentPrepareForm($form, []); + $plugin->onContentPrepareForm(new PrepareFormEvent('onContentPrepareForm', [ + 'subject' => $form, + 'data' => [], + ])); $this->assertFalse($form->getField('consentbox')); } @@ -91,7 +98,10 @@ public function testLoadConsentFieldInFormWrongApplication() $dispatcher = new Dispatcher(); $plugin = new ConfirmConsent($dispatcher, ['params' => []]); $plugin->setApplication($app); - $plugin->onContentPrepareForm($form, []); + $plugin->onContentPrepareForm(new PrepareFormEvent('onContentPrepareForm', [ + 'subject' => $form, + 'data' => [], + ])); $this->assertFalse($form->getField('consentbox')); }