Advertisement
kwasinski

AdminAction

May 11th, 2023
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | Software | 0 0
  1. <?php declare(strict_types = 1);
  2.  
  3. namespace NameSpace\Units\Admin\Actions;
  4.  
  5. use NameSpace\Units\Core\Actions\Action;
  6. use NameSpace\Units\Core\Registers\Interfaces\AbstractRegisterInterface;
  7. use NameSpace\Units\Core\Registers\Hook;
  8. use NameSpace\Support\Env;
  9.  
  10. class AdminAction extends Action implements AbstractRegisterInterface
  11. {
  12.     public const EXPORT_CAPABILITY = 'export';
  13.     public const ROLES_WITH_EXPORT_CAPABILITY = [
  14.         'administrator',
  15.     ];
  16.  
  17.     /**
  18.      * register
  19.      *
  20.      * @return void
  21.      */
  22.     public static function register(): void
  23.     {
  24.         self::instance()->add(
  25.             new Hook(
  26.                 hook: 'admin_init',
  27.                 callable: [
  28.                     static::class,
  29.                     'removeExportCapability',
  30.                 ],
  31.             ),
  32.         )->env(Env::ALL);
  33.     }
  34.  
  35.     /**
  36.      * removeExportCapability
  37.      *
  38.      * @return void
  39.      */
  40.     public static function removeExportCapability(): void
  41.     {
  42.         $rolesList = wp_roles()->roles;
  43.         $rolesList = array_filter(
  44.             array: $rolesList,
  45.             callback: static function ($roleName)
  46.             {
  47.                 return !in_array($roleName, self::ROLES_WITH_EXPORT_CAPABILITY);
  48.             },
  49.             mode: ARRAY_FILTER_USE_KEY,
  50.         );
  51.  
  52.         foreach ($rolesList as $roleName => $roleData)
  53.         {
  54.             $role = get_role($roleName);
  55.  
  56.             if (!$role->has_cap(self::EXPORT_CAPABILITY))
  57.             {
  58.                 continue;
  59.             }
  60.  
  61.             $role->remove_cap(self::EXPORT_CAPABILITY);
  62.         }
  63.     }
  64. }
  65.  
Tags: php wordpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement