Skip to content

Commit

Permalink
Introduce a new event to filter sites displayed on all websites dashb…
Browse files Browse the repository at this point in the history
…oard (#22306)

* Introduce a new event to filter sites displayed on all websites dashboard

Co-authored-by: Michal Kleiner <[email protected]>
  • Loading branch information
sgiehl and michalkleiner authored Jun 12, 2024
1 parent 3090b18 commit 88be1b3
Show file tree
Hide file tree
Showing 7 changed files with 1,174 additions and 17 deletions.
48 changes: 31 additions & 17 deletions plugins/MultiSites/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,33 @@ public function getAll($period, $date, $segment = false, $_restrictSitesToLogin
{
Piwik::checkUserHasSomeViewAccess();

$sites = $this->getSitesIdFromPattern($pattern, $_restrictSitesToLogin);
$idSites = $this->getSitesIdFromPattern($pattern, $_restrictSitesToLogin);

/**
* This event can be used to manipulate the sites being displayed on all websites dashboard.
*
* **Example**
*
* Piwik::addAction('MultiSites.filterSites', function (&$idSites) {
* $idSites = array_filter($idSites, function($idSite) {
* return $idSite !== 1
* });
* });
*
* @param array &$idSites List of idSites that the current user would be allowed to see in all websites dashboard.
*/
Piwik::postEvent('MultiSites.filterSites', [&$idSites]);

if (!empty($showColumns) && !is_array($showColumns)) {
$showColumns = explode(',', $showColumns);
}

if (empty($sites)) {
if (empty($idSites)) {
return new DataTable();
}

return $this->buildDataTable(
$sites,
$idSites,
$period,
$date,
$segment,
Expand Down Expand Up @@ -159,8 +174,12 @@ private function getSitesIdFromPattern($pattern, $_restrictSitesToLogin)

// Both calls above have called Site::setSitesFromArray. We now get these sites:
$sitesToProblablyAdd = Site::getSites();
$idSites = [];
foreach ($sitesToProblablyAdd as $site) {
$idSites[] = $site['idsite'];
}

return $sitesToProblablyAdd;
return $idSites;
}

/**
Expand All @@ -180,10 +199,14 @@ public function getOne($idSite, $period, $date, $segment = false, $_restrictSite
{
Piwik::checkUserHasViewAccess($idSite);

$sites = $this->getSiteFromId($idSite);
$site = $this->getSiteFromId($idSite);

if (empty($site)) {
return new DataTable();
}

return $this->buildDataTable(
$sites,
[$idSite],
$period,
$date,
$segment,
Expand Down Expand Up @@ -233,20 +256,11 @@ public function getAllWithGroups($period = null, $date = null, $segment = false,
private function getSiteFromId($idSite)
{
$idSite = (int) $idSite;
$sites = array(APISitesManager::getInstance()->getSiteFromId($idSite));

return $sites;
return APISitesManager::getInstance()->getSiteFromId($idSite);
}

private function buildDataTable($sitesToProblablyAdd, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested, $showColumns)
private function buildDataTable($idSites, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested, $showColumns)
{
$idSites = array();
if (!empty($sitesToProblablyAdd)) {
foreach ($sitesToProblablyAdd as $site) {
$idSites[] = $site['idsite'];
}
}

// build the archive type used to query archive data
$archive = Archive::build(
$idSites,
Expand Down
17 changes: 17 additions & 0 deletions plugins/MultiSites/tests/System/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Piwik\Plugins\MultiSites\tests\System;

use Piwik\Piwik;
use Piwik\Plugins\MultiSites\tests\Fixtures\ManySitesWithVisits;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

Expand Down Expand Up @@ -95,6 +96,22 @@ public function getApiForTesting()
];
}

/**
* @dataProvider getApiForTesting
*/
public function testApiFiltered($api, $params)
{
$params['testSuffix'] .= '_filtered';

Piwik::addAction('MultiSites.filterSites', function (&$idSites) {
$idSites = array_filter($idSites, function ($idSite) {
return $idSite != 2 && $idSite != 10;
});
});

$this->runApiTests($api, $params);
}

public static function getOutputPrefix()
{
return '';
Expand Down
Loading

0 comments on commit 88be1b3

Please sign in to comment.