remove all the magic, fix nullsafe warnings

This commit is contained in:
Jean-Christian Denis 2023-04-23 14:45:04 +02:00
parent 9356b136b8
commit 2682b07c4a
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951

View file

@ -15,12 +15,16 @@ declare(strict_types=1);
namespace Dotclear\Plugin\postInfoWidget; namespace Dotclear\Plugin\postInfoWidget;
use dcCore; use dcCore;
use dcRecord; use Dotclear\Database\MetaRecord;
use Dotclear\Helper\Date; use Dotclear\Helper\{
use Dotclear\Helper\Html\Html; Date,
use Dotclear\Helper\L10n; Html\Html,
use Dotclear\Plugin\widgets\WidgetsStack; L10n
use Dotclear\Plugin\widgets\WidgetsElement; };
use Dotclear\Plugin\widgets\{
WidgetsStack,
WidgetsElement
};
class Widgets class Widgets
{ {
@ -73,7 +77,7 @@ class Widgets
); );
if (dcCore::app()->plugins->moduleExists('tags')) { if (dcCore::app()->plugins->moduleExists('tags')) {
$w->postinfowidget->setting( $w->__get('postinfowidget')->setting(
'tag_str', 'tag_str',
__('Tags text (%T = tags list):'), __('Tags text (%T = tags list):'),
__('Tags: %T'), __('Tags: %T'),
@ -81,7 +85,7 @@ class Widgets
); );
} }
$w->postinfowidget $w->__get('postinfowidget')
->setting( ->setting(
'attachment_str', 'attachment_str',
__('Attachments text (%T = text, %D = numeric):'), __('Attachments text (%T = text, %D = numeric):'),
@ -171,7 +175,7 @@ class Widgets
# --BEHAVIOR-- postInfoWidgetAdmin # --BEHAVIOR-- postInfoWidgetAdmin
dcCore::app()->callBehavior('postInfoWidgetAdmin', $w); dcCore::app()->callBehavior('postInfoWidgetAdmin', $w);
$w->postinfowidget $w->__get('postinfowidget')
->addContentOnly() ->addContentOnly()
->addClass() ->addClass()
->addOffline(); ->addOffline();
@ -179,58 +183,61 @@ class Widgets
public static function publicWidget(WidgetsElement $w): string public static function publicWidget(WidgetsElement $w): string
{ {
if ($w->offline) { // nullsafe
if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->ctx)) {
return ''; return '';
} }
if (dcCore::app()->url->type != 'post' if ($w->__get('offline')
|| !dcCore::app()->ctx->posts->f('post_id')) { || dcCore::app()->url->type != 'post'
|| !dcCore::app()->ctx->__get('posts')->f('post_id')
) {
return ''; return '';
} }
$link = '<a href="%s">%s</a>'; $link = '<a href="%s">%s</a>';
$content = ''; $content = '';
if ($w->dt_str != '') { if ($w->__get('dt_str') != '') {
$content .= self::li( $content .= self::li(
$w, $w,
'date', 'date',
Date::str( Date::str(
$w->dt_str, $w->__get('dt_str'),
(int) strtotime(dcCore::app()->ctx->posts->f('post_dt')), (int) strtotime(dcCore::app()->ctx->__get('posts')->f('post_dt')),
dcCore::app()->blog->settings->get('system')->get('blog_timezone') dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->creadt_str != '') { if ($w->__get('creadt_str') != '') {
$content .= self::li( $content .= self::li(
$w, $w,
'create', 'create',
Date::str( Date::str(
$w->creadt_str, $w->__get('creadt_str'),
(int) strtotime(dcCore::app()->ctx->posts->post_creadt), (int) strtotime(dcCore::app()->ctx->__get('posts')->post_creadt),
dcCore::app()->blog->settings->get('system')->get('blog_timezone') dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->upddt_str != '') { if ($w->__get('upddt_str') != '') {
$content .= self::li( $content .= self::li(
$w, $w,
'update', 'update',
Date::str( Date::str(
$w->upddt_str, $w->__get('upddt_str'),
(int) strtotime(dcCore::app()->ctx->posts->f('post_upddt')), (int) strtotime(dcCore::app()->ctx->__get('posts')->f('post_upddt')),
dcCore::app()->blog->settings->get('system')->get('blog_timezone') dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->lang_str != '') { if ($w->__get('lang_str') != '') {
$ln = L10n::getISOcodes(); $ln = L10n::getISOcodes();
$lang_code = dcCore::app()->ctx->posts->f('post_lang') ? $lang_code = dcCore::app()->ctx->__get('posts')->f('post_lang') ?
dcCore::app()->ctx->posts->f('post_lang') : dcCore::app()->ctx->__get('posts')->f('post_lang') :
dcCore::app()->blog->settings->get('system')->get('lang'); dcCore::app()->blog->settings->get('system')->get('lang');
$lang_name = $ln[$lang_code] ?? $lang_code; $lang_name = $ln[$lang_code] ?? $lang_code;
$lang_flag = file_exists( $lang_flag = file_exists(
@ -249,24 +256,24 @@ class Widgets
str_replace( str_replace(
['%T', '%C', '%F'], ['%T', '%C', '%F'],
[$lang_name, $lang_code, $lang_flag], [$lang_name, $lang_code, $lang_flag],
Html::escapeHTML($w->lang_str) Html::escapeHTML($w->__get('lang_str'))
) )
); );
} }
if ($w->author_str != '') { if ($w->__get('author_str') != '') {
$content .= self::li( $content .= self::li(
$w, $w,
'author', 'author',
str_replace( str_replace(
'%T', '%T',
dcCore::app()->ctx->posts->getAuthorLink(), dcCore::app()->ctx->__get('posts')->getAuthorLink(),
Html::escapeHTML($w->author_str) Html::escapeHTML($w->__get('author_str'))
) )
); );
} }
if ($w->category_str != '' && dcCore::app()->ctx->posts->f('cat_id')) { if ($w->__get('category_str') != '' && dcCore::app()->ctx->__get('posts')->f('cat_id')) {
$content .= self::li( $content .= self::li(
$w, $w,
'category', 'category',
@ -274,18 +281,18 @@ class Widgets
'%T', '%T',
sprintf( sprintf(
$link, $link,
dcCore::app()->ctx->posts->getCategoryURL(), dcCore::app()->ctx->__get('posts')->__call('getCategoryURL', []),
Html::escapeHTML(dcCore::app()->ctx->posts->f('cat_title')) Html::escapeHTML(dcCore::app()->ctx->__get('posts')->f('cat_title'))
), ),
Html::escapeHTML($w->category_str) Html::escapeHTML($w->__get('category_str'))
) )
); );
} }
if ($w->tag_str != '' && dcCore::app()->plugins->moduleExists('tags')) { if ($w->__get('tag_str') != '' && dcCore::app()->plugins->moduleExists('tags')) {
$meta = dcCore::app()->meta->getMetadata([ $meta = dcCore::app()->meta->getMetadata([
'meta_type' => 'tag', 'meta_type' => 'tag',
'post_id' => dcCore::app()->ctx->posts->f('post_id'), 'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'),
]); ]);
$metas = []; $metas = [];
while ($meta->fetch()) { while ($meta->fetch()) {
@ -304,14 +311,14 @@ class Widgets
str_replace( str_replace(
'%T', '%T',
implode(', ', $metas), implode(', ', $metas),
Html::escapeHTML($w->tag_str) Html::escapeHTML($w->__get('tag_str'))
) )
); );
} }
} }
if ($w->attachment_str != '') { if ($w->__get('attachment_str') != '') {
$nb = dcCore::app()->ctx->posts->countMedia(); $nb = dcCore::app()->ctx->__get('posts')->__call('countMedia', []);
if ($nb == 0) { if ($nb == 0) {
$attachment_numeric = 0; $attachment_numeric = 0;
$attachment_textual = __('no attachment'); $attachment_textual = __('no attachment');
@ -345,13 +352,13 @@ class Widgets
str_replace( str_replace(
['%T', '%D'], ['%T', '%D'],
[$attachment_textual, $attachment_numeric], [$attachment_textual, $attachment_numeric],
Html::escapeHTML($w->attachment_str) Html::escapeHTML($w->__get('attachment_str'))
) )
); );
} }
if ($w->comment_str != '' && dcCore::app()->ctx->posts->commentsActive()) { if ($w->__get('comment_str') != '' && dcCore::app()->ctx->__get('posts')->__call('commentsActive', [])) {
$nb = (int) dcCore::app()->ctx->posts->f('nb_comment'); $nb = (int) dcCore::app()->ctx->__get('posts')->f('nb_comment');
if ($nb == 0) { if ($nb == 0) {
$comment_numeric = 0; $comment_numeric = 0;
$comment_textual = __('no comment'); $comment_textual = __('no comment');
@ -385,13 +392,13 @@ class Widgets
str_replace( str_replace(
['%T', '%D'], ['%T', '%D'],
[$comment_textual, $comment_numeric], [$comment_textual, $comment_numeric],
Html::escapeHTML($w->comment_str) Html::escapeHTML($w->__get('comment_str'))
) )
); );
} }
if ($w->trackback_str != '' && dcCore::app()->ctx->posts->trackbacksActive()) { if ($w->__get('trackback_str') != '' && dcCore::app()->ctx->__get('posts')->__call('trackbacksActive', [])) {
$nb = (int) dcCore::app()->ctx->posts->f('nb_trackback'); $nb = (int) dcCore::app()->ctx->__get('posts')->f('nb_trackback');
if ($nb == 0) { if ($nb == 0) {
$trackback_numeric = 0; $trackback_numeric = 0;
$trackback_textual = __('no trackback'); $trackback_textual = __('no trackback');
@ -425,12 +432,12 @@ class Widgets
str_replace( str_replace(
['%T', '%D'], ['%T', '%D'],
[$trackback_textual, $trackback_numeric], [$trackback_textual, $trackback_numeric],
Html::escapeHTML($w->trackback_str) Html::escapeHTML($w->__get('trackback_str'))
) )
); );
} }
if ($w->permalink_str) { if ($w->__get('permalink_str')) {
$content .= self::li( $content .= self::li(
$w, $w,
'permalink', 'permalink',
@ -439,17 +446,17 @@ class Widgets
[ [
sprintf( sprintf(
$link, $link,
dcCore::app()->ctx->posts->getURL(), dcCore::app()->ctx->__get('posts')->__call('getURL', []),
__('Permalink') __('Permalink')
), ),
dcCore::app()->ctx->posts->getURL(), dcCore::app()->ctx->__get('posts')->__call('getURL', []),
], ],
Html::escapeHTML($w->permalink_str) Html::escapeHTML($w->__get('permalink_str'))
) )
); );
} }
if ($w->feed && dcCore::app()->ctx->posts->commentsActive()) { if ($w->__get('feed') && dcCore::app()->ctx->__get('posts')->__call('commentsActive', [])) {
$content .= self::li( $content .= self::li(
$w, $w,
'feed', 'feed',
@ -458,19 +465,19 @@ class Widgets
dcCore::app()->blog->url . dcCore::app()->blog->url .
dcCore::app()->url->getBase('feed') . dcCore::app()->url->getBase('feed') .
'/atom/comments/' . '/atom/comments/' .
dcCore::app()->ctx->posts->f('post_id'), dcCore::app()->ctx->__get('posts')->f('post_id'),
__("This post's comments feed") __("This post's comments feed")
) )
); );
} }
if ($w->navprevpost) { if ($w->__get('navprevpost')) {
$npp = self::nav( $npp = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->__get('posts'),
-1, -1,
false, false,
__('Previous entry'), __('Previous entry'),
$w->navprevpost $w->__get('navprevpost')
); );
if ($npp) { if ($npp) {
$content .= self::li( $content .= self::li(
@ -480,13 +487,13 @@ class Widgets
); );
} }
} }
if ($w->navnextpost) { if ($w->__get('navnextpost')) {
$nnp = self::nav( $nnp = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->__get('posts'),
1, 1,
false, false,
__('Next entry'), __('Next entry'),
$w->navnextpost $w->__get('navnextpost')
); );
if ($nnp) { if ($nnp) {
$content .= self::li( $content .= self::li(
@ -497,13 +504,13 @@ class Widgets
} }
} }
if ($w->navprevcat) { if ($w->__get('navprevcat')) {
$npc = self::nav( $npc = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->__get('posts'),
-1, -1,
true, true,
__('Previous entry of this category'), __('Previous entry of this category'),
$w->navprevcat $w->__get('navprevcat')
); );
if ($npc) { if ($npc) {
$content .= self::li( $content .= self::li(
@ -514,13 +521,13 @@ class Widgets
} }
} }
if ($w->navnextcat) { if ($w->__get('navnextcat')) {
$nnc = self::nav( $nnc = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->__get('posts'),
1, 1,
true, true,
__('Next entry of this category'), __('Next entry of this category'),
$w->navnextcat $w->__get('navnextcat')
); );
if ($nnc) { if ($nnc) {
$content .= self::li( $content .= self::li(
@ -564,22 +571,27 @@ class Widgets
} }
//*/ //*/
return $w->renderDiv( return $w->renderDiv(
(bool) $w->content_only, (bool) $w->__get('content_only'),
'postinfowidget ' . $w->class, 'postinfowidget ' . $w->__get('class'),
'', '',
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . ($w->__get('title') ? $w->renderTitle(Html::escapeHTML($w->__get('title'))) : '') .
sprintf('<ul>%s</ul>', $content) sprintf('<ul>%s</ul>', $content)
); );
} }
public static function li(WidgetsElement $w, string $i, string $c): string public static function li(WidgetsElement $w, string $i, string $c): string
{ {
// nullsafe
if (is_null(dcCore::app()->blog)) {
return '';
}
$s = ' style="padding-left:%spx;background: transparent url(\'' . $s = ' style="padding-left:%spx;background: transparent url(\'' .
dcCore::app()->blog->getQmarkURL() . dcCore::app()->blog->getQmarkURL() .
'pf=postInfoWidget/img/%s%s.png\') no-repeat left center;"'; 'pf=postInfoWidget/img/%s%s.png\') no-repeat left center;"';
if ($w->style == 'small') { if ($w->__get('style') == 'small') {
$s = sprintf($s, 16, $i, '-small'); $s = sprintf($s, 16, $i, '-small');
} elseif ($w->style == 'normal') { } elseif ($w->__get('style') == 'normal') {
$s = sprintf($s, 20, $i, ''); $s = sprintf($s, 20, $i, '');
} else { } else {
$s = ''; $s = '';
@ -589,14 +601,19 @@ class Widgets
return sprintf($l, $i, $s, $c); return sprintf($l, $i, $s, $c);
} }
public static function nav(dcRecord $p, int $d, bool $r, string $t, string $c): string public static function nav(MetaRecord $p, int $d, bool $r, string $t, string $c): string
{ {
// nullsafe
if (is_null(dcCore::app()->blog)) {
return '';
}
$rs = dcCore::app()->blog->getNextPost($p, $d, $r); $rs = dcCore::app()->blog->getNextPost($p, $d, $r);
if (is_null($rs)) { if (is_null($rs)) {
return ''; return '';
} }
$l = '<a href="%s" title="%s">%s</a>'; $l = '<a href="%s" title="%s">%s</a>';
$u = $rs->getURL(); $u = $rs->__call('getURL', []);
$e = Html::escapeHTML($rs->f('post_title')); $e = Html::escapeHTML($rs->f('post_title'));
return str_replace( return str_replace(