From f76a9027f9842e3a5b531d27e0103b57c628e3a5 Mon Sep 17 00:00:00 2001 From: Bugo Date: Sun, 5 Jan 2025 10:36:40 +0500 Subject: [PATCH] Prevent some type errors --- .../Plugins/ArticleList/ArticleList.php | 19 +++++---- .../Plugins/BoardList/BoardList.php | 23 +++++------ .../Plugins/BoardNews/BoardNews.php | 11 +++-- .../Plugins/BoardStats/BoardStats.php | 17 ++++---- .../LightPortal/Plugins/Chart/Chart.php | 4 +- .../LightPortal/Plugins/Events/Events.php | 14 ++++--- .../Plugins/GalleryBlock/GalleryBlock.php | 8 ++-- src/Sources/LightPortal/Plugins/News/News.php | 5 ++- .../LightPortal/Plugins/PageList/PageList.php | 20 +++++----- .../LightPortal/Plugins/Polls/Polls.php | 7 ++-- .../Plugins/RandomPages/RandomPages.php | 16 +++++--- .../Plugins/RandomTopics/RandomTopics.php | 20 ++++++---- .../RecentAttachments/RecentAttachments.php | 13 ++++-- .../Plugins/RecentComments/RecentComments.php | 8 +++- .../Plugins/RecentPosts/RecentPosts.php | 23 ++++++----- .../Plugins/RecentPosts/template.php | 3 +- .../Plugins/RecentTopics/RecentTopics.php | 23 +++++++---- .../Plugins/RecentTopics/template.php | 3 +- .../Plugins/SimpleChat/SimpleChat.php | 4 +- .../Plugins/SimpleChat/template.php | 3 +- .../Plugins/SimpleFeeder/SimpleFeeder.php | 15 +++++-- .../LightPortal/Plugins/Swiper/Swiper.php | 3 +- .../LightPortal/Plugins/TagList/TagList.php | 18 ++++++--- .../Plugins/TinySlider/TinySlider.php | 9 ++--- .../Plugins/TopBoards/TopBoards.php | 8 ++-- .../LightPortal/Plugins/TopPages/TopPages.php | 34 ++++++++-------- .../Plugins/TopPosters/TopPosters.php | 13 +++--- .../Plugins/TopTopics/TopTopics.php | 24 ++++++----- .../Plugins/Translator/Translator.php | 3 +- .../Plugins/TrendingTopics/TrendingTopics.php | 16 ++++---- .../Plugins/WhosOnline/WhosOnline.php | 26 +++++++----- src/Sources/LightPortal/Utils/Content.php | 4 +- .../LightPortal/Utils/ParamWrapper.php | 40 +++++++++++++++++++ src/Sources/LightPortal/composer.json | 3 +- 34 files changed, 284 insertions(+), 176 deletions(-) create mode 100644 src/Sources/LightPortal/Utils/ParamWrapper.php diff --git a/src/Sources/LightPortal/Plugins/ArticleList/ArticleList.php b/src/Sources/LightPortal/Plugins/ArticleList/ArticleList.php index d52d5252e..fe07e9511 100644 --- a/src/Sources/LightPortal/Plugins/ArticleList/ArticleList.php +++ b/src/Sources/LightPortal/Plugins/ArticleList/ArticleList.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\ArticleList; @@ -32,8 +32,10 @@ use Bugo\LightPortal\UI\Partials\PageSelect; use Bugo\LightPortal\UI\Partials\TopicSelect; use Bugo\LightPortal\Utils\Content; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Setting; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -101,7 +103,7 @@ public function prepareBlockFields(Event $e): void ->setValue($options['seek_images']); } - public function getTopics(array $parameters): array + public function getTopics(ParamWrapper $parameters): array { if (empty($parameters['include_topics'])) return []; @@ -148,7 +150,7 @@ public function getTopics(array $parameters): array return $topics; } - public function getPages(array $parameters): array + public function getPages(ParamWrapper $parameters): array { if (empty($parameters['include_pages'])) return []; @@ -201,14 +203,17 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; + $type = Typed::int($parameters['display_type']); + $articles = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) - ->setFallback(fn() => empty($parameters['display_type']) ? $this->getTopics($parameters) : $this->getPages($parameters)); + ->setFallback(fn() => $type === 0 ? $this->getTopics($parameters) : $this->getPages($parameters)); if ($articles) { $articleList = Str::html('div')->class($this->name); + $bodyClass = Typed::string($parameters['body_class']); - if (empty($parameters['display_type'])) { + if ($type === 0) { foreach ($articles as $topic) { $content = Str::html(); @@ -230,7 +235,7 @@ public function prepareContent(Event $e): void ->setText($topic['title']) ); - $articleList->addHtml(sprintf(Utils::$context['lp_all_content_classes'][$parameters['body_class']], $content)); + $articleList->addHtml(sprintf(Utils::$context['lp_all_content_classes'][$bodyClass], $content)); } } else { foreach ($articles as $page) { @@ -258,7 +263,7 @@ public function prepareContent(Event $e): void ); $articleList->addHtml( - sprintf(Utils::$context['lp_all_content_classes'][$parameters['body_class']], $content) + sprintf(Utils::$context['lp_all_content_classes'][$bodyClass], $content) ); } } diff --git a/src/Sources/LightPortal/Plugins/BoardList/BoardList.php b/src/Sources/LightPortal/Plugins/BoardList/BoardList.php index 85cc7dbff..0a1928b80 100644 --- a/src/Sources/LightPortal/Plugins/BoardList/BoardList.php +++ b/src/Sources/LightPortal/Plugins/BoardList/BoardList.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\BoardList; @@ -26,6 +26,7 @@ use Bugo\LightPortal\Utils\Icon; use Bugo\LightPortal\Utils\MessageIndex; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -71,27 +72,25 @@ public function prepareBlockFields(Event $e): void ]); } - public function getData(): array - { - return MessageIndex::getBoardList(); - } - public function prepareContent(Event $e): void { - $parameters = $e->args->parameters; - $boardList = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . Utils::$context['user']['id']) ->setLifeTime($e->args->cacheTime) - ->setFallback(fn() => $this->getData()); + ->setFallback(fn() => MessageIndex::getBoardList()); if (empty($boardList)) return; Utils::$context['current_board'] ??= 0; + $parameters = $e->args->parameters; + + $categoryClass = Typed::string($parameters['category_class']); + $boardClass = Typed::string($parameters['board_class']); + foreach ($boardList as $category) { - if ($parameters['category_class']) { - echo sprintf($this->getCategoryClasses()[$parameters['category_class']], $category['name']); + if ($categoryClass) { + echo sprintf($this->getCategoryClasses()[$categoryClass], $category['name']); } $content = Str::html('ul')->class('smalltext'); @@ -123,7 +122,7 @@ public function prepareContent(Event $e): void $content->addHtml($li); } - echo sprintf(Utils::$context['lp_all_content_classes'][$parameters['board_class']], $content); + echo sprintf(Utils::$context['lp_all_content_classes'][$boardClass], $content); } } diff --git a/src/Sources/LightPortal/Plugins/BoardNews/BoardNews.php b/src/Sources/LightPortal/Plugins/BoardNews/BoardNews.php index d548f7286..b4d251c15 100644 --- a/src/Sources/LightPortal/Plugins/BoardNews/BoardNews.php +++ b/src/Sources/LightPortal/Plugins/BoardNews/BoardNews.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\BoardNews; @@ -25,6 +25,7 @@ use Bugo\LightPortal\UI\Fields\RangeField; use Bugo\LightPortal\Utils\MessageIndex; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -78,17 +79,15 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; - $teaserLength = empty($parameters['teaser_length']) ? null : $parameters['teaser_length']; - $boardNews = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) ->setFallback( fn() => $this->getFromSSI( 'boardNews', - (int) $parameters['board_id'], - (int) $parameters['num_posts'], + Typed::int($parameters['board_id']), + Typed::int($parameters['num_posts']), null, - $teaserLength, + Typed::int($parameters['teaser_length']), 'array' ) ); diff --git a/src/Sources/LightPortal/Plugins/BoardStats/BoardStats.php b/src/Sources/LightPortal/Plugins/BoardStats/BoardStats.php index 85d7526b6..fd7792418 100644 --- a/src/Sources/LightPortal/Plugins/BoardStats/BoardStats.php +++ b/src/Sources/LightPortal/Plugins/BoardStats/BoardStats.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\BoardStats; @@ -21,7 +21,9 @@ use Bugo\LightPortal\Plugins\Event; use Bugo\LightPortal\UI\Fields\CheckboxField; use Bugo\LightPortal\UI\Fields\NumberField; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -80,7 +82,7 @@ public function prepareBlockFields(Event $e): void ->setValue($options['update_interval']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { if ( empty($parameters['show_latest_member']) @@ -107,17 +109,14 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; + $cacheTime = Typed::int($parameters['update_interval']); + if ($this->request()->has('preview')) { - $parameters['update_interval'] = 0; + $cacheTime = 0; } - $parameters['show_latest_member'] ??= false; - $parameters['show_whos_online'] ??= false; - $parameters['show_basic_info'] ??= false; - $parameters['use_fa_icons'] ??= false; - $boardStats = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) - ->setLifeTime($parameters['update_interval'] ?? $e->args->cacheTime) + ->setLifeTime($cacheTime) ->setFallback(fn() => $this->getData($parameters)); if (empty($boardStats)) diff --git a/src/Sources/LightPortal/Plugins/Chart/Chart.php b/src/Sources/LightPortal/Plugins/Chart/Chart.php index 5fb4c39fd..7470358fd 100644 --- a/src/Sources/LightPortal/Plugins/Chart/Chart.php +++ b/src/Sources/LightPortal/Plugins/Chart/Chart.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\Chart; @@ -137,7 +137,7 @@ public function prepareContent(Event $e): void $type = $parameters['chart_type'] ?? $this->params['chart_type']; - $datasets = Utils::jsonDecode($parameters['datasets'] ?? $this->params['datasets'], true); + $datasets = Utils::jsonDecode($parameters['datasets'] ?? $this->params['datasets'], true) ?? []; array_walk($datasets, static fn(&$val) => $val['data'] = explode(', ', (string) $val['data'])); $datasets = json_encode($datasets); diff --git a/src/Sources/LightPortal/Plugins/Events/Events.php b/src/Sources/LightPortal/Plugins/Events/Events.php index 59acd38ee..aa0a3ba80 100644 --- a/src/Sources/LightPortal/Plugins/Events/Events.php +++ b/src/Sources/LightPortal/Plugins/Events/Events.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\Events; @@ -22,6 +22,8 @@ use Bugo\LightPortal\UI\Fields\CheckboxField; use Bugo\LightPortal\UI\Fields\NumberField; use Bugo\LightPortal\UI\Fields\RangeField; +use Bugo\LightPortal\Utils\ParamWrapper; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -88,14 +90,14 @@ public function changeIconSet(Event $e): void $e->args->set['event'] = 'fas fa-calendar-days'; } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $now = time(); $todayDate = date('Y-m-d', $now); $futureDate = empty($parameters['days_in_future']) ? $todayDate - : date('Y-m-d', ($now + $parameters['days_in_future'] * 24 * 60 * 60)); + : date('Y-m-d', ($now + (int) $parameters['days_in_future'] * 24 * 60 * 60)); $options = [ 'show_birthdays' => (bool) $parameters['show_birthdays'], @@ -110,12 +112,14 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; + $cacheTime = Typed::int($parameters['update_interval']); + if ($this->request()->has('preview')) { - $parameters['update_interval'] = 0; + $cacheTime = 0; } $data = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) - ->setLifeTime($parameters['update_interval'] ?? $e->args->cacheTime) + ->setLifeTime($cacheTime) ->setFallback(fn() => $this->getData($parameters)); $this->setTemplate(); diff --git a/src/Sources/LightPortal/Plugins/GalleryBlock/GalleryBlock.php b/src/Sources/LightPortal/Plugins/GalleryBlock/GalleryBlock.php index 2e646c85e..68fb9adf1 100644 --- a/src/Sources/LightPortal/Plugins/GalleryBlock/GalleryBlock.php +++ b/src/Sources/LightPortal/Plugins/GalleryBlock/GalleryBlock.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\GalleryBlock; @@ -23,7 +23,9 @@ use Bugo\LightPortal\Plugins\Event; use Bugo\LightPortal\UI\Fields\CustomField; use Bugo\LightPortal\UI\Fields\NumberField; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -64,7 +66,7 @@ public function prepareBlockFields(Event $e): void ->setValue($e->args->options['num_images']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { if (empty(Db::$db->list_tables(false, Config::$db_prefix . 'gallery_pic'))) return []; @@ -85,7 +87,7 @@ public function getData(array $parameters): array [ 'approved' => 1, 'categories' => $categories, - 'limit' => $parameters['num_images'], + 'limit' => Typed::int($parameters['num_images'], default: 10), ] ); diff --git a/src/Sources/LightPortal/Plugins/News/News.php b/src/Sources/LightPortal/Plugins/News/News.php index d3f4be9e8..1b712cd66 100644 --- a/src/Sources/LightPortal/Plugins/News/News.php +++ b/src/Sources/LightPortal/Plugins/News/News.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\News; @@ -19,6 +19,7 @@ use Bugo\LightPortal\Plugins\Block; use Bugo\LightPortal\Plugins\Event; use Bugo\LightPortal\UI\Fields\SelectField; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -64,6 +65,6 @@ public function getData(int $item = 0): string public function prepareContent(Event $e): void { - echo $this->getData($e->args->parameters['selected_item']) ?: $this->txt['no_items']; + echo $this->getData(Typed::int($e->args->parameters['selected_item'])) ?: $this->txt['no_items']; } } diff --git a/src/Sources/LightPortal/Plugins/PageList/PageList.php b/src/Sources/LightPortal/Plugins/PageList/PageList.php index 69b6f33ce..7350ff6a9 100644 --- a/src/Sources/LightPortal/Plugins/PageList/PageList.php +++ b/src/Sources/LightPortal/Plugins/PageList/PageList.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 24.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\PageList; @@ -29,8 +29,10 @@ use Bugo\LightPortal\UI\Partials\EntryTypeSelect; use Bugo\LightPortal\UI\Partials\CategorySelect; use Bugo\LightPortal\Utils\DateTime; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Setting; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -93,15 +95,15 @@ public function prepareBlockFields(Event $e): void ->setValue($options['num_pages']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $titles = app('title_list'); - $allCategories = app('category_list'); $categories = empty($parameters['categories']) ? null : explode(',', (string) $parameters['categories']); - - $type = empty($parameters['types']) ? EntryType::DEFAULT->name() : $parameters['types']; + $sort = Typed::string($parameters['sort'], default: 'page_id'); + $numPages = Typed::int($parameters['num_pages'], default: 10); + $type = Typed::string($parameters['types'], default: EntryType::DEFAULT->name()); $result = Db::$db->query('', ' SELECT @@ -124,8 +126,8 @@ public function getData(array $parameters): array 'current_time' => time(), 'permissions' => Permission::all(), 'categories' => $categories, - 'sort' => $parameters['sort'], - 'limit' => $parameters['num_pages'], + 'sort' => $sort, + 'limit' => $numPages, ] ); @@ -157,11 +159,9 @@ public function getData(array $parameters): array public function prepareContent(Event $e): void { - $parameters = $e->args->parameters; - $pageList = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) - ->setFallback(fn() => $this->getData($parameters)); + ->setFallback(fn() => $this->getData($e->args->parameters)); if ($pageList) { $ul = Str::html('ul', ['class' => 'normallist page_list']); diff --git a/src/Sources/LightPortal/Plugins/Polls/Polls.php b/src/Sources/LightPortal/Plugins/Polls/Polls.php index 20920cebf..1a12d4411 100644 --- a/src/Sources/LightPortal/Plugins/Polls/Polls.php +++ b/src/Sources/LightPortal/Plugins/Polls/Polls.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\Polls; @@ -23,6 +23,7 @@ use Bugo\LightPortal\UI\Fields\InputField; use Bugo\LightPortal\UI\Fields\SelectField; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -63,9 +64,7 @@ public function prepareBlockFields(Event $e): void public function prepareContent(Event $e): void { - $parameters = $e->args->parameters; - - $poll = $this->getFromSSI('showPoll', $parameters['selected_item'], 'array'); + $poll = $this->getFromSSI('showPoll', Typed::int($e->args->parameters['selected_item']), 'array'); if ($poll) { if ($poll['allow_vote']) { diff --git a/src/Sources/LightPortal/Plugins/RandomPages/RandomPages.php b/src/Sources/LightPortal/Plugins/RandomPages/RandomPages.php index 6f203f10e..9f59def2f 100644 --- a/src/Sources/LightPortal/Plugins/RandomPages/RandomPages.php +++ b/src/Sources/LightPortal/Plugins/RandomPages/RandomPages.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 24.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RandomPages; @@ -28,7 +28,9 @@ use Bugo\LightPortal\UI\Fields\NumberField; use Bugo\LightPortal\UI\Partials\CategorySelect; use Bugo\LightPortal\Utils\DateTime; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -85,11 +87,11 @@ public function prepareBlockFields(Event $e): void ->setValue($options['show_num_views']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $excludeCategories = empty($parameters['exclude_categories']) ? null : explode(',', (string) $parameters['exclude_categories']); $includeCategories = empty($parameters['include_categories']) ? null : explode(',', (string) $parameters['include_categories']); - $pagesCount = empty($parameters['num_pages']) ? 0 : (int) $parameters['num_pages']; + $pagesCount = Typed::int($parameters['num_pages']); if (empty($pagesCount)) return []; @@ -169,8 +171,11 @@ public function getData(array $parameters): array Db::$db->free_result($result); - if (empty($pageIds)) - return $this->getData(array_merge($parameters, ['num_pages' => $pagesCount - 1])); + if (empty($pageIds)) { + $parameters['num_pages'] = $pagesCount - 1; + + return $this->getData($parameters); + } $result = Db::$db->query('', ' SELECT @@ -234,7 +239,6 @@ public function getData(array $parameters): array public function prepareContent(Event $e): void { $parameters = $e->args->parameters; - $parameters['show_num_views'] ??= false; $randomPages = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) diff --git a/src/Sources/LightPortal/Plugins/RandomTopics/RandomTopics.php b/src/Sources/LightPortal/Plugins/RandomTopics/RandomTopics.php index 3edf485bf..5f8b7c85a 100644 --- a/src/Sources/LightPortal/Plugins/RandomTopics/RandomTopics.php +++ b/src/Sources/LightPortal/Plugins/RandomTopics/RandomTopics.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RandomTopics; @@ -25,7 +25,9 @@ use Bugo\LightPortal\UI\Fields\NumberField; use Bugo\LightPortal\UI\Partials\BoardSelect; use Bugo\LightPortal\Utils\DateTime; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -82,11 +84,11 @@ public function prepareBlockFields(Event $e): void ->setValue($options['show_num_views']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $excludeBoards = empty($parameters['exclude_boards']) ? null : explode(',', (string) $parameters['exclude_boards']); $includeBoards = empty($parameters['include_boards']) ? null : explode(',', (string) $parameters['include_boards']); - $topicsCount = empty($parameters['num_topics']) ? 0 : (int) $parameters['num_topics']; + $topicsCount = Typed::int($parameters['num_topics']); if (empty($topicsCount)) return []; @@ -150,8 +152,11 @@ public function getData(array $parameters): array Db::$db->free_result($result); - if (empty($topicIds)) - return $this->getData(array_merge($parameters, ['num_topics' => $topicsCount - 1])); + if (empty($topicIds)) { + $parameters['num_topics'] = $topicsCount - 1; + + return $this->getData($parameters); + } $result = Db::$db->query('', ' SELECT @@ -202,10 +207,10 @@ public function getData(array $parameters): array $topics = []; while ($row = Db::$db->fetch_assoc($result)) { $topics[] = [ - 'author_id' => $row['id_member'] ?? 0, + 'author_id' => (int) $row['id_member'], 'author_name' => $row['poster_name'], 'time' => (int) $row['poster_time'], - 'num_views' => $row['num_views'], + 'num_views' => (int) $row['num_views'], 'href' => Config::$scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new', 'title' => $row['subject'], 'is_new' => empty($row['is_read']), @@ -220,7 +225,6 @@ public function getData(array $parameters): array public function prepareContent(Event $e): void { $parameters = $e->args->parameters; - $parameters['show_num_views'] ??= false; $randomTopics = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) diff --git a/src/Sources/LightPortal/Plugins/RecentAttachments/RecentAttachments.php b/src/Sources/LightPortal/Plugins/RecentAttachments/RecentAttachments.php index c60d76309..b92e6460b 100644 --- a/src/Sources/LightPortal/Plugins/RecentAttachments/RecentAttachments.php +++ b/src/Sources/LightPortal/Plugins/RecentAttachments/RecentAttachments.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RecentAttachments; @@ -19,7 +19,9 @@ use Bugo\LightPortal\Plugins\Event; use Bugo\LightPortal\UI\Fields\NumberField; use Bugo\LightPortal\UI\Fields\TextField; +use Bugo\LightPortal\Utils\ParamWrapper; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -61,11 +63,16 @@ public function prepareBlockFields(Event $e): void ->setValue($options['extensions']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $extensions = empty($parameters['extensions']) ? [] : explode(',', (string) $parameters['extensions']); - return $this->getFromSSI('recentAttachments', $parameters['num_attachments'], $extensions, 'array'); + return $this->getFromSSI( + 'recentAttachments', + Typed::int($parameters['num_attachments'], default: 5), + $extensions, + 'array' + ); } public function prepareContent(Event $e): void diff --git a/src/Sources/LightPortal/Plugins/RecentComments/RecentComments.php b/src/Sources/LightPortal/Plugins/RecentComments/RecentComments.php index 4d7fa320f..010e2c842 100644 --- a/src/Sources/LightPortal/Plugins/RecentComments/RecentComments.php +++ b/src/Sources/LightPortal/Plugins/RecentComments/RecentComments.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RecentComments; @@ -23,6 +23,7 @@ use Bugo\LightPortal\UI\Fields\RangeField; use Bugo\LightPortal\Utils\DateTime; use Bugo\LightPortal\Utils\Str; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -128,7 +129,10 @@ public function prepareContent(Event $e): void $comments = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) ->setLifeTime($e->args->cacheTime) - ->setFallback(fn() => $this->getData((int) $parameters['num_comments'], (int) $parameters['length'])); + ->setFallback(fn() => $this->getData( + Typed::int($parameters['num_comments'], default: 10), + Typed::int($parameters['length'], default: 80) + )); if (empty($comments)) return; diff --git a/src/Sources/LightPortal/Plugins/RecentPosts/RecentPosts.php b/src/Sources/LightPortal/Plugins/RecentPosts/RecentPosts.php index ac2b16040..eadb696b5 100644 --- a/src/Sources/LightPortal/Plugins/RecentPosts/RecentPosts.php +++ b/src/Sources/LightPortal/Plugins/RecentPosts/RecentPosts.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RecentPosts; @@ -27,6 +27,8 @@ use Bugo\LightPortal\UI\Partials\TopicSelect; use Bugo\LightPortal\Utils\Avatar; use Bugo\LightPortal\Utils\DateTime; +use Bugo\LightPortal\Utils\ParamWrapper; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -140,16 +142,18 @@ public function prepareBlockFields(Event $e): void ->setValue($options['update_interval']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $excludeBoards = empty($parameters['exclude_boards']) ? [] : explode(',', (string) $parameters['exclude_boards']); $includeBoards = empty($parameters['include_boards']) ? [] : explode(',', (string) $parameters['include_boards']); $excludeTopics = empty($parameters['exclude_topics']) ? [] : explode(',', (string) $parameters['exclude_topics']); $includeTopics = empty($parameters['include_topics']) ? [] : explode(',', (string) $parameters['include_topics']); + $numPosts = Typed::int($parameters['num_posts'], default: 10); + $minMessageId = Config::$modSettings['maxMsgID'] - ( empty(Utils::$context['min_message_posts']) ? 25 : Utils::$context['min_message_posts'] - ) * min((int) $parameters['num_posts'], 5); + ) * min($numPosts, 5); $whereQuery = ' m.id_msg >= {int:min_message_id}' . (empty($excludeBoards) ? '' : ' @@ -173,10 +177,10 @@ public function getData(array $parameters): array 'queryPosts', $whereQuery, $whereQueryParams, - (int) $parameters['num_posts'], + $numPosts, 'm.id_msg DESC', 'array', - (bool) $parameters['limit_body'] + Typed::bool($parameters['limit_body']) ); if (empty($posts)) @@ -197,15 +201,14 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; + $cacheTime = Typed::int($parameters['update_interval']); + if ($this->request()->has('preview')) { - $parameters['update_interval'] = 0; + $cacheTime = 0; } - $parameters['show_avatars'] ??= false; - $parameters['limit_body'] ??= false; - $recentPosts = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) - ->setLifeTime($parameters['update_interval'] ?? $e->args->cacheTime) + ->setLifeTime($cacheTime) ->setFallback(fn() => $this->getData($parameters)); if (empty($recentPosts)) diff --git a/src/Sources/LightPortal/Plugins/RecentPosts/template.php b/src/Sources/LightPortal/Plugins/RecentPosts/template.php index 788d8cd39..e91dc860b 100644 --- a/src/Sources/LightPortal/Plugins/RecentPosts/template.php +++ b/src/Sources/LightPortal/Plugins/RecentPosts/template.php @@ -2,8 +2,9 @@ use Bugo\Compat\Config; use Bugo\Compat\Lang; +use Bugo\LightPortal\Utils\ParamWrapper; -function show_posts(array $posts, array $parameters, bool $full_width): void +function show_posts(array $posts, ParamWrapper $parameters, bool $full_width): void { if ($full_width) { echo ' diff --git a/src/Sources/LightPortal/Plugins/RecentTopics/RecentTopics.php b/src/Sources/LightPortal/Plugins/RecentTopics/RecentTopics.php index 1fd5c3f68..156134803 100644 --- a/src/Sources/LightPortal/Plugins/RecentTopics/RecentTopics.php +++ b/src/Sources/LightPortal/Plugins/RecentTopics/RecentTopics.php @@ -8,7 +8,7 @@ * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later * * @category plugin - * @version 22.12.24 + * @version 05.01.25 */ namespace Bugo\LightPortal\Plugins\RecentTopics; @@ -25,6 +25,8 @@ use Bugo\LightPortal\UI\Partials\BoardSelect; use Bugo\LightPortal\Utils\Avatar; use Bugo\LightPortal\Utils\DateTime; +use Bugo\LightPortal\Utils\ParamWrapper; +use WPLake\Typed\Typed; if (! defined('LP_NAME')) die('No direct access...'); @@ -117,12 +119,18 @@ public function prepareBlockFields(Event $e): void ->setValue($options['update_interval']); } - public function getData(array $parameters): array + public function getData(ParamWrapper $parameters): array { $excludeBoards = empty($parameters['exclude_boards']) ? null : explode(',', (string) $parameters['exclude_boards']); $includeBoards = empty($parameters['include_boards']) ? null : explode(',', (string) $parameters['include_boards']); - $topics = $this->getFromSSI('recentTopics', (int) $parameters['num_topics'], $excludeBoards, $includeBoards, 'array'); + $topics = $this->getFromSSI( + 'recentTopics', + Typed::int($parameters['num_topics'], default: 10), + $excludeBoards, + $includeBoards, + 'array' + ); if (empty($topics)) return []; @@ -142,15 +150,14 @@ public function prepareContent(Event $e): void { $parameters = $e->args->parameters; + $cacheTime = Typed::int($parameters['update_interval']); + if ($this->request()->has('preview')) { - $parameters['update_interval'] = 0; + $cacheTime = 0; } - $parameters['show_avatars'] ??= false; - $parameters['num_topics'] ??= 10; - $recentTopics = $this->cache($this->name . '_addon_b' . $e->args->id . '_u' . User::$info['id']) - ->setLifeTime($parameters['update_interval'] ?? $e->args->cacheTime) + ->setLifeTime($cacheTime) ->setFallback(fn() => $this->getData($parameters)); if (empty($recentTopics)) diff --git a/src/Sources/LightPortal/Plugins/RecentTopics/template.php b/src/Sources/LightPortal/Plugins/RecentTopics/template.php index 2d9aa6a5b..d5f41a414 100644 --- a/src/Sources/LightPortal/Plugins/RecentTopics/template.php +++ b/src/Sources/LightPortal/Plugins/RecentTopics/template.php @@ -3,8 +3,9 @@ use Bugo\Compat\Config; use Bugo\Compat\Lang; use Bugo\Compat\Utils; +use Bugo\LightPortal\Utils\ParamWrapper; -function show_topics(array $topics, array $parameters, bool $full_width): void +function show_topics(array $topics, ParamWrapper $parameters, bool $full_width): void { if ($full_width) { echo ' diff --git a/src/Sources/LightPortal/Plugins/SimpleChat/SimpleChat.php b/src/Sources/LightPortal/Plugins/SimpleChat/SimpleChat.php index 05bec2d6a..df36feac3 100644 --- a/src/Sources/LightPortal/Plugins/SimpleChat/SimpleChat.php +++ b/src/Sources/LightPortal/Plugins/SimpleChat/SimpleChat.php @@ -25,6 +25,8 @@ use Bugo\LightPortal\UI\Fields\RadioField; use Bugo\LightPortal\Utils\Avatar; +use Bugo\LightPortal\Utils\ParamWrapper; + use function array_combine; use function json_encode; use function show_chat_block; @@ -112,7 +114,7 @@ public function prepareBlockFields(Event $e): void ->setValue($options['window_height']); } - public function getData(int $block_id, array $parameters): array + public function getData(int $block_id, ParamWrapper $parameters): array { $messages = $this->chat->getMessages($block_id); diff --git a/src/Sources/LightPortal/Plugins/SimpleChat/template.php b/src/Sources/LightPortal/Plugins/SimpleChat/template.php index 011d92b1c..8153d6bf3 100644 --- a/src/Sources/LightPortal/Plugins/SimpleChat/template.php +++ b/src/Sources/LightPortal/Plugins/SimpleChat/template.php @@ -3,8 +3,9 @@ use Bugo\Compat\Config; use Bugo\Compat\Lang; use Bugo\Compat\Utils; +use Bugo\LightPortal\Utils\ParamWrapper; -function show_chat_block(int $id, array $parameters, bool $isInSidebar): void +function show_chat_block(int $id, ParamWrapper $parameters, bool $isInSidebar): void { echo /** @lang text */ '