enhancePostContent/src/Widgets.php

152 lines
3.8 KiB
PHP
Raw Normal View History

2021-08-23 23:55:52 +00:00
<?php
2021-09-02 18:35:25 +00:00
/**
* @brief enhancePostContent, a plugin for Dotclear 2
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-04-09 08:17:36 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\enhancePostContent;
2021-08-23 23:55:52 +00:00
use ArrayObject;
2023-04-09 08:17:36 +00:00
use dcCore;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2021-08-23 23:55:52 +00:00
/**
* @ingroup DC_PLUGIN_ENHANCEPOSTCONTENT
* @brief Filter posts content - widgets methods.
* @since 2.6
*/
2023-04-09 08:17:36 +00:00
class Widgets
2021-08-23 23:55:52 +00:00
{
/**
* Admin part for widget that show extracted content
2021-11-01 09:33:43 +00:00
*
* @param WidgetsStack $w WidgetsStack instance
*/
2023-04-09 08:17:36 +00:00
public static function initWidgets(WidgetsStack $w): void
{
$w->create(
'epclist',
2023-04-09 08:17:36 +00:00
My::name(),
[self::class, 'parseWidget'],
null,
__('List filtered contents.')
);
# Title
2021-10-31 22:22:34 +00:00
$w->epclist->addTitle(__('In this article'));
# Text
$w->epclist->setting(
'text',
__('Description:'),
'',
'text'
);
# Type
$w->epclist->setting(
'type',
__('Type:'),
'Definition',
'combo',
2023-04-21 13:46:40 +00:00
Epc::getFilters()->nid(true)
);
# Content
2023-04-21 22:21:30 +00:00
foreach (Epc::widgetAllowedTemplateValue() as $name => $info) {
$w->epclist->setting(
2023-04-21 22:21:30 +00:00
'content' . $info['id'],
sprintf(__('Enable filter on %s'), __($name)),
1,
'check'
);
}
# Show count
$w->epclist->setting(
'show_total',
__('Show the number of appearance'),
1,
'check'
);
2021-10-31 22:22:34 +00:00
# widget options
$w->epclist
->addContentOnly()
->addClass()
->addOffline();
}
/**
* Public part for widget that show extracted content
2021-11-01 09:33:43 +00:00
*
* @param WidgetsElement $w WidgetsElement instance
*/
2023-04-09 08:17:36 +00:00
public static function parseWidget(WidgetsElement $w): string
{
2021-10-31 22:22:34 +00:00
if ($w->offline) {
2023-04-09 08:17:36 +00:00
return '';
2021-10-31 22:22:34 +00:00
}
# Page
2023-04-21 00:10:54 +00:00
if (!dcCore::app()->blog?->settings->get(My::id())->get('active')
|| !in_array(dcCore::app()->ctx?->__get('current_tpl'), ['post.html', 'page.html'])
2021-10-31 22:22:34 +00:00
) {
2023-04-09 08:17:36 +00:00
return '';
}
# Content
$content = '';
2023-04-21 22:21:30 +00:00
foreach (Epc::widgetAllowedTemplateValue() as $info) {
$ns = 'content' . $info['id'];
if ($w->$ns && is_callable($info['cb'])) {
$content .= call_user_func(
2023-04-21 22:21:30 +00:00
$info['cb'],
$w
);
}
}
if (empty($content)) {
2023-04-09 08:17:36 +00:00
return '';
}
# Filter
2023-04-21 13:46:40 +00:00
$list = new ArrayObject();
$filter = Epc::getFilters()->get($w->type);
2023-04-21 13:46:40 +00:00
if (!is_null($filter)) {
$filter->widgetList($content, $w, $list);
}
if (!count($list)) {
2023-04-09 08:17:36 +00:00
return '';
}
# Parse result
$res = '';
2021-11-01 09:33:43 +00:00
foreach ($list as $line) {
2023-04-21 22:21:30 +00:00
if ((int) $line['total'] == 0) {
continue;
}
2023-04-21 22:21:30 +00:00
$res .= '<li>' . $line['replacement'] .
2021-11-01 09:33:43 +00:00
($w->show_total ? ' (' . $line['total'] . ')' : '') .
'</li>';
}
2023-04-09 08:17:36 +00:00
return empty($res) ? '' : $w->renderDiv(
(bool) $w->content_only,
2021-11-01 09:33:43 +00:00
$w->class,
'id="epc_' . $w->type . '"',
2023-04-09 08:17:36 +00:00
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') .
($w->text ? '<p>' . Html::escapeHTML($w->text) . '</p>' : '') .
2021-08-24 21:17:43 +00:00
'<ul>' . $res . '</ul>'
);
}
2021-11-01 09:33:43 +00:00
}