diff options
Diffstat (limited to 'engine/lib/plugins.php')
| -rw-r--r-- | engine/lib/plugins.php | 591 |
1 files changed, 364 insertions, 227 deletions
diff --git a/engine/lib/plugins.php b/engine/lib/plugins.php index 5cb6d23f6..d5d3db466 100644 --- a/engine/lib/plugins.php +++ b/engine/lib/plugins.php @@ -54,13 +54,15 @@ define('ELGG_PLUGIN_INTERNAL_PREFIX', 'elgg:internal:'); * @param string $dir A dir to scan for plugins. Defaults to config's plugins_path. * * @return array + * @since 1.8.0 + * @access private */ function elgg_get_plugin_ids_in_dir($dir = null) { if (!$dir) { $dir = elgg_get_plugins_path(); } - $plugin_idss = array(); + $plugin_ids = array(); $handle = opendir($dir); if ($handle) { @@ -85,14 +87,21 @@ function elgg_get_plugin_ids_in_dir($dir = null) { * * @todo Crappy name? * @return bool + * @since 1.8.0 + * @access private */ function elgg_generate_plugin_entities() { + // @todo $site unused, can remove? $site = get_config('site'); + $dir = elgg_get_plugins_path(); + $db_prefix = elgg_get_config('dbprefix'); $options = array( 'type' => 'object', 'subtype' => 'plugin', + 'selects' => array('plugin_oe.*'), + 'joins' => array("JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid"), 'limit' => ELGG_ENTITIES_NO_VALUE ); @@ -100,6 +109,7 @@ function elgg_generate_plugin_entities() { $old_access = access_get_show_hidden_status(); access_show_hidden_entities(true); $known_plugins = elgg_get_entities_from_relationship($options); + /* @var ElggPlugin[] $known_plugins */ if (!$known_plugins) { $known_plugins = array(); @@ -124,8 +134,6 @@ function elgg_generate_plugin_entities() { return false; } - $new_plugin_priority = elgg_get_max_plugin_priority() + 1; - // check real plugins against known ones foreach ($physical_plugins as $plugin_id) { // is this already in the db? @@ -133,23 +141,19 @@ function elgg_generate_plugin_entities() { $index = $id_map[$plugin_id]; $plugin = $known_plugins[$index]; // was this plugin deleted and its entity disabled? - if ($plugin->enabled != 'yes') { + if (!$plugin->isEnabled()) { $plugin->enable(); $plugin->deactivate(); - $plugin->setPriority($new_plugin_priority); - - $new_plugin_priority++; + $plugin->setPriority('last'); } // remove from the list of plugins to disable unset($known_plugins[$index]); } else { // add new plugins + // priority is force to last in save() if not set. $plugin = new ElggPlugin($plugin_id); $plugin->save(); - $plugin->setPriority($new_plugin_priority); - - $new_plugin_priority++; } } @@ -175,20 +179,40 @@ function elgg_generate_plugin_entities() { } /** + * Cache a reference to this plugin by its ID + * + * @param ElggPlugin $plugin + * + * @access private + */ +function _elgg_cache_plugin_by_id(ElggPlugin $plugin) { + $map = (array) elgg_get_config('plugins_by_id_map'); + $map[$plugin->getID()] = $plugin; + elgg_set_config('plugins_by_id_map', $map); +} + +/** * Returns an ElggPlugin object with the path $path. * - * @param string $id The id (dir name) of the plugin. NOT the guid. - * @return mixed ElggPlugin or false. + * @param string $plugin_id The id (dir name) of the plugin. NOT the guid. + * @return ElggPlugin|false + * @since 1.8.0 */ -function elgg_get_plugin_from_id($id) { - $id = sanitize_string($id); +function elgg_get_plugin_from_id($plugin_id) { + $map = (array) elgg_get_config('plugins_by_id_map'); + if (isset($map[$plugin_id])) { + return $map[$plugin_id]; + } + + $plugin_id = sanitize_string($plugin_id); $db_prefix = get_config('dbprefix'); $options = array( 'type' => 'object', 'subtype' => 'plugin', 'joins' => array("JOIN {$db_prefix}objects_entity oe on oe.guid = e.guid"), - 'wheres' => array("oe.title = '$id'"), + 'selects' => array("oe.title", "oe.description"), + 'wheres' => array("oe.title = '$plugin_id'"), 'limit' => 1 ); @@ -209,6 +233,7 @@ function elgg_get_plugin_from_id($id) { * {@link elgg_generate_plugin_objects()} first. * * @param string $id The plugin ID. + * @since 1.8.0 * @return bool */ function elgg_plugin_exists($id) { @@ -221,6 +246,8 @@ function elgg_plugin_exists($id) { * Returns the highest priority of the plugins * * @return int + * @since 1.8.0 + * @access private */ function elgg_get_max_plugin_priority() { $db_prefix = get_config('dbprefix'); @@ -235,30 +262,60 @@ function elgg_get_max_plugin_priority() { $data = get_data($q); if ($data) { - return $data[0]->max; + $max = $data[0]->max; + } else { + $max = 1; } // can't have a priority of 0. - return 1; + return ($max) ? $max : 1; +} + +/** + * Returns if a plugin is active for a current site. + * + * @param string $plugin_id The plugin ID + * @param int $site_guid The site guid + * @since 1.8.0 + * @return bool + */ +function elgg_is_active_plugin($plugin_id, $site_guid = null) { + if ($site_guid) { + $site = get_entity($site_guid); + } else { + $site = elgg_get_site_entity(); + } + + if (!($site instanceof ElggSite)) { + return false; + } + + $plugin = elgg_get_plugin_from_id($plugin_id); + + if (!$plugin) { + return false; + } + + return $plugin->isActive($site->guid); } /** * Loads all active plugins in the order specified in the tool admin panel. * - * @note This is called on every page load and includes additional checking that plugins - * are fit to be loaded. If a plugin is active and problematic, it will be disabled - * and a visible error emitted. + * @note This is called on every page load. If a plugin is active and problematic, it + * will be disabled and a visible error emitted. This does not check the deps system because + * that was too slow. * * @return bool + * @since 1.8.0 + * @access private */ function elgg_load_plugins() { - global $CONFIG; - $plugins_path = elgg_get_plugins_path(); - $start_flags = ELGG_PLUGIN_INCLUDE_START - | ELGG_PLUGIN_REGISTER_VIEWS - | ELGG_PLUGIN_REGISTER_LANGUAGES - | ELGG_PLUGIN_REGISTER_CLASSES; + $start_flags = ELGG_PLUGIN_INCLUDE_START | + ELGG_PLUGIN_REGISTER_VIEWS | + ELGG_PLUGIN_REGISTER_LANGUAGES | + ELGG_PLUGIN_REGISTER_CLASSES; if (!$plugins_path) { return false; @@ -266,44 +323,31 @@ function elgg_load_plugins() { // temporary disable all plugins if there is a file called 'disabled' in the plugin dir if (file_exists("$plugins_path/disabled")) { + if (elgg_is_admin_logged_in() && elgg_in_context('admin')) { + system_message(elgg_echo('plugins:disabled')); + } return false; } - // Load view caches if available - $cached_view_paths = elgg_filepath_cache_load('views'); - $cached_view_types = elgg_filepath_cache_load('view_types'); - $cached_view_info = is_string($cached_view_paths) && is_string($cached_view_types); - - if ($cached_view_info) { - $CONFIG->views = unserialize($cached_view_paths); - $CONFIG->view_types = unserialize($cached_view_types); - - // don't need to register views + if (elgg_get_config('system_cache_loaded')) { $start_flags = $start_flags & ~ELGG_PLUGIN_REGISTER_VIEWS; } + if (elgg_get_config('i18n_loaded_from_cache')) { + $start_flags = $start_flags & ~ELGG_PLUGIN_REGISTER_LANGUAGES; + } + $return = true; $plugins = elgg_get_plugins('active'); if ($plugins) { foreach ($plugins as $plugin) { - // check if plugin can be started and try to start it. - // if anything is bad, disable it and emit a message. - if (!$plugin->isValid()) { - $plugin->deactivate(); - $msg = elgg_echo('PluginException:MisconfiguredPlugin', array($plugin->getID(), $plugin->guid)); - register_error($msg); - $return = false; - - continue; - } - try { $plugin->start($start_flags); } catch (Exception $e) { $plugin->deactivate(); $msg = elgg_echo('PluginException:CannotStart', array($plugin->getID(), $plugin->guid, $e->getMessage())); - register_error($msg); + elgg_add_admin_notice('cannot_start' . $plugin->getID(), $msg); $return = false; continue; @@ -311,24 +355,19 @@ function elgg_load_plugins() { } } - // Cache results - if (!$cached_view_info) { - elgg_filepath_cache_save('views', serialize($CONFIG->views)); - elgg_filepath_cache_save('view_types', serialize($CONFIG->view_types)); - } - return $return; } /** * Returns an ordered list of plugins * - * @param string $status The status of the plugins. active, inactive, or all. - * @param bool $include_deleted Include physically deleted (and so inactive and disabled) plugins? - * @param mixed $site_guid Optional site guid - * @return array + * @param string $status The status of the plugins. active, inactive, or all. + * @param mixed $site_guid Optional site guid + * @return ElggPlugin[] + * @since 1.8.0 + * @access private */ -function elgg_get_plugins($status = 'active', $include_deleted = false, $site_guid = NULL) { +function elgg_get_plugins($status = 'active', $site_guid = null) { $db_prefix = get_config('dbprefix'); $priority = elgg_namespace_plugin_private_setting('internal', 'priority'); @@ -342,7 +381,11 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu 'type' => 'object', 'subtype' => 'plugin', 'limit' => ELGG_ENTITIES_NO_VALUE, - 'joins' => array("JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid"), + 'selects' => array('plugin_oe.*'), + 'joins' => array( + "JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid", + "JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid" + ), 'wheres' => array("ps.name = '$priority'"), 'order_by' => "CAST(ps.value as unsigned), e.guid" ); @@ -367,15 +410,9 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu break; } - if ($include_deleted) { - $old_id = elgg_set_ignore_access(true); - } - + $old_ia = elgg_set_ignore_access(true); $plugins = elgg_get_entities_from_relationship($options); - - if ($include_deleted) { - elgg_set_ignore_access($old_ia); - } + elgg_set_ignore_access($old_ia); return $plugins; } @@ -390,11 +427,13 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu * * @param array $order An array of plugin ids in the order to set them * @return bool + * @since 1.8.0 + * @access private */ function elgg_set_plugin_priorities(array $order) { $name = elgg_namespace_plugin_private_setting('internal', 'priority'); - $plugins = elgg_get_plugins('any', true); + $plugins = elgg_get_plugins('any'); if (!$plugins) { return false; } @@ -405,6 +444,7 @@ function elgg_set_plugin_priorities(array $order) { // though we do start with 1 $order = array_values($order); + $missing_plugins = array(); foreach ($plugins as $plugin) { $plugin_id = $plugin->getID(); @@ -421,9 +461,9 @@ function elgg_set_plugin_priorities(array $order) { } } - // set the missing plugins priorities + // set the missing plugins' priorities if ($return && $missing_plugins) { - if (!$priority) { + if (!isset($priority)) { $priority = 0; } foreach ($missing_plugins as $plugin) { @@ -443,37 +483,29 @@ function elgg_set_plugin_priorities(array $order) { * * @todo Can this be done in a single sql command? * @return bool + * @since 1.8.0 + * @access private */ function elgg_reindex_plugin_priorities() { return elgg_set_plugin_priorities(array()); } /** - * Loads plugins - * - * @deprecate 1.8 - * - * @return bool - */ -function load_plugins() { - elgg_deprecated_notice('load_plugins() is deprecated by elgg_load_plugins()', 1.8); - - return elgg_load_plugins(); -} - -/** * Namespaces a string to be used as a private setting for a plugin. * * @param string $type The type of value: user_setting or internal. * @param string $name The name to namespace. * @param string $id The plugin's ID to namespace with. Required for user_setting. * @return string + * @since 1.8.0 + * @access private */ function elgg_namespace_plugin_private_setting($type, $name, $id = null) { switch ($type) { -// case 'setting': -// $name = ELGG_PLUGIN_SETTING_PREFIX . $name; -// break; + // commented out because it breaks $plugin->$name access to variables + //case 'setting': + // $name = ELGG_PLUGIN_SETTING_PREFIX . $name; + // break; case 'user_setting': if (!$id) { @@ -500,9 +532,11 @@ function elgg_namespace_plugin_private_setting($type, $name, $id = null) { * context from the main script filename called by * the browser. Default = false. * - * @since 1.8 - * * @return string|false Plugin name, or false if no plugin name was called + * @since 1.8.0 + * @access private + * + * @todo get rid of this */ function elgg_get_calling_plugin_id($mainfilename = false) { if (!$mainfilename) { @@ -517,8 +551,9 @@ function elgg_get_calling_plugin_id($mainfilename = false) { } } } else { - if (preg_match("/pg\/([a-zA-Z0-9\-\_]*)\//", $_SERVER['REQUEST_URI'], $matches)) { - return $matches[1]; + //@todo this is a hack -- plugins do not have to match their page handler names! + if ($handler = get_input('handler', FALSE)) { + return $handler; } else { $file = $_SERVER["SCRIPT_NAME"]; $file = str_replace("\\", "/", $file); @@ -547,7 +582,8 @@ function elgg_get_calling_plugin_id($mainfilename = false) { * @param string $name A specific provided name to return. Requires $provide_type. * * @return array - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_get_plugins_provides($type = null, $name = null) { static $provides = null; @@ -557,11 +593,16 @@ function elgg_get_plugins_provides($type = null, $name = null) { $provides = array(); foreach ($active_plugins as $plugin) { - if ($plugin_provides = $plugin->manifest->getProvides()) { + $plugin_provides = array(); + $manifest = $plugin->getManifest(); + if ($manifest instanceof ElggPluginManifest) { + $plugin_provides = $plugin->getManifest()->getProvides(); + } + if ($plugin_provides) { foreach ($plugin_provides as $provided) { $provides[$provided['type']][$provided['name']] = array( 'version' => $provided['version'], - 'provided_by' => $plugin_id + 'provided_by' => $plugin->getID() ); } } @@ -598,22 +639,22 @@ function elgg_get_plugins_provides($type = null, $name = null) { * 'status' => bool Does the provide exist?, * 'value' => string The version provided * ) - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_check_plugins_provides($type, $name, $version = null, $comparison = 'ge') { - if (!$provided = elgg_get_plugins_provides($type, $name)) { + $provided = elgg_get_plugins_provides($type, $name); + if (!$provided) { return array( 'status' => false, 'version' => '' ); } - if ($provided) { - if ($version) { - $status = version_compare($provided['version'], $version, $comparison); - } else { - $status = true; - } + if ($version) { + $status = version_compare($provided['version'], $version, $comparison); + } else { + $status = true; } return array( @@ -634,11 +675,13 @@ function elgg_check_plugins_provides($type, $name, $version = null, $comparison * * @param array $dep An ElggPluginPackage dependency array * @return array + * @since 1.8.0 + * @access private */ function elgg_get_plugin_dependency_strings($dep) { - $dep_system = elgg_get_array_value('type', $dep); - $info = elgg_get_array_value('dep', $dep); - $type = elgg_get_array_value('type', $info); + $dep_system = elgg_extract('type', $dep); + $info = elgg_extract('dep', $dep); + $type = elgg_extract('type', $info); if (!$dep_system || !$info || !$type) { return false; @@ -667,6 +710,7 @@ function elgg_get_plugin_dependency_strings($dep) { 'requires' 'plugin oauth_lib' <1.3 1.3 'downgrade' 'requires' 'php setting bob' >3 3 'change it' 'conflicts' 'php setting' >3 4 'change it' + 'conflicted''plugin profile' any 1.8 'disable profile' 'provides' 'plugin oauth_lib' 1.3 -- -- 'priority' 'before blog' -- after 'move it' */ @@ -705,9 +749,9 @@ function elgg_get_plugin_dependency_strings($dep) { case 'plugin': $strings['name'] = elgg_echo('ElggPlugin:Dependencies:Plugin', array($info['name'])); - $expected = $info['version'] ? "$comparison {$info['version']}" : ''; + $expected = $info['version'] ? "$comparison {$info['version']}" : elgg_echo('any'); $strings['expected_value'] = $expected; - $strings['local_value'] = $dep['value']; + $strings['local_value'] = $dep['value'] ? $dep['value'] : '--'; $strings['comment'] = ''; break; @@ -721,10 +765,18 @@ function elgg_get_plugin_dependency_strings($dep) { break; } - if ($dep['status']) { - $strings['comment'] = elgg_echo('ok'); + if ($dep['type'] == 'suggests') { + if ($dep['status']) { + $strings['comment'] = elgg_echo('ok'); + } else { + $strings['comment'] = elgg_echo('ElggPlugin:Dependencies:Suggests:Unsatisfied'); + } } else { - $strings['comment'] = elgg_echo('error'); + if ($dep['status']) { + $strings['comment'] = elgg_echo('ok'); + } else { + $strings['comment'] = elgg_echo('error'); + } } return $strings; @@ -734,7 +786,8 @@ function elgg_get_plugin_dependency_strings($dep) { * Returns the ElggPlugin entity of the last plugin called. * * @return mixed ElggPlugin or false - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_get_calling_plugin_entity() { $plugin_id = elgg_get_calling_plugin_id(); @@ -747,51 +800,39 @@ function elgg_get_calling_plugin_entity() { } /** - * Find the plugin settings for a user. + * Returns an array of all plugin settings for a user. * - * @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. + * @param mixed $user_guid The user GUID or null for the currently logged in user. + * @param string $plugin_id The plugin ID + * @param bool $return_obj Return settings as an object? This can be used to in reusable + * views where the settings are passed as $vars['entity']. + * @return array + * @since 1.8.0 */ -function find_plugin_usersettings($plugin_id = null, $user_guid = 0) { - $plugin_id = sanitise_string($plugin_id); - $user_guid = (int)$user_guid; - $db_prefix = get_config('db_prefix'); - $ps_prefix = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:"); - $ps_prefix_len = strlen($ps_prefix); - - if (!$plugin_id) { - $plugin_id = elgg_get_calling_plugin_id(); +function elgg_get_all_plugin_user_settings($user_guid = null, $plugin_id = null, $return_obj = false) { + if ($plugin_id) { + $plugin = elgg_get_plugin_from_id($plugin_id); + } else { + $plugin = elgg_get_calling_plugin_entity(); } - if ($user_guid == 0) { - $user_guid = get_loggedin_userid(); + if (!$plugin instanceof ElggPlugin) { + return false; } - // Get private settings for user - $q = "SELECT * FROM {$db_prefix}private_settings - WHERE entity_guid = $user_guid - AND name LIKE '$ps_prefix$plugin_id'"; + $settings = $plugin->getAllUserSettings($user_guid); - $private_settings = get_data($q); - if ($private_settings) { + if ($settings && $return_obj) { $return = new stdClass; - foreach ($private_settings as $setting) { - $name = substr($setting->name, $ps_prefix_len); - $value = $setting->value; - - // @todo why? - if (strpos($key, $ps_prefix) === 0) { - $return->$name = $value; - } + foreach ($settings as $k => $v) { + $return->$k = $v; } return $return; + } else { + return $settings; } - - return false; } /** @@ -804,112 +845,83 @@ function find_plugin_usersettings($plugin_id = null, $user_guid = 0) { * is detected from where you are calling from. * * @return bool + * @since 1.8.0 */ -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 = get_loggedin_user(); +function elgg_set_plugin_user_setting($name, $value, $user_guid = null, $plugin_id = null) { + 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); } /** - * Clears a user-specific plugin setting + * Unsets a user-specific plugin setting * - * @param str $name Name of the plugin setting - * @param int $user_guid Defaults to logged in user - * @param str $plugin_id Defaults to contextual plugin name + * @param string $name Name of the setting + * @param int $user_guid Defaults to logged in user + * @param string $plugin_id Defaults to contextual plugin name * - * @return bool Success + * @return bool + * @since 1.8.0 */ -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 = get_loggedin_user(); +function elgg_unset_plugin_user_setting($name, $user_guid = null, $plugin_id = null) { + 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->unsetUserSetting($name, $user_guid); } /** * Get a user specific setting for a plugin. * - * @param string $name The name. + * @param string $name The name of the setting. * @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 + * @since 1.8.0 */ -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 = get_loggedin_user(); +function elgg_get_plugin_user_setting($name, $user_guid = null, $plugin_id = null) { + 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); } /** * Set a setting for a plugin. * - * @param string $name The name - note, can't be "title". + * @param string $name The name of the setting - note, can't be "title". * @param mixed $value The value. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * - * @return int|false + * @return bool + * @since 1.8.0 */ -function set_plugin_setting($name, $value, $plugin_id = null) { +function elgg_set_plugin_setting($name, $value, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -926,13 +938,15 @@ function set_plugin_setting($name, $value, $plugin_id = null) { /** * Get setting for a plugin. * - * @param string $name The name. + * @param string $name The name of the setting. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return mixed + * @since 1.8.0 + * @todo make $plugin_id required in future version */ -function get_plugin_setting($name, $plugin_id = "") { +function elgg_get_plugin_setting($name, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -947,15 +961,16 @@ function get_plugin_setting($name, $plugin_id = "") { } /** - * Clear a plugin setting. + * Unsets a plugin setting. * - * @param string $name The name. + * @param string $name The name of the setting. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return bool + * @since 1.8.0 */ -function clear_plugin_setting($name, $plugin_id = "") { +function elgg_unset_plugin_setting($name, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -966,36 +981,107 @@ function clear_plugin_setting($name, $plugin_id = "") { return false; } - return $plugin->removeSetting($name); + return $plugin->unsetSetting($name); } /** - * Clear all plugin settings. + * Unsets all plugin settings for a plugin. * * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return bool - * @since 1.7.0 + * @since 1.8.0 */ -function clear_all_plugin_settings($plugin_id = "") { +function elgg_unset_all_plugin_settings($plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { $plugin = elgg_get_calling_plugin_entity(); } - if ($plugin) { - $plugin->removeAllSettings(); + if (!$plugin) { + return false; } - return false; + return $plugin->unsetAllSettings(); +} + +/** + * Returns entities based upon plugin settings. + * Takes all the options for {@see elgg_get_entities_from_private_settings()} + * in addition to the ones below. + * + * @param array $options Array in the format: + * + * plugin_id => NULL|STR The plugin id. Defaults to calling plugin + * + * plugin_user_setting_names => NULL|ARR private setting names + * + * plugin_user_setting_values => NULL|ARR metadata values + * + * plugin_user_setting_name_value_pairs => NULL|ARR ( + * name => 'name', + * value => 'value', + * 'operand' => '=', + * ) + * Currently if multiple values are sent via + * an array (value => array('value1', 'value2') + * the pair's operand will be forced to "IN". + * + * plugin_user_setting_name_value_pairs_operator => NULL|STR The operator to use for combining + * (name = value) OPERATOR (name = value); default AND + * + * @return mixed int If count, int. If not count, array. false on errors. + */ +function elgg_get_entities_from_plugin_user_settings(array $options = array()) { + // if they're passing it don't bother + if (!isset($options['plugin_id'])) { + $options['plugin_id'] = elgg_get_calling_plugin_id(); + } + + $singulars = array('plugin_user_setting_name', 'plugin_user_setting_value', + 'plugin_user_setting_name_value_pair'); + + $options = elgg_normalise_plural_options_array($options, $singulars); + + // rewrite plugin_user_setting_name_* to the right PS ones. + $map = array( + 'plugin_user_setting_names' => 'private_setting_names', + 'plugin_user_setting_values' => 'private_setting_values', + 'plugin_user_setting_name_value_pairs' => 'private_setting_name_value_pairs', + 'plugin_user_setting_name_value_pairs_operator' => 'private_setting_name_value_pairs_operator' + ); + + foreach ($map as $plugin => $private) { + if (!isset($options[$plugin])) { + continue; + } + + if (isset($options[$private])) { + if (!is_array($options[$private])) { + $options[$private] = array($options[$private]); + } + + $options[$private] = array_merge($options[$private], $options[$plugin]); + } else { + $options[$private] = $options[$plugin]; + } + } + + + $plugin_id = $options['plugin_id']; + $prefix = elgg_namespace_plugin_private_setting('user_setting', '', $plugin_id); + $options['private_setting_name_prefix'] = $prefix; + + return elgg_get_entities_from_private_settings($options); } /** * Register object, plugin entities as ElggPlugin classes * - * @return void + * @return void + * @access private */ function plugin_run_once() { add_subtype("object", "plugin", "ElggPlugin"); @@ -1004,12 +1090,13 @@ function plugin_run_once() { /** * Runs unit tests for the entity objects. * - * @param sting $hook unit_test + * @param string $hook unit_test * @param string $type system * @param mixed $value Array of tests * @param mixed $params Params * * @return array + * @access private */ function plugins_test($hook, $type, $value, $params) { global $CONFIG; @@ -1018,15 +1105,63 @@ function plugins_test($hook, $type, $value, $params) { } /** - * Initialise the file modules. - * Listens to system boot and registers any appropriate file types and classes + * Checks on deactivate plugin event if disabling it won't create unmet dependencies and blocks disable in such case. + * + * @param string $event deactivate + * @param string $type plugin + * @param array $params Parameters array containing entry with ELggPlugin instance under 'plugin_entity' key + * @return bool false to block plugin deactivation action + * + * @access private + */ +function _plugins_deactivate_dependency_check($event, $type, $params) { + $plugin_id = $params['plugin_entity']->getManifest()->getPluginID(); + $plugin_name = $params['plugin_entity']->getManifest()->getName(); + + $active_plugins = elgg_get_plugins(); + + $dependents = array(); + foreach ($active_plugins as $plugin) { + $manifest = $plugin->getManifest(); + $requires = $manifest->getRequires(); + + foreach ($requires as $required) { + if ($required['type'] == 'plugin' && $required['name'] == $plugin_id) { + // there are active dependents + $dependents[$manifest->getPluginID()] = $plugin; + } + } + } + + if ($dependents) { + $list = '<ul>'; + // construct error message and prevent disabling + foreach ($dependents as $dependent) { + $list .= '<li>' . $dependent->getManifest()->getName() . '</li>'; + } + $list .= '</ul>'; + + register_error(elgg_echo('ElggPlugin:Dependencies:ActiveDependent', array($plugin_name, $list))); + + return false; + } +} + +/** + * Initialize the plugin system + * Listens to system init and registers actions * * @return void + * @access private */ function plugin_init() { run_function_once("plugin_run_once"); elgg_register_plugin_hook_handler('unit_test', 'system', 'plugins_test'); + + // note - plugins are booted by the time this handler is registered + // deactivation due to error may have already occurred + elgg_register_event_handler('deactivate', 'plugin', '_plugins_deactivate_dependency_check'); elgg_register_action("plugins/settings/save", '', 'admin'); elgg_register_action("plugins/usersettings/save"); @@ -1037,6 +1172,8 @@ function plugin_init() { elgg_register_action('admin/plugins/deactivate_all', '', 'admin'); elgg_register_action('admin/plugins/set_priority', '', 'admin'); + + elgg_register_library('elgg:markdown', elgg_get_root_path() . 'vendors/markdown/markdown.php'); } -elgg_register_event_handler('init', 'system', 'plugin_init');
\ No newline at end of file +elgg_register_event_handler('init', 'system', 'plugin_init'); |
