From 592ff7dbbc1421281d0c0c243d89db117de7043c Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 25 Mar 2023 21:36:05 +0100 Subject: [PATCH] use namespace --- src/Backend.php | 32 +++++- src/Frontend.php | 100 ++++-------------- src/Widgets.php | 266 +++++++++++++++++++++++++++++++---------------- 3 files changed, 227 insertions(+), 171 deletions(-) diff --git a/src/Backend.php b/src/Backend.php index 40d440d..0c3ea96 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,8 +10,32 @@ * @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); -require __DIR__ . '/_widgets.php'; +namespace Dotclear\Plugin\lastBlogUpdate; + +use dcCore; +use dcNsProcess; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN'); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } +} diff --git a/src/Frontend.php b/src/Frontend.php index 5f1b08a..c5d9411 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,90 +10,32 @@ * @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\lastBlogUpdate; -function lastBlogUpdateWidgetPublic($w) +use dcCore; +use dcNsProcess; + +class Frontend extends dcNsProcess { - if ($w->offline) { - return null; + public static function init(): bool + { + static::$init = true; + + return static::$init; } - # Nothing to display - if (!$w->checkHomeOnly(dcCore::app()->url->type) - || !$w->blog_show && !$w->post_show && !$w->comment_show && !$w->media_show - || !$w->blog_text && !$w->post_text && !$w->comment_text && !$w->media_text) { - return null; - } - - $blog = $post = $comment = $media = $addons = ''; - - # Blog - if ($w->blog_show && $w->blog_text) { - $title = $w->blog_title ? sprintf('%s', html::escapeHTML($w->blog_title)) : ''; - $text = dt::str($w->blog_text, dcCore::app()->blog->upddt, dcCore::app()->blog->settings->system->blog_timezone); - $blog = sprintf('
  • %s%s
  • ', $title, $text); - } - - # Post - if ($w->post_show && $w->post_text) { - $rs = dcCore::app()->blog->getPosts(['limit' => 1, 'no_content' => true]); - if (!$rs->isEmpty()) { - $title = $w->post_title ? sprintf('%s', html::escapeHTML($w->post_title)) : ''; - $text = dt::str($w->post_text, strtotime($rs->post_upddt), dcCore::app()->blog->settings->system->blog_timezone); - $link = $rs->getURL(); - $over = $rs->post_title; - - $post = sprintf('
  • %s%s
  • ', $title, $link, $over, $text); + public static function process(): bool + { + if (!static::$init) { + return false; } + + dcCore::app()->addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; } - - # Comment - if ($w->comment_show && $w->comment_text) { - $rs = dcCore::app()->blog->getComments(['limit' => 1, 'no_content' => true]); - if (!$rs->isEmpty()) { - $title = $w->comment_title ? sprintf('%s', html::escapeHTML($w->comment_title)) : ''; - $text = dt::str($w->comment_text, strtotime($rs->comment_upddt), dcCore::app()->blog->settings->system->blog_timezone); - $link = dcCore::app()->blog->url . dcCore::app()->getPostPublicURL($rs->post_type, html::sanitizeURL($rs->post_url)) . '#c' . $rs->comment_id; - $over = $rs->post_title; - - $comment = sprintf('
  • %s%s
  • ', $title, $link, $over, $text); - } - } - - # Media - if ($w->media_show && $w->media_text) { - $rs = dcCore::app()->con->select( - 'SELECT media_upddt FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' . - "WHERE media_path='" . dcCore::app()->con->escape(dcCore::app()->blog->settings->system->public_path) . "' " . - 'ORDER BY media_upddt DESC ' . dcCore::app()->con->limit(1) - ); - - if (!$rs->isEmpty()) { - $title = $w->media_title ? sprintf('%s', html::escapeHTML($w->media_title)) : ''; - $text = dt::str($w->media_text, strtotime($rs->f('media_upddt')), dcCore::app()->blog->settings->system->blog_timezone); - - $media = sprintf('
  • %s%s
  • ', $title, $text); - } - } - - # --BEHAVIOR-- lastBlogUpdateWidgetParse - $addons = dcCore::app()->callBehavior('lastBlogUpdateWidgetParse', $w); - - # Nothing to display - if (!$blog && !$post && !$comment && !$media && !$addons) { - return null; - } - - # Display - return $w->renderDiv( - $w->content_only, - 'lastblogupdate ' . $w->class, - '', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . - sprintf('', $blog . $post . $comment . $media . $addons) - ); } diff --git a/src/Widgets.php b/src/Widgets.php index da830c7..899e6b8 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -10,102 +10,192 @@ * @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', 'lastBlogUpdateWidgetAdmin'); +namespace Dotclear\Plugin\lastBlogUpdate; -function lastBlogUpdateWidgetAdmin($w) +use dcCore; +use dcMedia; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsStack; +use Dotclear\Plugin\widgets\WidgetsElement; +use dt; + +class Widgets { - $w + public static function initWidgets(WidgetsStack $w): void + { + $w ->create( 'lastblogupdate', __('LastBlogUpdate: dates of lastest updates'), - 'lastBlogUpdateWidgetPublic', + [self::class, 'parseWidget'], null, 'Show the dates of last updates of your blog in a widget' ) - ->addTitle(__('Dates of lastest updates')) - ->setting( - 'blog_show', - __('Show blog update'), - 1, - 'check' - ) - ->setting( - 'blog_title', - __('Title for blog update:'), - __('Blog:'), - 'text' - ) - ->setting( - 'blog_text', - __('Text for blog update:'), - __('%Y-%m-%d %H:%M'), - 'text' - ) - ->setting( - 'post_show', - __('Show entry update'), - 1, - 'check' - ) - ->setting( - 'post_title', - __('Title for entries update:'), - __('Entries:'), - 'text' - ) - ->setting( - 'post_text', - __('Text for entries update:'), - __('%Y-%m-%d %H:%M'), - 'text' - ) - ->setting( - 'comment_show', - __('Show comment update'), - 1, - 'check' - ) - ->setting( - 'comment_title', - __('Title for comments update:'), - __('Comments:'), - 'text' - ) - ->setting( - 'comment_text', - __('Text for comments update:'), - __('%Y-%m-%d %H:%M'), - 'text' - ) - ->setting( - 'media_show', - __('Show media update'), - 1, - 'check' - ) - ->setting( - 'media_title', - __('Title for media update:'), - __('Medias:'), - 'text' - ) - ->setting( - 'media_text', - __('Text for media update:'), - __('%Y-%m-%d %H:%M'), - 'text' + ->addTitle(__('Dates of lastest updates')) + ->setting( + 'blog_show', + __('Show blog update'), + 1, + 'check' + ) + ->setting( + 'blog_title', + __('Title for blog update:'), + __('Blog:'), + 'text' + ) + ->setting( + 'blog_text', + __('Text for blog update:'), + __('%Y-%m-%d %H:%M'), + 'text' + ) + ->setting( + 'post_show', + __('Show entry update'), + 1, + 'check' + ) + ->setting( + 'post_title', + __('Title for entries update:'), + __('Entries:'), + 'text' + ) + ->setting( + 'post_text', + __('Text for entries update:'), + __('%Y-%m-%d %H:%M'), + 'text' + ) + ->setting( + 'comment_show', + __('Show comment update'), + 1, + 'check' + ) + ->setting( + 'comment_title', + __('Title for comments update:'), + __('Comments:'), + 'text' + ) + ->setting( + 'comment_text', + __('Text for comments update:'), + __('%Y-%m-%d %H:%M'), + 'text' + ) + ->setting( + 'media_show', + __('Show media update'), + 1, + 'check' + ) + ->setting( + 'media_title', + __('Title for media update:'), + __('Medias:'), + 'text' + ) + ->setting( + 'media_text', + __('Text for media update:'), + __('%Y-%m-%d %H:%M'), + 'text' + ); + + # --BEHAVIOR-- lastBlogUpdateWidgetInit + dcCore::app()->callBehavior('lastBlogUpdateWidgetInit', $w); + + $w->lastblogupdate + ->addHomeOnly() + ->addContentOnly() + ->addClass() + ->addOffline(); + } + + public static function parseWidget(WidgetsElement $w): string + { + if ($w->offline) { + return ''; + } + + # Nothing to display + if (!$w->checkHomeOnly(dcCore::app()->url->type) + || !$w->blog_show && !$w->post_show && !$w->comment_show && !$w->media_show + || !$w->blog_text && !$w->post_text && !$w->comment_text && !$w->media_text) { + return ''; + } + + $blog = $post = $comment = $media = $addons = ''; + + # Blog + if ($w->blog_show && $w->blog_text) { + $title = $w->blog_title ? sprintf('%s', Html::escapeHTML($w->blog_title)) : ''; + $text = dt::str($w->blog_text, (int) dcCore::app()->blog->upddt, dcCore::app()->blog->settings->get('system')->get('blog_timezone')); + $blog = sprintf('
  • %s %s
  • ', $title, $text); + } + + # Post + if ($w->post_show && $w->post_text) { + $rs = dcCore::app()->blog->getPosts(['limit' => 1, 'no_content' => true]); + if (!$rs->isEmpty()) { + $title = $w->post_title ? sprintf('%s', Html::escapeHTML($w->post_title)) : ''; + $text = dt::str($w->post_text, (int) strtotime($rs->f('post_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone')); + $link = $rs->getURL(); + $over = $rs->f('post_title'); + + $post = sprintf('
  • %s %s
  • ', $title, $link, $over, $text); + } + } + + # Comment + if ($w->comment_show && $w->comment_text) { + $rs = dcCore::app()->blog->getComments(['limit' => 1, 'no_content' => true]); + if (!$rs->isEmpty()) { + $title = $w->comment_title ? sprintf('%s', Html::escapeHTML($w->comment_title)) : ''; + $text = dt::str($w->comment_text, (int) strtotime($rs->f('comment_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone')); + $link = dcCore::app()->blog->url . dcCore::app()->getPostPublicURL($rs->f('post_type'), Html::sanitizeURL($rs->f('post_url'))) . '#c' . $rs->f('comment_id'); + $over = $rs->f('post_title'); + + $comment = sprintf('
  • %s %s
  • ', $title, $link, $over, $text); + } + } + + # Media + if ($w->media_show && $w->media_text) { + $rs = dcCore::app()->con->select( + 'SELECT media_upddt FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' . + "WHERE media_path='" . dcCore::app()->con->escapeStr(dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " . + 'ORDER BY media_upddt DESC ' . dcCore::app()->con->limit(1) + ); + + if (!$rs->isEmpty()) { + $title = $w->media_title ? sprintf('%s', Html::escapeHTML($w->media_title)) : ''; + $text = dt::str($w->media_text, (int) strtotime($rs->f('media_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone')); + + $media = sprintf('
  • %s %s
  • ', $title, $text); + } + } + + # --BEHAVIOR-- lastBlogUpdateWidgetParse + $addons = dcCore::app()->callBehavior('lastBlogUpdateWidgetParse', $w); + + # Nothing to display + if (!$blog && !$post && !$comment && !$media && !$addons) { + return ''; + } + + # Display + return $w->renderDiv( + (bool) $w->content_only, + 'lastblogupdate ' . $w->class, + '', + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . + sprintf('', $blog . $post . $comment . $media . $addons) ); - - # --BEHAVIOR-- lastBlogUpdateWidgetInit - dcCore::app()->callBehavior('lastBlogUpdateWidgetInit', $w); - - $w->lastblogupdate - ->addHomeOnly() - ->addContentOnly() - ->addClass() - ->addOffline(); + } }