PDF rausgenommen
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\CustomPiwikJs\TrackingCode;
|
||||
|
||||
/**
|
||||
* Used for when running Piwik tracker tests. We simply include all custom tracker files there.
|
||||
*/
|
||||
class JsTestPluginTrackerFiles extends PluginTrackerFiles
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->ignoreMinified = true;
|
||||
}
|
||||
|
||||
protected function isPluginActivated($pluginName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\CustomPiwikJs\TrackingCode;
|
||||
|
||||
use Piwik\Piwik;
|
||||
|
||||
class PiwikJsManipulator
|
||||
{
|
||||
const HOOK = '/*!!! pluginTrackerHook */';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $content;
|
||||
|
||||
/** @var PluginTrackerFiles */
|
||||
private $pluginTrackerFiles;
|
||||
|
||||
public function __construct($content, PluginTrackerFiles $pluginTrackerFiles)
|
||||
{
|
||||
$this->content = $content;
|
||||
$this->pluginTrackerFiles = $pluginTrackerFiles;
|
||||
}
|
||||
|
||||
public function manipulateContent()
|
||||
{
|
||||
$files = $this->pluginTrackerFiles->find();
|
||||
|
||||
foreach ($files as $file) {
|
||||
$trackerExtension = $this->getSignatureWithContent($file->getName(), $file->getContent());
|
||||
|
||||
// for some reasons it is /*!!! in piwik.js minified and /*!! in js/piwik.js unminified
|
||||
$this->content = str_replace(array(self::HOOK, '/*!! pluginTrackerHook */'), self::HOOK . $trackerExtension, $this->content);
|
||||
}
|
||||
|
||||
$content = $this->content;
|
||||
|
||||
/**
|
||||
* Triggered after the Matomo JavaScript tracker has been generated and shortly before the tracker file
|
||||
* is written to disk. You can listen to this event to for example automatically append some code to the JS
|
||||
* tracker file.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* function onManipulateJsTracker (&$content) {
|
||||
* $content .= "\nPiwik.DOM.onLoad(function () { console.log('loaded'); });";
|
||||
* }
|
||||
*
|
||||
* @param string $content the generated JavaScript tracker code
|
||||
*/
|
||||
Piwik::postEvent('CustomMatomoJs.manipulateJsTracker', array(&$content));
|
||||
$this->content = $content;
|
||||
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
private function getSignatureWithContent($name, $content)
|
||||
{
|
||||
return sprintf(
|
||||
"\n\n/* GENERATED: %s */\n%s\n/* END GENERATED: %s */\n",
|
||||
$name,
|
||||
$content,
|
||||
$name
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\CustomPiwikJs\TrackingCode;
|
||||
|
||||
use Piwik\Piwik;
|
||||
use Piwik\Plugin;
|
||||
use Piwik\Plugins\CustomPiwikJs\File;
|
||||
|
||||
class PluginTrackerFiles
|
||||
{
|
||||
const TRACKER_FILE = 'tracker.js';
|
||||
const MIN_TRACKER_FILE = 'tracker.min.js';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $dir;
|
||||
|
||||
/**
|
||||
* @var Plugin\Manager
|
||||
*/
|
||||
private $pluginManager;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $ignoreMinified = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->dir = PIWIK_DOCUMENT_ROOT . '/plugins/';
|
||||
$this->pluginManager = Plugin\Manager::getInstance();
|
||||
}
|
||||
|
||||
public function ignoreMinified()
|
||||
{
|
||||
$this->ignoreMinified = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File[]
|
||||
*/
|
||||
public function find()
|
||||
{
|
||||
$jsFiles = array();
|
||||
|
||||
if (!$this->ignoreMinified) {
|
||||
$trackerFiles = \_glob($this->dir . '*/' . self::MIN_TRACKER_FILE);
|
||||
|
||||
foreach ($trackerFiles as $trackerFile) {
|
||||
$plugin = $this->getPluginNameFromFile($trackerFile);
|
||||
if ($this->isPluginActivated($plugin)) {
|
||||
$jsFiles[$plugin] = new File($trackerFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$trackerFiles = \_glob($this->dir . '*/' . self::TRACKER_FILE);
|
||||
|
||||
foreach ($trackerFiles as $trackerFile) {
|
||||
$plugin = $this->getPluginNameFromFile($trackerFile);
|
||||
if (!isset($jsFiles[$plugin])) {
|
||||
if ($this->isPluginActivated($plugin)) {
|
||||
$jsFiles[$plugin] = new File($trackerFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($jsFiles as $plugin => $file) {
|
||||
if (!$this->shouldIncludeFile($plugin)) {
|
||||
unset($jsFiles[$plugin]);
|
||||
}
|
||||
}
|
||||
|
||||
return $jsFiles;
|
||||
}
|
||||
|
||||
protected function shouldIncludeFile($pluginName)
|
||||
{
|
||||
$shouldAddFile = true;
|
||||
|
||||
/**
|
||||
* Detect if a custom tracker file should be added to the piwik.js tracker or not.
|
||||
*
|
||||
* This is useful for example if a plugin only wants to add its tracker file when the plugin is configured.
|
||||
*
|
||||
* @param bool &$shouldAddFile Decides whether the tracker file belonging to the given plugin should be added or not.
|
||||
* @param string $pluginName The name of the plugin this file belongs to
|
||||
*/
|
||||
Piwik::postEvent('CustomPiwikJs.shouldAddTrackerFile', array(&$shouldAddFile, $pluginName));
|
||||
|
||||
return $shouldAddFile;
|
||||
}
|
||||
|
||||
protected function isPluginActivated($pluginName)
|
||||
{
|
||||
return $this->pluginManager->isPluginActivated($pluginName);
|
||||
}
|
||||
|
||||
protected function getPluginNameFromFile($file)
|
||||
{
|
||||
$file = str_replace(array($this->dir, self::TRACKER_FILE, self::MIN_TRACKER_FILE), '', $file);
|
||||
return trim($file, '/');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user