rockschtar/wordpress-role
Composer 安装命令:
composer require rockschtar/wordpress-role
包简介
WordPress Role Abstraction
README 文档
README
A PHP library that lets you define WordPress roles as typed classes. Instead of scattering add_role() / remove_role() calls across your plugin, each role becomes a self-contained class that knows its own name, display label, and capabilities. Register and unregister it with a single static call.
Designed for composer-based WordPress projects such as roots/bedrock or johnpbloch/wordpress.
Requirements
- PHP 8.3
- Composer
Installation
composer require rockschtar/wordpress-role
How to
1. Define a role
Extend Role and implement the three required methods. All methods must be static.
use Rockschtar\WordPress\Role\Role; class FAQManagerRole extends Role { public static function roleName(): string { return 'faq_manager'; } public static function displayName(): string { return __('FAQ Manager', 'my-textdomain'); } public static function capabilities(): array { return [ 'edit_faq', 'read_faq', 'delete_faq', ]; } }
By default the role inherits all capabilities from the built-in subscriber role. Override inheritFrom() to use a different base:
protected function inheritFrom(): string { return 'editor'; }
2. Register and unregister
Wire the role to your plugin's activation and deactivation hooks:
register_activation_hook(MY_PLUGIN_FILE, [FAQManagerRole::class, 'register']); register_deactivation_hook(MY_PLUGIN_FILE, [FAQManagerRole::class, 'unregister']);
When your plugin defines several roles, group them together:
const ROLES = [ FAQManagerRole::class, EditorPlusRole::class, ]; register_activation_hook(MY_PLUGIN_FILE, function () { foreach (ROLES as $role) { $role::register(); } }); register_deactivation_hook(MY_PLUGIN_FILE, function () { foreach (ROLES as $role) { $role::unregister(); } });
Examples
Check if the current user has the role:
if (current_user_can(FAQManagerRole::roleName())) { // ... }
Assign the role to a user, e.g. after registration:
$user->set_role(FAQManagerRole::roleName());
Use the role in a REST API permission callback:
register_rest_route('my-plugin/v1', '/faqs', [ 'methods' => 'GET', 'callback' => 'my_plugin_get_faqs', 'permission_callback' => fn() => current_user_can(FAQManagerRole::roleName()), ]);
Available hooks and filters
| Hook | Type | Description |
|---|---|---|
rswpr_before_register_role |
action | Fires before a role is registered. Receives the Role instance. |
rswpr_after_register_role |
action | Fires after a role is registered. Receives the Role instance. |
rswpr_before_unregister_role |
action | Fires before a role is removed. Receives the Role instance. |
rswpr_after_unregister_role |
action | Fires after a role is removed. Receives the Role instance. |
rswpr_default_inherit_from_role |
filter | Override the default inherited role (subscriber). |
rswp_get_wp_role |
filter | Filter the WP_Role object returned by getWPRole(). |
License
rockschtar/wordpress-role is open source and released under the MIT license. See LICENSE for details.
rockschtar/wordpress-role 适用场景与选型建议
rockschtar/wordpress-role 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.37k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rockschtar/wordpress-role 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rockschtar/wordpress-role 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-08