aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-08 20:30:06 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-08 20:30:06 +0000
commitcf13bfcc6ca27f5a73de1542e17cdba3e512b615 (patch)
tree902ba1602850a5cc0da8cbbaee51cc8ef6d1aa9b
parentccb6d625239b59b283b4d3fc00da704ab743d74c (diff)
downloadelgg-cf13bfcc6ca27f5a73de1542e17cdba3e512b615.tar.gz
elgg-cf13bfcc6ca27f5a73de1542e17cdba3e512b615.tar.bz2
Fixes #2869: User settings for plugins work again.
git-svn-id: http://code.elgg.org/elgg/trunk@8080 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/classes/ElggPlugin.php77
-rw-r--r--engine/lib/plugins.php79
2 files changed, 90 insertions, 66 deletions
diff --git a/engine/classes/ElggPlugin.php b/engine/classes/ElggPlugin.php
index b82e92fba..ec28c8064 100644
--- a/engine/classes/ElggPlugin.php
+++ b/engine/classes/ElggPlugin.php
@@ -323,42 +323,99 @@ class ElggPlugin extends ElggObject {
/**
* Returns a user's setting for this plugin
*
- * @param int $user_guid The user GUID
* @param string $name The setting name
+ * @param int $user_guid The user GUID
*
* @return mixed The setting string value or false
*/
- public function getUserSetting($user_guid, $name) {
+ public function getUserSetting($name, $user_guid = null) {
+ $user_guid = (int)$user_guid;
+
+ if ($user_guid) {
+ $user = get_entity($user_guid);
+ } else {
+ $user = elgg_get_logged_in_user_entity();
+ }
+
+ if (!($user instanceof ElggUser)) {
+ return false;
+ }
+
$name = elgg_namespace_plugin_private_setting('user_setting', $name, $this->getID());
- return get_private_setting($user_guid, $name);
+ return get_private_setting($user->guid, $name);
}
/**
* Sets a user setting for a plugin
*
- * @param int $user_guid The user GUID
* @param string $name The setting name
* @param string $value The setting value
+ * @param int $user_guid The user GUID
*
* @return mixed The new setting ID or false
*/
- public function setUserSetting($user_guid, $name, $value) {
+ public function setUserSetting($name, $value, $user_guid = null) {
+ $user_guid = (int)$user_guid;
+
+ if ($user_guid) {
+ $user = get_entity($user_guid);
+ } else {
+ $user = elgg_get_logged_in_user_entity();
+ }
+
+ if (!($user instanceof ElggUser)) {
+ return false;
+ }
+
+ // Hook to validate setting
+ // note this doesn't pass the namespaced name!
+ $value = elgg_trigger_plugin_hook('plugin:usersetting', 'user', array(
+ 'user' => $user,
+ 'plugin' => $this->getID(),
+ 'name' => $name,
+ 'value' => $value
+ ), $value);
+
+ // set the namespaced name.
$name = elgg_namespace_plugin_private_setting('user_setting', $name, $this->getID());
- return set_private_setting($user_guid, $name, $value);
+
+ return set_private_setting($user->guid, $name, $value);
}
/**
* Removes a user setting name and value.
*
- * @param int $user_guid The user GUID
* @param string $name The user setting name
- *
+ * @param int $user_guid The user GUID
* @return bool
*/
- public function removeUserSetting($user_guid, $name) {
+ public function removeUserSetting($name, $user_guid = null) {
+ $user_guid = (int)$user_guid;
+
+ if ($user_guid) {
+ $user = get_entity($user_guid);
+ } else {
+ $user = elgg_get_logged_in_user_entity();
+ }
+
+ if (!($user instanceof ElggUser)) {
+ return false;
+ }
+
+ // Hook to validate setting
+ // note this doesn't pass the namespaced name!
+ $value = elgg_trigger_plugin_hook('plugin:usersetting', 'user', array(
+ 'user' => $user,
+ 'plugin' => $this->getID(),
+ 'name' => $name,
+ 'value' => $value
+ ), $value);
+
+ // set the namespaced name.
$name = elgg_namespace_plugin_private_setting('user_setting', $name, $this->getID());
- return remove_private_setting($user_guid, $name);
+
+ return remove_private_setting($user->guid, $name);
}
diff --git a/engine/lib/plugins.php b/engine/lib/plugins.php
index 85f821315..f8177a5f7 100644
--- a/engine/lib/plugins.php
+++ b/engine/lib/plugins.php
@@ -761,7 +761,7 @@ function elgg_get_calling_plugin_entity() {
* @param string $plugin_id Plugin name.
* @param int $user_guid The guid who's settings to retrieve.
*
- * @return array of settings in an associative array minus prefix.
+ * @return StdClass Object with all user settings.
*/
function find_plugin_usersettings($plugin_id = null, $user_guid = 0) {
$plugin_id = sanitise_string($plugin_id);
@@ -815,34 +815,17 @@ function find_plugin_usersettings($plugin_id = null, $user_guid = 0) {
* @return bool
*/
function set_plugin_usersetting($name, $value, $user_guid = 0, $plugin_id = "") {
- $plugin_id = sanitise_string($plugin_id);
- $user_guid = (int)$user_guid;
- $name = sanitise_string($name);
-
- if (!$plugin_id) {
- $plugin_id = elgg_get_calling_plugin_id();
- }
-
- $user = get_entity($user_guid);
- if (!$user) {
- $user = elgg_get_logged_in_user_entity();
+ if ($plugin_id) {
+ $plugin = elgg_get_plugin_from_id($plugin_id);
+ } else {
+ $plugin = elgg_get_calling_plugin_entity();
}
- if (($user) && ($user instanceof ElggUser)) {
- $name = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name");
-
- // Hook to validate setting
- $value = elgg_trigger_plugin_hook('plugin:usersetting', 'user', array(
- 'user' => $user,
- 'plugin' => $plugin_id,
- 'name' => $name,
- 'value' => $value
- ), $value);
-
- return set_private_setting($user->guid, $name, $value);
+ if (!$plugin) {
+ return false;
}
- return false;
+ return $plugin->setUserSetting($name, $value, $user_guid);
}
/**
@@ -855,25 +838,17 @@ function set_plugin_usersetting($name, $value, $user_guid = 0, $plugin_id = "")
* @return bool Success
*/
function clear_plugin_usersetting($name, $user_guid = 0, $plugin_id = '') {
- $plugin_id = sanitise_string($plugin_id);
- $name = sanitise_string($name);
-
- if (!$plugin_id) {
- $plugin_id = elgg_get_calling_plugin_id();
- }
-
- $user = get_entity((int) $user_guid);
- if (!$user) {
- $user = elgg_get_logged_in_user_entity();
+ if ($plugin_id) {
+ $plugin = elgg_get_plugin_from_id($plugin_id);
+ } else {
+ $plugin = elgg_get_calling_plugin_entity();
}
- if (($user) && ($user instanceof ElggUser)) {
- $prefix = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name");
-
- return remove_private_setting($user->getGUID(), $prefix);
+ if (!$plugin) {
+ return false;
}
- return FALSE;
+ return $plugin->removeUserSetting($user_guid, $name);
}
/**
@@ -882,30 +857,22 @@ function clear_plugin_usersetting($name, $user_guid = 0, $plugin_id = '') {
* @param string $name The name.
* @param int $user_guid Guid of owning user
* @param string $plugin_id Optional plugin name, if not specified
- * then it is detected from where you are calling from.
+ * it is detected from where you are calling.
*
* @return mixed
*/
function get_plugin_usersetting($name, $user_guid = 0, $plugin_id = "") {
- $plugin_id = sanitise_string($plugin_id);
- $user_guid = (int)$user_guid;
- $name = sanitise_string($name);
-
- if (!$plugin_id) {
- $plugin_id = elgg_get_calling_plugin_id();
- }
-
- $user = get_entity($user_guid);
- if (!$user) {
- $user = elgg_get_logged_in_user_entity();
+ if ($plugin_id) {
+ $plugin = elgg_get_plugin_from_id($plugin_id);
+ } else {
+ $plugin = elgg_get_calling_plugin_entity();
}
- if (($user) && ($user instanceof ElggUser)) {
- $name = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name");
- return get_private_setting($user->guid, $name);
+ if (!$plugin) {
+ return false;
}
- return false;
+ return $plugin->getUserSetting($name, $user_guid);
}
/**