fix nullsafe warnings

This commit is contained in:
Jean-Christian Paul Denis 2023-04-22 14:15:17 +02:00
parent db5713dd2b
commit ee0a70c634
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951

View File

@ -28,7 +28,12 @@ class Widgets
{ {
public static function initWidgets(WidgetsStack $w): void public static function initWidgets(WidgetsStack $w): void
{ {
# Create widget // nullsafe
if (is_null(dcCore::app()->blog)) {
return;
}
// Create widget
$w->create( $w->create(
'lastpostsextend', 'lastpostsextend',
__('Last entries (Extended)'), __('Last entries (Extended)'),
@ -36,17 +41,18 @@ class Widgets
null, null,
__('Extended list of entries') __('Extended list of entries')
); );
# Title // Title
$w->lastpostsextend->addTitle(__('Last entries')); $w->lastpostsextend->addTitle(__('Last entries'));
# type
// post type
$posttypes = [ $posttypes = [
__('Post') => 'post', __('Post') => 'post',
__('Page') => 'page', __('Page') => 'page',
__('Gallery') => 'galitem', __('Gallery') => 'galitem',
]; ];
# plugin muppet types // plugin muppet types
if (dcCore::app()->plugins->moduleExists('muppet')) { if (dcCore::app()->plugins->moduleExists('muppet') && class_exists('\muppet')) {
$muppet_types = muppet::getPostTypes(); $muppet_types = \muppet::getPostTypes();
if (is_array($muppet_types) && !empty($muppet_types)) { if (is_array($muppet_types) && !empty($muppet_types)) {
foreach ($muppet_types as $k => $v) { foreach ($muppet_types as $k => $v) {
$posttypes[$v['name']] = $k; $posttypes[$v['name']] = $k;
@ -60,7 +66,8 @@ class Widgets
'combo', 'combo',
$posttypes $posttypes
); );
# Category (post and page have same category)
// Category (post and page have same category)
$rs = dcCore::app()->blog->getCategories([ $rs = dcCore::app()->blog->getCategories([
'post_type' => 'post', 'post_type' => 'post',
]); ]);
@ -82,7 +89,8 @@ class Widgets
$categories $categories
); );
unset($rs, $categories); unset($rs, $categories);
# Pasworded
// Passworded
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'passworded', 'passworded',
__('Protection:'), __('Protection:'),
@ -94,7 +102,8 @@ class Widgets
__('only with password') => 'yes', __('only with password') => 'yes',
] ]
); );
# Status
// Status
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'status', 'status',
__('Status:'), __('Status:'),
@ -108,21 +117,24 @@ class Widgets
__('published') => '1', __('published') => '1',
] ]
); );
# Selected entries only
// Selected entries only
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'selectedonly', 'selectedonly',
__('Selected entries only'), __('Selected entries only'),
0, 0,
'check' 'check'
); );
# Updated entries only
// Updated entries only
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'updatedonly', 'updatedonly',
__('Updated entries only'), __('Updated entries only'),
0, 0,
'check' 'check'
); );
# Tag
// Tag
if (dcCore::app()->plugins->moduleExists('tags')) { if (dcCore::app()->plugins->moduleExists('tags')) {
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'tag', 'tag',
@ -131,21 +143,24 @@ class Widgets
'text' 'text'
); );
} }
# Search
// Search
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'search', 'search',
__('Limit to words:'), __('Limit to words:'),
'', '',
'text' 'text'
); );
# Entries limit
// Entries limit
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'limit', 'limit',
__('Entries limit:'), __('Entries limit:'),
10, 10,
'text' 'text'
); );
# Sort type
// Sort
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'sortby', 'sortby',
__('Order by:'), __('Order by:'),
@ -157,7 +172,6 @@ class Widgets
__('Comments') => 'nb_comment', __('Comments') => 'nb_comment',
] ]
); );
# Sort order
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'sort', 'sort',
__('Sort:'), __('Sort:'),
@ -168,7 +182,8 @@ class Widgets
__('Descending') => 'desc', __('Descending') => 'desc',
] ]
); );
# First image
// First image
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'firstimage', 'firstimage',
__('Show entries first image:'), __('Show entries first image:'),
@ -183,28 +198,32 @@ class Widgets
__('original') => 'o', __('original') => 'o',
] ]
); );
# With excerpt
// With excerpt
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'excerpt', 'excerpt',
__('Show entries excerpt'), __('Show entries excerpt'),
0, 0,
'check' 'check'
); );
# Excerpt length
// Excerpt cut length
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'excerptlen', 'excerptlen',
__('Excerpt length:'), __('Excerpt length:'),
100, 100,
'text' 'text'
); );
# Comment count
// Comment count
$w->lastpostsextend->setting( $w->lastpostsextend->setting(
'commentscount', 'commentscount',
__('Show comments count'), __('Show comments count'),
0, 0,
'check' 'check'
); );
# common
// commons
$w->lastpostsextend $w->lastpostsextend
->addHomeOnly() ->addHomeOnly()
->addContentOnly() ->addContentOnly()
@ -214,40 +233,41 @@ class Widgets
public static function parseWidget(WidgetsElement $w): string public static function parseWidget(WidgetsElement $w): string
{ {
// Widget is offline & Home page only
if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->blog) || $w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return '';
}
// Need posts excerpt
if ($w->excerpt) {
$params['columns'][] = 'post_excerpt';
}
// prepare request params
$params = [ $params = [
'sql' => '', 'sql' => '',
'columns' => [], 'columns' => [],
'from' => '', 'from' => '',
]; ];
# Widget is offline & Home page only // Passworded
if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return '';
}
# Need posts excerpt
if ($w->excerpt) {
$params['columns'][] = 'post_excerpt';
}
# Passworded
if ($w->passworded == 'yes') { if ($w->passworded == 'yes') {
$params['sql'] .= 'AND post_password IS NOT NULL '; $params['sql'] .= 'AND post_password IS NOT NULL ';
} elseif ($w->passworded == 'no') { } elseif ($w->passworded == 'no') {
$params['sql'] .= 'AND post_password IS NULL '; $params['sql'] .= 'AND post_password IS NULL ';
} }
# Status // Status
if ($w->status != 'all') { if ($w->status != 'all') {
$params['post_status'] = $w->status; $params['post_status'] = $w->status;
} }
# Search words // Search words
if ('' != $w->search) { if ('' != $w->search) {
$params['search'] = $w->search; $params['search'] = $w->search;
} }
# Updated posts only // Updated posts only
if ($w->updatedonly) { if ($w->updatedonly) {
$params['sql'] .= 'AND post_creadt < post_upddt ' . $params['sql'] .= 'AND post_creadt < post_upddt ' .
'AND post_dt < post_upddt '; 'AND post_dt < post_upddt ';
@ -266,15 +286,15 @@ class Widgets
$params['limit'] = abs((int) $w->limit); $params['limit'] = abs((int) $w->limit);
$params['no_content'] = true; $params['no_content'] = true;
# Selected posts only // Selected posts only
if ($w->selectedonly) { if ($w->selectedonly) {
$params['post_selected'] = 1; $params['post_selected'] = 1;
} }
# Type // Post type
$params['post_type'] = $w->posttype; $params['post_type'] = $w->posttype;
# Category // Category
if ($w->category) { if ($w->category) {
if ($w->category == 'null') { if ($w->category == 'null') {
$params['sql'] .= ' AND P.cat_id IS NULL '; $params['sql'] .= ' AND P.cat_id IS NULL ';
@ -285,7 +305,7 @@ class Widgets
} }
} }
# Tags // Tags
if (dcCore::app()->plugins->moduleExists('tags') && $w->tag) { if (dcCore::app()->plugins->moduleExists('tags') && $w->tag) {
$tags = explode(',', $w->tag); $tags = explode(',', $w->tag);
foreach ($tags as $i => $tag) { foreach ($tags as $i => $tag) {
@ -303,15 +323,18 @@ class Widgets
false false
); );
# No result // No result
if ($rs->isEmpty()) { if ($rs->isEmpty()) {
return ''; return '';
} }
# Return // Parse result
$res = $w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : ''; $res = $w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '';
while ($rs->fetch()) { while ($rs->fetch()) {
if (is_null(dcCore::app()->blog)) { // phpstan ignores previous check !?
break;
}
$published = ((int) $rs->f('post_status')) == dcBlog::POST_PUBLISHED; $published = ((int) $rs->f('post_status')) == dcBlog::POST_PUBLISHED;
$res .= '<li>' . $res .= '<li>' .
@ -328,12 +351,12 @@ class Widgets
Html::escapeHTML($rs->f('post_title')) . Html::escapeHTML($rs->f('post_title')) .
'</' . ($published ? 'a' : 'span') . '>'; '</' . ($published ? 'a' : 'span') . '>';
# Nb comments // Nb comments
if ($w->commentscount && $published) { if ($w->commentscount && $published) {
$res .= ' (' . $rs->nb_comment . ')'; $res .= ' (' . $rs->nb_comment . ')';
} }
# First image // First image
if ($w->firstimage != '') { if ($w->firstimage != '') {
$res .= self::entryFirstImage( $res .= self::entryFirstImage(
$rs->f('post_type'), $rs->f('post_type'),
@ -342,7 +365,7 @@ class Widgets
); );
} }
# Excerpt // Excerpt
if ($w->excerpt) { if ($w->excerpt) {
$excerpt = $rs->f('post_excerpt'); $excerpt = $rs->f('post_excerpt');
if ($rs->f('post_format') == 'wiki') { if ($rs->f('post_format') == 'wiki') {
@ -373,7 +396,7 @@ class Widgets
private static function entryFirstImage(string $type, int|string $id, string $size = 's'): string private static function entryFirstImage(string $type, int|string $id, string $size = 's'): string
{ {
if (!in_array($type, ['post', 'page', 'galitem'])) { if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->blog) || !in_array($type, ['post', 'page', 'galitem'])) {
return ''; return '';
} }
@ -391,8 +414,8 @@ class Widgets
$size = 's'; $size = 's';
} }
$p_url = dcCore::app()->blog->settings->get('system')->get('public_url'); $p_url = (string) dcCore::app()->blog->settings->get('system')->get('public_url');
$p_site = preg_replace( $p_site = (string) preg_replace(
'#^(.+?//.+?)/(.*)$#', '#^(.+?//.+?)/(.*)$#',
'$1', '$1',
dcCore::app()->blog->url dcCore::app()->blog->url