PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.0
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 2 years ago MeasurableSetting.php 2 years ago MeasurableSettings.php 2 years ago
MeasurableSettings.php
125 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 * Constructor.
42 * @param int $idSite If creating settings for a new site that is not created yet, use idSite = 0
43 * @param string|null $idMeasurableType If null, idType will be detected from idSite
44 * @throws Exception
45 */
46 public function __construct($idSite, $idMeasurableType = null)
47 {
48 parent::__construct();
49 $this->idSite = (int) $idSite;
50 if (!empty($idMeasurableType)) {
51 $this->idMeasurableType = $idMeasurableType;
52 } elseif (!empty($idSite)) {
53 $this->idMeasurableType = Site::getTypeFor($idSite);
54 } else {
55 throw new Exception('No idType specified for ' . get_class($this));
56 }
57 $this->init();
58 }
59 protected function hasMeasurableType($typeId)
60 {
61 return $typeId === $this->idMeasurableType;
62 }
63 /**
64 * Creates a new measurable setting.
65 *
66 * Settings will be displayed in the UI depending on the order of `makeSetting` calls. This means you can define
67 * the order of the displayed settings by calling makeSetting first for more important settings.
68 *
69 * @param string $name The name of the setting that shall be created
70 * @param mixed $defaultValue The default value for this setting. Note the value will not be converted to the
71 * specified type.
72 * @param string $type The PHP internal type the value of this setting should have.
73 * Use one of FieldConfig::TYPE_* constants
74 * @param \Closure $fieldConfigCallback A callback method to configure the field that shall be displayed in the
75 * UI to define the value for this setting
76 * @return MeasurableSetting Returns an instance of the created measurable setting.
77 * @throws Exception
78 */
79 protected function makeSetting($name, $defaultValue, $type, $fieldConfigCallback)
80 {
81 $setting = new \Piwik\Settings\Measurable\MeasurableSetting($name, $defaultValue, $type, $this->pluginName, $this->idSite);
82 $setting->setConfigureCallback($fieldConfigCallback);
83 $this->addSetting($setting);
84 return $setting;
85 }
86 /**
87 * @internal
88 * @param $name
89 * @param $defaultValue
90 * @param $type
91 * @param $configureCallback
92 * @return MeasurableProperty
93 * @throws Exception
94 */
95 protected function makeProperty($name, $defaultValue, $type, $configureCallback)
96 {
97 $setting = new \Piwik\Settings\Measurable\MeasurableProperty($name, $defaultValue, $type, $this->pluginName, $this->idSite);
98 $setting->setConfigureCallback($configureCallback);
99 $this->addSetting($setting);
100 return $setting;
101 }
102 /**
103 * Saves (persists) the current measurable setting values in the database.
104 *
105 * Will trigger an event to notify plugins that a value has been changed.
106 */
107 public function save()
108 {
109 parent::save();
110 /**
111 * Triggered after a plugin settings have been updated.
112 *
113 * **Example**
114 *
115 * Piwik::addAction('MeasurableSettings.updated', function (MeasurableSettings $settings) {
116 * $value = $settings->someSetting->getValue();
117 * // Do something with the new setting value
118 * });
119 *
120 * @param Settings $settings The plugin settings object.
121 */
122 Piwik::postEvent('MeasurableSettings.updated', array($this, $this->idSite));
123 }
124 }
125