koblog/bl-kernel/boot/rules/60.plugins.php

150 lines
4.2 KiB
PHP
Raw Normal View History

2015-06-27 00:12:26 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
// ============================================================================
2020-11-01 11:55:34 +01:00
// Global Variables
2015-06-27 00:12:26 +02:00
// ============================================================================
$plugins = array(
2021-09-05 16:18:01 +02:00
'siteHead' => array(),
'siteBodyBegin' => array(),
'siteBodyEnd' => array(),
'siteSidebar' => array(),
'beforeSiteLoad' => array(),
'afterSiteLoad' => array(),
'pageBegin' => array(),
'pageEnd' => array(),
'beforeAdminLoad' => array(),
'afterAdminLoad' => array(),
'adminHead' => array(),
'adminBodyBegin' => array(),
'adminBodyEnd' => array(),
'adminSidebar' => array(),
'adminContentSidebar' => array(),
'dashboard' => array(),
'beforeAll' => array(),
'afterAll' => array(),
'paginator' => array(),
'beforePageModify' => array(),
'beforePageDelete' => array(),
'afterPageCreate' => array(),
'afterPageModify' => array(),
'afterPageDelete' => array(),
'loginHead' => array(),
'loginBodyBegin' => array(),
'loginBodyEnd' => array(),
'all' => array() // $plugins['all'] keep installed and not installed plugins
2015-06-27 00:12:26 +02:00
);
2020-11-01 11:55:34 +01:00
// This array has only the installed plugins
// The array key is the "plugin class name" and the value is the object
// pluginsInstalled[pluginClass] = $Plugin
2019-05-09 17:19:56 +02:00
$pluginsInstalled = array();
2015-06-27 00:12:26 +02:00
// ============================================================================
// Functions
// ============================================================================
2016-01-14 05:42:18 +01:00
function buildPlugins()
2015-06-27 00:12:26 +02:00
{
global $plugins;
2019-05-09 17:19:56 +02:00
global $pluginsInstalled;
global $L;
global $site;
2015-06-27 00:12:26 +02:00
2020-11-01 11:55:34 +01:00
// This array is only to get the hooks names
$pluginsHooks = $plugins;
unset($pluginsHooks['all']); // remove "all" because is not a valid hook
2017-07-29 00:08:19 +02:00
// Get declared clasess BEFORE load plugins clasess
2015-06-27 00:12:26 +02:00
$currentDeclaredClasess = get_declared_classes();
2020-11-01 11:55:34 +01:00
// Load plugins clasess
2019-03-10 18:27:24 +01:00
$list = Filesystem::listDirectories(PATH_PLUGINS);
2017-07-29 01:33:30 +02:00
foreach ($list as $pluginPath) {
2021-09-05 16:18:01 +02:00
if (file_exists($pluginPath . DS . 'plugin.php')) {
include_once($pluginPath . DS . 'plugin.php');
2016-05-07 05:10:10 +02:00
}
2015-06-27 00:12:26 +02:00
}
// Get plugins clasess loaded
$pluginsDeclaredClasess = array_diff(get_declared_classes(), $currentDeclaredClasess);
2017-07-29 00:08:19 +02:00
foreach ($pluginsDeclaredClasess as $pluginClass) {
2015-06-27 00:12:26 +02:00
$Plugin = new $pluginClass;
2017-07-29 00:08:19 +02:00
// Check if the plugin is translated
2021-09-05 16:18:01 +02:00
$languageFilename = PATH_PLUGINS . $Plugin->directoryName() . DS . 'languages' . DS . $site->language() . '.json';
2019-03-10 18:27:24 +01:00
if (!Sanitize::pathFile($languageFilename)) {
2021-09-05 16:18:01 +02:00
$languageFilename = PATH_PLUGINS . $Plugin->directoryName() . DS . 'languages' . DS . DEFAULT_LANGUAGE_FILE;
2015-07-03 22:44:26 +02:00
}
2015-07-14 04:16:28 +02:00
2016-01-14 05:42:18 +01:00
$database = file_get_contents($languageFilename);
$database = json_decode($database, true);
2015-07-03 22:44:26 +02:00
2017-07-29 00:08:19 +02:00
// Set name and description from the language file
2021-09-05 16:18:01 +02:00
$Plugin->setMetadata('name', $database['plugin-data']['name']);
$Plugin->setMetadata('description', $database['plugin-data']['description']);
2016-01-14 05:42:18 +01:00
2020-11-01 11:55:34 +01:00
// Remove name and description from the language and includes new words to the global language dictionary
2016-01-14 05:42:18 +01:00
unset($database['plugin-data']);
2017-07-29 00:08:19 +02:00
if (!empty($database)) {
$L->add($database);
2016-01-14 05:42:18 +01:00
}
2015-07-03 22:44:26 +02:00
2017-07-29 00:08:19 +02:00
// $plugins['all'] Array with all plugins, installed and not installed
2016-08-09 04:43:33 +02:00
$plugins['all'][$pluginClass] = $Plugin;
2016-06-20 02:45:09 +02:00
2017-07-29 00:08:19 +02:00
if ($Plugin->installed()) {
2020-11-01 11:55:34 +01:00
// Include the plugin installed in the global array
$pluginsInstalled[$pluginClass] = $Plugin;
// Define new hooks from custom hooks
2020-05-12 18:24:06 +02:00
if (!empty($Plugin->customHooks)) {
2020-11-01 11:55:34 +01:00
foreach ($Plugin->customHooks as $hook) {
if (!isset($plugins[$hook])) {
$plugins[$hook] = array();
$pluginsHooks[$hook] = array();
2020-05-12 18:24:06 +02:00
}
}
}
2020-11-01 11:55:34 +01:00
// Insert the plugin into the hooks
2021-09-05 16:18:01 +02:00
foreach ($pluginsHooks as $hook => $value) {
2020-11-01 11:55:34 +01:00
if (method_exists($Plugin, $hook)) {
array_push($plugins[$hook], $Plugin);
2015-06-27 00:12:26 +02:00
}
}
}
2017-07-30 23:15:33 +02:00
2019-03-10 18:27:24 +01:00
// Sort the plugins by the position for the site sidebar
2021-09-05 16:18:01 +02:00
uasort(
$plugins['siteSidebar'],
function ($a, $b) {
return $a->position() <=> $b->position();
2020-05-12 18:24:06 +02:00
}
);
// Sort the plugins by the position for the dashboard
2021-09-05 16:18:01 +02:00
uasort(
$plugins['dashboard'],
function ($a, $b) {
return $a->position() <=> $b->position();
}
);
2015-06-27 00:12:26 +02:00
}
}
// ============================================================================
// Main
// ============================================================================
2016-01-14 05:42:18 +01:00
buildPlugins();