PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Settings / Measurable / MeasurableSettings.php
matomo / app / core / Settings / Measurable Last commit date
MeasurableProperty.php 1 month ago MeasurableSetting.php 1 month ago MeasurableSettings.php 1 month ago
MeasurableSettings.php
124 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Settings\Measurable;
10
11 use Piwik\Piwik;
12 use Piwik\Settings\Settings;
13 use Piwik\Site;
14 use Exception;
15 /**
16 * Base class of all measurable settings providers. Plugins that define their own configuration settings
17 * can extend this class to easily make their measurable settings available to Piwik users.
18 *
19 * Descendants of this class should implement the {@link init()} method and call the
20 * {@link makeSetting()} method for each of the measurable's settings.
21 *
22 * For an example, see the {@link Piwik\Plugins\ExampleSettingsPlugin\MeasurableSettings} plugin.
23 *
24 * $settingsProvider = new Piwik\Plugin\SettingsProvider(); // get this instance via dependency injection
25 * $measurableSettings = $settingProvider->getMeasurableSettings($yourPluginName, $idsite, $idType = null);
26 * $measurableSettings->yourSetting->getValue();
27 *
28 * @api
29 */
30 abstract class MeasurableSettings extends Settings
31 {
32 /**
33 * @var int
34 */
35 protected $idSite;
36 /**
37 * @var string
38 */
39 protected $idMeasurableType;
40 /**
41 * @param int $idSite If creating settings for a new site that is not created yet, use idSite = 0
42 * @param string|null $idMeasurableType If null, idType will be detected from idSite
43 * @throws Exception
44 */
45 public function __construct($idSite, $idMeasurableType = null)
46 {
47 parent::__construct();
48 $this->idSite = (int) $idSite;
49 if (!empty($idMeasurableType)) {
50 $this->idMeasurableType = $idMeasurableType;
51 } elseif (!empty($idSite)) {
52 $this->idMeasurableType = Site::getTypeFor($idSite);
53 } else {
54 throw new Exception('No idType specified for ' . get_class($this));
55 }
56 $this->init();
57 }
58 protected function hasMeasurableType($typeId)
59 {
60 return $typeId === $this->idMeasurableType;
61 }
62 /**
63 * Creates a new measurable setting.
64 *
65 * Settings will be displayed in the UI depending on the order of `makeSetting` calls. This means you can define
66 * the order of the displayed settings by calling makeSetting first for more important settings.
67 *
68 * @param string $name The name of the setting that shall be created
69 * @param mixed $defaultValue The default value for this setting. Note the value will not be converted to the
70 * specified type.
71 * @param string $type The PHP internal type the value of this setting should have.
72 * Use one of FieldConfig::TYPE_* constants
73 * @param \Closure $fieldConfigCallback A callback method to configure the field that shall be displayed in the
74 * UI to define the value for this setting
75 * @return MeasurableSetting Returns an instance of the created measurable setting.
76 * @throws Exception
77 */
78 protected function makeSetting($name, $defaultValue, $type, $fieldConfigCallback)
79 {
80 $setting = new \Piwik\Settings\Measurable\MeasurableSetting($name, $defaultValue, $type, $this->pluginName, $this->idSite);
81 $setting->setConfigureCallback($fieldConfigCallback);
82 $this->addSetting($setting);
83 return $setting;
84 }
85 /**
86 * @internal
87 * @param $name
88 * @param $defaultValue
89 * @param $type
90 * @param $configureCallback
91 * @return MeasurableProperty
92 * @throws Exception
93 */
94 protected function makeProperty($name, $defaultValue, $type, $configureCallback)
95 {
96 $setting = new \Piwik\Settings\Measurable\MeasurableProperty($name, $defaultValue, $type, $this->pluginName, $this->idSite);
97 $setting->setConfigureCallback($configureCallback);
98 $this->addSetting($setting);
99 return $setting;
100 }
101 /**
102 * Saves (persists) the current measurable setting values in the database.
103 *
104 * Will trigger an event to notify plugins that a value has been changed.
105 */
106 public function save()
107 {
108 parent::save();
109 /**
110 * Triggered after a plugin settings have been updated.
111 *
112 * **Example**
113 *
114 * Piwik::addAction('MeasurableSettings.updated', function (MeasurableSettings $settings) {
115 * $value = $settings->someSetting->getValue();
116 * // Do something with the new setting value
117 * });
118 *
119 * @param Settings $settings The plugin settings object.
120 */
121 Piwik::postEvent('MeasurableSettings.updated', array($this, $this->idSite));
122 }
123 }
124