Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make feature gates individually dismissible per user (4 Backport) #21590

Merged
merged 20 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/CoreVue/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ declare global {
sendContentAsDownload(filename: string, content: any, mimeType?: string): void;
showVisitorProfilePopup(visitorId: string, idSite: string|number): void;
hideAjaxError(): void;
showAjaxLoading(loadingDivID?: string): void;
hideAjaxLoading(loadingDivID?: string): void;
refreshAfter(timeoutPeriod: number): void;
}

Expand Down
49 changes: 49 additions & 0 deletions plugins/ProfessionalServices/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/

namespace Piwik\Plugins\ProfessionalServices;

use Piwik\Piwik;
use Piwik\Plugins\ProfessionalServices\Widgets\DismissibleWidget;

class API extends \Piwik\Plugin\API
{
/**
* @var PromoWidgetDismissal
*/
private $promoWidgetDismissal;

public function __construct(PromoWidgetDismissal $promoWidgetDismissal)
{
$this->promoWidgetDismissal = $promoWidgetDismissal;
}

/**
* Dismisses a promo widget to no longer be shown in the menu
*
* @internal
*
* @param string $widgetName
* @return bool
* @throws \Piwik\NoAccessException
*/
public function dismissWidget(string $widgetName): bool
{
Piwik::checkUserIsNotAnonymous();

if (!DismissibleWidget::exists($widgetName)) {
throw new \Exception('Can\'t dismiss unknown widget ' . $widgetName);
}

$this->promoWidgetDismissal->dismissPromoWidget($widgetName);

return true;
}
}
15 changes: 14 additions & 1 deletion plugins/ProfessionalServices/ProfessionalServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function registerEvents()
'Template.afterVisitorProfileOverview' => 'getSessionRecordingPromo',
'Template.afterPagePerformanceReport' => 'getSeoWebVitalsPromo',
'Template.afterSearchEngines' => 'getSeoWebVitalsPromo',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
);
}

Expand All @@ -43,6 +44,19 @@ public function getStylesheetFiles(&$stylesheets)
$stylesheets[] = 'plugins/ProfessionalServices/stylesheets/widget.less';
}

public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = 'ProfessionalServices_DismissedNotification';
$translationKeys[] = 'ProfessionalServices_PromoFunnels';
$translationKeys[] = 'ProfessionalServices_PromoFormAnalytics';
$translationKeys[] = 'ProfessionalServices_PromoMediaAnalytics';
$translationKeys[] = 'ProfessionalServices_PromoAbTesting';
$translationKeys[] = 'ProfessionalServices_PromoHeatmaps';
$translationKeys[] = 'ProfessionalServices_PromoSessionRecording';
$translationKeys[] = 'ProfessionalServices_PromoCustomReports';
$translationKeys[] = 'ProfessionalServices_PromoCrashAnalytics';
}

public function isRequestForDashboardWidget()
{
$isWidget = Common::getRequestVar('widget', 0, 'int');
Expand Down Expand Up @@ -157,5 +171,4 @@ public function getSeoWebVitalsPromo(&$out)
$out .= $view->render();
}
}

}
14 changes: 12 additions & 2 deletions plugins/ProfessionalServices/PromoWidgetApplicable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ class PromoWidgetApplicable
*/
private $config;

public function __construct(Manager $manager, Config $config)
/**
* @var PromoWidgetDismissal
*/
private $promoWidgetDismissal;

public function __construct(Manager $manager, Config $config, PromoWidgetDismissal $promoWidgetDismissal)
{
$this->manager = $manager;
$this->config = $config;
$this->promoWidgetDismissal = $promoWidgetDismissal;
}

public function check(string $pluginName): bool
public function check(string $pluginName, string $widgetName): bool
{
if (Advertising::isAdsEnabledInConfig($this->config->General) === false) {
return false;
Expand All @@ -44,6 +50,10 @@ public function check(string $pluginName): bool
return false;
}

if ($this->promoWidgetDismissal->isPromoWidgetDismissedForCurrentUser($widgetName)) {
return false;
}

return $this->manager->isPluginActivated($pluginName) === false;
}
}
44 changes: 44 additions & 0 deletions plugins/ProfessionalServices/PromoWidgetDismissal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\ProfessionalServices;

use Piwik\Option;
use Piwik\Piwik;

class PromoWidgetDismissal
{
private const DISMISSED_WIDGET_OPTION_NAME = 'ProfessionalServices.DismissedWidget.%s.%s';

public function dismissPromoWidget(string $widgetName): void
{
Option::set($this->getDismissedWidgetOptionName($widgetName), time());
}

public function isPromoWidgetDismissedForCurrentUser(string $widgetName): bool
{
$isAnonUser = Piwik::isUserIsAnonymous();

if ($isAnonUser) {
return false;
}

return $this->isPromoWidgetDismissed($widgetName);
}

private function isPromoWidgetDismissed(string $widgetName): bool
{
return Option::get($this->getDismissedWidgetOptionName($widgetName)) > 0;
}

private function getDismissedWidgetOptionName(string $widgetName): string
{
return sprintf(self::DISMISSED_WIDGET_OPTION_NAME, $widgetName, Piwik::getCurrentUserLogin());
}
}
24 changes: 24 additions & 0 deletions plugins/ProfessionalServices/Widgets/DismissibleWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\ProfessionalServices\Widgets;

use Piwik\Widget\Widget;

abstract class DismissibleWidget extends Widget
{
public static function getDismissibleWidgetName(): string
{
return substr(strrchr(static::class, "\\"), 1);
}

public static function exists(string $widgetName): bool
{
return class_exists(substr(__CLASS__, 0, strrpos(__CLASS__, "\\")) . "\\" . $widgetName);
}
}
8 changes: 5 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoAbTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoAbTesting extends Widget
class PromoAbTesting extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'AbTesting';

public static function configure(WidgetConfig $config)
{
parent::configure($config);
$config->setCategoryId('ProfessionalServices_PromoAbTesting');
$config->setSubcategoryId('ProfessionalServices_PromoOverview');
$config->setIsNotWidgetizable();

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +37,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', $pluginInfo['displayName']);
$view->listOfFeatures = [
Expand Down
7 changes: 4 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoCrashAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoCrashAnalytics extends Widget
class PromoCrashAnalytics extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'CrashAnalytics';

Expand All @@ -26,7 +25,7 @@ public static function configure(WidgetConfig $config)

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +36,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', $pluginInfo['displayName']);
$view->listOfFeatures = [
Expand Down
7 changes: 4 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoCustomReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoCustomReports extends Widget
class PromoCustomReports extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'CustomReports';

Expand All @@ -26,7 +25,7 @@ public static function configure(WidgetConfig $config)

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +36,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', $pluginInfo['displayName']);
$view->listOfFeatures = [
Expand Down
7 changes: 4 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoFormAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoFormAnalytics extends Widget
class PromoFormAnalytics extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'FormAnalytics';

Expand All @@ -26,7 +25,7 @@ public static function configure(WidgetConfig $config)

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +36,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', $pluginInfo['displayName']);
$view->listOfFeatures = [
Expand Down
7 changes: 4 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoFunnels.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoFunnels extends Widget
class PromoFunnels extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'Funnels';

Expand All @@ -26,7 +25,7 @@ public static function configure(WidgetConfig $config)

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +36,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', $pluginInfo['displayName']);
$view->listOfFeatures = [
Expand Down
7 changes: 4 additions & 3 deletions plugins/ProfessionalServices/Widgets/PromoHeatmaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\View;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;

class PromoHeatmaps extends Widget
class PromoHeatmaps extends DismissibleWidget
{
private const PROMO_PLUGIN_NAME = 'HeatmapSessionRecording';

Expand All @@ -26,7 +25,7 @@ public static function configure(WidgetConfig $config)

$promoWidgetApplicable = StaticContainer::get('Piwik\Plugins\ProfessionalServices\PromoWidgetApplicable');

$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME);
$isEnabled = $promoWidgetApplicable->check(self::PROMO_PLUGIN_NAME, self::getDismissibleWidgetName());
$config->setIsEnabled($isEnabled);
}

Expand All @@ -37,6 +36,8 @@ public function render()

$view = new View('@ProfessionalServices/pluginAdvertising');
$view->plugin = $pluginInfo;
$view->widgetName = self::getDismissibleWidgetName();
$view->userCanDismiss = Piwik::isUserIsAnonymous() === false;

$view->title = Piwik::translate('ProfessionalServices_PromoUnlockPowerOf', 'Heatmaps');
$view->imageName = 'ad-heatmaps.png';
Expand Down
Loading
Loading