Inject ROOT_PATH in plugin instead of regenerating it everywhere

This commit is contained in:
ArthurHoaro 2020-10-16 13:06:06 +02:00
parent 7f5250421b
commit 3adbdc2a83
7 changed files with 14 additions and 9 deletions

View file

@ -106,6 +106,7 @@ abstract class ShaarliVisitorController
'target' => $template,
'loggedin' => $this->container->loginManager->isLoggedIn(),
'basePath' => $this->container->basePath,
'rootPath' => preg_replace('#/index\.php$#', '', $this->container->basePath),
'bookmarkService' => $this->container->bookmarkService
];
}

View file

@ -104,6 +104,7 @@ class PluginManager
'target' => '_PAGE_',
'loggedin' => '_LOGGEDIN_',
'basePath' => '_BASE_PATH_',
'rootPath' => '_ROOT_PATH_',
'bookmarkService' => '_BOOKMARK_SERVICE_',
];

View file

@ -148,11 +148,16 @@ If a file needs to be included in server end, use simple relative path:
`PluginManager::$PLUGINS_PATH . '/mything/template.html'`.
If it needs to be included in front end side (e.g. an image),
the relative path must be prefixed with special data `_BASE_PATH_`:
`($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH . '/mything/picture.png`.
the relative path must be prefixed with special data:
* if it's a link that will need to be processed by Shaarli, use `_BASE_PATH_`:
for e.g. `$data['_BASE_PATH_'] . '/admin/tools`.
* if you want to include an asset, you need to add the root URL (base path without `/index.php`, for people using Shaarli without URL rewriting), then use `_ROOT_PATH_`:
for e.g
`$['_ROOT_PATH_'] . '/' . PluginManager::$PLUGINS_PATH . '/mything/picture.png`.
Note that special placeholders for CSS and JS files (respectively `css_files` and `js_files`) are already prefixed
with the base path in template files.
with the root path in template files.
### It's not working!

View file

@ -17,8 +17,7 @@ use Shaarli\Plugin\PluginManager;
function hook_archiveorg_render_linklist($data)
{
$archive_html = file_get_contents(PluginManager::$PLUGINS_PATH . '/archiveorg/archiveorg.html');
$rootPath = preg_replace('#/index\.php$#', '', $data['_BASE_PATH_'] ?? '');
$path = $rootPath . '/' . PluginManager::$PLUGINS_PATH;
$path = ($data['_ROOT_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH;
foreach ($data['links'] as &$value) {
$isNote = startsWith($value['real_url'], '/shaare/');

View file

@ -54,7 +54,7 @@ function hook_isso_render_linklist($data, $conf)
if ($conf->get('resource.theme') === 'default') {
$button .= '<i class="linklist-plugin-icon fa fa-comment"></i>';
} else {
$button .= '<img class="linklist-plugin-icon" src="plugins/isso/comment.png" ';
$button .= '<img class="linklist-plugin-icon" src="'. $data['_ROOT_PATH_'].'/plugins/isso/comment.png" ';
$button .= 'title="Comment on this shaare" alt="Comments" />';
}
$button .= '</a></span>';

View file

@ -19,8 +19,7 @@ function hook_qrcode_render_linklist($data)
{
$qrcode_html = file_get_contents(PluginManager::$PLUGINS_PATH . '/qrcode/qrcode.html');
$rootPath = preg_replace('#/index\.php$#', '', $data['_BASE_PATH_'] ?? '');
$path = $rootPath . '/' . PluginManager::$PLUGINS_PATH;
$path = ($data['_ROOT_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH;
foreach ($data['links'] as &$value) {
$qrcode = sprintf(
$qrcode_html,

View file

@ -45,7 +45,7 @@ function hook_wallabag_render_linklist($data, $conf)
$wallabagHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/wallabag/wallabag.html');
$linkTitle = t('Save to wallabag');
$path = ($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH;
$path = ($data['_ROOT_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH;
foreach ($data['links'] as &$value) {
$wallabag = sprintf(