From a77741d8902c4beec422988bc226d89853ed5241 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 4 Nov 2023 21:51:11 +0100 Subject: [PATCH] code review --- CHANGELOG.md | 6 +++ README.md | 4 +- _define.php | 2 +- dcstore.xml | 4 +- src/BackendBehaviors.php | 12 +++++- src/Frontend.php | 5 +-- src/Manage.php | 11 ++--- src/ManageVars.php | 86 +++++++++++++++++++++++++++------------- src/My.php | 1 + src/Pager.php | 2 +- 10 files changed, 87 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9727000..355aaf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +templator 1.7.1 - 2023.11.04 +=========================================================== +* Require Dotclear 2.28 +* Require PHP 8.1 +* Code review (phpstan) + templator 1.7 - 2023.10.20 =========================================================== * Require Dotclear 2.28 diff --git a/README.md b/README.md index 48e716e..3519d4c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # README -[![Release](https://img.shields.io/badge/release-1.7-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/templator/releases) -![Date](https://img.shields.io/badge/date-2023.10.20-c44d58.svg) +[![Release](https://img.shields.io/badge/release-1.7.1-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/templator/releases) +![Date](https://img.shields.io/badge/date-2023.11.04-c44d58.svg) [![Dotclear](https://img.shields.io/badge/dotclear-v2.28-137bbb.svg)](https://fr.dotclear.org/download) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-9ac123.svg)](https://plugins.dotaddict.org/dc2/details/templator) [![License](https://img.shields.io/badge/license-GPL--2.0-ececec.svg)](https://git.dotclear.watch/JcDenis/templator/src/branch/master/LICENSE) diff --git a/_define.php b/_define.php index d8720ce..789fb10 100644 --- a/_define.php +++ b/_define.php @@ -18,7 +18,7 @@ $this->registerModule( 'Templates engine', 'Create and select more templates for your posts', 'Osku and contributors', - '1.7', + '1.7.1', [ 'requires' => [['core', '2.28']], 'permissions' => 'My', diff --git a/dcstore.xml b/dcstore.xml index e0dffde..ff09fb6 100644 --- a/dcstore.xml +++ b/dcstore.xml @@ -2,10 +2,10 @@ Templates engine - 1.7 + 1.7.1 Osku and contributors Create and select more templates for your posts - https://git.dotclear.watch/JcDenis/templator/releases/download/v1.7/plugin-templator.zip + https://git.dotclear.watch/JcDenis/templator/releases/download/v1.7.1/plugin-templator.zip 2.28 https://git.dotclear.watch/JcDenis/templator/src/branch/master/README.md https://git.dotclear.watch/JcDenis/templator/issues diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php index df76dd4..61fb81f 100644 --- a/src/BackendBehaviors.php +++ b/src/BackendBehaviors.php @@ -35,6 +35,10 @@ class BackendBehaviors return My::jsLoad('admin'); } + /** + * @param ArrayObject $main_items + * @param ArrayObject $sidebar_items + */ public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?MetaRecord $post): void { $selected = ''; @@ -75,6 +79,9 @@ class BackendBehaviors ); } + /** + * @param ArrayObject $post + */ public static function adminPostsActionsCallback(ActionsPosts $pa, ArrayObject $post): void { # No entry @@ -126,6 +133,9 @@ class BackendBehaviors $pa->endPage(); } + /** + * @param ArrayObject $sorts + */ public static function adminFiltersListsV2(ArrayObject $sorts): void { $sorts[My::id()] = [ @@ -142,7 +152,7 @@ class BackendBehaviors } /** - * @return array + * @return array */ private static function getTemplateCombo(): array { diff --git a/src/Frontend.php b/src/Frontend.php index 545c584..77249b0 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -29,10 +29,7 @@ class Frontend extends Process return false; } - App::frontend()->template()->setPath( - App::frontend()->template()->getPath(), - Templator::instance()->getPath() - ); + App::frontend()->template()->appendPath(Templator::instance()->getPath()); App::behavior()->addBehaviors([ 'urlHandlerBeforeGetData' => function ($_): void { diff --git a/src/Manage.php b/src/Manage.php index f95b9a0..7919c70 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -117,7 +117,7 @@ class Manage extends Process $v->media->removeItem($file); App::meta()->delMeta($file, 'template'); - if (!dcCore::app()->error->flag()) { + if (!App::error()->flag()) { Notices::addSuccessNotice(__('The template has been successfully removed.')); My::redirect(['part' => 'files']); } @@ -515,11 +515,8 @@ class Manage extends Process # Get posts try { - $posts = App::meta()->getPostsByMeta($params); - if (is_null($posts)) { - throw new Exception(__('Failed to get posts meta')); - } - $counter = App::meta()->getPostsByMeta($params, true)?->f(0); + $posts = App::meta()->getPostsByMeta($params); + $counter = App::meta()->getPostsByMeta($params, true)->f(0); $counter = is_numeric($counter) ? (int) $counter : 0; $post_list = new ListingPosts($posts, $counter); } catch (Exception $e) { @@ -544,7 +541,7 @@ class Manage extends Process '

' . sprintf(__('Unselect template "%s"'), '' . $file . '') . '

' . '

' . __('Back') . '

'; - if (!App::error()->flag() && isset($posts)) { + if (!App::error()->flag() && isset($posts) && isset($post_list)) { if ($posts->isEmpty() && !$filter->show()) { echo '

' . __('There is no entries') . '

'; } else { diff --git a/src/ManageVars.php b/src/ManageVars.php index d2121cb..e1f1270 100644 --- a/src/ManageVars.php +++ b/src/ManageVars.php @@ -21,32 +21,64 @@ use Exception; */ class ManageVars { - /** @var ManageVars $container Self instance */ + /** + * Self instance. + * + * @var ManageVars $container + */ private static $container; - /** @var string $name The requested manage part name*/ + /** + * The requested manage part name. + * + * @var string $name + */ public readonly string $name; - /** @var string $part The requested manage part */ + /** + * The requested manage part. + * + * @var string $part + */ public readonly string $part; - /** @var MediaInterface $media The limited media instance */ + /** + * The limited media instance. + * + * @var MediaInterface $media + */ public readonly MediaInterface $media; - /** @var array $items The media items */ + /** + * The media items. + * + * @var array $items + */ public readonly array $items; - /** @var array $categories The blog categories list */ + /** + * The blog categories list. + * + * @var array $categories + */ public readonly array $categories; - /** @var bool $has_categories Blog has categories */ + /** + * Blog has categories. + * + * @var bool $has_categories + */ public readonly bool $has_categories; - /** @var array $sources The templates list */ + /** + * The templates list. + * + * @var array $sources + */ public readonly array $sources; /** - * Constructo sets properties. + * Constructor sets properties. */ public function __construct() { @@ -61,8 +93,7 @@ class ManageVars $this->media->chdir(Templator::MY_TPL_DIR); // For users with only templator permission, we use sudo. App::auth()->sudo($this->media->getDir(...)); - $dir = $this->media->dir; - $this->items = array_values($dir['files']); + $this->items = array_values($this->media->getFiles()); // categories $categories_combo = []; @@ -70,28 +101,27 @@ class ManageVars try { $categories = App::blog()->getCategories(['post_type' => 'post']); - if (!is_null($categories)) { - $l = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1; - $full_name = [is_string($categories->f('cat_title')) ? $categories->f('cat_title') : '']; - while ($categories->fetch()) { - $id = is_numeric($categories->f('cat_id')) ? (int) $categories->f('cat_id') : 1; - $level = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1; - $title = is_string($categories->f('cat_title')) ? $categories->f('cat_title') : ''; + $l = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1; + $full_name = [is_string($categories->f('cat_title')) ? $categories->f('cat_title') : '']; - if ($level < $l) { - $full_name = []; - } elseif ($level == $l) { - array_pop($full_name); - } - $full_name[] = Html::escapeHTML($title); + while ($categories->fetch()) { + $id = is_numeric($categories->f('cat_id')) ? (int) $categories->f('cat_id') : 1; + $level = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1; + $title = is_string($categories->f('cat_title')) ? $categories->f('cat_title') : ''; - $categories_combo[implode(' › ', $full_name)] = $id; - - $l = $level; + if ($level < $l) { + $full_name = []; + } elseif ($level == $l) { + array_pop($full_name); } - $has_categories = !$categories->isEmpty(); + $full_name[] = Html::escapeHTML($title); + + $categories_combo[implode(' › ', $full_name)] = $id; + + $l = $level; } + $has_categories = !$categories->isEmpty(); } catch (Exception $e) { } $this->categories = $categories_combo; diff --git a/src/My.php b/src/My.php index 04d13d0..bb19814 100644 --- a/src/My.php +++ b/src/My.php @@ -27,6 +27,7 @@ class My extends MyPlugin public static function checkCustomContext(int $context): ?bool { return match ($context) { + // Add templator perm to backend self::BACKEND, self::MENU, self::MANAGE => App::task()->checkContext('BACKEND') && App::auth()->check(App::auth()->makePermissions([ self::PERMISSION_TEMPLATOR, diff --git a/src/Pager.php b/src/Pager.php index e8ff585..06fba72 100644 --- a/src/Pager.php +++ b/src/Pager.php @@ -73,7 +73,7 @@ class Pager $params['post_type'] = ''; try { - $counter = App::meta()->getPostsByMeta($params, true)?->f(0); + $counter = App::meta()->getPostsByMeta($params, true)->f(0); $counter = is_numeric($counter) ? (int) $counter : 0; $url = My::manageUrl([ 'part' => 'posts',