PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago MeasurableSetting.php 6 years ago MeasurableSettings.php 6 years ago
MeasurableSettings.php
142 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Settings\Measurable;
10
11 use Piwik\Db;
12 use Piwik\Piwik;
13 use Piwik\Settings\Settings;
14 use Piwik\Settings\Storage;
15 use Piwik\Site;
16 use Exception;
17
18 /**
19 * Base class of all measurable settings providers. Plugins that define their own configuration settings
20 * can extend this class to easily make their measurable settings available to Piwik users.
21 *
22 * Descendants of this class should implement the {@link init()} method and call the
23 * {@link makeSetting()} method for each of the measurable's settings.
24 *
25 * For an example, see the {@link Piwik\Plugins\ExampleSettingsPlugin\MeasurableSettings} plugin.
26 *
27 * $settingsProvider = new Piwik\Plugin\SettingsProvider(); // get this instance via dependency injection
28 * $measurableSettings = $settingProvider->getMeasurableSettings($yourPluginName, $idsite, $idType = null);
29 * $measurableSettings->yourSetting->getValue();
30 *
31 * @api
32 */
33 abstract class MeasurableSettings extends Settings
34 {
35 /**
36 * @var int
37 */
38 protected $idSite;
39
40 /**
41 * @var string
42 */
43 protected $idMeasurableType;
44
45 /**
46 * Constructor.
47 * @param int $idSite If creating settings for a new site that is not created yet, use idSite = 0
48 * @param string|null $idMeasurableType If null, idType will be detected from idSite
49 * @throws Exception
50 */
51 public function __construct($idSite, $idMeasurableType = null)
52 {
53 parent::__construct();
54
55 $this->idSite = (int) $idSite;
56
57 if (!empty($idMeasurableType)) {
58 $this->idMeasurableType = $idMeasurableType;
59 } elseif (!empty($idSite)) {
60 $this->idMeasurableType = Site::getTypeFor($idSite);
61 } else {
62 throw new Exception('No idType specified for ' . get_class($this));
63 }
64
65 $this->init();
66 }
67
68 protected function hasMeasurableType($typeId)
69 {
70 return $typeId === $this->idMeasurableType;
71 }
72
73 /**
74 * Creates a new measurable setting.
75 *
76 * Settings will be displayed in the UI depending on the order of `makeSetting` calls. This means you can define
77 * the order of the displayed settings by calling makeSetting first for more important settings.
78 *
79 * @param string $name The name of the setting that shall be created
80 * @param mixed $defaultValue The default value for this setting. Note the value will not be converted to the
81 * specified type.
82 * @param string $type The PHP internal type the value of this setting should have.
83 * Use one of FieldConfig::TYPE_* constancts
84 * @param \Closure $fieldConfigCallback A callback method to configure the field that shall be displayed in the
85 * UI to define the value for this setting
86 * @return MeasurableSetting Returns an instance of the created measurable setting.
87 * @throws Exception
88 */
89 protected function makeSetting($name, $defaultValue, $type, $fieldConfigCallback)
90 {
91 $setting = new MeasurableSetting($name, $defaultValue, $type, $this->pluginName, $this->idSite);
92 $setting->setConfigureCallback($fieldConfigCallback);
93
94 $this->addSetting($setting);
95
96 return $setting;
97 }
98
99 /**
100 * @internal
101 * @param $name
102 * @param $defaultValue
103 * @param $type
104 * @param $configureCallback
105 * @return MeasurableProperty
106 * @throws Exception
107 */
108 protected function makeProperty($name, $defaultValue, $type, $configureCallback)
109 {
110 $setting = new MeasurableProperty($name, $defaultValue, $type, $this->pluginName, $this->idSite);
111 $setting->setConfigureCallback($configureCallback);
112
113 $this->addSetting($setting);
114
115 return $setting;
116 }
117
118 /**
119 * Saves (persists) the current measurable setting values in the database.
120 *
121 * Will trigger an event to notify plugins that a value has been changed.
122 */
123 public function save()
124 {
125 parent::save();
126
127 /**
128 * Triggered after a plugin settings have been updated.
129 *
130 * **Example**
131 *
132 * Piwik::addAction('MeasurableSettings.updated', function (MeasurableSettings $settings) {
133 * $value = $settings->someSetting->getValue();
134 * // Do something with the new setting value
135 * });
136 *
137 * @param Settings $settings The plugin settings object.
138 */
139 Piwik::postEvent('MeasurableSettings.updated', array($this, $this->idSite));
140 }
141 }
142