fix nullsafe et type hint

This commit is contained in:
Jean-Christian Denis 2023-04-22 00:59:08 +02:00
parent 740c674b0f
commit 89b41e849d
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951

View file

@ -15,27 +15,30 @@ declare(strict_types=1);
namespace Dotclear\Plugin\entryPhotoExifWidget; namespace Dotclear\Plugin\entryPhotoExifWidget;
use dcCore; use dcCore;
use Dotclear\Helper\Date;
use Dotclear\Helper\File\Image\ImageMeta;
use Dotclear\Helper\File\Path; use Dotclear\Helper\File\Path;
use Dotclear\Helper\Html\Html; use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsElement; use Dotclear\Plugin\widgets\WidgetsElement;
use Dotclear\Plugin\widgets\WidgetsStack; use Dotclear\Plugin\widgets\WidgetsStack;
use dt;
use imageMeta;
class Widgets class Widgets
{ {
public static $supported_post_type = ['post', 'page', 'gal', 'galitem']; public static array $supported_post_type = ['post', 'page', 'gal', 'galitem'];
public static $widget_content = '<ul>%s</ul>'; public static string $widget_content = '<ul>%s</ul>';
public static $widget_text = '<li class="epew-%s"><strong>%s</strong> %s</li>'; public static string $widget_text = '<li class="epew-%s"><strong>%s</strong> %s</li>';
public static $widget_thumb = '<li><img class="img-thumbnail" alt="%s" src="%s" /></li>'; public static string $widget_thumb = '<li><img class="img-thumbnail" alt="%s" src="%s" /></li>';
public static function initWidgets(WidgetsStack $w): void public static function initWidgets(WidgetsStack $w): void
{ {
if (is_null(dcCore::app()->blog)) {
return;
}
$categories_combo = ['-' => '', __('Uncategorized') => 'null']; $categories_combo = ['-' => '', __('Uncategorized') => 'null'];
$categories = dcCore::app()->blog->getCategories(); $categories = dcCore::app()->blog->getCategories();
while ($categories->fetch()) { while ($categories->fetch()) {
$cat_title = Html::escapeHTML($categories->cat_title); $categories_combo[Html::escapeHTML($categories->f('cat_title'))] = $categories->f('cat_id');
$categories_combo[$cat_title] = $categories->cat_id;
} }
$thumbnail_combo = [ $thumbnail_combo = [
@ -165,29 +168,34 @@ class Widgets
public static function renderWidget(WidgetsElement $w): string public static function renderWidget(WidgetsElement $w): string
{ {
# Widget is offline // Widget is offline
if ($w->offline) { if ($w->offline) {
return ''; return '';
} }
# Not in post context // nullsafe
if (!dcCore::app()->ctx->exists('posts') || !dcCore::app()->ctx->posts->post_id) { if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->ctx)) {
return ''; return '';
} }
# Not supported post type // Not in post context
if (!in_array(dcCore::app()->ctx->posts->post_type, self::$supported_post_type)) { if (!dcCore::app()->ctx->exists('posts') || !dcCore::app()->ctx->__get('posts')->f('post_id')) {
return ''; return '';
} }
# Category limit // Not supported post type
if ($w->category == 'null' && dcCore::app()->ctx->posts->cat_id !== null if (!in_array(dcCore::app()->ctx->__get('posts')->f('post_type'), self::$supported_post_type)) {
|| $w->category != 'null' && $w->category != '' && $w->category != dcCore::app()->ctx->posts->cat_id) { return '';
}
// Category limit
if ($w->category == 'null' && dcCore::app()->ctx->__get('posts')->f('cat_id') !== null
|| $w->category != 'null' && $w->category != '' && $w->category != dcCore::app()->ctx->__get('posts')->f('cat_id')) {
return ''; return '';
} }
# Content lookup # Content lookup
$text = dcCore::app()->ctx->posts->post_excerpt_xhtml . dcCore::app()->ctx->posts->post_content_xhtml; $text = dcCore::app()->ctx->__get('posts')->f('post_excerpt_xhtml') . dcCore::app()->ctx->__get('posts')->f('post_content_xhtml');
# Find source images # Find source images
$images = self::getImageSource($text, $w->thumbsize); $images = self::getImageSource($text, $w->thumbsize);
@ -226,11 +234,6 @@ class Widgets
$contents .= $content; $contents .= $content;
} }
# Nothing found
if (empty($contents)) {
return '';
}
# Paste widget # Paste widget
return $w->renderDiv( return $w->renderDiv(
(bool) $w->content_only, (bool) $w->content_only,
@ -241,11 +244,15 @@ class Widgets
); );
} }
public static function getImageSource($subject, $size = '') public static function getImageSource(string $subject, string $size = ''): array
{ {
if (is_null(dcCore::app()->blog)) {
return [];
}
# Path and url # Path and url
$p_url = dcCore::app()->blog->settings->system->public_url; $p_url = (string) dcCore::app()->blog->settings->get('system')->get('public_url');
$p_site = preg_replace('#^(.+?//.+?)/(.*)$#', '$1', dcCore::app()->blog->url); $p_site = (string) preg_replace('#^(.+?//.+?)/(.*)$#', '$1', dcCore::app()->blog->url);
$p_root = dcCore::app()->blog->public_path; $p_root = dcCore::app()->blog->public_path;
# Image pattern # Image pattern
@ -254,7 +261,7 @@ class Widgets
# No image # No image
if (!preg_match_all($pattern, $subject, $m)) { if (!preg_match_all($pattern, $subject, $m)) {
return; return [];
} }
$res = $duplicate = []; $res = $duplicate = [];
@ -315,8 +322,12 @@ class Widgets
return $res; return $res;
} }
public static function getImageMeta($src) public static function getImageMeta(?string $src): array
{ {
if (is_null(dcCore::app()->blog)) {
return [];
}
$metas = [ $metas = [
'Title' => [__('Title:'), ''], 'Title' => [__('Title:'), ''],
'Description' => [__('Description:'), ''], 'Description' => [__('Description:'), ''],
@ -361,7 +372,7 @@ class Widgets
return $metas; return $metas;
} }
$m = imageMeta::readMeta($src); $m = ImageMeta::readMeta($src);
# Title # Title
if (!empty($m['Title'])) { if (!empty($m['Title'])) {
@ -388,9 +399,9 @@ class Widgets
# DateTimeOriginal # DateTimeOriginal
if (!empty($m['DateTimeOriginal'])) { if (!empty($m['DateTimeOriginal'])) {
$dt_ft = dcCore::app()->blog->settings->system->date_format . ', ' . dcCore::app()->blog->settings->system->time_format; $dt_ft = dcCore::app()->blog->settings->get('system')->get('date_format') . ', ' . dcCore::app()->blog->settings->get('system')->get('time_format');
$dt_tz = dcCore::app()->blog->settings->system->blog_timezone; $dt_tz = dcCore::app()->blog->settings->get('system')->get('blog_timezone');
$metas['DateTimeOriginal'][1] = dt::dt2str($dt_ft, $m['DateTimeOriginal'], $dt_tz); $metas['DateTimeOriginal'][1] = Date::dt2str($dt_ft, $m['DateTimeOriginal'], $dt_tz);
} }
# Make # Make