Piwik\
Base class of all Plugin Descriptor classes.
Any plugin that wants to add event observers to one of Matomo (formerly Piwik)'s hooks, or has special installation/uninstallation logic must implement this class. Plugins that can specify everything they need to in the plugin.json files, such as themes, don't need to implement this class.
Class implementations should be named after the plugin they are a part of
(eg, class UserCountry extends Plugin
).
In addition to providing a place for plugins to install/uninstall themselves and add event observers, this class is also responsible for loading metadata found in the plugin.json file.
The plugin.json file must exist in the root directory of a plugin. It can contain the following information:
true
or false
. If true
, the plugin will be treated as a theme.How to extend
use Piwik\Common;
use Piwik\Plugin;
use Piwik\Db;
class MyPlugin extends Plugin
{
public function registerEvents()
{
return array(
'API.getReportMetadata' => 'getReportMetadata',
'Another.event' => array(
'function' => 'myOtherPluginFunction',
'after' => true // executes this callback after others
)
);
}
public function install()
{
Db::exec("CREATE TABLE " . Common::prefixTable('mytable') . "...");
}
public function uninstall()
{
Db::exec("DROP TABLE IF EXISTS " . Common::prefixTable('mytable'));
}
public function getReportMetadata(&$metadata)
{
// ...
}
public function myOtherPluginFunction()
{
// ...
}
}
The class defines the following methods:
__construct()
— Constructor.reloadPluginInformation()
getInformation()
— Returns plugin information, including:isPremiumFeature()
registerEvents()
— Returns a list of events with associated event observers.postLoad()
— This method is executed after a plugin is loaded and translations are registered.requiresInternetConnection()
— Defines whether the whole plugin requires a working internet connection If set to true, the plugin will be automatically unloaded if enable_internet_features
is 0, even if the plugin is activatedinstall()
— Installs the plugin.uninstall()
— Uninstalls the plugins.activate()
— Executed every time the plugin is enabled.deactivate()
— Executed every time the plugin is disabled.getVersion()
— Returns the plugin version number.isTheme()
— Returns true
if this plugin is a theme, false
if otherwise.getPluginName()
ash; Returns the plugin's base class name without the namespace, e.g., "UserCountry"
when the plugin class is "Piwik\Plugins\UserCountry\UserCountry"
.findComponent()
— Tries to find a component such as a Menu or Tasks within this plugin.findMultipleComponents()
hasMissingDependencies()
— Detect whether there are any missing dependencies.getMissingDependencies()
getMissingDependenciesAsString()
— Returns a string (translated) describing the missing requirements for this plugin and the given Matomo versionschedulePluginReArchiving()
— Schedules re-archiving of this plugin's reports from when this plugin was last deactivated to now.getPluginNameFromBacktrace()
— Extracts the plugin name from a backtrace array.getPluginNameFromNamespace()
— Extracts the plugin name from a namespace name or a fully qualified class name.getPluginLastActivationTime()
getPluginLastDeactivationTime()
getChanges()
— Get all changes for this plugin__construct()
Constructor.
$pluginName
(string
|bool
) —
A plugin name to force. If not supplied, it is set to the last part of the class name.Exception
— If plugin metadata is defined in both the getInformation() method
and the plugin.json file.reloadPluginInformation()
getInformation()
Returns plugin information, including:
array
value.isPremiumFeature()
registerEvents()
Since Matomo 2.15.0
Returns a list of events with associated event observers.
Derived classes should use this method to associate callbacks with events.
Returns: array
—
eg,
array(
'API.getReportMetadata' => 'myPluginFunction',
'Another.event' => array(
'function' => 'myOtherPluginFunction',
'after' => true // execute after callbacks w/o ordering
)
'Yet.Another.event' => array(
'function' => 'myOtherPluginFunction',
'before' => true // execute before callbacks w/o ordering
)
)
postLoad()
This method is executed after a plugin is loaded and translations are registered.
Useful for initialization code that uses translated strings.
requiresInternetConnection()
Defines whether the whole plugin requires a working internet connection
If set to true, the plugin will be automatically unloaded if enable_internet_features
is 0,
even if the plugin is activated
bool
value.install()
Installs the plugin. Derived classes should implement this class if the plugin needs to:
Exception
— if installation of fails for some reason.uninstall()
Uninstalls the plugins. Derived classes should implement this method if the changes made in install() need to be undone during uninstallation.
In most cases, if you have an install() method, you should provide an uninstall() method.
Exception
— if uninstallation of fails for some reason.activate()
Executed every time the plugin is enabled.
deactivate()
Executed every time the plugin is disabled.
getVersion()
Returns the plugin version number.
string
value.isTheme()
Returns true
if this plugin is a theme, false
if otherwise.
bool
value.getPluginName()
Returns the plugin's base class name without the namespace,
e.g., "UserCountry"
when the plugin class is "Piwik\Plugins\UserCountry\UserCountry"
.
string
value.findComponent()
Tries to find a component such as a Menu or Tasks within this plugin.
It accepts the following parameter(s):
$componentName
(string
) —
The name of the component you want to look for. In case you request a component named 'Menu' it'll look for a file named 'Menu.php' within the root of the plugin folder that implements a class named Piwik\Plugin\$PluginName\Menu . If such a file exists but does not implement this class it'll silently ignored.$expectedSubclass
(string
) —
If not empty, a check will be performed whether a found file extends the given subclass. If the requested file exists but does not extend this class a warning will be shown to advice a developer to extend this certain class.Returns: string
|null
—
Null if the requested component does not exist or an instance of the found
component.
findMultipleComponents()
It accepts the following parameter(s):
$directoryWithinPlugin
$expectedSubclass
It does not return anything or a mixed result.
hasMissingDependencies()
Detect whether there are any missing dependencies.
$piwikVersion
(null
) —
Defaults to the current Matomo versionbool
value.getMissingDependencies()
It accepts the following parameter(s):
$piwikVersion
It does not return anything or a mixed result.
getMissingDependenciesAsString()
Returns a string (translated) describing the missing requirements for this plugin and the given Matomo version
It accepts the following parameter(s):
$piwikVersion
(string
) —Returns: string
—
"AnonymousPiwikUsageMeasurement requires PIWIK >=3.0.0"
schedulePluginReArchiving()
Schedules re-archiving of this plugin's reports from when this plugin was last deactivated to now. If the last time core:archive was run is earlier than the plugin's last deactivation time, then we use that time instead.
Note: this only works for CLI archiving setups.
Note: the time frame is limited by the [General] rearchive_reports_in_past_last_n_months
INI config value.
DI\DependencyException
DI\NotFoundException
getPluginNameFromBacktrace()
Extracts the plugin name from a backtrace array. Returns false
if we can't find one.
It accepts the following parameter(s):
$backtrace
(array
) —
The result of debug_backtrace() or Exception::getTrace().Returns: string
|false
—
getPluginNameFromNamespace()
Extracts the plugin name from a namespace name or a fully qualified class name. Returns false
if we can't find one.
It accepts the following parameter(s):
$namespaceOrClassName
(string
) —
The namespace or class string.Returns: string
|false
—
getPluginLastActivationTime()
getPluginLastDeactivationTime()
getChanges()
Get all changes for this plugin
array
—
Array of changes
[{"title":"abc","description":"xyz","linkName":"def","link":"https://link","version":"1.2.3"}]