diff options
Diffstat (limited to 'engine/classes/ElggPlugin.php')
| -rw-r--r-- | engine/classes/ElggPlugin.php | 220 |
1 files changed, 135 insertions, 85 deletions
diff --git a/engine/classes/ElggPlugin.php b/engine/classes/ElggPlugin.php index 34e8e6732..545b9a53c 100644 --- a/engine/classes/ElggPlugin.php +++ b/engine/classes/ElggPlugin.php @@ -9,8 +9,8 @@ * @subpackage Plugins.Settings */ class ElggPlugin extends ElggObject { - public $package; - public $manifest; + private $package; + private $manifest; private $path; private $pluginID; @@ -30,15 +30,15 @@ class ElggPlugin extends ElggObject { $this->access_id = ACCESS_PUBLIC; } - /** * Loads the plugin by GUID or path. * * @warning Unlike other ElggEntity objects, you cannot null instantiate * ElggPlugin. You must point it to an actual plugin GUID or location. * - * @param mixed $plugin The GUID of the ElggPlugin object or the path of - * the plugin to load. + * @param mixed $plugin The GUID of the ElggPlugin object or the path of the plugin to load. + * + * @throws PluginException */ public function __construct($plugin) { if (!$plugin) { @@ -78,20 +78,9 @@ class ElggPlugin extends ElggObject { parent::__construct($existing_guid); } - // We have to let the entity load so we can manipulate it with the API. - // If the path is wrong or would cause an exception, catch it, - // disable the plugin, and emit an error. - try { - $this->package = new ElggPluginPackage($this->path, false); - $this->manifest = $this->package->getManifest(); - } catch (Exception $e) { - // we always have to allow the entity to load. - elgg_log("Failed to load $this->guid as a plugin. " . $e->getMessage(), 'WARNING'); - $this->errorMsg = $e->getmessage(); - } + _elgg_cache_plugin_by_id($this); } - /** * Save the plugin object. Make sure required values exist. * @@ -130,6 +119,21 @@ class ElggPlugin extends ElggObject { } /** + * Returns the manifest's name if available, otherwise the ID. + * + * @return string + * @since 1.8.1 + */ + public function getFriendlyName() { + $manifest = $this->getManifest(); + if ($manifest) { + return $manifest->getName(); + } + + return $this->getID(); + } + + /** * Returns the plugin's full path with trailing slash. * * @return string @@ -138,17 +142,33 @@ class ElggPlugin extends ElggObject { return sanitise_filepath($this->path); } - /** * Sets the location of this plugin. * - * @param path $id The path to the plugin's dir. + * @param string $id The path to the plugin's dir. * @return bool */ public function setID($id) { return $this->attributes['title'] = $id; } + /** + * Returns an array of available markdown files for this plugin + * + * @return array + */ + public function getAvailableTextFiles() { + $filenames = $this->getPackage()->getTextFilenames(); + + $files = array(); + foreach ($filenames as $filename) { + if ($this->canReadFile($filename)) { + $files[$filename] = "$this->path/$filename"; + } + } + + return $files; + } // Load Priority @@ -162,7 +182,6 @@ class ElggPlugin extends ElggObject { return $this->$name; } - /** * Sets the priority of the plugin * @@ -248,8 +267,6 @@ class ElggPlugin extends ElggObject { /** * Returns a plugin setting * - * @todo These need to be namespaced - * * @param string $name The setting name * @return mixed */ @@ -282,27 +299,21 @@ class ElggPlugin extends ElggObject { $private_settings = get_data($q); - if ($private_settings) { - $return = array(); + $return = array(); + if ($private_settings) { foreach ($private_settings as $setting) { - $name = substr($setting->name, $ps_prefix_len); - $value = $setting->value; - - $return[$name] = $value; + $return[$setting->name] = $setting->value; } - - return $return; } - return false; + return $return; } /** * Set a plugin setting for the plugin * * @todo This will only work once the plugin has a GUID. - * @todo These need to be namespaced. * * @param string $name The name to set * @param string $value The value to set @@ -313,18 +324,10 @@ class ElggPlugin extends ElggObject { if (!$this->guid) { return false; } - // Hook to validate setting - $value = elgg_trigger_plugin_hook('plugin:setting', 'plugin', array( - 'plugin' => $this->pluginID, - 'plugin_object' => $this, - 'name' => $name, - 'value' => $value - ), $value); return $this->set($name, $value); } - /** * Removes a plugin setting name and value. * @@ -336,7 +339,6 @@ class ElggPlugin extends ElggObject { return remove_private_setting($this->guid, $name); } - /** * Removes all settings for this plugin. * @@ -346,11 +348,14 @@ class ElggPlugin extends ElggObject { */ public function unsetAllSettings() { $db_prefix = get_config('dbprefix'); - $ps_prefix = elgg_namespace_plugin_private_setting('setting', ''); + + $us_prefix = elgg_namespace_plugin_private_setting('user_setting', '', $this->getID()); + $is_prefix = elgg_namespace_plugin_private_setting('internal', '', $this->getID()); $q = "DELETE FROM {$db_prefix}private_settings WHERE entity_guid = $this->guid - AND name NOT LIKE '$ps_prefix%'"; + AND name NOT LIKE '$us_prefix%' + AND name NOT LIKE '$is_prefix%'"; return delete_data($q); } @@ -416,20 +421,18 @@ class ElggPlugin extends ElggObject { $private_settings = get_data($q); - if ($private_settings) { - $return = array(); + $return = array(); + if ($private_settings) { foreach ($private_settings as $setting) { $name = substr($setting->name, $ps_prefix_len); $value = $setting->value; $return[$name] = $value; } - - return $return; } - return false; + return $return; } /** @@ -455,10 +458,11 @@ class ElggPlugin extends ElggObject { } // Hook to validate setting - // note this doesn't pass the namespaced name! - $value = elgg_trigger_plugin_hook('plugin:usersetting', 'user', array( + // note: this doesn't pass the namespaced name + $value = elgg_trigger_plugin_hook('usersetting', 'plugin', array( 'user' => $user, - 'plugin' => $this->getID(), + 'plugin' => $this, + 'plugin_id' => $this->getID(), 'name' => $name, 'value' => $value ), $value); @@ -496,7 +500,6 @@ class ElggPlugin extends ElggObject { return remove_private_setting($user->guid, $name); } - /** * Removes all User Settings for this plugin * @@ -517,7 +520,6 @@ class ElggPlugin extends ElggObject { return delete_data($q); } - /** * Removes this plugin's user settings for all users. * @@ -543,7 +545,7 @@ class ElggPlugin extends ElggObject { * Returns if the plugin is complete, meaning has all required files * and Elgg can read them and they make sense. * - * @todo bad name? This could be confused with isValid() from ElggPackage. + * @todo bad name? This could be confused with isValid() from ElggPluginPackage. * * @return bool */ @@ -553,20 +555,19 @@ class ElggPlugin extends ElggObject { return false; } - if (!$this->package instanceof ElggPluginPackage) { + if (!$this->getPackage() instanceof ElggPluginPackage) { $this->errorMsg = elgg_echo('ElggPlugin:NoPluginPackagePackage', array($this->getID(), $this->guid)); return false; } - if (!$this->package->isValid()) { - $this->errorMsg = $this->package->getError(); + if (!$this->getPackage()->isValid()) { + $this->errorMsg = $this->getPackage()->getError(); return false; } return true; } - /** * Is this plugin active? * @@ -591,17 +592,23 @@ class ElggPlugin extends ElggObject { return check_entity_relationship($this->guid, 'active_plugin', $site->guid); } - /** * Checks if this plugin can be activated on the current * Elgg installation. * + * @todo remove $site_guid param or implement it + * * @param mixed $site_guid Optional site guid * @return bool */ public function canActivate($site_guid = null) { - if ($this->package) { - return $this->package->isValid() && $this->package->checkDependencies(); + if ($this->getPackage()) { + $result = $this->getPackage()->isValid() && $this->getPackage()->checkDependencies(); + if (!$result) { + $this->errorMsg = $this->getPackage()->getError(); + } + + return $result; } return false; @@ -640,9 +647,9 @@ class ElggPlugin extends ElggObject { // if there are any on_enable functions, start the plugin now and run them // Note: this will not run re-run the init hooks! if ($return) { - if ($this->canIncludeFile('activate.php')) { - $flags = ELGG_PLUGIN_INCLUDE_START | ELGG_PLUGIN_REGISTER_CLASSES - | ELGG_PLUGIN_REGISTER_LANGUAGES | ELGG_PLUGIN_REGISTER_VIEWS; + if ($this->canReadFile('activate.php')) { + $flags = ELGG_PLUGIN_INCLUDE_START | ELGG_PLUGIN_REGISTER_CLASSES | + ELGG_PLUGIN_REGISTER_LANGUAGES | ELGG_PLUGIN_REGISTER_VIEWS; $this->start($flags); @@ -660,7 +667,6 @@ class ElggPlugin extends ElggObject { return false; } - /** * Deactivates the plugin. * @@ -682,7 +688,7 @@ class ElggPlugin extends ElggObject { // run any deactivate code if ($return) { - if ($this->canIncludeFile('deactivate.php')) { + if ($this->canReadFile('deactivate.php')) { $return = $this->includeFile('deactivate.php'); } } @@ -694,7 +700,6 @@ class ElggPlugin extends ElggObject { } } - /** * Start the plugin. * @@ -703,10 +708,15 @@ class ElggPlugin extends ElggObject { * @throws PluginException */ public function start($flags) { - if (!$this->canActivate()) { - return false; - } + //if (!$this->canActivate()) { + // return false; + //} + // include classes + if ($flags & ELGG_PLUGIN_REGISTER_CLASSES) { + $this->registerClasses(); + } + // include start file if ($flags & ELGG_PLUGIN_INCLUDE_START) { $this->includeFile('start.php'); @@ -722,11 +732,6 @@ class ElggPlugin extends ElggObject { $this->registerLanguages(); } - // include classes - if ($flags & ELGG_PLUGIN_REGISTER_CLASSES) { - $this->registerClasses(); - } - return true; } @@ -750,7 +755,7 @@ class ElggPlugin extends ElggObject { $filepath = "$this->path/$filename"; - if (!$this->canIncludeFile($filename)) { + if (!$this->canReadFile($filename)) { $msg = elgg_echo('ElggPlugin:Exception:CannotIncludeFile', array($filename, $this->getID(), $this->guid, $this->path)); throw new PluginException($msg); @@ -765,8 +770,8 @@ class ElggPlugin extends ElggObject { * @param string $filename The name of the file * @return bool */ - protected function canIncludeFile($filename) { - return file_exists($this->path.'/'.$filename); + protected function canReadFile($filename) { + return is_readable($this->path . '/' . $filename); } /** @@ -846,12 +851,7 @@ class ElggPlugin extends ElggObject { return true; } - // but need to have working ones. - if (!elgg_register_classes($classes_path)) { - $msg = elgg_echo('ElggPlugin:Exception:CannotRegisterClasses', - array($this->getID(), $this->guid, $classes_path)); - throw new PluginException($msg); - } + elgg_register_classes($classes_path); return true; } @@ -893,7 +893,9 @@ class ElggPlugin extends ElggObject { } /** - * Save a value to private settings. + * Save a value as private setting or attribute. + * + * Attributes include title and description. * * @param string $name Name * @param mixed $value Value @@ -911,6 +913,14 @@ class ElggPlugin extends ElggObject { return true; } else { + // Hook to validate setting + $value = elgg_trigger_plugin_hook('setting', 'plugin', array( + 'plugin_id' => $this->pluginID, + 'plugin' => $this, + 'name' => $name, + 'value' => $value + ), $value); + return $this->setPrivateSetting($name, $value); } } @@ -953,4 +963,44 @@ class ElggPlugin extends ElggObject { public function getError() { return $this->errorMsg; } + + /** + * Returns this plugin's ElggPluginManifest object + * + * @return ElggPluginManifest + */ + public function getManifest() { + if ($this->manifest instanceof ElggPluginManifest) { + return $this->manifest; + } + + try { + $this->manifest = $this->getPackage()->getManifest(); + } catch (Exception $e) { + elgg_log("Failed to load manifest for plugin $this->guid. " . $e->getMessage(), 'WARNING'); + $this->errorMsg = $e->getmessage(); + } + + return $this->manifest; + } + + /** + * Returns this plugin's ElggPluginPackage object + * + * @return ElggPluginPackage + */ + public function getPackage() { + if ($this->package instanceof ElggPluginPackage) { + return $this->package; + } + + try { + $this->package = new ElggPluginPackage($this->path, false); + } catch (Exception $e) { + elgg_log("Failed to load package for $this->guid. " . $e->getMessage(), 'WARNING'); + $this->errorMsg = $e->getmessage(); + } + + return $this->package; + } } |
