]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #1468: Cleaned up logic for enabling and disabling plugins. Checking for arrays...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 29 Jan 2010 22:19:22 +0000 (22:19 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 29 Jan 2010 22:19:22 +0000 (22:19 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3859 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/plugins.php

index d7c154b5f2492f552d3add2a4f0e089b82dca6b8..0df6ff03e8b2d632cedf9c7b38276be9299dfd7e 100644 (file)
@@ -622,20 +622,20 @@ function enable_plugin($plugin, $site_guid = 0) {
                throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $site_guid, "ElggSite"));
        }
 
-       $enabled = $site->getMetaData('enabled_plugins');
-       $new_enabled = array();
-       if ($enabled) {
+       // getMetadata() doesn't return an array if only one plugin is enabled
+       if ($enabled = $site->enabled_plugins) {
                if (!is_array($enabled)) {
-                       $new_enabled[] = $enabled;
-               } else {
-                       $new_enabled = $enabled;
+                       $enabled = array($enabled);
                }
+       } else {
+               $enabled = array();
        }
-       $new_enabled[] = $plugin;
-       $new_enabled = array_unique($new_enabled);
 
-       $return = $site->setMetaData('enabled_plugins', $new_enabled);
-       $ENABLED_PLUGINS_CACHE = $new_enabled;
+       $enabled[] = $plugin;
+       $enabled = array_unique($enabled);
+
+       $return = $site->setMetaData('enabled_plugins', $enabled);
+       $ENABLED_PLUGINS_CACHE = $enabled;
 
        return $return;
 }
@@ -666,17 +666,24 @@ function disable_plugin($plugin, $site_guid = 0) {
                throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $site_guid, "ElggSite"));
        }
 
-       $enabled = $site->getMetaData('enabled_plugins');
-       $new_enabled = array();
-
-       foreach ($enabled as $plug) {
-               if ($plugin != $plug) {
-                       $new_enabled[] = $plug;
+       // getMetadata() doesn't return an array if only one plugin is enabled
+       if ($enabled = $site->enabled_plugins) {
+               if (!is_array($enabled)) {
+                       $enabled = array($enabled);
                }
+       } else {
+               $enabled = array();
+       }
+
+       // remove the disabled plugin from the array
+       if (FALSE !== $i = array_search($plugin, $enabled)) {
+               unset($enabled[$i]);
        }
 
-       $return = $site->setMetaData('enabled_plugins', $new_enabled);
-       $ENABLED_PLUGINS_CACHE = $new_enabled;
+       // if we're unsetting all the plugins, this will return an empty array.
+       // it will fail with FALSE, though.
+       $return = (FALSE === $site->enabled_plugins = $enabled) ? FALSE : TRUE;
+       $ENABLED_PLUGINS_CACHE = $enabled;
 
        return $return;
 }
@@ -700,14 +707,17 @@ function is_plugin_enabled($plugin, $site_guid = 0) {
                $site_guid = $CONFIG->site_guid;
        }
 
-
        if (!$ENABLED_PLUGINS_CACHE) {
                $site = get_entity($site_guid);
                if (!($site instanceof ElggSite)) {
                        throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $site_guid, "ElggSite"));
                }
 
-               $ENABLED_PLUGINS_CACHE = $site->enabled_plugins;
+               $enabled_plugins = $site->enabled_plugins;
+               if ($enabled_plugins && !is_array($enabled_plugins)) {
+                       $enabled_plugins = array($enabled_plugins);
+               }
+               $ENABLED_PLUGINS_CACHE = $enabled_plugins;
        }
 
        foreach ($ENABLED_PLUGINS_CACHE as $e) {