Skip to content

Commit

Permalink
feat(plugins): it is now allowed to set arrays as private settings
Browse files Browse the repository at this point in the history
fixes #8081
  • Loading branch information
jdalsem committed Nov 18, 2022
1 parent 3c58dae commit de41523
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 12 deletions.
3 changes: 0 additions & 3 deletions docs/guides/settings.rst
Expand Up @@ -122,9 +122,6 @@ or for group settings:

The ``$plugin_id`` needs to be provided when setting plugin (user)settings.

.. warning::

Plugin settings only supports `scalar <https://www.php.net/manual/en/function.is-scalar.php>`_ values, so no objects or arrays.

Default plugin (group|user) settings
------------------------------------
Expand Down
7 changes: 1 addition & 6 deletions engine/classes/Elgg/Traits/Entity/PluginSettings.php
Expand Up @@ -14,7 +14,7 @@ trait PluginSettings {
*
* @param string $plugin_id plugin ID
* @param string $name setting name
* @param mixed $value setting value (needs to be a scalar)
* @param mixed $value setting value
*
* @return bool
*/
Expand All @@ -26,11 +26,6 @@ public function setPluginSetting(string $plugin_id, string $name, $value): bool
'value' => $value,
], $value);

if (isset($value) && !is_scalar($value)) {
elgg_log("Invalid value type provided to save plugin setting '{$name}' for plugin '{$plugin_id}' only scalars are allowed", 'ERROR');
return false;
}

$name = $this->getNamespacedPluginSettingName($plugin_id, $name);

return $this->setMetadata($name, $value);
Expand Down
Expand Up @@ -83,8 +83,8 @@ public function testCanSetUserSetting() {
$this->assertTrue($user->setPluginSetting('test_plugin', 'foo1', 'bar1'));
$this->assertEquals('bar1', $user->getPluginSetting('test_plugin', 'foo1'));

$this->assertFalse($user->setPluginSetting('test_plugin', 'foo2', ['bar1', 'bar2']));
$this->assertEmpty($user->getPluginSetting('test_plugin', 'foo2'));
$this->assertTrue($user->setPluginSetting('test_plugin', 'foo2', ['bar1', 'bar2']));
$this->assertEquals(['bar1', 'bar2'], $user->getPluginSetting('test_plugin', 'foo2'));

$this->assertTrue($user->setPluginSetting('test_plugin', 'foo3', 'bar3'));

Expand Down
Expand Up @@ -79,6 +79,7 @@ public function setPluginSettingProvider() {
['test_plugin', 'foo', 1.23],
['test_plugin', 'foo', false],
['test_plugin', 'foo', true],
['test_plugin', 'multiple', ['a', 'b']],
];
}

Expand Down Expand Up @@ -110,7 +111,6 @@ public function testUseEventToConvertInvalidPluginSettingValue($invalid_value) {
public function invalidPluginSettingValueProvider() {
return [
[new \stdClass()],
[['a', 'b']],
];
}
}

0 comments on commit de41523

Please sign in to comment.