@@ -33,8 +33,18 @@ public function registerPostTypes($postTypes): void
3333 $ type ->slug = $ slug ;
3434 $ type ->icon = $ data ['icon ' ];
3535
36+ $ category_type = CategoryType::whereSlug ($ slug . '-categories ' )->first ();
37+
38+ if (! $ category_type ) {
39+ $ category_type = new CategoryType ();
40+ }
41+
42+ $ category_type ->name = $ data ['name ' ] ?? $ name . ' Categories ' ;
43+ $ category_type ->singular_name = $ data ['name_singular ' ] ?? Str::singular ($ data ['name ' ] ?? $ name ) . ' Category ' ;
44+ $ category_type ->slug = $ slug . '_categories ' ;
45+
46+ $ category_type ->save ();
3647
37- $ category_type = CategoryType::whereSlug (Str::singular ($ slug ) . '-categories ' )->first ();
3848 $ type ->categoryType ()->associate ($ category_type );
3949
4050 $ type ->features = $ data ['features ' ];
@@ -194,5 +204,85 @@ public function adminMenuItems(array $menus = [])
194204 return $ menus ;
195205 }
196206
207+ /**
208+ * Load up permissions for all the Post Types
209+ *
210+ * @return array
211+ */
212+ public function seedPostTypePermissions (array $ existing_permissions = []): array
213+ {
214+ $ data = [];
215+ $ all_post_types = PostType::all ();
216+
217+ foreach ($ all_post_types as $ post_type ) {
218+ $ permissions = $ this ->constructPostTypePermissions ($ post_type );
219+ $ existing = $ existing_permissions [$ post_type ->permission_slug ] ?? [];
220+ $ data [$ post_type ->permission_slug ] = array_merge ($ existing , $ permissions );
221+ }
222+
223+ return array_merge ($ existing_permissions , $ data );
224+ }
197225
226+ /**
227+ * Permission template for the post types
228+ *
229+ * @param PostType $post_type
230+ * @return string[]
231+ */
232+ protected function constructPostTypePermissions (PostType $ post_type ): array
233+ {
234+ $ slug = $ post_type ->permission_slug ;
235+ $ title = Str::lower ($ post_type ->name );
236+
237+ return [
238+ 'edit_ ' . $ slug => 'Edit own ' . $ title ,
239+ 'edit_others_ ' . $ slug => 'Edit all ' . $ title ,
240+ 'delete_ ' . $ slug => 'Delete own ' . $ title ,
241+ 'delete_others_ ' . $ slug => 'Delete all ' . $ title ,
242+ 'view_ ' . $ slug => 'View own ' . $ title ,
243+ 'view_others_ ' . $ slug => 'View all ' . $ title ,
244+ 'force_delete_ ' . $ slug => 'Force delete own ' . $ title ,
245+ 'force_delete_others_ ' . $ slug => 'Force delete all ' . $ title ,
246+ 'publish_ ' . $ slug => 'Publish own ' . $ title ,
247+ 'publish_others_ ' . $ slug => 'Publish all ' . $ title ,
248+ 'import_ ' . $ slug => 'Import ' . $ title ,
249+ ];
250+ }
251+
252+ /**
253+ * Load up permissions for all the Category Types
254+ *
255+ * @returns array;
256+ */
257+ public function seedCategoryTypePermissions (array $ existing_permissions = []): array
258+ {
259+ $ data = [];
260+ $ all_category_types = CategoryType::all ();
261+
262+ foreach ($ all_category_types as $ category_type ) {
263+ $ permissions = $ this ->constructCategoryTypePermissions ($ category_type );
264+ $ existing = $ existing_permissions [$ category_type ->permission_slug ] ?? [];
265+ $ data [$ category_type ->permission_slug ] = array_merge ($ existing , $ permissions );
266+ }
267+ return array_merge ($ existing_permissions , $ data );
268+ }
269+
270+ /**
271+ * Permission template for the category types
272+ *
273+ * @param CategoryType $category_type
274+ * @return string[]
275+ */
276+ protected function constructCategoryTypePermissions (CategoryType $ category_type ): array
277+ {
278+ $ slug = $ category_type ->permission_slug ;
279+ $ title = Str::lower ($ category_type ->name );
280+
281+ return [
282+ 'edit_ ' . $ slug => 'Edit ' . $ title ,
283+ 'delete_ ' . $ slug => 'Delete ' . $ title ,
284+ 'view_ ' . $ slug => 'View ' . $ title ,
285+ 'import_ ' . $ slug => 'Import ' . $ title ,
286+ ];
287+ }
198288}
0 commit comments