Chuyển tới nội dung chính
Phiên bản: 8.0.0

Taxonomy

File: packages/skilldo/cms/src/Taxonomy/Taxonomy.php
Namespace: SkillDo\Cms\Taxonomy\Taxonomy
Alias ngắn: \Taxonomy

Taxonomy là hệ thống cho phép Plugin đăng ký các loại nội dung (Post Type)loại danh mục (Category Type) riêng, tích hợp sẵn vào giao diện Admin của CMS mà không cần viết lại toàn bộ logic.

Ví dụ thực tế: Plugin video gallery đăng ký video-gallery (Post Type) và video-category (Category Type). CMS tự động tạo menu quản lý, form thêm/sửa/xóa, phân quyền theo cấu hình.


1. Post Type

1.1 Đăng Ký Post Type

Taxonomy::addPost(string $postType, array $args): bool

Thường được gọi trong TaxonomyService của Plugin, được khởi động qua ServiceProvider.

<?php
namespace MyPlugin\Services;

use SkillDo\Cms\Taxonomy\Taxonomy;

class MyPluginTaxonomyService
{
static function register(): void
{
Taxonomy::addPost('services', [
'labels' => [
'name' => 'Dịch vụ', // Tên hiển thị (số nhiều)
'singular_name' => 'Dịch vụ', // Tên hiển thị (số ít)
],
'public' => true, // Hiển thị ở Frontend
'show_in_nav_menus' => true, // Hiển thị trong quản lý Menu
'show_in_nav_admin' => true, // Hiển thị trong Admin navigation
'menu_position' => 5, // Vị trí trong sidebar Admin
'menu_icon' => '<i class="fas fa-cogs"></i>',
'supports' => [
'info' => ['title', 'excerpt', 'content'],
'media' => ['image'],
'seo' => ['slug', 'seo_title', 'seo_keywords', 'seo_description'],
'theme' => ['theme_layout', 'theme_view'],
],
'capabilities' => [
'view' => 'view_services',
'add' => 'add_services',
'edit' => 'edit_services',
'delete' => 'delete_services',
],
]);
}
}

1.2 Tham Số $args cho Post Type

Tham sốKiểuMô tả
labels.namestringTên hiển thị dạng số nhiều (vd: "Dịch vụ")
labels.singular_namestringTên hiển thị dạng số ít
publicboolHiển thị ngoài Frontend và Admin
show_in_nav_menusboolHiển thị trong trang quản lý Menu (Giao diện → Menu)
show_in_nav_adminboolHiển thị trong thanh điều hướng Admin
exclude_from_searchboolẨn khỏi kết quả tìm kiếm (mặc định true)
menu_positionintThứ tự trong sidebar Admin (càng nhỏ càng lên trên)
menu_iconstringHTML icon cho menu item
supportsarrayMap nhóm → danh sách field. Key là tên nhóm (info, media, seo, theme), value là mảng tên field
capabilitiesarrayMapping capability cho từng hành động: view, add, edit, delete

1.3 Các Nhóm và Field Hỗ Trợ trong supports

supports là một associative array với key là tên nhóm và value là danh sách field trong nhóm đó:

'supports' => [
'info' => ['title', 'excerpt', 'content'],
'media' => ['image'],
'seo' => ['slug', 'seo_title', 'seo_keywords', 'seo_description'],
'theme' => ['theme_layout', 'theme_view'],
],
NhómField khả dụng
infotitle, excerpt, content, public
mediaimage
seoslug, seo_title, seo_description, seo_keywords
themetheme_layout, theme_view
FieldMô tả
titleTiêu đề
excerptMô tả ngắn
contentNội dung (WYSIWYG)
imageẢnh đại diện
publicTrạng thái hiển thị (public/private)
slugĐường dẫn URL
seo_titleTiêu đề SEO
seo_descriptionMô tả SEO
seo_keywordsTừ khóa SEO
theme_layoutChọn layout của theme
theme_viewChọn view template

2. Category Type

2.1 Đăng Ký Category Type

Gọi ngay sau khi đăng ký Post Type, truyền $postType để liên kết 2 loại với nhau.

Taxonomy::addCategory(string $cateType, string $postType, array $args): bool
Taxonomy::addCategory('service-category', 'services', [
'labels' => [
'name' => 'Danh mục dịch vụ',
'singular_name' => 'Danh mục dịch vụ',
],
'public' => true,
'show_in_nav_menus' => true, // Hiển thị trong quản lý Menu
'show_in_nav_admin' => true, // Hiển thị trong Admin navigation
'parent' => true, // Cho phép danh mục cha/con (phân cấp)
'supports' => [
'info' => ['name', 'excerpt'],
'media' => ['image'],
'seo' => ['slug', 'seo_title', 'seo_keywords', 'seo_description'],
'theme' => ['theme_layout', 'theme_view'],
'category' => ['parent_id'],
],
]);

2.2 Tham Số $args cho Category Type

Tham sốKiểuMô tả
labels.namestringTên hiển thị
labels.singular_namestringTên số ít
publicboolHiển thị Frontend
show_in_nav_menusboolHiển thị trong quản lý Menu
show_in_nav_adminboolHiển thị trong Admin navigation
exclude_from_searchboolẨn khỏi kết quả tìm kiếm (mặc định true)
parentboolCho phép danh mục phân cấp cha/con
show_admin_columnboolHiển thị cột category trong bảng Admin của Post Type
supportsarrayMap nhóm → danh sách field (tương tự Post Type, thêm nhóm category với field parent_id)
capabilitiesarrayedit, delete

3. Ví Dụ Thực Tế — Đăng Ký Trong ServiceProvider

<?php
// plugins/my-plugin/app/Providers/MyPluginServiceProvider.php

namespace MyPlugin\Providers;

use MyPlugin\Services\MyPluginTaxonomyService;
use SkillDo\ServiceProvider;

class MyPluginServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Đăng ký Taxonomy khi CMS khởi động
add_action('init', [MyPluginTaxonomyService::class, 'register']);
}
}

4. Các Method Khác của Taxonomy

Kiểm Tra & Lấy Thông Tin

// Kiểm tra Post Type đã được đăng ký chưa
$exists = Taxonomy::hasPost('services');
// true / false

// Kiểm tra Category Type đã được đăng ký chưa
$exists = Taxonomy::hasCategory('service-category');
// true / false

// Lấy danh sách tất cả Post Type đã đăng ký (keyed by postType, kèm đầy đủ config)
$postTypes = Taxonomy::getPost();
// ['post' => [...], 'services' => [...], ...]

// Lấy thông tin config của một Post Type cụ thể
$detail = Taxonomy::getPost('services');
// ['labels' => [...], 'public' => true, ...]

// Alias của getPost() không tham số — tường minh hơn khi cần toàn bộ data
$allPosts = Taxonomy::getPostDetail();

// Lấy tất cả Category Type đã đăng ký (keyed by cateType, kèm đầy đủ config)
$cateTypes = Taxonomy::getCategory();

// Lấy thông tin config của một Category Type cụ thể
$detail = Taxonomy::getCategory('service-category');
// ['labels' => [...], 'public' => true, 'post_type' => 'services', ...]

// Lấy danh sách Category Type liên kết với một Post Type
// $output = TaxonomyService::OUT_NAMES → trả về mảng tên (mặc định)
// $output = TaxonomyService::OUT_DETAILS → trả về mảng config đầy đủ
$linkedCats = Taxonomy::getCategoryByPost('services');
// ['service-category']

$linkedCatsDetail = Taxonomy::getCategoryByPost('services', \SkillDo\Cms\Taxonomy\TaxonomyService::OUT_DETAILS);
// ['service-category' => ['labels' => [...], ...]]

// Cũng chấp nhận object có thuộc tính post_type
$linkedCats = Taxonomy::getCategoryByPost($postObject);

Hủy Đăng Ký (Dùng trong DeactivatorService)

// Hủy đăng ký Post Type (đồng thời gỡ liên kết với tất cả Category Type)
Taxonomy::removePost('services');

// Hủy đăng ký Category Type hoàn toàn (đồng thời gỡ khỏi tất cả Post Type)
Taxonomy::removeCategory('service-category');

// Chỉ gỡ liên kết Category Type khỏi một Post Type cụ thể (giữ lại Category Type)
Taxonomy::removeCategory('service-category', 'services');