Skip to content

Commit

Permalink
Prevent some type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Jan 5, 2025
1 parent c552ff9 commit f76a902
Show file tree
Hide file tree
Showing 34 changed files with 284 additions and 176 deletions.
19 changes: 12 additions & 7 deletions src/Sources/LightPortal/Plugins/ArticleList/ArticleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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 [];
Expand Down Expand Up @@ -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 [];
Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
);
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/Sources/LightPortal/Plugins/BoardList/BoardList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/Sources/LightPortal/Plugins/BoardNews/BoardNews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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'
)
);
Expand Down
17 changes: 8 additions & 9 deletions src/Sources/LightPortal/Plugins/BoardStats/BoardStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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'])
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/Sources/LightPortal/Plugins/Chart/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 9 additions & 5 deletions src/Sources/LightPortal/Plugins/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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'],
Expand All @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions src/Sources/LightPortal/Plugins/GalleryBlock/GalleryBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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 [];
Expand All @@ -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),
]
);

Expand Down
5 changes: 3 additions & 2 deletions src/Sources/LightPortal/Plugins/News/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...');
Expand Down Expand Up @@ -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'];
}
}
Loading

0 comments on commit f76a902

Please sign in to comment.