From cf13bfcc6ca27f5a73de1542e17cdba3e512b615 Mon Sep 17 00:00:00 2001 From: brettp Date: Tue, 8 Feb 2011 20:30:06 +0000 Subject: Fixes #2869: User settings for plugins work again. git-svn-id: http://code.elgg.org/elgg/trunk@8080 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/classes/ElggPlugin.php | 77 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 10 deletions(-) (limited to 'engine/classes/ElggPlugin.php') 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); } -- cgit v1.2.3