Skip to content

Commit

Permalink
Followup work for the EOS plugin (joomla#40652)
Browse files Browse the repository at this point in the history
* Followup work for the EOS plugin

* short lines

* cs

* guest

* revert

* More checks

* debug

* more blind tests

* cs

* make PHP 7.2 compatible

* Improve docs

Signed-off-by: Allon Moritz <[email protected]>

* More docs

* date

---------

Signed-off-by: Allon Moritz <[email protected]>
  • Loading branch information
laoneo authored May 25, 2023
1 parent dd23e49 commit 18a29a6
Showing 1 changed file with 53 additions and 30 deletions.
83 changes: 53 additions & 30 deletions plugins/quickicon/eos/src/Extension/Eos.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

use Exception;
use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Application\CMSWebApplicationInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Event\SubscriberInterface;
Expand All @@ -34,12 +34,12 @@ final class Eos extends CMSPlugin implements SubscriberInterface
use DatabaseAwareTrait;

/**
* The EOS date for 4.4. and beyond
* The EOS date for 4.4.
*
* @var string
* @since __DEPLOY_VERSION__
*/
private const EOS_DATE = '2025-10-25';
private const EOS_DATE = '2025-10-17';

/**
* Load the language file on instantiation.
Expand All @@ -50,21 +50,21 @@ final class Eos extends CMSPlugin implements SubscriberInterface
protected $autoloadLanguage = false;

/**
* Holding the current valid message to be shown
* Holding the current valid message to be shown.
*
* @var array
* @since __DEPLOY_VERSION__
*/
private array $currentMessage = [];
private $currentMessage = [];

/**
* Are the messages initialised
* Are the messages initialized.
*
* @var bool
* @since __DEPLOY_VERSION__
*/

private bool $messagesInitialized = false;
private $messagesInitialized = false;

/**
* Returns an array of events this subscriber will listen to.
Expand All @@ -82,7 +82,7 @@ public static function getSubscribedEvents(): array
}

/**
* Check and show the the alert and quickicon message
* Check and show the the alert and quickicon message.
*
* This method is called when the Quick Icons module is constructing its set
* of icons. You can return an array which defines a single icon and it will
Expand All @@ -98,25 +98,42 @@ public static function getSubscribedEvents(): array
*/
public function getEndOfServiceNotification(QuickIconsEvent $event): void
{
if ($event->getContext() !== $this->params->get('context', 'update_quickicon') || !$this->shouldDisplayMessage() || !$this->messagesInitialized && $this->setMessage() == []) {
$app = $this->getApplication();

if (
$event->getContext() !== $this->params->get('context', 'update_quickicon')
|| !$this->shouldDisplayMessage()
|| (!$this->messagesInitialized && $this->setMessage() == [])
|| !$app instanceof CMSWebApplicationInterface
) {
return;
}

$this->loadLanguage();

// Show this only when not snoozed
if ($this->params->get('last_snoozed_id', 0) < $this->currentMessage['id']) {
// Build the message to be displayed in the cpanel
$messageText = Text::sprintf($this->currentMessage['messageText'], HTMLHelper::_('date', Eos::EOS_DATE, Text::_('DATE_FORMAT_LC3')), $this->currentMessage['messageLink']);
$messageText = sprintf(
$app->getLanguage()->_($this->currentMessage['messageText']),
HTMLHelper::_('date', Eos::EOS_DATE, $app->getLanguage()->_('DATE_FORMAT_LC3')),
$this->currentMessage['messageLink']
);
if ($this->currentMessage['snoozable']) {
$messageText .= '<p><button class="btn btn-warning eosnotify-snooze-btn" type="button" >' . Text::_('PLG_QUICKICON_EOS_SNOOZE_BUTTON') . '</button></p>';
$messageText .= '<p><button class="btn btn-warning eosnotify-snooze-btn" type="button" >';
$messageText .= $app->getLanguage()->_('PLG_QUICKICON_EOS_SNOOZE_BUTTON') . '</button></p>';
}
$this->getApplication()->enqueueMessage($messageText, $this->currentMessage['messageType']);
$app->enqueueMessage($messageText, $this->currentMessage['messageType']);
}

$this->getApplication()->getDocument()->getWebAssetManager()->registerAndUseScript('plg_quickicon_eos.script', 'plg_quickicon_eos/snooze.js', [], ['type' => 'module']);
$app->getDocument()->getWebAssetManager()
->registerAndUseScript('plg_quickicon_eos.script', 'plg_quickicon_eos/snooze.js', [], ['type' => 'module']);

$result = $event->getArgument('result', []);
$messageTextQuickIcon = Text::sprintf($this->currentMessage['quickiconText'], HTMLHelper::_('date', Eos::EOS_DATE, Text::_('DATE_FORMAT_LC3')));
$messageTextQuickIcon = sprintf(
$app->getLanguage()->_($this->currentMessage['quickiconText']),
HTMLHelper::_('date', Eos::EOS_DATE, $app->getLanguage()->_('DATE_FORMAT_LC3'))
);

// The message as quickicon

Expand All @@ -135,7 +152,7 @@ public function getEndOfServiceNotification(QuickIconsEvent $event): void
}

/**
* Save the plugin parameters
* Save the plugin parameters.
*
* @return bool
*
Expand All @@ -157,7 +174,7 @@ private function saveParams(): bool
}

/**
* Determines if the message and quickicon should be displayed
* Determines if the message and quickicon should be displayed.
*
* @return bool
*
Expand All @@ -167,16 +184,18 @@ private function saveParams(): bool
*/
private function shouldDisplayMessage(): bool
{
return !$this->getApplication()->isClient('administrator')
|| $this->getApplication()->getIdentity()->guest
|| $this->getApplication()->getDocument()->getType() !== 'html'
|| $this->getApplication()->getInput()->getCmd('tmpl', 'index') === 'component'
|| $this->getApplication()->getInput()->get('option') !== 'com_cpanel'
? false : true;
// Show only on administration part
return $this->getApplication()->isClient('administrator')
// Only show for HTML requests
&& $this->getApplication()->getDocument()->getType() === 'html'
// Don't show in modal
&& $this->getApplication()->getInput()->getCmd('tmpl', 'index') !== 'component'
// Only show in cpanel
&& $this->getApplication()->getInput()->get('option') === 'com_cpanel';
}

/**
* Return the texts to be displayed based on the time until we reach EOS
* Return the texts to be displayed based on the time until we reach EOS.
*
* @param int $monthsUntilEOS The months until we reach EOS
* @param int $inverted Have we surpassed the EOS date
Expand All @@ -200,6 +219,7 @@ private function getMessageInfo(int $monthsUntilEOS, int $inverted): array
'snoozable' => false,
];
}

// The security support is ending in 6 months
if ($monthsUntilEOS < 6) {
return [
Expand All @@ -213,6 +233,7 @@ private function getMessageInfo(int $monthsUntilEOS, int $inverted): array
'snoozable' => true,
];
}

// We are in security only mode now, 12 month to go from now on
if ($monthsUntilEOS < 12) {
return [
Expand All @@ -226,6 +247,7 @@ private function getMessageInfo(int $monthsUntilEOS, int $inverted): array
'snoozable' => true,
];
}

// We still have 16 month to go, lets remind our users about the pre upgrade checker
if ($monthsUntilEOS < 16) {
return [
Expand All @@ -239,6 +261,7 @@ private function getMessageInfo(int $monthsUntilEOS, int $inverted): array
'snoozable' => true,
];
}

// Lets start our messages 2 month after the initial release, still 22 month to go
if ($monthsUntilEOS < 22) {
return [
Expand All @@ -257,7 +280,7 @@ private function getMessageInfo(int $monthsUntilEOS, int $inverted): array
}

/**
* Check if current user is allowed to send the data
* Check if current user is allowed to send the data.
*
* @return bool
*
Expand All @@ -271,13 +294,13 @@ private function isAllowedUser(): bool
}

/**
* User hit the snooze button
* User hit the snooze button.
*
* @return string
*
* @since __DEPLOY_VERSION__
*
* @throws Notallowed If user is not allowed.
* @throws Notallowed If user is not allowed
*
* @throws Exception
*/
Expand All @@ -287,9 +310,11 @@ public function onAjaxEos(): string
if (!$this->messagesInitialized && $this->setMessage() == []) {
return '';
}

if (!$this->isAllowedUser()) {
throw new Notallowed(Text::_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
throw new Notallowed($this->getApplication()->getLanguage()->_('JGLOBAL_AUTH_ACCESS_DENIED'), 403);
}

// Make sure only snoozable messages can be snoozed
if ($this->currentMessage['snoozable']) {
$this->params->set('last_snoozed_id', $this->currentMessage['id']);
Expand All @@ -300,9 +325,7 @@ public function onAjaxEos(): string
}

/**
* setMessage
*
* Calculates how many days and selects correct message
* Calculates how many days and selects correct message.
*
* @return array
*
Expand Down

0 comments on commit 18a29a6

Please sign in to comment.