enhancePostContent/src/Widgets.php

160 lines
4.0 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
2023-04-09 08:17:36 +00:00
$filters = Epc::getFilters();
2021-11-01 09:33:43 +00:00
$types = [];
foreach ($filters as $id => $filter) {
if ($filter->widget != '') {
$types[$filter->name] = $id;
}
}
$w->epclist->setting(
'type',
__('Type:'),
'Definition',
'combo',
$types
);
# Content
2023-04-09 08:17:36 +00:00
$contents = Epc::defaultAllowedWidgetValues();
2021-11-01 09:33:43 +00:00
foreach ($contents as $k => $v) {
$w->epclist->setting(
'content' . $v['id'],
sprintf(__('Enable filter on %s'), __($k)),
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-09 08:17:36 +00:00
if (!dcCore::app()->blog->settings->get(My::id())->get('active')
2022-11-13 20:30:48 +00:00
|| !in_array(dcCore::app()->ctx->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-09 08:17:36 +00:00
foreach (Epc::defaultAllowedWidgetValues() as $k => $v) {
$ns = 'content' . $v['id'];
2021-10-31 22:22:34 +00:00
if ($w->$ns && is_callable($v['cb'])) {
$content .= call_user_func(
2021-10-31 22:22:34 +00:00
$v['cb'],
$w
);
}
}
if (empty($content)) {
2023-04-09 08:17:36 +00:00
return '';
}
# Filter
$list = new ArrayObject();
2023-04-09 08:17:36 +00:00
$filters = Epc::getFilters();
2021-10-31 22:22:34 +00:00
if (isset($filters[$w->type])) {
$filters[$w->type]->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) {
if (empty($line['matches'][0]['match'])) {
continue;
}
2021-11-01 09:33:43 +00:00
$res .= '<li>' . $line['matches'][0]['match'] .
($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
}