Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Jul 21, 2023
1 parent 675ca15 commit cb8dd85
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 28 deletions.
19 changes: 15 additions & 4 deletions core/SiteContentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class SiteContentDetector
{
/**
* @var array<string, ?array<string, SiteContentDetectionAbstract>>
* @var array<string, array<string, SiteContentDetectionAbstract>>
*/
public $detectedContent = [
SiteContentDetectionAbstract::TYPE_TRACKER => [],
Expand Down Expand Up @@ -82,6 +82,8 @@ public static function getSiteContentDetectionsByType()
}

/**
* Returns the site content detection object with the provided id, or null if it can't be found
*
* @param string $id
* @return SiteContentDetectionAbstract|null
*/
Expand Down Expand Up @@ -128,7 +130,11 @@ private function resetDetections(): void
* the details of the detected content
*
* @param array $detectContent Array of content type for which to check, defaults to all, limiting this list
* will speed up the detection check
* will speed up the detection check.
* Allowed values are:
* * empty array - to run all detections
* * an array containing ids of detections, e.g. Wordpress::getId() or any of the
* type constants, e.g. SiteContentDetectionAbstract::TYPE_TRACKER
* @param ?int $idSite Override the site ID, will use the site from the current request if null
* @param ?array $siteResponse String containing the site data to search, if blank then data will be retrieved
* from the current request site via an http request
Expand Down Expand Up @@ -207,6 +213,12 @@ public function wasDetected(string $detectionClassId): bool
return false;
}

/**
* Returns an array containing ids of all detected detections of the given type
*
* @param string $type One of the SiteContentDetectionAbstract::TYPE_* constants
* @return array
*/
public function getDetectsByType(string $type): array
{
$detected = [];
Expand All @@ -217,7 +229,6 @@ public function getDetectsByType(string $type): array
}
}


return $detected;
}

Expand Down Expand Up @@ -308,7 +319,7 @@ private function saveToCache(string $cacheKey, int $cacheLife): void
*
* @return void
*/
private function detectionChecks($detectContent): void
private function detectionChecks(array $detectContent): void
{
$detections = $this->getSiteContentDetectionsByType();

Expand Down
2 changes: 1 addition & 1 deletion plugins/Installation/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function trackingCode()

$trackingUrl = trim(SettingsPiwik::getPiwikUrl(), '/') . '/' . $javascriptGenerator->getPhpTrackerEndpoint();

$this->siteContentDetector->detectContent();
$this->siteContentDetector->detectContent([], $idSite);

$emailTemplateData = [
'jsTag' => $rawJsTag,
Expand Down
3 changes: 2 additions & 1 deletion plugins/SitesManager/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function siteWithoutDataTabs()
Piwik::postEvent('SitesManager.showMatomoLinksInTrackingCodeEmail', [&$showMatomoLinks]);

$googleAnalyticsImporterMessage = '';
if (Manager::getInstance()->isPluginLoaded('GoogleAnalyticsImporter')) {
if (!Manager::getInstance()->isPluginLoaded('GoogleAnalyticsImporter')) {
$googleAnalyticsImporterMessage = '<h3>' . Piwik::translate('CoreAdminHome_ImportFromGoogleAnalytics') . '</h3>'
. '<p>' . Piwik::translate('CoreAdminHome_ImportFromGoogleAnalyticsDescription', ['<a href="https://plugins.matomo.org/GoogleAnalyticsImporter" rel="noopener noreferrer" target="_blank">', '</a>']) . '</p>'
. '<p></p>';
Expand Down Expand Up @@ -265,6 +265,7 @@ public function siteWithoutDataTabs()
$instructionUrl = $obj->getInstructionUrl();

Piwik::postEvent('Template.siteWithoutDataTab.' . $obj::getId() . '.content', [&$tabContent]);
Piwik::postEvent('Template.siteWithoutDataTab.' . $obj::getId() . '.others', [&$othersInstruction]);

if (!empty($tabContent) && $obj->shouldShowInstructionTab($this->siteContentDetector)) {
$templateData['tabs'][] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ final public static function getContentType(): string
* @return bool
*/
abstract public function checkIsConnected(?string $data = null, ?array $headers = null): bool;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ public function detectByContent(?string $data = null, ?array $headers = null): b
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ public function detectByContent(?string $data = null, ?array $headers = null): b

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ abstract class SiteContentDetectionAbstract
public const TYPE_CONSENT_MANAGER = 4;
public const TYPE_OTHER = 99;

/**
* Holds the detection state, once it was executed
*
* @var null|bool
*/
protected $wasDetected = null;

public function __construct()
{
}

/**
* Returns the ID of the current detection. Automatically built from the class name (without namespace)
*
* @return string
*/
public static function getId(): string
{
$classParts = explode('\\', static::class);
Expand Down Expand Up @@ -123,4 +121,4 @@ public function renderOthersInstruction(): ?string
{
return null;
}
}
}
2 changes: 0 additions & 2 deletions plugins/SitesManager/SitesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,6 @@ public function getClientSideTranslationKeys(&$translationKeys)
$translationKeys[] = "SitesManager_EmailInstructionsButton";
$translationKeys[] = "SitesManager_EmailInstructionsSubject";
$translationKeys[] = "SitesManager_JsTrackingTagHelp";
$translationKeys[] = "SitesManager_SiteWithoutDataSinglePageApplication";
$translationKeys[] = "SitesManager_SiteWithoutDataSinglePageApplicationDescription";
$translationKeys[] = 'SitesManager_SiteWithoutDataTitle';
$translationKeys[] = 'SitesManager_SiteWithoutDataDescription';
$translationKeys[] = 'SitesManager_SiteWithoutDataMessageDisappears';
Expand Down
4 changes: 2 additions & 2 deletions plugins/SitesManager/templates/_siteWithoutDataTabs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<div class="col s12 tabs-row">
<ul class="tabs no-data-screen-ul-tabs">

<li class="tab col {{ columnClass }}"><a {% if cms|length and (consentManagerName == false) and (activeTab == '') %} class="active" {% endif %} href="#integrations">{{ 'SitesManager_Integrations'|translate }}</a></li>
<li class="tab col {{ columnClass }}"><a {% if (cms|length == 0 and activeTab == '') or activeTab == 'consentManager') %} class="active" {% endif %} href="#tracking-code">{{ 'CoreAdminHome_TrackingCode'|translate }}</a></li>
<li class="tab col {{ columnClass }}"><a {% if cms|length and consentManagerName == false and activeTab == '' %} class="active" {% endif %} href="#integrations">{{ 'SitesManager_Integrations'|translate }}</a></li>
<li class="tab col {{ columnClass }}"><a {% if (cms|length == 0 or consentManagerName) and activeTab == '' %} class="active" {% endif %} href="#tracking-code">{{ 'CoreAdminHome_TrackingCode'|translate }}</a></li>

{% for tab in tabs %}
<li class="tab col {{ columnClass }}"><a href="#{{ tab.id|lower }}"{% if activeTab == tab.id %} class="active"{% endif %}>{{ tab.name }}</a></li>
Expand Down
2 changes: 1 addition & 1 deletion plugins/SitesManager/templates/_trackingCodeEmail.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{ 'SitesManager_GADetectedEmail'|translate('Google Analytics 3', 'GA', 'https://matomo.org/faq/how-to/migrate-from-google-analytics-3-to-matomo/')|raw }}

{% endif %}
{% if 'GoogleAnalytics3' in trackers %}
{% if 'GoogleAnalytics4' in trackers %}
{{ 'SitesManager_GADetectedEmail'|translate('Google Analytics 4', 'GA', 'https://matomo.org/faq/how-to/migrate-from-google-analytics-4-to-matomo/')|raw }}

{% endif %}
Expand Down
1 change: 0 additions & 1 deletion tests/PHPUnit/Fixtures/DisableSiteContentDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
namespace Piwik\Tests\Fixtures;

use Piwik\Plugins\SitesManager\SitesManager;
use Piwik\Tests\Framework\Fixture;
use Piwik\SiteContentDetector;
use Piwik\Tests\Framework\Mock\FakeSiteContentDetector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Piwik\Plugins\SitesManager\SiteContentDetection\Cloudflare;
use Piwik\Plugins\SitesManager\SiteContentDetection\WordPress;
use Piwik\Plugins\SitesManager\SitesManager;
use Piwik\Tests\Framework\Fixture;
use Piwik\SiteContentDetector;
use Piwik\Tests\Framework\Mock\FakeSiteContentDetector;
Expand Down
12 changes: 9 additions & 3 deletions tests/PHPUnit/Integration/SiteContentDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
*/
class SiteContentDetectorTest extends IntegrationTestCase
{


public function testSiteWithMultipleDetections()
{
$scd = new SiteContentDetector();
$scd->detectContent([], null, [
'data' => "<html lang='en'><head><title>A site</title></head><script src='https://localhost.com/js/react.min.js'></script><script src='https://osano.com/uhs9879874hthg.js'></script><script>Osano.cm.addEventListener('osano-cm-consent-changed', (change) => { console.log('cm-change'); consentSet(change); });</script></><body>A site<img src='/wp-content/uploads/images.gif'</body></html>",
'data' => "<html lang='en'>
<head>
<title>A site</title>
<script src='https://localhost.com/js/react.min.js'></script>
<script src='https://osano.com/uhs9879874hthg.js'></script>
<script>Osano.cm.addEventListener('osano-cm-consent-changed', (change) => { console.log('cm-change'); consentSet(change); });</script>
</head>
<body>A site<img src='/wp-content/uploads/images.gif'</body>
</html>",
'headers' => [
'CF-RAY' => 'test'
],
Expand Down

0 comments on commit cb8dd85

Please sign in to comment.