dcLatestVersions/src/Widgets.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2015-04-22 15:07:20 +00:00
<?php
2022-11-20 17:33:02 +00:00
/**
* @brief dcLatestVersions, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis, Pierre Van Glabeke
*
* @copyright Jean-Christian Denis
2022-11-20 17:33:02 +00:00
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-03-26 21:55:27 +00:00
declare(strict_types=1);
2015-04-22 15:07:20 +00:00
2023-03-26 21:55:27 +00:00
namespace Dotclear\Plugin\dcLatestVersions;
2015-04-22 15:07:20 +00:00
2023-03-26 21:55:27 +00:00
use dcCore;
use dcUpdate;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2015-04-22 15:07:20 +00:00
2023-03-26 21:55:27 +00:00
class Widgets
2015-04-22 15:07:20 +00:00
{
2023-03-26 21:55:27 +00:00
public static function initWidgets(WidgetsStack $w): void
2021-08-27 15:53:07 +00:00
{
$w
->create(
'dclatestversionswidget',
2023-03-26 21:55:27 +00:00
My::name(),
[self::class, 'parseWidget'],
2021-08-27 15:53:07 +00:00
null,
2022-11-16 22:03:38 +00:00
__('Show the latest available versions of Dotclear')
2021-08-27 15:53:07 +00:00
)
->addTitle(
2023-03-26 21:55:27 +00:00
My::name()
2021-08-27 15:53:07 +00:00
)
->setting(
'text',
__('Text (%r = release, %v = version, %u = url):'),
__('<strong>%r: </strong> <a href="%u" title="Download Dotclear %v">%v</a>'),
'text'
)
->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
}
2023-03-26 21:55:27 +00:00
public static function parseWidget(WidgetsElement $w): string
2021-08-27 15:53:07 +00:00
{
2023-03-26 21:55:27 +00:00
if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type) || $w->text == '') {
return '';
2021-08-27 15:53:07 +00:00
}
2023-04-20 20:39:11 +00:00
// nullsafe PHP < 8.0
if (is_null(dcCore::app()->blog)) {
return '';
}
2021-08-27 15:53:07 +00:00
# Builds to check
2023-03-26 21:55:27 +00:00
$builds = explode(',', (string) dcCore::app()->blog->settings->get(My::id())->get('builds'));
if (empty($builds[0])) {
2023-03-26 21:55:27 +00:00
return '';
2021-08-27 15:53:07 +00:00
}
$li = [];
2022-11-16 22:03:38 +00:00
foreach ($builds as $build) {
2021-08-27 15:53:07 +00:00
$build = strtolower(trim($build));
if (empty($build)) {
continue;
}
$updater = new dcUpdate(
DC_UPDATE_URL,
'dotclear',
$build,
DC_TPL_CACHE . '/versions'
);
if (false === $updater->check('0')) {
continue;
}
$li[] = sprintf('<li>%s</li>', str_replace(
[
'%r',
'%v',
2022-11-16 22:03:38 +00:00
'%u',
2021-08-27 15:53:07 +00:00
],
[
$build,
$updater->getVersion(),
2022-11-16 22:03:38 +00:00
$updater->getFileURL(),
2021-08-27 15:53:07 +00:00
],
$w->text
));
}
if (empty($li)) {
2023-03-26 21:55:27 +00:00
return '';
2021-08-27 15:53:07 +00:00
}
# Display
return $w->renderDiv(
2023-03-26 21:55:27 +00:00
(bool) $w->content_only,
2022-11-16 22:03:38 +00:00
'dclatestversionswidget ' . $w->class,
'',
2023-03-26 21:55:27 +00:00
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . sprintf('<ul>%s</ul>', implode('', $li))
2021-08-27 15:53:07 +00:00
);
}
2022-11-16 22:03:38 +00:00
}