Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types = 1);
- namespace NameSpace\Units\Admin\Actions;
- use NameSpace\Units\Core\Actions\Action;
- use NameSpace\Units\Core\Registers\Interfaces\AbstractRegisterInterface;
- use NameSpace\Units\Core\Registers\Hook;
- use NameSpace\Support\Env;
- class AdminAction extends Action implements AbstractRegisterInterface
- {
- public const EXPORT_CAPABILITY = 'export';
- public const ROLES_WITH_EXPORT_CAPABILITY = [
- 'administrator',
- ];
- /**
- * register
- *
- * @return void
- */
- public static function register(): void
- {
- self::instance()->add(
- new Hook(
- hook: 'admin_init',
- callable: [
- static::class,
- 'removeExportCapability',
- ],
- ),
- )->env(Env::ALL);
- }
- /**
- * removeExportCapability
- *
- * @return void
- */
- public static function removeExportCapability(): void
- {
- $rolesList = wp_roles()->roles;
- $rolesList = array_filter(
- array: $rolesList,
- callback: static function ($roleName)
- {
- return !in_array($roleName, self::ROLES_WITH_EXPORT_CAPABILITY);
- },
- mode: ARRAY_FILTER_USE_KEY,
- );
- foreach ($rolesList as $roleName => $roleData)
- {
- $role = get_role($roleName);
- if (!$role->has_cap(self::EXPORT_CAPABILITY))
- {
- continue;
- }
- $role->remove_cap(self::EXPORT_CAPABILITY);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement