From 49b6387fb24a12fb75b91c0fe6ae4148e0a2dda6 Mon Sep 17 00:00:00 2001 From: Ransom Roberson Date: Mon, 28 Nov 2022 16:13:57 -0500 Subject: [PATCH] Don't check permissions until Craft is ready --- CHANGELOG.md | 5 ++++ composer.json | 11 +++++++- src/Plugin.php | 74 ++++++++++++++++++++++++++------------------------ 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 184c63c..4e23c83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Bulk Edit Changelog +## 4.0.1 - 2022-10-28 + +### Fixed +- Fixed "Element query executed before Craft is fully initialized." warning + ## 4.0.0 - 2022-10-24 ### Added diff --git a/composer.json b/composer.json index bb3383f..aafed56 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,6 @@ "name": "venveo/craft-bulkedit", "description": "Bulk edit Craft CMS element fields", "type": "craft-plugin", - "version": "4.0.0", "keywords": [ "craft", "cms", @@ -35,5 +34,15 @@ "handle": "venveo-bulk-edit", "changelogUrl": "https://raw.githubusercontent.com/venveo/craft-bulkedit/master/CHANGELOG.md", "class": "venveo\\bulkedit\\Plugin" + }, + "config": { + "sort-packages": true, + "platform": { + "php": "8.0.2" + }, + "allow-plugins": { + "yiisoft/yii2-composer": true, + "craftcms/plugin-installer": true + } } } diff --git a/src/Plugin.php b/src/Plugin.php index 7592009..597fb67 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -21,6 +21,7 @@ use craft\events\RegisterElementActionsEvent; use craft\events\RegisterUserPermissionsEvent; use craft\services\UserPermissions; +use craft\web\Application; use venveo\bulkedit\elements\actions\BulkEditElementAction; use venveo\bulkedit\services\BulkEdit; use yii\base\Event; @@ -78,50 +79,51 @@ public function init() 'permissions' => $permissions ]; }); + Craft::$app->on(Application::EVENT_INIT, function() { + if (Craft::$app->request->isCpRequest) { + if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ENTRIES)) { + Event::on(Entry::class, Element::EVENT_REGISTER_ACTIONS, + function (RegisterElementActionsEvent $event) { + $event->actions[] = BulkEditElementAction::class; + } + ); + } - if (Craft::$app->request->isCpRequest) { - if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ENTRIES)) { - Event::on(Entry::class, Element::EVENT_REGISTER_ACTIONS, - function(RegisterElementActionsEvent $event) { - $event->actions[] = BulkEditElementAction::class; - } - ); - } - - if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_CATEGORIES)) { - Event::on(Category::class, Element::EVENT_REGISTER_ACTIONS, - function(RegisterElementActionsEvent $event) { - $event->actions[] = BulkEditElementAction::class; - } - ); - } - - if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ASSETS)) { - Event::on(Asset::class, Element::EVENT_REGISTER_ACTIONS, - function(RegisterElementActionsEvent $event) { - $event->actions[] = BulkEditElementAction::class; - } - ); - } + if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_CATEGORIES)) { + Event::on(Category::class, Element::EVENT_REGISTER_ACTIONS, + function (RegisterElementActionsEvent $event) { + $event->actions[] = BulkEditElementAction::class; + } + ); + } - if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_USERS)) { - Event::on(User::class, Element::EVENT_REGISTER_ACTIONS, - function(RegisterElementActionsEvent $event) { - $event->actions[] = BulkEditElementAction::class; - } - ); - } + if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ASSETS)) { + Event::on(Asset::class, Element::EVENT_REGISTER_ACTIONS, + function (RegisterElementActionsEvent $event) { + $event->actions[] = BulkEditElementAction::class; + } + ); + } - if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_PRODUCTS)) { - if (Craft::$app->plugins->isPluginInstalled('commerce') && class_exists(Product::class)) { - Event::on(Product::class, Element::EVENT_REGISTER_ACTIONS, - function(RegisterElementActionsEvent $event) { + if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_USERS)) { + Event::on(User::class, Element::EVENT_REGISTER_ACTIONS, + function (RegisterElementActionsEvent $event) { $event->actions[] = BulkEditElementAction::class; } ); } + + if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_PRODUCTS)) { + if (Craft::$app->plugins->isPluginInstalled('commerce') && class_exists(Product::class)) { + Event::on(Product::class, Element::EVENT_REGISTER_ACTIONS, + function (RegisterElementActionsEvent $event) { + $event->actions[] = BulkEditElementAction::class; + } + ); + } + } } - } + }); } /**