rockschtar/wordpress-role 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rockschtar/wordpress-role

Composer 安装命令:

composer require rockschtar/wordpress-role

包简介

WordPress Role Abstraction

README 文档

README

CI

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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 rockschtar/wordpress-role 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-08