From 3092154cdafdb5dd79198e8ead68e222045aaa52 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 9 Apr 2023 10:17:36 +0200 Subject: [PATCH] use namespace (WIP) --- inc/class.epcupgrade.php | 127 ------ src/Backend.php | 204 +++++---- src/BackendList.php | 24 +- src/Epc.php | 76 ++-- src/EpcFilter.php | 42 +- src/EpcRecord.php | 114 +++-- src/Filter/EpcFilterAbbreviation.php | 24 +- src/Filter/EpcFilterAcronym.php | 24 +- src/Filter/EpcFilterCitation.php | 24 +- src/Filter/EpcFilterDefinition.php | 24 +- src/Filter/EpcFilterLink.php | 24 +- src/Filter/EpcFilterReplace.php | 18 +- src/Filter/EpcFilterSearch.php | 21 +- src/Filter/EpcFilterTag.php | 29 +- src/Filter/EpcFilterTwitter.php | 18 +- src/Filter/EpcFilterUpdate.php | 18 +- src/Frontend.php | 67 ++- src/Install.php | 241 ++++++++--- src/Manage.php | 612 ++++++++++++++------------- src/ManageVars.php | 62 +++ src/My.php | 67 +++ src/Prepend.php | 116 ++--- src/Uninstall.php | 201 +++++---- src/Widgets.php | 56 ++- 24 files changed, 1241 insertions(+), 992 deletions(-) delete mode 100644 inc/class.epcupgrade.php create mode 100644 src/ManageVars.php create mode 100644 src/My.php diff --git a/inc/class.epcupgrade.php b/inc/class.epcupgrade.php deleted file mode 100644 index 90ca979..0000000 --- a/inc/class.epcupgrade.php +++ /dev/null @@ -1,127 +0,0 @@ -getVersion(basename(dirname('../' . __DIR__))); - - if ($current && version_compare($current, '0.6.6', '<=')) { - self::upTo00060607(); - } - - if ($current && version_compare($current, '2021.10.06', '<=')) { - self::upTo20211006(); - } - - if ($current && version_compare($current, '2022.11.20', '<=')) { - self::upTo20221120(); - } - } - - /** - * 0.6.6 - * - * - filters move from settings to dedicated table - */ - private static function upTo00060607() - { - # Move old filters lists from settings to database - $f = dcCore::app()->con->select('SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . " WHERE setting_ns='enhancePostContent' AND blog_id IS NOT NULL "); - - while ($f->fetch()) { - if (preg_match('#enhancePostContent_(.*?)List#', $f->setting_id, $m)) { - $curlist = @unserialize($f->setting_value); - if (is_array($curlist)) { - foreach ($curlist as $k => $v) { - $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME); - dcCore::app()->con->writeLock(dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME); - - $cur->epc_id = dcCore::app()->con->select('SELECT MAX(epc_id) FROM ' . dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME . ' ')->f(0) + 1; - $cur->blog_id = $f->blog_id; - $cur->epc_filter = strtolower($m[1]); - $cur->epc_key = $k; - $cur->epc_value = $v; - - $cur->insert(); - dcCore::app()->con->unlock(); - } - } - dcCore::app()->con->execute('DELETE FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . " WHERE setting_id='" . $f->setting_id . "' AND setting_ns='enhancePostContent' AND blog_id='" . $f->blog_id . "' "); - } - } - } - - /** - * 2021.10.06 - * - * - filters change name to id - */ - private static function upTo20211006() - { - # Move old filter name to filter id - $rs = dcCore::app()->con->select('SELECT epc_id, epc_filter FROM ' . dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME); - while ($rs->fetch()) { - $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME); - - $cur->epc_filter = strtolower($rs->epc_filter); - - $cur->update('WHERE epc_id = ' . $rs->epc_id . ' '); - dcCore::app()->blog->triggerBlog(); - } - } - - /** - * 2022.11.20 - * - * - setting id changes to shorter one, - * - setting ns changes to abstract one (no real changes), - * - setting value change from serialize to json_encode (if it's array) - */ - private static function upTo20221120() - { - // list of settings using serialize values to move to json - $ids = [ - 'allowedtplvalues', - 'allowedpubpages', - ]; - foreach (enhancePostContent::getFilters() as $id => $f) { - $ids[] = $id; - } - - // get all enhancePostContent settings - $record = dcCore::app()->con->select( - 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . - "WHERE setting_ns = 'enhancePostContent' " - ); - - // update settings id, ns, value - while ($record->fetch()) { - if (preg_match('/^enhancePostContent_(.*?)$/', $record->setting_id, $match)) { - $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); - $cur->setting_id = $match[1]; - $cur->setting_ns = basename(dirname('../' . __DIR__)); - - if (in_array($match[1], $ids)) { - $cur->setting_value = json_encode(unserialize($record->setting_value)); - } - - $cur->update("WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'enhancePostContent' "); - } - } - } -} diff --git a/src/Backend.php b/src/Backend.php index 1dadfcb..2cefb47 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,91 +10,121 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +use ArrayObject; +use dcAdmin; +use dcCore; +use dcPage; +use dcFavorites; +use dcNsProcess; +use dcSettings; + +use form; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && My::phpCompliant() + && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcCore::app()->auth::PERMISSION_CONTENT_ADMIN, + ]), dcCore::app()->blog->id); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + # Admin menu + dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( + My::name(), + dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + dcPage::getPF(My::id() . '/icon.svg'), + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) + ); + + dcCore::app()->addBehaviors([ + # Dashboard favorites + 'adminDashboardFavoritesV2' => function (dcFavorites $favs): void { + $favs->register(My::id(), [ + 'title' => My::name(), + 'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + 'small-icon' => dcPage::getPF(My::id() . '/icon.svg'), + 'large-icon' => dcPage::getPF(My::id() . '/icon.svg'), + 'permissions' => dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_CONTENT_ADMIN]), + ]); + }, + # Preference form + 'adminBlogPreferencesFormV2' => function (dcSettings $blog_settings):void { + $active = (bool) $blog_settings->get(My::id())->get('active'); + $allowedtplvalues = Epc::blogAllowedTplValues(); + $allowedpubpages = Epc::blogAllowedPubPages(); + + echo + '

' . My::name() . '

' . + '
' . + '
' . + '

' . + '

' . + __('This enable public widgets and contents filter.') . + '

' . + '

' . + __('Set content filters') . '

' . + '
' . + '
' . + '
' . __('Extra') . '
' . + '

' . __('This is a special feature to edit list of allowed template values and public pages where this plugin works.') . '

' . + '

' . + form::field('epc_allowedtplvalues', 100, 0, Epc::implode($allowedtplvalues)) . '

' . + '

' . __('Use "readable_name1:template_value1;readable_name2:template_value2;" like "entry content:EntryContent;entry excerpt:EntryExcerpt;".') . '

' . + '

' . + form::field('epc_allowedpubpages', 100, 0, Epc::implode($allowedpubpages)) . '

' . + '

' . __('Use "readable_name1:template_page1;readable_name2:template_page2;" like "post page:post.html;home page:home.html;".') . '

' . + '
' . + '
' . + '
' . + '
'; + }, + # Save preference + 'adminBeforeBlogSettingsUpdate' => function (dcSettings $blog_settings): void { + $active = !empty($_POST['epc_active']); + $allowedtplvalues = Epc::explode($_POST['epc_allowedtplvalues']); + $allowedpubpages = Epc::explode($_POST['epc_allowedpubpages']); + + $blog_settings->get(My::id())->put('active', $active); + $blog_settings->get(My::id())->put('allowedtplvalues', json_encode($allowedtplvalues)); + $blog_settings->get(My::id())->put('allowedpubpages', json_encode($allowedpubpages)); + }, + # List filter + 'adminFiltersListsV2' => function (ArrayObject $sorts): void { + $sorts['epc'] = [ + My::name(), + [ + __('Date') => 'epc_upddt', + __('Key') => 'epc_key', + __('Value') => 'epc_value', + __('ID') => 'epc_id', + ], + 'epc_upddt', + 'desc', + [__('records per page'), 20], + ]; + }, + # Widgets + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } } - -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - -require __DIR__ . '/_widgets.php'; - -# Admin menu -dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( - __('Enhance post content'), - dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), - urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')), - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']), - dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) -); - -# Dashboard favorites -dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs) { - $favs->register(basename(__DIR__), [ - 'title' => __('Enhance post content'), - 'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), - 'small-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')), - 'large-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')), - 'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), - ]); -}); - -# Preference form -dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function (dcSettings $blog_settings) { - $active = (bool) $blog_settings->get(basename(__DIR__))->get('active'); - $allowedtplvalues = enhancePostContent::blogAllowedTplValues(); - $allowedpubpages = enhancePostContent::blogAllowedPubPages(); - - echo - '

' . __('Enhance post content') . '

' . - '
' . - '
' . - '

' . - '

' . - __('This enable public widgets and contents filter.') . - '

' . - '

' . - __('Set content filters') . '

' . - '
' . - '
' . - '
' . __('Extra') . '
' . - '

' . __('This is a special feature to edit list of allowed template values and public pages where this plugin works.') . '

' . - '

' . - form::field('epc_allowedtplvalues', 100, 0, enhancePostContent::implode($allowedtplvalues)) . '

' . - '

' . __('Use "readable_name1:template_value1;readable_name2:template_value2;" like "entry content:EntryContent;entry excerpt:EntryExcerpt;".') . '

' . - '

' . - form::field('epc_allowedpubpages', 100, 0, enhancePostContent::implode($allowedpubpages)) . '

' . - '

' . __('Use "readable_name1:template_page1;readable_name2:template_page2;" like "post page:post.html;home page:home.html;".') . '

' . - '
' . - '
' . - '
' . - '
'; -}); - -# Save preference -dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings $blog_settings) { - $active = !empty($_POST['epc_active']); - $allowedtplvalues = enhancePostContent::explode($_POST['epc_allowedtplvalues']); - $allowedpubpages = enhancePostContent::explode($_POST['epc_allowedpubpages']); - - $blog_settings->get(basename(__DIR__))->put('active', $active); - $blog_settings->get(basename(__DIR__))->put('allowedtplvalues', json_encode($allowedtplvalues)); - $blog_settings->get(basename(__DIR__))->put('allowedpubpages', json_encode($allowedpubpages)); -}); - -# List filter -dcCore::app()->addBehavior('adminFiltersListsV2', function (ArrayObject $sorts) { - $sorts['epc'] = [ - __('Enhance post content'), - [ - __('Date') => 'epc_upddt', - __('Key') => 'epc_key', - __('Value') => 'epc_value', - __('ID') => 'epc_id', - ], - 'epc_upddt', - 'desc', - [__('records per page'), 20], - ]; -}); diff --git a/src/BackendList.php b/src/BackendList.php index dae547e..2456493 100644 --- a/src/BackendList.php +++ b/src/BackendList.php @@ -10,16 +10,24 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +use adminGenericListV2; +use dcCore; +use dcPager; +use Dotclear\Helper\Html\Form\Checkbox; +use Dotclear\Helper\Html\Html; + +use dt; /** * @ingroup DC_PLUGIN_PERIODICAL * @brief Periodical - admin pager methods. * @since 2.6 */ -class adminEpcList extends adminGenericList +class BackendList extends adminGenericListV2 { public function display($filter, $pager_url, $enclose_block = '') { @@ -65,12 +73,12 @@ class adminEpcList extends adminGenericList } } - private function line($checked) + private function line(bool $checked): string { $cols = [ - 'check' => '' . form::checkbox(['epc_id[]'], $this->rs->epc_id, ['checked' => $checked]) . '', - 'key' => '' . html::escapeHTML($this->rs->epc_key) . '', - 'value' => '' . html::escapeHTML($this->rs->epc_value) . '', + 'check' => '' . (new Checkbox(['epc_id[]'], $checked))->value($this->rs->epc_id)->render() . '', + 'key' => '' . Html::escapeHTML($this->rs->epc_key) . '', + 'value' => '' . Html::escapeHTML($this->rs->epc_value) . '', 'date' => '' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->epc_upddt) . '', ]; diff --git a/src/Epc.php b/src/Epc.php index d915869..8158b7c 100644 --- a/src/Epc.php +++ b/src/Epc.php @@ -10,6 +10,16 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +use ArrayObject; +use dcCore; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsElement; +use Exception; + # l10n __('entry excerpt'); __('entry content'); @@ -21,7 +31,7 @@ __('search results page'); __('atom feeds'); __('RSS feeds'); -class enhancePostContent +class Epc { protected static $default_filters = null; public static $epcFilterLimit = []; @@ -30,9 +40,9 @@ class enhancePostContent # Default definition # - public static function defaultAllowedTplValues() + public static function defaultAllowedTplValues(): array { - $rs = new arrayObject([ + $rs = new ArrayObject([ 'entry excerpt' => 'EntryExcerpt', 'entry content' => 'EntryContent', 'comment content' => 'CommentContent', @@ -43,28 +53,27 @@ class enhancePostContent return iterator_to_array($rs, true); } - public static function blogAllowedTplValues() + public static function blogAllowedTplValues(): array { - dcCore::app()->blog->settings->addNamespace(basename(dirname('../' . __DIR__))); $rs = json_decode(dcCore::app()->blog->settings->get(basename(dirname('../' . __DIR__)))->get('allowedtplvalues')); return is_array($rs) ? $rs : self::defaultAllowedTplValues(); } - public static function defaultAllowedWidgetValues() + public static function defaultAllowedWidgetValues(): array { - $rs = new arrayObject([ + $rs = new ArrayObject([ 'entry excerpt' => [ 'id' => 'entryexcerpt', - 'cb' => ['enhancePostContent','widgetContentEntryExcerpt'], + 'cb' => [self::class,'widgetContentEntryExcerpt'], ], 'entry content' => [ 'id' => 'entrycontent', - 'cb' => ['enhancePostContent','widgetContentEntryContent'], + 'cb' => [self::class,'widgetContentEntryContent'], ], 'comment content' => [ 'id' => 'commentcontent', - 'cb' => ['enhancePostContent','widgetContentCommentContent'], + 'cb' => [self::class,'widgetContentCommentContent'], ], ]); @@ -73,9 +82,9 @@ class enhancePostContent return iterator_to_array($rs, true); } - public static function defaultAllowedPubPages() + public static function defaultAllowedPubPages(): array { - $rs = new arrayObject([ + $rs = new ArrayObject([ 'home page' => 'home.html', 'post page' => 'post.html', 'category page' => 'category.html', @@ -89,19 +98,18 @@ class enhancePostContent return iterator_to_array($rs, true); } - public static function blogAllowedPubPages() + public static function blogAllowedPubPages(): array { - dcCore::app()->blog->settings->addNamespace(basename(dirname('../' . __DIR__))); - $rs = json_decode(dcCore::app()->blog->settings->get(basename(dirname('../' . __DIR__)))->get('allowedpubpages')); + $rs = json_decode(dcCore::app()->blog->settings->get(My::id())->get('allowedpubpages')); return is_array($rs) ? $rs : self::defaultAllowedPubPages(); } - public static function getFilters() + public static function getFilters(): ?array { if (self::$default_filters === null) { $final = $sort = []; - $filters = new arrayObject(); + $filters = new ArrayObject(); try { dcCore::app()->callBehavior('enhancePostContentFilters', $filters); @@ -122,7 +130,7 @@ class enhancePostContent return self::$default_filters; } - public static function testContext($tag, $args, $filter) + public static function testContext(string $tag, array $args, EpcFilter $filter): bool { return is_array($filter->pubPages) && in_array(dcCore::app()->ctx->current_tpl, $filter->pubPages) @@ -136,7 +144,7 @@ class enhancePostContent ; } - public static function replaceString($p, $r, $s, $filter, $before = '\b', $after = '\b') + public static function replaceString(string $p, string $r, string $s, EpcFilter $filter, string $before = '\b', string $after = '\b'): string { # Limit if ($filter->limit > 0) { @@ -164,7 +172,7 @@ class enhancePostContent $tags = implode('|', $ignore_tags); } if (!empty($tags)) { - $s = preg_replace_callback('#(<(' . $tags . ')[^>]*?>)(.*?)()#s', ['enhancePostContent', 'removeTags'], $s); + $s = preg_replace_callback('#(<(' . $tags . ')[^>]*?>)(.*?)()#s', [self::class, 'removeTags'], $s); } # Remove words inside html tag (class, title, alt, href, ...) $s = preg_replace('#(ççççç(' . $p . '(s|))ççççç)(?=[^<]+>)#s' . $i, '$2$4', $s); @@ -176,7 +184,7 @@ class enhancePostContent return $s = preg_replace('#ççççç(.*?)ççççç#s', '$1', $s); } - public static function matchString($p, $r, $s, $filter, $before = '\b', $after = '\b') + public static function matchString(string $p, string $r, string $s, EcpFilter $filter, string $before = '\b', string $after = '\b'): array { # Case sensitive $i = $filter->nocase ? 'i' : ''; @@ -202,22 +210,22 @@ class enhancePostContent return ['total' => $t, 'matches' => $m]; } - public static function quote($s) + public static function quote(string $s): string { return preg_quote($s, '#'); } - public static function removeTags($m) + public static function removeTags(array $m): string { return $m[1] . preg_replace('#ççççç(?!ççççç)#s', '$1', $m[3]) . $m[4]; } - public static function decodeTags($t) + public static function decodeTags(string $t): array { return preg_match_all('#([A-Za-z0-9]+)#', (string) $t, $m) ? $m[1] : []; } - public static function implode($a) + public static function implode(array|string $a): string { if (is_string($a)) { return $a; @@ -234,7 +242,7 @@ class enhancePostContent return $r; } - public static function explode($s) + public static function explode(array|string $s): array { if (is_array($s)) { return $s; @@ -256,8 +264,8 @@ class enhancePostContent continue; } - $key = html::escapeHTML(trim($cur[0])); - $val = html::escapeHTML(trim($cur[1])); + $key = Html::escapeHTML(trim($cur[0])); + $val = Html::escapeHTML(trim($cur[1])); if (empty($key) || empty($val)) { continue; @@ -273,7 +281,7 @@ class enhancePostContent # Widgets # - public static function widgetContentEntryExcerpt($w) + public static function widgetContentEntryExcerpt(?WidgetsElement $w = null): string { if (!dcCore::app()->ctx->exists('posts')) { return null; @@ -281,13 +289,13 @@ class enhancePostContent $res = ''; while (dcCore::app()->ctx->posts->fetch()) { - $res .= dcCore::app()->ctx->posts->post_excerpt; + $res .= dcCore::app()->ctx->posts->f('post_excerpt'); } return $res; } - public static function widgetContentEntryContent() + public static function widgetContentEntryContent(): string { if (!dcCore::app()->ctx->exists('posts')) { return null; @@ -295,13 +303,13 @@ class enhancePostContent $res = ''; while (dcCore::app()->ctx->posts->fetch()) { - $res .= dcCore::app()->ctx->posts->post_content; + $res .= dcCore::app()->ctx->posts->f('post_content'); } return $res; } - public static function widgetContentCommentContent() + public static function widgetContentCommentContent(): string { if (!dcCore::app()->ctx->exists('posts')) { return null; @@ -310,7 +318,7 @@ class enhancePostContent $res = ''; $post_ids = []; while (dcCore::app()->ctx->posts->fetch()) { - $comments = dcCore::app()->blog->getComments(['post_id' => dcCore::app()->ctx->posts->post_id]); + $comments = dcCore::app()->blog->getComments(['post_id' => dcCore::app()->ctx->posts->f('post_id')]); while ($comments->fetch()) { $res .= $comments->getContent(); } diff --git a/src/EpcFilter.php b/src/EpcFilter.php index 805597a..c5c35db 100644 --- a/src/EpcFilter.php +++ b/src/EpcFilter.php @@ -10,7 +10,16 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -abstract class epcFilter +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +use ArrayObject; +use dcCore; +use dcRecord; +use Dotclear\Plugin\widgets\WidgetsElement; + +abstract class EpcFilter { private $id = 'undefined'; private $records = null; @@ -42,7 +51,7 @@ abstract class epcFilter $this->blogSettings(); } - public static function create(arrayObject $o) + public static function create(ArrayObject $o): void { $c = get_called_class(); $o->append(new $c()); @@ -53,7 +62,7 @@ abstract class epcFilter return $this->id; } - final public function __get($k) + final public function __get(string $k): mixed { if (isset($this->properties[$k])) { return $this->properties[$k]; @@ -65,19 +74,19 @@ abstract class epcFilter return null; } - final public function __set($k, $v) + final public function __set(string $k, mixed $v): void { if (isset($this->settings[$k])) { $this->settings[$k] = $v; } } - final public function property($k) + final public function property(string $k): mixed { return $this->properties[$k] ?? null; } - final protected function setProperties($property, $value = null): bool + final protected function setProperties(array|string $property, mixed $value = null): bool { $properties = is_array($property) ? $property : [$property => $value]; foreach ($properties as $k => $v) { @@ -89,12 +98,12 @@ abstract class epcFilter return true; } - final public function setting($k) + final public function setting(string $k): mixed { return $this->settings[$k] ?? null; } - final protected function setSettings($setting, $value = null): bool + final protected function setSettings(array|string $setting, mixed $value = null): bool { $settings = is_array($setting) ? $setting : [$setting => $value]; foreach ($settings as $k => $v) { @@ -106,9 +115,9 @@ abstract class epcFilter return true; } - private function blogSettings() + private function blogSettings(): void { - $opt = json_decode((string) dcCore::app()->blog->settings->get(basename(dirname('../' . __DIR__)))->get($this->id)); + $opt = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get($this->id)); if (empty($opt)) { $opt = []; @@ -136,11 +145,10 @@ abstract class epcFilter } } - final public function records() + final public function records(): ?dcRecord { if ($this->records === null && $this->has_list) { - $records = new epcRecords(); - $this->records = $records->getRecords(['epc_filter' => $this->id()]); + $this->records = EpcRecord::getRecords(['epc_filter' => $this->id()]); } return $this->records; @@ -148,13 +156,13 @@ abstract class epcFilter abstract protected function init(): string; - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { - return null; + return; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { - return null; + return; } } diff --git a/src/EpcRecord.php b/src/EpcRecord.php index 8ee6b0c..28242af 100644 --- a/src/EpcRecord.php +++ b/src/EpcRecord.php @@ -10,20 +10,17 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class epcRecords +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +use cursor; +use dcCore; +use dcRecord; + +class EpcRecord { - public $con; - public $table; - public $blog; - - public function __construct() - { - $this->con = dcCore::app()->con; - $this->table = dcCore::app()->prefix . initEnhancePostContent::TABLE_NAME; - $this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id); - } - - public function getRecords($params, $count_only = false) + public static function getRecords(array $params, bool $count_only = false): dcRecord { if ($count_only) { $strReq = 'SELECT count(E.epc_id) '; @@ -37,19 +34,19 @@ class epcRecords 'E.epc_filter, E.epc_key, E.epc_value '; } - $strReq .= 'FROM ' . $this->table . ' E '; + $strReq .= 'FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' E '; if (!empty($params['from'])) { $strReq .= $params['from'] . ' '; } - $strReq .= "WHERE E.blog_id = '" . $this->blog . "' "; + $strReq .= "WHERE E.blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' "; if (isset($params['epc_type'])) { if (is_array($params['epc_type']) && !empty($params['epc_type'])) { - $strReq .= 'AND E.epc_type ' . $this->con->in($params['epc_type']); + $strReq .= 'AND E.epc_type ' . dcCore::app()->con->in($params['epc_type']); } elseif ($params['epc_type'] != '') { - $strReq .= "AND E.epc_type = '" . $this->con->escape($params['epc_type']) . "' "; + $strReq .= "AND E.epc_type = '" . dcCore::app()->con->escapeStr((string) $params['epc_type']) . "' "; } } else { $strReq .= "AND E.epc_type = 'epc' "; @@ -57,9 +54,9 @@ class epcRecords if (isset($params['epc_filter'])) { if (is_array($params['epc_filter']) && !empty($params['epc_filter'])) { - $strReq .= 'AND E.epc_filter ' . $this->con->in($params['epc_filter']); + $strReq .= 'AND E.epc_filter ' . dcCore::app()->con->in($params['epc_filter']); } elseif ($params['epc_filter'] != '') { - $strReq .= "AND E.epc_filter = '" . $this->con->escape($params['epc_filter']) . "' "; + $strReq .= "AND E.epc_filter = '" . dcCore::app()->con->escapeStr((string) $params['epc_filter']) . "' "; } } @@ -69,16 +66,16 @@ class epcRecords } else { $params['epc_id'] = [(int) $params['epc_id']]; } - $strReq .= 'AND E.epc_id ' . $this->con->in($params['epc_id']); + $strReq .= 'AND E.epc_id ' . dcCore::app()->con->in($params['epc_id']); } elseif (isset($params['not_id']) && is_numeric($params['not_id'])) { $strReq .= "AND NOT E.epc_id = '" . $params['not_id'] . "' "; } if (isset($params['epc_key'])) { if (is_array($params['epc_key']) && !empty($params['epc_key'])) { - $strReq .= 'AND E.epc_key ' . $this->con->in($params['epc_key']); + $strReq .= 'AND E.epc_key ' . dcCore::app()->con->in($params['epc_key']); } elseif ($params['epc_key'] != '') { - $strReq .= "AND E.epc_key = '" . $this->con->escape($params['epc_key']) . "' "; + $strReq .= "AND E.epc_key = '" . dcCore::app()->con->escapeStr((string) $params['epc_key']) . "' "; } } @@ -88,75 +85,71 @@ class epcRecords if (!$count_only) { if (!empty($params['order'])) { - $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; + $strReq .= 'ORDER BY ' . dcCore::app()->con->escapeStr((string) $params['order']) . ' '; } else { $strReq .= 'ORDER BY E.epc_key ASC '; } } if (!$count_only && !empty($params['limit'])) { - $strReq .= $this->con->limit($params['limit']); + $strReq .= dcCore::app()->con->limit($params['limit']); } - return $this->con->select($strReq); + return new dcRecord(dcCore::app()->con->select($strReq)); } - public function addRecord($cur) + public static function addRecord(cursor $cur): int { - $this->con->writeLock($this->table); + dcCore::app()->con->writeLock(dcCore::app()->prefix . My::TABLE_NAME); try { - $cur->epc_id = $this->getNextId(); - $cur->blog_id = $this->blog; - $cur->epc_upddt = date('Y-m-d H:i:s'); + $cur->setField('epc_id', self::getNextId()); + $cur->setField('blog_id', dcCore::app()->blog->id); + $cur->setField('epc_upddt', date('Y-m-d H:i:s')); - $this->getCursor($cur); + self::getCursor($cur); $cur->insert(); - $this->con->unlock(); + dcCore::app()->con->unlock(); } catch (Exception $e) { - $this->con->unlock(); + dcCore::app()->con->unlock(); throw $e; } - $this->trigger(); + self::trigger(); # --BEHAVIOR-- enhancePostContentAfterAddRecord dcCore::app()->callBehavior('enhancePostContentAfterAddRecord', $cur); - return $cur->epc_id; + return (int) $cur->getField('epc_id'); } - public function updRecord($id, $cur) + public static function updRecord(int $id, cursor $cur): void { - $id = (int) $id; - if (empty($id)) { throw new Exception(__('No such record ID')); } - $cur->epc_upddt = date('Y-m-d H:i:s'); + $cur->setField('epc_upddt', date('Y-m-d H:i:s')); - $cur->update('WHERE epc_id = ' . $id . " AND blog_id = '" . $this->blog . "' "); - $this->trigger(); + $cur->update('WHERE epc_id = ' . $id . " AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' "); + self::trigger(); # --BEHAVIOR-- enhancePostContentAfterUpdRecord dcCore::app()->callBehavior('enhancePostContentAfterUpdRecord', $cur, $id); } - public function isRecord($filter, $key, $not_id = null) + public static function isRecord(string $filter, string $key, int $not_id = null): bool { - return 0 < $this->getRecords([ + return 0 < self::getRecords([ 'epc_filter' => $filter, 'epc_key' => $key, 'not_id' => $not_id, ], true)->f(0); } - public function delRecord($id) + public static function delRecord(int $id): void { - $id = (int) $id; - if (empty($id)) { throw new Exception(__('No such record ID')); } @@ -164,42 +157,41 @@ class epcRecords # --BEHAVIOR-- enhancePostContentBeforeDelRecord dcCore::app()->callBehavior('enhancePostContentbeforeDelRecord', $id); - $this->con->execute( - 'DELETE FROM ' . $this->table . ' ' . + dcCore::app()->con->execute( + 'DELETE FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' ' . 'WHERE epc_id = ' . $id . ' ' . - "AND blog_id = '" . $this->blog . "' " + "AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' " ); - $this->trigger(); + self::trigger(); } - private function getNextId() + private static function getNextId(): int { - return $this->con->select( - 'SELECT MAX(epc_id) FROM ' . $this->table . ' ' + return (int) dcCore::app()->con->select( + 'SELECT MAX(epc_id) FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' ' )->f(0) + 1; } - public function openCursor() + public static function openCursor(): cursor { - return $this->con->openCursor($this->table); + return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME); } - private function getCursor($cur, $epc_id = null) + private static function getCursor(cursor $cur): void { - if ($cur->epc_key == '') { + if ($cur->getField('epc_key') == '') { throw new Exception(__('No record key')); } - if ($cur->epc_value == '') { + if ($cur->getField('epc_value') == '') { throw new Exception(__('No record value')); } - if ($cur->epc_filter == '') { + if ($cur->getField('epc_filter') == '') { throw new Exception(__('No record filter')); } - $epc_id = is_int($epc_id) ? $epc_id : $cur->epc_id; } - private function trigger() + private static function trigger(): void { dcCore::app()->blog->triggerBlog(); } diff --git a/src/Filter/EpcFilterAbbreviation.php b/src/Filter/EpcFilterAbbreviation.php index 2b47df1..264cd69 100644 --- a/src/Filter/EpcFilterAbbreviation.php +++ b/src/Filter/EpcFilterAbbreviation.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterAbbreviation extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterAbbreviation extends EpcFilter { protected function init(): string { @@ -39,31 +43,27 @@ class epcFilterAbbreviation extends epcFilter return 'abbreviation'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, __($this->records()->epc_value), '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { while ($this->records()->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $this->records()->epc_key, sprintf($this->widget, __($this->records()->epc_value), '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterAcronym.php b/src/Filter/EpcFilterAcronym.php index d3cfc53..5aa1ba8 100644 --- a/src/Filter/EpcFilterAcronym.php +++ b/src/Filter/EpcFilterAcronym.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterAcronym extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterAcronym extends EpcFilter { protected function init(): string { @@ -39,31 +43,27 @@ class epcFilterAcronym extends epcFilter return 'acronym'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, __($this->records()->epc_value), '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { while ($this->records()->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $this->records()->epc_key, sprintf($this->widget, __($this->records()->epc_value), '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterCitation.php b/src/Filter/EpcFilterCitation.php index 7a75bc3..43a8494 100644 --- a/src/Filter/EpcFilterCitation.php +++ b/src/Filter/EpcFilterCitation.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterCitation extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterCitation extends EpcFilter { protected function init(): string { @@ -40,31 +44,27 @@ class epcFilterCitation extends epcFilter return 'citation'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, __($this->records()->epc_value), '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { while ($this->records()->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $this->records()->epc_key, sprintf($this->widget, __($this->records()->epc_value), '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterDefinition.php b/src/Filter/EpcFilterDefinition.php index 39684fa..387c15d 100644 --- a/src/Filter/EpcFilterDefinition.php +++ b/src/Filter/EpcFilterDefinition.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterDefinition extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterDefinition extends EpcFilter { protected function init(): string { @@ -39,31 +43,27 @@ class epcFilterDefinition extends epcFilter return 'definition'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, __($this->records()->epc_value), '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { while ($this->records()->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $this->records()->epc_key, sprintf($this->widget, __($this->records()->epc_value), '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterLink.php b/src/Filter/EpcFilterLink.php index bc570be..c145e58 100644 --- a/src/Filter/EpcFilterLink.php +++ b/src/Filter/EpcFilterLink.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterLink extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterLink extends EpcFilter { protected function init(): string { @@ -39,31 +43,27 @@ class epcFilterLink extends epcFilter return 'link'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, '\\1', $this->records()->epc_value, '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { while ($this->records()->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $this->records()->epc_key, sprintf($this->widget, $this->records()->epc_value, $this->records()->epc_value, '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterReplace.php b/src/Filter/EpcFilterReplace.php index 11e049f..1d7c228 100644 --- a/src/Filter/EpcFilterReplace.php +++ b/src/Filter/EpcFilterReplace.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterReplace extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterReplace extends EpcFilter { protected function init(): string { @@ -40,17 +44,15 @@ class epcFilterReplace extends epcFilter return 'replace'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, $this->records()->epc_value, '\\2'), $args[0], $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterSearch.php b/src/Filter/EpcFilterSearch.php index c5dd2d8..b362d58 100644 --- a/src/Filter/EpcFilterSearch.php +++ b/src/Filter/EpcFilterSearch.php @@ -10,11 +10,16 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterSearch extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use dcCore; +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterSearch extends EpcFilter { protected function init(): string { @@ -39,23 +44,21 @@ class epcFilterSearch extends epcFilter return 'search'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { if (empty(dcCore::app()->public->search)) { - return null; + return; } $searchs = explode(' ', dcCore::app()->public->search); foreach ($searchs as $k => $v) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $v, sprintf($this->replace, '\\1'), $args[0], $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterTag.php b/src/Filter/EpcFilterTag.php index 8b33c3f..e81fe78 100644 --- a/src/Filter/EpcFilterTag.php +++ b/src/Filter/EpcFilterTag.php @@ -10,11 +10,16 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterTag extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use dcCore; +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterTag extends EpcFilter { protected function init(): string { @@ -38,43 +43,39 @@ class epcFilterTag extends epcFilter return 'tag'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { if (!dcCore::app()->plugins->moduleExists('tags')) { - return null; + return; } $metas = dcCore::app()->meta->getMetadata(['meta_type' => 'tag']); while ($metas->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $metas->meta_id, sprintf($this->replace, dcCore::app()->blog->url . dcCore::app()->url->getBase('tag') . '/' . $metas->meta_id, '\\1'), $args[0], $this ); } - - return null; } - public function widgetList($content, $w, &$list) + public function widgetList(string $content, WidgetsElement $w, array &$list): void { if (!dcCore::app()->plugins->moduleExists('tags')) { - return null; + return; } $metas = dcCore::app()->meta->getMetadata(['meta_type' => 'tag']); while ($metas->fetch()) { - $list[] = enhancePostContent::matchString( + $list[] = Epc::matchString( $metas->meta_id, sprintf($this->widget, dcCore::app()->blog->url . dcCore::app()->url->getBase('tag') . '/' . $metas->meta_id, '\\1'), $content, $this ); } - - return null; } } diff --git a/src/Filter/EpcFilterTwitter.php b/src/Filter/EpcFilterTwitter.php index 3d79d8a..030b284 100644 --- a/src/Filter/EpcFilterTwitter.php +++ b/src/Filter/EpcFilterTwitter.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterTwitter extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterTwitter extends EpcFilter { protected function init(): string { @@ -37,9 +41,9 @@ class epcFilterTwitter extends epcFilter return 'twitter'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( '[A-Za-z0-9_]{2,}', sprintf($this->replace, 'http://twitter.com/\\1', '\\1'), $args[0], @@ -47,7 +51,5 @@ class epcFilterTwitter extends epcFilter '[^@]@', '\b' ); - - return null; } } diff --git a/src/Filter/EpcFilterUpdate.php b/src/Filter/EpcFilterUpdate.php index 7f8d1bb..3092f0a 100644 --- a/src/Filter/EpcFilterUpdate.php +++ b/src/Filter/EpcFilterUpdate.php @@ -10,11 +10,15 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class epcFilterUpdate extends epcFilter +namespace Dotclear\Plugin\enhancePostContent\Filter; + +use Dotclear\Plugin\enhancePostContent\Epc; +use Dotclear\Plugin\enhancePostContent\EpcFilter; +use Dotclear\Plugin\widgets\WidgetsElement; + +class EpcFilterUpdate extends EpcFilter { protected function init(): string { @@ -40,17 +44,15 @@ class epcFilterUpdate extends epcFilter return 'update'; } - public function publicContent($tag, $args) + public function publicContent(string $tag, array $args): void { while ($this->records()->fetch()) { - $args[0] = enhancePostContent::replaceString( + $args[0] = Epc::replaceString( $this->records()->epc_key, sprintf($this->replace, '\\1', $this->records()->epc_value), $args[0], $this ); } - - return null; } } diff --git a/src/Frontend.php b/src/Frontend.php index d9c54cb..e951d21 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,30 +10,53 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -require __DIR__ . '/_widgets.php'; +namespace Dotclear\Plugin\enhancePostContent; -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); +use dcCore; +use dcNsProcess; +use dcUtils; -if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) { - return null; -} +class Frontend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = My::phpCompliant(); -// Add filters CSS to page header -dcCore::app()->addBehavior('publicHeadContent', function () { - echo dcUtils::cssLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('epccss')); -}); -// Filter template blocks content -dcCore::app()->addBehavior('publicBeforeContentFilterV2', function ($tag, $args) { - $filters = enhancePostContent::getFilters(); - - foreach ($filters as $id => $filter) { - if (!enhancePostContent::testContext($tag, $args, $filter)) { - continue; - } - $filter->publicContent($tag, $args); + return static::$init; } -}); + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + return null; + } + + dcCore::app()->addBehaviors([ + // add CSS URL to header + 'publicHeadContent' => function (): void { + echo dcUtils::cssLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('epccss')); + }, + // Filter template blocks content + 'publicBeforeContentFilterV2' => function (string $tag, array $args): void { + $filters = Epc::getFilters(); + + foreach ($filters as $id => $filter) { + if (!Epc::testContext($tag, $args, $filter)) { + continue; + } + $filter->publicContent($tag, $args); + } + }, + // Widgets + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } +} diff --git a/src/Install.php b/src/Install.php index 1d2ff1a..370f243 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,71 +10,200 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -try { - // Version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +namespace Dotclear\Plugin\enhancePostContent; + +use dbStruct; +use dcCore; +use dcNamespace; +use dcNsProcess; +use Exception; + +class Install extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && My::phpCompliant() + && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); + + return static::$init; } - // Database - $s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); - $s->{initEnhancePostContent::TABLE_NAME} - ->epc_id('bigint', 0, false) - ->blog_id('varchar', 32, false) - ->epc_type('varchar', 32, false, "'epc'") - ->epc_filter('varchar', 64, false) - ->epc_key('varchar', 255, false) - ->epc_value('text', 0, false) - ->epc_upddt('timestamp', 0, false, 'now()') + public static function process(): bool + { + if (!static::$init) { + return false; + } - ->primary('pk_epc', 'epc_id') - ->index('idx_epc_blog_id', 'btree', 'blog_id') - ->index('idx_epc_type', 'btree', 'epc_type') - ->index('idx_epc_filter', 'btree', 'epc_filter') - ->index('idx_epc_key', 'btree', 'epc_key'); + try { - $si = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); - $changes = $si->synchronize($s); - $s = null; + // Database + $s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); + $s->{My::TABLE_NAME} + ->epc_id('bigint', 0, false) + ->blog_id('varchar', 32, false) + ->epc_type('varchar', 32, false, "'epc'") + ->epc_filter('varchar', 64, false) + ->epc_key('varchar', 255, false) + ->epc_value('text', 0, false) + ->epc_upddt('timestamp', 0, false, 'now()') - // Uppgrade - epcUpgrade::growUp(); + ->primary('pk_epc', 'epc_id') + ->index('idx_epc_blog_id', 'btree', 'blog_id') + ->index('idx_epc_type', 'btree', 'epc_type') + ->index('idx_epc_filter', 'btree', 'epc_filter') + ->index('idx_epc_key', 'btree', 'epc_key'); - // Settings - $s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); + (new dbStruct(dcCore::app()->con, dcCore::app()->prefix))->synchronize($s); + $s = null; - $s->put('active', false, 'boolean', 'Enable enhancePostContent', false, true); - $s->put('list_sortby', 'epc_key', 'string', 'Admin records list field order', false, true); - $s->put('list_order', 'desc', 'string', 'Admin records list order', false, true); - $s->put('list_nb', 20, 'integer', 'Admin records list nb per page', false, true); - $s->put('allowedtplvalues', json_encode(enhancePostContent::defaultAllowedTplValues()), 'string', 'List of allowed template values', false, true); - $s->put('allowedpubpages', json_encode(enhancePostContent::defaultAllowedPubPages()), 'string', 'List of allowed template pages', false, true); + // Uppgrade + self::growUp(); - // Filters settings - $filters = enhancePostContent::getFilters(); - foreach ($filters as $id => $filter) { - // Only editable options - $opt = [ - 'nocase' => $filter->nocase, - 'plural' => $filter->plural, - 'style' => $filter->style, - 'notag' => $filter->notag, - 'tplValues' => $filter->tplValues, - 'pubPages' => $filter->pubPages, + // Settings + $s = dcCore::app()->blog->settings->get(My::id()); + + $s->put('active', false, 'boolean', 'Enable enhancePostContent', false, true); + $s->put('list_sortby', 'epc_key', 'string', 'Admin records list field order', false, true); + $s->put('list_order', 'desc', 'string', 'Admin records list order', false, true); + $s->put('list_nb', 20, 'integer', 'Admin records list nb per page', false, true); + $s->put('allowedtplvalues', json_encode(Epc::defaultAllowedTplValues()), 'string', 'List of allowed template values', false, true); + $s->put('allowedpubpages', json_encode(Epc::defaultAllowedPubPages()), 'string', 'List of allowed template pages', false, true); + + // Filters settings + $filters = Epc::getFilters(); + foreach ($filters as $id => $filter) { + // Only editable options + $opt = [ + 'nocase' => $filter->nocase, + 'plural' => $filter->plural, + 'style' => $filter->style, + 'notag' => $filter->notag, + 'tplValues' => $filter->tplValues, + 'pubPages' => $filter->pubPages, + ]; + $s->put($id, json_encode($opt), 'string', 'Settings for ' . $id, false, true); + } + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + + return false; + } + } + + /** + * Check upgrade to apply + */ + public static function growUp() + { + $current = dcCore::app()->getVersion(My::id()); + + if ($current && version_compare($current, '0.6.6', '<=')) { + self::upTo00060607(); + } + + if ($current && version_compare($current, '2021.10.06', '<=')) { + self::upTo20211006(); + } + + if ($current && version_compare($current, '2022.11.20', '<=')) { + self::upTo20221120(); + } + } + + /** + * 0.6.6 + * + * - filters move from settings to dedicated table + */ + private static function upTo00060607() + { + # Move old filters lists from settings to database + $record = dcCore::app()->con->select('SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . " WHERE setting_ns='enhancePostContent' AND blog_id IS NOT NULL "); + + while ($record->fetch()) { + if (preg_match('#enhancePostContent_(.*?)List#', $record->f('setting_id'), $m)) { + $curlist = @unserialize($record->f('setting_value')); + if (is_array($curlist)) { + foreach ($curlist as $k => $v) { + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME); + dcCore::app()->con->writeLock(dcCore::app()->prefix . My::TABLE_NAME); + + $cur->setField('epc_id', (int) dcCore::app()->con->select('SELECT MAX(epc_id) FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' ')->f(0) + 1); + $cur->setField('blog_id', $record->f('blog_id')); + $cur->setField('epc_filter', strtolower($m[1])); + $cur->setField('epc_key', $k); + $cur->setField('epc_value', $v); + + $cur->insert(); + dcCore::app()->con->unlock(); + } + } + dcCore::app()->con->execute('DELETE FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . " WHERE setting_id='" . $record->f('setting_id') . "' AND setting_ns='enhancePostContent' AND blog_id='" . $record->f('blog_id') . "' "); + } + } + } + + /** + * 2021.10.06 + * + * - filters change name to id + */ + private static function upTo20211006() + { + # Move old filter name to filter id + $record = dcCore::app()->con->select('SELECT epc_id, epc_filter FROM ' . dcCore::app()->prefix . My::TABLE_NAME); + while ($record->fetch()) { + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME); + + $cur->setField('epc_filter', strtolower($record->f('epc_filter'))); + + $cur->update('WHERE epc_id = ' . $record->f('epc_id') . ' '); + dcCore::app()->blog->triggerBlog(); + } + } + + /** + * 2022.11.20 + * + * - setting id changes to shorter one, + * - setting ns changes to abstract one (no real changes), + * - setting value change from serialize to json_encode (if it's array) + */ + private static function upTo20221120() + { + // list of settings using serialize values to move to json + $ids = [ + 'allowedtplvalues', + 'allowedpubpages', ]; - $s->put($id, json_encode($opt), 'string', 'Settings for ' . $id, false, true); + foreach (Epc::getFilters() as $id => $f) { + $ids[] = $id; + } + + // get all enhancePostContent settings + $record = dcCore::app()->con->select( + 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . + "WHERE setting_ns = 'enhancePostContent' " + ); + + // update settings id, ns, value + while ($record->fetch()) { + if (preg_match('/^enhancePostContent_(.*?)$/', $record->f('setting_id'), $match)) { + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); + $cur->setField('setting_id', $match[1]); + $cur->setField('setting_ns', My::id()); + + if (in_array($match[1], $ids)) { + $cur->setfield('setting_value', json_encode(unserialize($record->f('setting_value')))); + } + + $cur->update("WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'enhancePostContent' "); + } + } } - - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); } - -return false; diff --git a/src/Manage.php b/src/Manage.php index d4a1de9..7e1d978 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -10,329 +10,349 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -dcPage::check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_CONTENT_ADMIN, -])); +namespace Dotclear\Plugin\enhancePostContent; -# -- Prepare queries and object -- +use dcAdminFilters; +use adminGenericFilterV2; +use dcCore; +use dcNsProcess; +use dcPage; +use Dotclear\Helper\Html\Html; +use Dotclear\Helper\Network\Http; +use Exception; -$_filters = enhancePostContent::getFilters(); +use form; -$filters_id = $filters_combo = []; -foreach ($_filters as $id => $filter) { - $filters_id[$id] = $filter->name; - $filters_combo[$filter->name] = $id; -} +class Manage extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && My::phpCompliant() + && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcCore::app()->auth::PERMISSION_CONTENT_ADMIN, + ]), dcCore::app()->blog->id); -$action = $_POST['action'] ?? ''; -$part = $_REQUEST['part'] ?? key($filters_id); - -if (!isset($filters_id[$part])) { - return null; -} - -$header = ''; -$filter = $_filters[$part]; -$records = new epcRecords(); - -# -- Action -- - -if (!empty($action)) { - # --BEHAVIOR-- enhancePostContentAdminSave - dcCore::app()->callBehavior('enhancePostContentAdminSave'); -} - -try { - # Update filter settings - if ($action == 'savefiltersetting') { - # Parse filters options - $f = [ - 'nocase' => !empty($_POST['filter_nocase']), - 'plural' => !empty($_POST['filter_plural']), - 'limit' => abs((int) $_POST['filter_limit']), - 'style' => (array) $_POST['filter_style'], - 'notag' => (string) $_POST['filter_notag'], - 'tplValues' => (array) $_POST['filter_tplValues'], - 'pubPages' => (array) $_POST['filter_pubPages'], - ]; - - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - dcCore::app()->blog->settings->get(basename(__DIR__))->put($filter->id(), json_encode($f)); - - dcCore::app()->blog->triggerBlog(); - - dcAdminNotices::addSuccessNotice( - __('Filter successfully updated.') - ); - - dcCore::app()->adminurl->redirect( - 'admin.plugin.' . basename(__DIR__), - ['part' => $part], - '#settings' - ); + return static::$init; } - # Add new filter record - if ($action == 'savenewrecord' - && !empty($_POST['new_key']) - && !empty($_POST['new_value']) - ) { - $cur = $records->openCursor(); - $cur->epc_filter = $filter->id(); - $cur->epc_key = html::escapeHTML($_POST['new_key']); - $cur->epc_value = html::escapeHTML($_POST['new_value']); - - if ($records->isRecord($cur->epc_filter, $cur->epc_key)) { - dcAdminNotices::addErrorNotice(__('Key already exists for this filter')); - } else { - $records->addRecord($cur); - - dcCore::app()->blog->triggerBlog(); - - dcAdminNotices::addSuccessNotice( - __('Filter successfully updated.') - ); - } - dcCore::app()->adminurl->redirect( - 'admin.plugin.' . basename(__DIR__), - ['part' => $part], - '#record' - ); - } - - # Update filter records - if ($action == 'deleterecords' && $filter->has_list - && !empty($_POST['epc_id']) && is_array($_POST['epc_id']) - ) { - foreach ($_POST['epc_id'] as $id) { - $records->delRecord($id); + public static function process(): bool + { + if (!static::$init) { + return false; } - dcCore::app()->blog->triggerBlog(); + $current = ManageVars::init(); - dcAdminNotices::addSuccessNotice( - __('Filter successfully updated.') + if (dcCore::app()->error->flag()) { + return true; + } + + if (!empty($current->action)) { + # --BEHAVIOR-- enhancePostContentAdminSave + dcCore::app()->callBehavior('enhancePostContentAdminSave'); + } + + try { + # Update filter settings + if ($current->action == 'savefiltersetting') { + # Parse filters options + $f = [ + 'nocase' => !empty($_POST['filter_nocase']), + 'plural' => !empty($_POST['filter_plural']), + 'limit' => abs((int) $_POST['filter_limit']), + 'style' => (array) $_POST['filter_style'], + 'notag' => (string) $_POST['filter_notag'], + 'tplValues' => (array) $_POST['filter_tplValues'], + 'pubPages' => (array) $_POST['filter_pubPages'], + ]; + + dcCore::app()->blog->settings->get(My::id())->put($current->filter->id(), json_encode($f)); + + dcCore::app()->blog->triggerBlog(); + + dcPage::addSuccessNotice( + __('Filter successfully updated.') + ); + + dcCore::app()->adminurl->redirect( + 'admin.plugin.' . My::id(), + ['part' => $current->part], + '#settings' + ); + } + + # Add new filter record + if ($current->action == 'savenewrecord' + && !empty($_POST['new_key']) + && !empty($_POST['new_value']) + ) { + $cur = EpcRecord::openCursor(); + $cur->setField('epc_filter', $current->filter->id()); + $cur->setField('epc_key', Html::escapeHTML($_POST['new_key'])); + $cur->setField('epc_value', Html::escapeHTML($_POST['new_value'])); + + if (EpcRecord::isRecord($cur->getField('epc_filter'), $cur->getField('epc_key'))) { + dcPage::addErrorNotice(__('Key already exists for this filter')); + } else { + EpcRecord::addRecord($cur); + + dcCore::app()->blog->triggerBlog(); + + dcPage::addSuccessNotice( + __('Filter successfully updated.') + ); + } + dcCore::app()->adminurl->redirect( + 'admin.plugin.' . My::id(), + ['part' => $current->part], + '#record' + ); + } + + # Update filter records + if ($current->action == 'deleterecords' + && $current->filter->has_list + && !empty($_POST['epc_id']) + && is_array($_POST['epc_id']) + ) { + foreach ($_POST['epc_id'] as $id) { + EpcRecord::delRecord((int) $id); + } + + dcCore::app()->blog->triggerBlog(); + + dcPage::addSuccessNotice( + __('Filter successfully updated.') + ); + + if (!empty($_REQUEST['redir'])) { + Http::redirect($_REQUEST['redir']); + } else { + dcCore::app()->adminurl->redirect( + 'admin.plugin.' . My::id(), + ['part' => $current->part], + '#record' + ); + } + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + return true; + } + + public static function render(): void + { + if (!static::$init) { + return; + } + + $current = ManageVars::init(); + + # -- Prepare page -- + $header = ''; + if ($current->filter->has_list) { + $sorts = new adminGenericFilterV2('epc'); + $sorts->add(dcAdminFilters::getPageFilter()); + $sorts->add('part', $current->part); + + $params = $sorts->params(); + $params['epc_filter'] = $current->filter->id(); + + try { + $list = EpcRecord::getRecords($params); + $counter = EpcRecord::getRecords($params, true); + $pager = new BackendList($list, (int) $counter->f(0)); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + $header = $sorts->js(dcCore::app()->adminurl->get('admin.plugin.' . My::id(), ['part' => $current->part], '&') . '#record'); + } + + # Page headers + dcPage::openModule( + My::name(), + dcPage::jsPageTabs() . + dcPage::jsModuleLoad(My::id() . '/js/backend.js') . + $header . + + # --BEHAVIOR-- enhancePostContentAdminHeader + dcCore::app()->callBehavior('enhancePostContentAdminHeader') ); - if (!empty($_REQUEST['redir'])) { - http::redirect($_REQUEST['redir']); - } else { - dcCore::app()->adminurl->redirect( - 'admin.plugin.' . basename(__DIR__), - ['part' => $part], - '#record' - ); + # Page title + echo + dcPage::breadcrumb([ + __('Plugins') => '', + My::name() => '', + $current->filter->name => '', + ]) . + dcPage::notices() . + + # Filters select menu list + '
' . + '

' . + form::combo('part', $current->combo, $current->part) . ' ' . + '' . + form::hidden('p', My::id()) . '

' . + '
'; + + # Filter title and description + echo ' +

' . $current->filter->name . '

+

' . $current->filter->help . '

'; + + # Filter settings + echo ' +
+
+ +
+

' . __('Pages to be filtered') . '

'; + + foreach (Epc::blogAllowedPubPages() as $k => $v) { + echo ' +

'; } - } -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); -} -# -- Prepare page -- + echo ' +
+

' . __('Filtering') . '

-if ($filter->has_list) { - $sorts = new adminGenericFilter(dcCore::app(), 'epc'); - $sorts->add(dcAdminFilters::getPageFilter()); - $sorts->add('part', $part); +

- $params = $sorts->params(); - $params['epc_filter'] = $filter->id(); +

- try { - $list = $records->getRecords($params); - $counter = $records->getRecords($params, true); - $pager = new adminEpcList(dcCore::app(), $list, $counter->f(0)); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - } +

' . + form::number('filter_limit', ['min' => 0, 'max' => 99, 'default' => (int) $current->filter->limit]) . ' +

+

' . __('Leave it blank or set it to 0 for no limit') . '

- $header = $sorts->js(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), ['part' => $part], '&') . '#record'); -} +
+

' . __('Contents to be filtered') . '

'; -# -- Display page -- + foreach (Epc::blogAllowedTplValues() as $k => $v) { + echo ' +

'; + } -# Page headers -echo ' -' . __('Enhance post content') . '' . -dcPage::jsPageTabs() . -dcPage::jsModuleLoad(basename(__DIR__) . '/js/index.js') . -$header . + echo ' +
+

' . __('Style') . '

'; -# --BEHAVIOR-- enhancePostContentAdminHeader -dcCore::app()->callBehavior('enhancePostContentAdminHeader') . ' + foreach ($current->filter->class as $k => $v) { + echo ' +

' . + form::field( + ['filter_style[]', 'filter_style' . $k], + 60, + 255, + Html::escapeHTML($current->filter->style[$k]) + ) . + '

'; + } -' . + echo ' +

' . sprintf(__('The inserted HTML tag looks like: %s'), Html::escapeHTML(str_replace('%s', '...', $current->filter->replace))) . '

-# Page title -dcPage::breadcrumb([ - __('Plugins') => '', - __('Enhance post content') => '', - $filter->name => '', -]) . -dcPage::notices() . - -# Filters select menu list -'' . -'

' . -form::combo('part', $filters_combo, $part) . ' ' . -'' . -form::hidden('p', basename(__DIR__)) . '

' . -''; - -# Filter title and description -echo ' -

' . $filter->name . '

-

' . $filter->help . '

'; - -# Filter settings -echo ' -
-
- -
-

' . __('Pages to be filtered') . '

'; - -foreach (enhancePostContent::blogAllowedPubPages() as $k => $v) { - echo ' -

'; -} - -echo ' -
-

' . __('Filtering') . '

- -

- -

- -

' . -form::number('filter_limit', ['min' => 0, 'max' => 99, 'default' => (int) $filter->limit]) . ' -

-

' . __('Leave it blank or set it to 0 for no limit') . '

- -
-

' . __('Contents to be filtered') . '

'; - -foreach (enhancePostContent::blogAllowedTplValues() as $k => $v) { - echo ' -

'; -} - -echo ' -
-

' . __('Style') . '

'; - -foreach ($filter->class as $k => $v) { - echo ' -

' . - form::field( - ['filter_style[]', 'filter_style' . $k], - 60, - 255, - html::escapeHTML($filter->style[$k]) - ) . - '

'; -} - -echo ' -

' . sprintf(__('The inserted HTML tag looks like: %s'), html::escapeHTML(str_replace('%s', '...', $filter->replace))) . '

- -

' . -form::field('filter_notag', 60, 255, html::escapeHTML($filter->notag)) . ' -

-

' . __('This is the list of HTML tags where content will be ignored.') . ' ' . -('' != $filter->htmltag ? '' : sprintf(__('Tag "%s" always be ignored.'), $filter->htmltag)) . '

-
-
-

' . -dcCore::app()->formNonce() . -form::hidden(['action'], 'savefiltersetting') . -form::hidden(['part'], $part) . ' - -

-
- -
-
'; - -# Filter records list -if ($filter->has_list && isset($sorts) && isset($pager)) { - $pager_url = dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), array_diff_key($sorts->values(true), ['page' => ''])) . '&page=%s#record'; - - echo ' -
'; - - $sorts->display(['admin.plugin.' . basename(__DIR__), '#record'], form::hidden('p', basename(__DIR__)) . form::hidden('part', $part)); - - $pager->display( - $sorts, - $pager_url, - '
' . - '%s' . - - '
' . - '

' . - - '

' . - form::hidden('action', 'deleterecords') . - '

' . - dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.' . basename(__DIR__), array_merge(['p' => basename(__DIR__)], $sorts->values(true))) . - form::hidden('redir', dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), $sorts->values(true))) . +

' . + form::field('filter_notag', 60, 255, Html::escapeHTML($current->filter->notag)) . ' +

+

' . __('This is the list of HTML tags where content will be ignored.') . ' ' . + ('' != $current->filter->htmltag ? '' : sprintf(__('Tag "%s" always be ignored.'), $current->filter->htmltag)) . '

+
+
+

' . dcCore::app()->formNonce() . - '

' . - '
' - ); + form::hidden(['action'], 'savefiltersetting') . + form::hidden(['part'], $current->part) . ' + +

+
- echo '
'; + +
'; - # New record - echo ' -
-
' . + # Filter records list + if ($current->filter->has_list && isset($sorts) && isset($pager)) { + $pager_url = dcCore::app()->adminurl->get('admin.plugin.' . My::id(), array_diff_key($sorts->values(true), ['page' => ''])) . '&page=%s#record'; - '

' . - form::field('new_key', 60, 255, ['extra_html' => 'required']) . - '

' . + echo ' +
'; - '

' . - form::field('new_value', 60, 255, ['extra_html' => 'required']) . - '

+ $sorts->display(['admin.plugin.' . My::id(), '#record'], form::hidden('p', My::id()) . form::hidden('part', $current->part)); -

' . - form::hidden(['action'], 'savenewrecord') . - form::hidden(['part'], $part) . - dcCore::app()->formNonce() . ' - -

- -
'; + $pager->display( + $sorts, + $pager_url, + '
' . + '%s' . + + '
' . + '

' . + + '

' . + form::hidden('action', 'deleterecords') . + '

' . + dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.' . My::id(), array_merge(['p' => My::id()], $sorts->values(true))) . + form::hidden('redir', dcCore::app()->adminurl->get('admin.plugin.' . My::id(), $sorts->values(true))) . + dcCore::app()->formNonce() . + '
' . + '
' + ); + + echo '
'; + + # New record + echo ' +
+
' . + + '

' . + form::field('new_key', 60, 255, ['extra_html' => 'required']) . + '

' . + + '

' . + form::field('new_value', 60, 255, ['extra_html' => 'required']) . + '

+ +

' . + form::hidden(['action'], 'savenewrecord') . + form::hidden(['part'], $current->part) . + dcCore::app()->formNonce() . ' + +

+
+
'; + } + + # --BEHAVIOR-- enhancePostContentAdminPage + dcCore::app()->callBehavior('enhancePostContentAdminPage'); + + dcPage::helpBlock('enhancePostContent'); + dcPage::closeModule(); + } } - -# --BEHAVIOR-- enhancePostContentAdminPage -dcCore::app()->callBehavior('enhancePostContentAdminPage'); - -dcPage::helpBlock('enhancePostContent'); - -echo ''; diff --git a/src/ManageVars.php b/src/ManageVars.php new file mode 100644 index 0000000..5ca789e --- /dev/null +++ b/src/ManageVars.php @@ -0,0 +1,62 @@ + $filter) { + $filters_id[$id] = $filter->name; + $filters_combo[$filter->name] = $id; + } + + $part = $_REQUEST['part'] ?? key($filters_id); + + if (!isset($filters_id[$part])) { + throw new Exception(__('no filters')); + } + + $this->action = $_POST['action'] ?? ''; + $this->part = $part; + $this->filter = $_filters[$part]; + $this->combo = $filters_combo; + } + + public static function init(): ManageVars + { + if (!(self::$container instanceof self)) { + self::$container = new self(); + } + + return self::$container; + } +} diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..b6936ab --- /dev/null +++ b/src/My.php @@ -0,0 +1,67 @@ +plugins->moduleInfo(self::id(), 'name')); + } + + /** + * Check php version + */ + public static function phpCompliant(): bool + { + return version_compare(phpversion(), self::PHP_MIN, '>='); + } +} diff --git a/src/Prepend.php b/src/Prepend.php index a558dd6..eed14b8 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -10,69 +10,73 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -$filters = [ - 'Tag', - 'Search', - 'Acronym', - 'Abbreviation', - 'Definition', - 'Citation', - 'Link', - 'Replace', - 'Update', - 'Twitter', -]; +namespace Dotclear\Plugin\enhancePostContent; -$d = __DIR__ . '/inc/'; +use dcCore; +use dcNsProcess; +use Dotclear\Helper\Html\Html; -Clearbricks::lib()->autoload([ - 'enhancePostContent' => $d . 'class.enhancepostcontent.php', - 'epcFilter' => $d . 'class.epcfilter.php', - 'epcRecords' => $d . 'class.epcrecords.php', - 'epcUpgrade' => $d . 'class.epcupgrade.php', - 'adminEpcList' => $d . 'class.adminepclist.php', -]); +class Prepend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = My::phpCompliant(); -foreach ($filters as $f) { - Clearbricks::lib()->autoload(['epcFilter' . $f => $d . 'class.epcfilter' . strtolower($f) . '.php']); - dcCore::app()->addBehavior('enhancePostContentFilters', ['epcFilter' . $f, 'create']); -} + return static::$init; + } -dcCore::app()->url->register( - 'epccss', - 'epc.css', - '^epc\.css', - function ($args) { - $css = []; - $filters = enhancePostContent::getFilters(); - - foreach ($filters as $id => $filter) { - if ('' == $filter->class || '' == $filter->style) { - continue; - } - - $res = ''; - foreach ($filter->class as $k => $class) { - $styles = $filter->style; - $style = html::escapeHTML(trim($styles[$k])); - if ('' != $style) { - $res .= $class . ' {' . $style . '} '; - } - } - - if (!empty($res)) { - $css[] = '/* CSS for enhancePostContent ' . $id . " */ \n" . $res . "\n"; - } + public static function process(): bool + { + if (!static::$init) { + return false; } - header('Content-Type: text/css; charset=UTF-8'); + $dir = __DIR__ . DIRECTORY_SEPARATOR . 'Filter' . DIRECTORY_SEPARATOR; + $ns = __NAMESPACE__ . '\\Filter\\'; - echo implode("\n", $css); + dcCore::app()->autoload->addNamespace($ns, $dir); - exit; + foreach (My::DEFAULT_FILTERS as $f) { + dcCore::app()->addBehavior('enhancePostContentFilters', [$ns . 'EpcFilter' . $f, 'create']); + } + + dcCore::app()->url->register( + 'epccss', + 'epc.css', + '^epc\.css', + function (string $args): void { + $css = []; + $filters = Epc::getFilters(); + + foreach ($filters as $id => $filter) { + if ('' == $filter->class || '' == $filter->style) { + continue; + } + + $res = ''; + foreach ($filter->class as $k => $class) { + $styles = $filter->style; + $style = Html::escapeHTML(trim($styles[$k])); + if ('' != $style) { + $res .= $class . ' {' . $style . '} '; + } + } + + if (!empty($res)) { + $css[] = '/* CSS for enhancePostContent ' . $id . " */ \n" . $res . "\n"; + } + } + + header('Content-Type: text/css; charset=UTF-8'); + + echo implode("\n", $css); + + exit; + } + ); + + return true; } -); +} diff --git a/src/Uninstall.php b/src/Uninstall.php index 5132938..a97e668 100644 --- a/src/Uninstall.php +++ b/src/Uninstall.php @@ -10,94 +10,115 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return; +declare(strict_types=1); + +namespace Dotclear\Plugin\enhancePostContent; + +class Uninstall +{ + protected static bool $init = false; + + public static function init(): bool + { + self::$init = defined('DC_RC_PATH'); + + return self::$init; + } + + public static function process($uninstaller): ?bool + { + if (!self::$init) { + return false; + } + + $uninstaller->addUserAction( + /* type */ + 'settings', + /* action */ + 'delete_all', + /* ns */ + My::id(), + /* description */ + __('delete all settings') + ); + + $uninstaller->addUserAction( + /* type */ + 'tables', + /* action */ + 'delete', + /* ns */ + My::TABLE_NAME, + /* desc */ + __('delete table') + ); + + $uninstaller->addUserAction( + /* type */ + 'plugins', + /* action */ + 'delete', + /* ns */ + My::id(), + /* description */ + __('delete plugin files') + ); + + $uninstaller->addUserAction( + /* type */ + 'versions', + /* action */ + 'delete', + /* ns */ + My::id(), + /* description */ + __('delete the version number') + ); + + $uninstaller->addDirectAction( + /* type */ + 'settings', + /* action */ + 'delete_all', + /* ns */ + My::id(), + /* description */ + sprintf(__('delete all %s settings'), My::id()) + ); + + $uninstaller->addDirectAction( + /* type */ + 'tables', + /* action */ + 'delete', + /* ns */ + My::TABLE_NAME, + /* desc */ + sprintf(__('delete %s table'), My::id()) + ); + + $uninstaller->addDirectAction( + /* type */ + 'plugins', + /* action */ + 'delete', + /* ns */ + My::id(), + /* description */ + sprintf(__('delete %s plugin files'), My::id()) + ); + + $uninstaller->addDirectAction( + /* type */ + 'versions', + /* action */ + 'delete', + /* ns */ + My::id(), + /* description */ + sprintf(__('delete %s version number'),My::id()) + ); + + return true; + } } - -$this->addUserAction( - /* type */ - 'settings', - /* action */ - 'delete_all', - /* ns */ - basename(__DIR__), - /* description */ - __('delete all settings') -); - -$this->addUserAction( - /* type */ - 'tables', - /* action */ - 'delete', - /* ns */ - initEnhancePostContent::TABLE_NAME, - /* desc */ - __('delete table') -); - -$this->addUserAction( - /* type */ - 'plugins', - /* action */ - 'delete', - /* ns */ - basename(__DIR__), - /* description */ - __('delete plugin files') -); - -$this->addUserAction( - /* type */ - 'versions', - /* action */ - 'delete', - /* ns */ - basename(__DIR__), - /* description */ - __('delete the version number') -); - -$this->addDirectAction( - /* type */ - 'settings', - /* action */ - 'delete_all', - /* ns */ - basename(__DIR__), - /* description */ - sprintf(__('delete all %s settings'), basename(__DIR__)) -); - -$this->addDirectAction( - /* type */ - 'tables', - /* action */ - 'delete', - /* ns */ - initEnhancePostContent::TABLE_NAME, - /* desc */ - sprintf(__('delete %s table'), basename(__DIR__)) -); - -$this->addDirectAction( - /* type */ - 'plugins', - /* action */ - 'delete', - /* ns */ - basename(__DIR__), - /* description */ - sprintf(__('delete %s plugin files'), basename(__DIR__)) -); - -$this->addDirectAction( - /* type */ - 'versions', - /* action */ - 'delete', - /* ns */ - basename(__DIR__), - /* description */ - sprintf(__('delete %s version number'), basename(__DIR__)) -); diff --git a/src/Widgets.php b/src/Widgets.php index f86617a..7cb659e 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -10,33 +10,33 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -dcCore::app()->addBehavior( - 'initWidgets', - ['enhancePostContentWidget', 'adminContentList'] -); +namespace Dotclear\Plugin\enhancePostContent; + +use dcCore; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsStack; +use Dotclear\Plugin\widgets\WidgetsElement; /** * @ingroup DC_PLUGIN_ENHANCEPOSTCONTENT * @brief Filter posts content - widgets methods. * @since 2.6 */ -class enhancePostContentWidget +class Widgets { /** * Admin part for widget that show extracted content * * @param dcWidgets $w dcWidgets instance */ - public static function adminContentList($w) + public static function initWidgets(WidgetsStack $w): void { $w->create( 'epclist', - __('Enhance post content'), - ['enhancePostContentWidget', 'publicContentList'], + My::name(), + [self::class, 'parseWidget'], null, __('List filtered contents.') ); @@ -50,7 +50,7 @@ class enhancePostContentWidget 'text' ); # Type - $filters = enhancePostContent::getFilters(); + $filters = Epc::getFilters(); $types = []; foreach ($filters as $id => $filter) { $types[$filter->name] = $id; @@ -63,7 +63,7 @@ class enhancePostContentWidget $types ); # Content - $contents = enhancePostContent::defaultAllowedWidgetValues(); + $contents = Epc::defaultAllowedWidgetValues(); foreach ($contents as $k => $v) { $w->epclist->setting( 'content' . $v['id'], @@ -105,24 +105,22 @@ class enhancePostContentWidget * * @param dcWidget $w dcWidget instance */ - public static function publicContentList($w) + public static function parseWidget(WidgetsElement $w): string { if ($w->offline) { - return null; + return ''; } - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - # Page - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active') + if (!dcCore::app()->blog->settings->get(My::id())->get('active') || !in_array(dcCore::app()->ctx->current_tpl, ['post.html', 'page.html']) ) { - return null; + return ''; } # Content $content = ''; - foreach (enhancePostContent::defaultAllowedWidgetValues() as $k => $v) { + foreach (Epc::defaultAllowedWidgetValues() as $k => $v) { $ns = 'content' . $v['id']; if ($w->$ns && is_callable($v['cb'])) { $content .= call_user_func_array( @@ -133,12 +131,12 @@ class enhancePostContentWidget } if (empty($content)) { - return null; + return ''; } # Filter $list = []; - $filters = enhancePostContent::getFilters(); + $filters = Epc::getFilters(); if (isset($filters[$w->type])) { $filters[$w->type]->nocase = $w->nocase; @@ -147,7 +145,7 @@ class enhancePostContentWidget } if (empty($list)) { - return null; + return ''; } # Parse result @@ -162,16 +160,12 @@ class enhancePostContentWidget ''; } - if (empty($res)) { - return null; - } - - return $w->renderDiv( - $w->content_only, + return empty($res) ? '' : $w->renderDiv( + (bool) $w->content_only, $w->class, 'id="epc_' . $w->type . '"', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . - ($w->text ? '

' . html::escapeHTML($w->text) . '

' : '') . + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . + ($w->text ? '

' . Html::escapeHTML($w->text) . '

' : '') . '' ); }