Skip to content

Commit

Permalink
Fixed issue #17972: events are dispatched if the plugin is deactivated (
Browse files Browse the repository at this point in the history
#2335)

Dev: Add param to load init
Dev: set it to false in PluginManagerController in all case
Dev: active plugin are loaded via PluginManager->loadPlugins
Dev: use plugin model active status in PluginManager
Dev: init the plugin before activate
  • Loading branch information
Shnoulle committed Jul 26, 2022
1 parent c7104a5 commit d0c99be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions application/controllers/admin/PluginManagerController.php
Expand Up @@ -49,7 +49,7 @@ function ($installedPlugin) {
foreach ($aoPlugins as $oPlugin) {
/* @var $plugin Plugin */
if (array_key_exists($oPlugin->name, $aDiscoveredPlugins)) {
$plugin = App()->getPluginManager()->loadPlugin($oPlugin->name, $oPlugin->id);
$plugin = App()->getPluginManager()->loadPlugin($oPlugin->name, $oPlugin->id, false);
if ($plugin) {
$aPluginSettings = $plugin->getPluginSettings(false);
$data[] = array(
Expand Down Expand Up @@ -113,8 +113,8 @@ private function activate($id)
if (!is_null($oPlugin)) {
$iStatus = $oPlugin->active;
if ($iStatus == 0) {
// Load the plugin:
App()->getPluginManager()->loadPlugin($oPlugin->name, $id);
// Load the plugin (and init)
App()->getPluginManager()->loadPlugin($oPlugin->name, $id, true);
$result = App()->getPluginManager()->dispatchEvent(new PluginEvent('beforeActivate', $this), $oPlugin->name);
if ($result->get('success', true)) {
$iStatus = 1;
Expand Down Expand Up @@ -184,7 +184,7 @@ public function configure($id)
}

$arPlugin = Plugin::model()->findByPk($id)->attributes;
$oPluginObject = App()->getPluginManager()->loadPlugin($arPlugin['name'], $arPlugin['id']);
$oPluginObject = App()->getPluginManager()->loadPlugin($arPlugin['name'], $arPlugin['id'], false);

if ($arPlugin === null) {
Yii::app()->user->setFlash('error', gT('The plugin was not found.'));
Expand Down
10 changes: 5 additions & 5 deletions application/libraries/PluginManager/PluginManager.php
Expand Up @@ -69,7 +69,7 @@ public function getInstalledPlugins()

foreach ($records as $record) {
// Only add plugins we can find
if ($this->loadPlugin($record->name) !== false) {
if ($this->loadPlugin($record->name, $record->id, $record->active) !== false) {
$plugins[$record->id] = $record;
}
}
Expand Down Expand Up @@ -268,10 +268,11 @@ public function getPluginInfo($pluginClass, $pluginDir = null)
*
* @param string $pluginName
* @param int $id Identifier used for identifying a specific plugin instance.
* @param boolean $init init plugin if method is available
* If ommitted will return the first instantiated plugin with the given name.
* @return iPlugin|null The plugin or null when missing
*/
public function loadPlugin($pluginName, $id = null)
public function loadPlugin($pluginName, $id = null, $init = true)
{
// If the id is not set we search for the plugin.
if (!isset($id)) {
Expand All @@ -285,8 +286,7 @@ public function loadPlugin($pluginName, $id = null)
if ($this->getPluginInfo($pluginName) !== false) {
if (class_exists($pluginName)) {
$this->plugins[$id] = new $pluginName($this, $id);

if (method_exists($this->plugins[$id], 'init')) {
if ($init && method_exists($this->plugins[$id], 'init')) {
$this->plugins[$id]->init();
}
} else {
Expand Down Expand Up @@ -332,7 +332,7 @@ public function loadAllPlugins()
{
$records = Plugin::model()->findAll();
foreach ($records as $record) {
$this->loadPlugin($record->name, $record->id);
$this->loadPlugin($record->name, $record->id, $record->active);
}
}

Expand Down

0 comments on commit d0c99be

Please sign in to comment.