From d98f871d19a21c074c694ba0bbc2045313151632 Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Sat, 13 Apr 2024 12:02:09 +0300 Subject: [PATCH] carft 5: initial release --- CHANGELOG.md | 3 ++ composer.json | 4 +- src/migrations/Install.php | 2 - .../Migrators/AuthorPageMigrator.php | 10 ++++- .../Migrators/BlogChannelMigrator.php | 9 +++- .../Migrators/BlogFieldGroupMigrator.php | 42 ------------------- .../Migrators/BlogListingMigrator.php | 10 ++++- src/migrations/Migrators/TagPageMigrator.php | 10 ++++- src/services/EntryTypeService.php | 5 ++- src/services/FieldsService.php | 19 --------- src/services/IdsService.php | 2 +- src/services/SectionService.php | 20 ++++++--- src/services/SeedingService.php | 12 +++--- 13 files changed, 65 insertions(+), 83 deletions(-) delete mode 100644 src/migrations/Migrators/BlogFieldGroupMigrator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f6a3d5e..3f67c33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Release Notes for Blogify +## 3.0.0 - 2024-04-13 +- Craft 5: Initial Release + ## 2.0.3 - 2022-07-10 ### Added - Add dedicated log file diff --git a/composer.json b/composer.json index dcb5753..0753677 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ "docs": "https://github.com/matfish2/craft-blogify/blob/master/README.md" }, "require": { - "craftcms/cms": "^4.0.4", - "craftcms/redactor": "^3.0.0" + "craftcms/cms": "^5.0.4", + "craftcms/redactor": "^4.0.0" }, "autoload": { "psr-4": { diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 92a89be..95d0fba 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -33,7 +33,6 @@ public function safeUp() BlogListingMigrator::add(); BlogCategoriesMigrator::add(); BlogTagsMigrator::add(); - BlogFieldGroupMigrator::add(); BlogAssetsVolumeMigrator::add(); BlogThumbnailTransform::add(); TagPageMigrator::add(); @@ -51,7 +50,6 @@ public function safeDown() BlogListingMigrator::remove(); BlogCategoriesMigrator::remove(); BlogTagsMigrator::remove(); - BlogFieldGroupMigrator::remove(); BlogAssetsVolumeMigrator::remove(); BlogThumbnailTransform::remove(); TagPageMigrator::remove(); diff --git a/src/migrations/Migrators/AuthorPageMigrator.php b/src/migrations/Migrators/AuthorPageMigrator.php index 7422b9e..56f7e8c 100644 --- a/src/migrations/Migrators/AuthorPageMigrator.php +++ b/src/migrations/Migrators/AuthorPageMigrator.php @@ -5,6 +5,7 @@ use Craft; +use craft\models\EntryType; use craft\models\Section; use craft\models\Section_SiteSettings; use matfish\Blogify\Handles; @@ -16,11 +17,18 @@ class AuthorPageMigrator extends Migrator public static function add(): bool { + $entryType = new EntryType([ + 'name' => 'Author Page', + 'handle' => 'authorPage', + 'hasTitleField' => true, + ]); + return (new SectionService())->add('Blog Author Page', Handles::AUTHOR_PAGE, Section::TYPE_SINGLE, '/author', - 'filters/author/_entry' + 'filters/author/_entry', + $entryType ); } diff --git a/src/migrations/Migrators/BlogChannelMigrator.php b/src/migrations/Migrators/BlogChannelMigrator.php index 8295878..42c002e 100644 --- a/src/migrations/Migrators/BlogChannelMigrator.php +++ b/src/migrations/Migrators/BlogChannelMigrator.php @@ -5,6 +5,7 @@ use Craft; +use craft\models\EntryType; use craft\models\Section; use matfish\Blogify\Handles; use matfish\Blogify\services\SectionService; @@ -15,7 +16,13 @@ public static function add(): bool { \Craft::$app->cache->delete(Handles::CHANNEL); - return (new SectionService())->add('Blog', Handles::CHANNEL, Section::TYPE_CHANNEL, '/{slug}', 'post/_entry'); + $entryType = new EntryType([ + 'name' => 'Post', + 'handle' => 'post', + 'hasTitleField' => true, + + ]); + return (new SectionService())->add('Blog', Handles::CHANNEL, Section::TYPE_CHANNEL, '/{slug}', 'post/_entry', $entryType); } public static function remove(): bool diff --git a/src/migrations/Migrators/BlogFieldGroupMigrator.php b/src/migrations/Migrators/BlogFieldGroupMigrator.php deleted file mode 100644 index 18243d9..0000000 --- a/src/migrations/Migrators/BlogFieldGroupMigrator.php +++ /dev/null @@ -1,42 +0,0 @@ - Handles::BLOG_FIELDS_GROUP_NAME - ]); - - return Craft::$app->fields->saveGroup($group); - - } - - public static function remove(): bool - { - $name = Handles::BLOG_FIELDS_GROUP_NAME; - - $group = FieldGroupRecord::findOne([ - 'name' => $name - ]); - - if ($group) { - blogify_log("Removing {$name} group"); - $group->delete(); - return true; - } - - blogify_log("Field group {$name} not found. Skipping."); - return false; - } -} \ No newline at end of file diff --git a/src/migrations/Migrators/BlogListingMigrator.php b/src/migrations/Migrators/BlogListingMigrator.php index 33c7ae9..0db0625 100644 --- a/src/migrations/Migrators/BlogListingMigrator.php +++ b/src/migrations/Migrators/BlogListingMigrator.php @@ -4,6 +4,7 @@ namespace matfish\Blogify\migrations\Migrators; use Craft; +use craft\models\EntryType; use craft\models\Section; use craft\models\Section_SiteSettings; use matfish\Blogify\Handles; @@ -13,11 +14,18 @@ class BlogListingMigrator extends Migrator { public static function add(): bool { + $entryType = new EntryType([ + 'name' => 'Blog Index', + 'handle' => 'blogIndex', + 'hasTitleField' => true, + ]); + return (new SectionService())->add('Blog Listing', Handles::LISTING, Section::TYPE_SINGLE, '/index', - 'listing/_entry' + 'listing/_entry', + $entryType ); } diff --git a/src/migrations/Migrators/TagPageMigrator.php b/src/migrations/Migrators/TagPageMigrator.php index 59cd242..24e8f5a 100644 --- a/src/migrations/Migrators/TagPageMigrator.php +++ b/src/migrations/Migrators/TagPageMigrator.php @@ -3,6 +3,7 @@ namespace matfish\Blogify\migrations\Migrators; +use craft\models\EntryType; use craft\models\Section; use matfish\Blogify\Handles; use matfish\Blogify\services\SectionService; @@ -12,11 +13,18 @@ class TagPageMigrator extends Migrator public static function add(): bool { + $entryType = new EntryType([ + 'name' => 'Tag Page', + 'handle' => 'tagPage', + 'hasTitleField' => true, + ]); + return (new SectionService())->add('Blog Tag Page', Handles::TAG_PAGE, Section::TYPE_SINGLE, '/tag', - 'filters/tag/_entry' + 'filters/tag/_entry', + $entryType ); } diff --git a/src/services/EntryTypeService.php b/src/services/EntryTypeService.php index c8f758d..9e5172d 100644 --- a/src/services/EntryTypeService.php +++ b/src/services/EntryTypeService.php @@ -21,7 +21,8 @@ public function addFields($fields) }, $fields); - $section = Craft::$app->sections->getSectionByHandle(Handles::CHANNEL); + $sectionService = Craft::$app->getEntries(); + $section = $sectionService->getSectionByHandle(Handles::CHANNEL); $entryType = $section->getEntryTypes()[0]; $layout = $entryType->getFieldLayout(); @@ -47,6 +48,6 @@ public function addFields($fields) $entryType->setFieldLayout($layout); - return Craft::$app->sections->saveEntryType($entryType); + return $sectionService->saveEntryType($entryType); } } \ No newline at end of file diff --git a/src/services/FieldsService.php b/src/services/FieldsService.php index bb52141..1aa879d 100644 --- a/src/services/FieldsService.php +++ b/src/services/FieldsService.php @@ -20,12 +20,10 @@ public function add($name, $handle, $type, $attributes = null, $settings = null) blogify_log("Creating field {$name}"); - $groupId = $this->getFieldGroupId(); $fieldsService = Craft::$app->getFields(); $params = [ 'type' => $type, - 'groupId' => $groupId, 'name' => $name, 'handle' => $handle ]; @@ -60,21 +58,4 @@ public function remove($handle): bool return false; } - - private function getFieldGroupId() - { - $group = FieldGroup::findOne([ - 'name' => Handles::BLOG_FIELDS_GROUP_NAME - ]); - - if ($group) { - return $group->id; - } - - $group = FieldGroup::findOne('1=1'); - - blogify_log("Failed to find blog fields group. Using {$group->name} group instead"); - - return $group->id; - } } \ No newline at end of file diff --git a/src/services/IdsService.php b/src/services/IdsService.php index f397409..3f9777f 100644 --- a/src/services/IdsService.php +++ b/src/services/IdsService.php @@ -36,7 +36,7 @@ public function tagsGroupId() protected function getSectionIdByHandle($handle) { return blogify_cache($handle, function () use ($handle) { - return (int)Craft::$app->sections->getSectionByHandle($handle)->id; + return (int)Craft::$app->getEntries()->getSectionByHandle($handle)->id; }); } } \ No newline at end of file diff --git a/src/services/SectionService.php b/src/services/SectionService.php index 25641f7..4e380ab 100644 --- a/src/services/SectionService.php +++ b/src/services/SectionService.php @@ -3,17 +3,19 @@ namespace matfish\Blogify\services; use Craft; +use craft\models\EntryType; use craft\models\Section; use craft\models\Section_SiteSettings; class SectionService { - public function add($name, $handle, $type, $url, $template): bool + public function add($name, $handle, $type, $url, $template, EntryType $entryType): bool { blogify_log("Creating section {$name}"); - $section = Craft::$app->sections->getSectionByHandle($handle); + $sectionService = Craft::$app->getEntries(); + $section = $sectionService->getSectionByHandle($handle); if ($section) { blogify_log("Section {$name} exists. Skipping"); @@ -39,7 +41,14 @@ public function add($name, $handle, $type, $url, $template): bool 'siteSettings' => $allSitesSettings ]); - if (!Craft::$app->sections->saveSection($section)) { + $sectionService->saveEntryType($entryType); + $et = $sectionService->getEntryTypeByHandle('posts'); + + $section->setEntryTypes([ + $et + ]); + + if (!$sectionService->saveSection($section)) { blogify_log("Failed to create section {$name}"); blogify_log(json_encode($section->getErrors())); throw new \Exception("Failed to create section {$name}"); @@ -51,11 +60,12 @@ public function add($name, $handle, $type, $url, $template): bool public function remove($handle): bool { blogify_log("Removing section {$handle}"); + $sectionService = Craft::$app->getEntries(); - $section = Craft::$app->sections->getSectionByHandle($handle); + $section = $sectionService->getSectionByHandle($handle); if ($section) { - return Craft::$app->sections->deleteSection($section); + return $sectionService->deleteSection($section); } return false; diff --git a/src/services/SeedingService.php b/src/services/SeedingService.php index d5871f3..8913f1a 100644 --- a/src/services/SeedingService.php +++ b/src/services/SeedingService.php @@ -22,8 +22,8 @@ public function seed() $tags = $this->generateTags(); $categories = $this->generateCategories(); $images = $this->generateImages(); - - $section = Craft::$app->sections->getSectionByHandle(Handles::CHANNEL); + $sectionService = Craft::$app->getEntries(); + $section = $sectionService->getSectionByHandle(Handles::CHANNEL); $entryType = $section->getEntryTypes()[0]; $entry = new Entry([ @@ -32,7 +32,7 @@ public function seed() 'typeId' => $entryType->id, 'authorId' => FakerService::arrayElement($this->getUsers()), 'title' => FakerService::sentence(), - 'postDate'=>FakerService::date() + 'postDate' => FakerService::date() ]); $entry->setFieldValue(Handles::POST_IMAGE, [FakerService::arrayElement($images)]); @@ -111,7 +111,7 @@ private function generateImages() $path = new Path(); $tempDirPath = $path->getTempPath(); - for ($i=1; $i<=11; $i++) { + for ($i = 1; $i <= 11; $i++) { $image = "img_{$i}"; // move file from plugin assets to project temp folder $filename = $image . '.jpg'; @@ -158,8 +158,8 @@ private function uploadNewAsset($folder, string $path, $filename) private function getUsers() { - return array_map(function($user) { + return array_map(function ($user) { return $user->id; - },User::findAll()); + }, User::findAll()); } } \ No newline at end of file